12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 12d467d0bcSZheng Yongjun * Vitaly E. Lavrov RTA_OK arithmetic was wrong. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15ee5d032fSJakub Kicinski #include <linux/bitops.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/socket.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/timer.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/sockios.h> 241da177e4SLinus Torvalds #include <linux/net.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/mm.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/interrupt.h> 291da177e4SLinus Torvalds #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/skbuff.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/security.h> 336756ae4bSStephen Hemminger #include <linux/mutex.h> 341823730fSThomas Graf #include <linux/if_addr.h> 3577162022SJohn Fastabend #include <linux/if_bridge.h> 36f6f6424bSJiri Pirko #include <linux/if_vlan.h> 37ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 3877162022SJohn Fastabend #include <linux/etherdevice.h> 3958038695SMartin KaFai Lau #include <linux/bpf.h> 401da177e4SLinus Torvalds 417c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/netdevice.h> 451da177e4SLinus Torvalds #include <net/ip.h> 461da177e4SLinus Torvalds #include <net/protocol.h> 471da177e4SLinus Torvalds #include <net/arp.h> 481da177e4SLinus Torvalds #include <net/route.h> 491da177e4SLinus Torvalds #include <net/udp.h> 50ea697639SDaniel Borkmann #include <net/tcp.h> 511da177e4SLinus Torvalds #include <net/sock.h> 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 5314c0b97dSThomas Graf #include <net/fib_rules.h> 54e2849863SThomas Graf #include <net/rtnetlink.h> 5530ffee84SJohannes Berg #include <net/net_namespace.h> 561da177e4SLinus Torvalds 57a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 582dba407fSNikolay Aleksandrov #define RTNL_SLAVE_MAX_TYPE 40 59ccf8dbcdSKees Cook 60e0d087afSEric Dumazet struct rtnl_link { 61e2849863SThomas Graf rtnl_doit_func doit; 62e2849863SThomas Graf rtnl_dumpit_func dumpit; 63e4202511SFlorian Westphal struct module *owner; 6462256f98SFlorian Westphal unsigned int flags; 65addf9b90SFlorian Westphal struct rcu_head rcu; 66e2849863SThomas Graf }; 67e2849863SThomas Graf 686756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds void rtnl_lock(void) 711da177e4SLinus Torvalds { 726756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 731da177e4SLinus Torvalds } 74e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 751da177e4SLinus Torvalds 7679ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 7779ffdfc6SKirill Tkhai { 7879ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 7979ffdfc6SKirill Tkhai } 8079ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8179ffdfc6SKirill Tkhai 821b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 831b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 841b5c5493SEric Dumazet { 851b5c5493SEric Dumazet if (head && tail) { 861b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 871b5c5493SEric Dumazet defer_kfree_skb_list = head; 881b5c5493SEric Dumazet } 891b5c5493SEric Dumazet } 901b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 911b5c5493SEric Dumazet 926756ae4bSStephen Hemminger void __rtnl_unlock(void) 931da177e4SLinus Torvalds { 941b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 951b5c5493SEric Dumazet 961b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 971b5c5493SEric Dumazet 986756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 991b5c5493SEric Dumazet 1001b5c5493SEric Dumazet while (head) { 1011b5c5493SEric Dumazet struct sk_buff *next = head->next; 1021b5c5493SEric Dumazet 1031b5c5493SEric Dumazet kfree_skb(head); 1041b5c5493SEric Dumazet cond_resched(); 1051b5c5493SEric Dumazet head = next; 1061b5c5493SEric Dumazet } 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds void rtnl_unlock(void) 1101da177e4SLinus Torvalds { 11158ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1121da177e4SLinus Torvalds netdev_run_todo(); 1131da177e4SLinus Torvalds } 114e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1151da177e4SLinus Torvalds 1166756ae4bSStephen Hemminger int rtnl_trylock(void) 1176756ae4bSStephen Hemminger { 1186756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1196756ae4bSStephen Hemminger } 120e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1216756ae4bSStephen Hemminger 122c9c1014bSPatrick McHardy int rtnl_is_locked(void) 123c9c1014bSPatrick McHardy { 124c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 125c9c1014bSPatrick McHardy } 126e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 127c9c1014bSPatrick McHardy 1286f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1296f99528eSVlad Buslov { 1306f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1316f99528eSVlad Buslov } 1326f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1336f99528eSVlad Buslov 134a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1350cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 136a898def2SPaul E. McKenney { 137a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 138a898def2SPaul E. McKenney } 139a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 140a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 141a898def2SPaul E. McKenney 14251e13685SJakub Kicinski static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 143e2849863SThomas Graf 144e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 145e2849863SThomas Graf { 146e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 147e2849863SThomas Graf 148e2849863SThomas Graf /* 149e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 150e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 151e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 152e2849863SThomas Graf */ 153e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 154e2849863SThomas Graf 155e2849863SThomas Graf return msgindex; 156e2849863SThomas Graf } 157e2849863SThomas Graf 158addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 159addf9b90SFlorian Westphal { 16051e13685SJakub Kicinski struct rtnl_link __rcu **tab; 161addf9b90SFlorian Westphal 162addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 163addf9b90SFlorian Westphal protocol = PF_UNSPEC; 164addf9b90SFlorian Westphal 165addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 166addf9b90SFlorian Westphal if (!tab) 167addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 168addf9b90SFlorian Westphal 16951e13685SJakub Kicinski return rcu_dereference_rtnl(tab[msgtype]); 170addf9b90SFlorian Westphal } 171addf9b90SFlorian Westphal 172e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 173e4202511SFlorian Westphal int protocol, int msgtype, 174c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 175b97bac64SFlorian Westphal unsigned int flags) 176e2849863SThomas Graf { 177b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 178b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 179e2849863SThomas Graf int msgindex; 180addf9b90SFlorian Westphal int ret = -ENOBUFS; 181e2849863SThomas Graf 18225239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 183e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 184e2849863SThomas Graf 185addf9b90SFlorian Westphal rtnl_lock(); 18651e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 187e2849863SThomas Graf if (tab == NULL) { 188addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 189addf9b90SFlorian Westphal if (!tab) 190addf9b90SFlorian Westphal goto unlock; 191e2849863SThomas Graf 192addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1936853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 194e2849863SThomas Graf } 195e2849863SThomas Graf 196addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 197addf9b90SFlorian Westphal if (old) { 198addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 199addf9b90SFlorian Westphal if (!link) 200addf9b90SFlorian Westphal goto unlock; 201addf9b90SFlorian Westphal } else { 202addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 203addf9b90SFlorian Westphal if (!link) 204addf9b90SFlorian Westphal goto unlock; 205addf9b90SFlorian Westphal } 206e2849863SThomas Graf 207e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 208e4202511SFlorian Westphal link->owner = owner; 209e4202511SFlorian Westphal 210addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 211addf9b90SFlorian Westphal if (doit) 212addf9b90SFlorian Westphal link->doit = doit; 213addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 214addf9b90SFlorian Westphal if (dumpit) 215addf9b90SFlorian Westphal link->dumpit = dumpit; 216addf9b90SFlorian Westphal 217addf9b90SFlorian Westphal link->flags |= flags; 218addf9b90SFlorian Westphal 219addf9b90SFlorian Westphal /* publish protocol:msgtype */ 220addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 221addf9b90SFlorian Westphal ret = 0; 222addf9b90SFlorian Westphal if (old) 223addf9b90SFlorian Westphal kfree_rcu(old, rcu); 224addf9b90SFlorian Westphal unlock: 225addf9b90SFlorian Westphal rtnl_unlock(); 226addf9b90SFlorian Westphal return ret; 227e2849863SThomas Graf } 228e4202511SFlorian Westphal 229e4202511SFlorian Westphal /** 230e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 231e4202511SFlorian Westphal * 232e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 233e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 234e4202511SFlorian Westphal * @msgtype: rtnetlink message type 235e4202511SFlorian Westphal * @doit: Function pointer called for each request message 236e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 237d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 238e4202511SFlorian Westphal * 239e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 240e4202511SFlorian Westphal */ 241e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 242e4202511SFlorian Westphal int protocol, int msgtype, 243e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 244e4202511SFlorian Westphal unsigned int flags) 245e4202511SFlorian Westphal { 246e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 247e4202511SFlorian Westphal doit, dumpit, flags); 248e4202511SFlorian Westphal } 249e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 250e4202511SFlorian Westphal 251e4202511SFlorian Westphal /** 25216feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 253e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 254e4202511SFlorian Westphal * @msgtype: rtnetlink message type 255e4202511SFlorian Westphal * @doit: Function pointer called for each request message 256e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 257d467d0bcSZheng Yongjun * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions 258e4202511SFlorian Westphal * 259e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 260e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 261e4202511SFlorian Westphal * specified protocol family and message type is received. 262e4202511SFlorian Westphal * 263e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 264e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 265e4202511SFlorian Westphal * family exists. 266e2849863SThomas Graf */ 267e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 268c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 269b97bac64SFlorian Westphal unsigned int flags) 270e2849863SThomas Graf { 27116feebcfSFlorian Westphal int err; 27216feebcfSFlorian Westphal 27316feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27416feebcfSFlorian Westphal flags); 27516feebcfSFlorian Westphal if (err) 27616feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 27716feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 278e2849863SThomas Graf } 279e2849863SThomas Graf 280e2849863SThomas Graf /** 281e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 282e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 283e2849863SThomas Graf * @msgtype: rtnetlink message type 284e2849863SThomas Graf * 285e2849863SThomas Graf * Returns 0 on success or a negative error code. 286e2849863SThomas Graf */ 287e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 288e2849863SThomas Graf { 28951e13685SJakub Kicinski struct rtnl_link __rcu **tab; 29051e13685SJakub Kicinski struct rtnl_link *link; 291e2849863SThomas Graf int msgindex; 292e2849863SThomas Graf 29325239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 294e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 295e2849863SThomas Graf 2966853dd48SFlorian Westphal rtnl_lock(); 297addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 298addf9b90SFlorian Westphal if (!tab) { 2996853dd48SFlorian Westphal rtnl_unlock(); 300e2849863SThomas Graf return -ENOENT; 3016853dd48SFlorian Westphal } 302e2849863SThomas Graf 30351e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 3044fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 3056853dd48SFlorian Westphal rtnl_unlock(); 306e2849863SThomas Graf 307addf9b90SFlorian Westphal kfree_rcu(link, rcu); 308addf9b90SFlorian Westphal 309e2849863SThomas Graf return 0; 310e2849863SThomas Graf } 311e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 312e2849863SThomas Graf 313e2849863SThomas Graf /** 314e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 315e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 316e2849863SThomas Graf * 317e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 318e2849863SThomas Graf * of a certain protocol family. 319e2849863SThomas Graf */ 320e2849863SThomas Graf void rtnl_unregister_all(int protocol) 321e2849863SThomas Graf { 32251e13685SJakub Kicinski struct rtnl_link __rcu **tab; 32351e13685SJakub Kicinski struct rtnl_link *link; 324addf9b90SFlorian Westphal int msgindex; 325019a3169SFlorian Westphal 32625239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 327e2849863SThomas Graf 328019a3169SFlorian Westphal rtnl_lock(); 32951e13685SJakub Kicinski tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 330f707ef61SSabrina Dubroca if (!tab) { 331f707ef61SSabrina Dubroca rtnl_unlock(); 332f707ef61SSabrina Dubroca return; 333f707ef61SSabrina Dubroca } 3346853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 335addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 33651e13685SJakub Kicinski link = rtnl_dereference(tab[msgindex]); 337addf9b90SFlorian Westphal if (!link) 338addf9b90SFlorian Westphal continue; 339addf9b90SFlorian Westphal 3404fc29989SYajun Deng RCU_INIT_POINTER(tab[msgindex], NULL); 341addf9b90SFlorian Westphal kfree_rcu(link, rcu); 342addf9b90SFlorian Westphal } 343019a3169SFlorian Westphal rtnl_unlock(); 344019a3169SFlorian Westphal 3456853dd48SFlorian Westphal synchronize_net(); 3466853dd48SFlorian Westphal 347addf9b90SFlorian Westphal kfree(tab); 348e2849863SThomas Graf } 349e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3501da177e4SLinus Torvalds 35138f7b870SPatrick McHardy static LIST_HEAD(link_ops); 35238f7b870SPatrick McHardy 353c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 354c63044f0SEric Dumazet { 355c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 356c63044f0SEric Dumazet 357c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 358c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 359c63044f0SEric Dumazet return ops; 360c63044f0SEric Dumazet } 361c63044f0SEric Dumazet return NULL; 362c63044f0SEric Dumazet } 363c63044f0SEric Dumazet 36438f7b870SPatrick McHardy /** 36538f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 36638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 36738f7b870SPatrick McHardy * 36838f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 36938f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 37038f7b870SPatrick McHardy * must be called before registering the devices. 37138f7b870SPatrick McHardy * 37238f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 37338f7b870SPatrick McHardy */ 37438f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 37538f7b870SPatrick McHardy { 376c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 377c63044f0SEric Dumazet return -EEXIST; 378c63044f0SEric Dumazet 3798c713dc9SJohannes Berg /* The check for alloc/setup is here because if ops 380b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 381b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 382b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 383b0ab2fabSJiri Pirko */ 3848c713dc9SJohannes Berg if ((ops->alloc || ops->setup) && !ops->dellink) 38523289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3862d85cba2SPatrick McHardy 38738f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 38838f7b870SPatrick McHardy return 0; 38938f7b870SPatrick McHardy } 39038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 39138f7b870SPatrick McHardy 39238f7b870SPatrick McHardy /** 39338f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 39438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 39538f7b870SPatrick McHardy * 39638f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 39738f7b870SPatrick McHardy */ 39838f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 39938f7b870SPatrick McHardy { 40038f7b870SPatrick McHardy int err; 40138f7b870SPatrick McHardy 402ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 403ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 404ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 405ccf8dbcdSKees Cook return -EINVAL; 406ccf8dbcdSKees Cook 40738f7b870SPatrick McHardy rtnl_lock(); 40838f7b870SPatrick McHardy err = __rtnl_link_register(ops); 40938f7b870SPatrick McHardy rtnl_unlock(); 41038f7b870SPatrick McHardy return err; 41138f7b870SPatrick McHardy } 41238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 41338f7b870SPatrick McHardy 414669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 415669f87baSPavel Emelyanov { 416669f87baSPavel Emelyanov struct net_device *dev; 41723289a37SEric Dumazet LIST_HEAD(list_kill); 41823289a37SEric Dumazet 419669f87baSPavel Emelyanov for_each_netdev(net, dev) { 42023289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 42123289a37SEric Dumazet ops->dellink(dev, &list_kill); 422669f87baSPavel Emelyanov } 42323289a37SEric Dumazet unregister_netdevice_many(&list_kill); 424669f87baSPavel Emelyanov } 425669f87baSPavel Emelyanov 42638f7b870SPatrick McHardy /** 42738f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 42838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 42938f7b870SPatrick McHardy * 430554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 431554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 432554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 43338f7b870SPatrick McHardy */ 43438f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 43538f7b870SPatrick McHardy { 436881d966bSEric W. Biederman struct net *net; 4372d85cba2SPatrick McHardy 438881d966bSEric W. Biederman for_each_net(net) { 439669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 440881d966bSEric W. Biederman } 44138f7b870SPatrick McHardy list_del(&ops->list); 44238f7b870SPatrick McHardy } 44338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 44438f7b870SPatrick McHardy 445200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 446200b916fSCong Wang * devices unregistering in any network namespace. 447200b916fSCong Wang */ 448200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 449200b916fSCong Wang { 450200b916fSCong Wang struct net *net; 451200b916fSCong Wang bool unregistering; 452ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 453200b916fSCong Wang 454ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 455200b916fSCong Wang for (;;) { 456200b916fSCong Wang unregistering = false; 457200b916fSCong Wang rtnl_lock(); 458f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 459f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 460f0b07bb1SKirill Tkhai */ 461200b916fSCong Wang for_each_net(net) { 462ede6c39cSEric Dumazet if (atomic_read(&net->dev_unreg_count) > 0) { 463200b916fSCong Wang unregistering = true; 464200b916fSCong Wang break; 465200b916fSCong Wang } 466200b916fSCong Wang } 467200b916fSCong Wang if (!unregistering) 468200b916fSCong Wang break; 469200b916fSCong Wang __rtnl_unlock(); 470ff960a73SPeter Zijlstra 471ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 472200b916fSCong Wang } 473ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 474200b916fSCong Wang } 475200b916fSCong Wang 47638f7b870SPatrick McHardy /** 47738f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 47838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47938f7b870SPatrick McHardy */ 48038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 48138f7b870SPatrick McHardy { 4828518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4834420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 484200b916fSCong Wang rtnl_lock_unregistering_all(); 48538f7b870SPatrick McHardy __rtnl_link_unregister(ops); 48638f7b870SPatrick McHardy rtnl_unlock(); 4874420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 48838f7b870SPatrick McHardy } 48938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 49038f7b870SPatrick McHardy 491ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 492ba7d49b1SJiri Pirko { 493ba7d49b1SJiri Pirko struct net_device *master_dev; 494ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4958515ae38SFlorian Westphal size_t size = 0; 496ba7d49b1SJiri Pirko 4978515ae38SFlorian Westphal rcu_read_lock(); 4988515ae38SFlorian Westphal 4998515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 500ba7d49b1SJiri Pirko if (!master_dev) 5018515ae38SFlorian Westphal goto out; 5028515ae38SFlorian Westphal 503ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5046049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5058515ae38SFlorian Westphal goto out; 506ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5078515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 508ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5098515ae38SFlorian Westphal 5108515ae38SFlorian Westphal out: 5118515ae38SFlorian Westphal rcu_read_unlock(); 5128515ae38SFlorian Westphal return size; 513ba7d49b1SJiri Pirko } 514ba7d49b1SJiri Pirko 51538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 51638f7b870SPatrick McHardy { 51738f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 51838f7b870SPatrick McHardy size_t size; 51938f7b870SPatrick McHardy 52038f7b870SPatrick McHardy if (!ops) 52138f7b870SPatrick McHardy return 0; 52238f7b870SPatrick McHardy 523369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 524369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 52538f7b870SPatrick McHardy 52638f7b870SPatrick McHardy if (ops->get_size) 52738f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 528369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 52938f7b870SPatrick McHardy ops->get_size(dev); 53038f7b870SPatrick McHardy 53138f7b870SPatrick McHardy if (ops->get_xstats_size) 532369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 533369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 53438f7b870SPatrick McHardy 535ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 536ba7d49b1SJiri Pirko 53738f7b870SPatrick McHardy return size; 53838f7b870SPatrick McHardy } 53938f7b870SPatrick McHardy 540f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 541f8ff182cSThomas Graf 542f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 543f8ff182cSThomas Graf { 544f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 545f8ff182cSThomas Graf 546a100243dSCong Wang ASSERT_RTNL(); 547a100243dSCong Wang 548a100243dSCong Wang list_for_each_entry(ops, &rtnl_af_ops, list) { 549f8ff182cSThomas Graf if (ops->family == family) 550f8ff182cSThomas Graf return ops; 551f8ff182cSThomas Graf } 552f8ff182cSThomas Graf 553f8ff182cSThomas Graf return NULL; 554f8ff182cSThomas Graf } 555f8ff182cSThomas Graf 556f8ff182cSThomas Graf /** 557f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 558f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 559f8ff182cSThomas Graf * 560f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 561f8ff182cSThomas Graf */ 5623678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 563f8ff182cSThomas Graf { 564f8ff182cSThomas Graf rtnl_lock(); 5655fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 566f8ff182cSThomas Graf rtnl_unlock(); 567f8ff182cSThomas Graf } 568f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 569f8ff182cSThomas Graf 570f8ff182cSThomas Graf /** 571f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 572f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 573f8ff182cSThomas Graf */ 574f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 575f8ff182cSThomas Graf { 576f8ff182cSThomas Graf rtnl_lock(); 5775fa85a09SFlorian Westphal list_del_rcu(&ops->list); 578f8ff182cSThomas Graf rtnl_unlock(); 5795fa85a09SFlorian Westphal 5805fa85a09SFlorian Westphal synchronize_rcu(); 581f8ff182cSThomas Graf } 582f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 583f8ff182cSThomas Graf 584b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 585b1974ed0SArad, Ronen u32 ext_filter_mask) 586f8ff182cSThomas Graf { 587f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 588f8ff182cSThomas Graf size_t size; 589f8ff182cSThomas Graf 590f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 591f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 592f8ff182cSThomas Graf 5935fa85a09SFlorian Westphal rcu_read_lock(); 5945fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 595f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 596f8ff182cSThomas Graf /* AF_* + nested data */ 597f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 598b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 599f8ff182cSThomas Graf } 600f8ff182cSThomas Graf } 6015fa85a09SFlorian Westphal rcu_read_unlock(); 602f8ff182cSThomas Graf 603f8ff182cSThomas Graf return size; 604f8ff182cSThomas Graf } 605f8ff182cSThomas Graf 606ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 607ba7d49b1SJiri Pirko { 608ba7d49b1SJiri Pirko struct net_device *master_dev; 6094c82a95eSFlorian Westphal bool ret = false; 610ba7d49b1SJiri Pirko 6114c82a95eSFlorian Westphal rcu_read_lock(); 6124c82a95eSFlorian Westphal 6134c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 614813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6154c82a95eSFlorian Westphal ret = true; 6164c82a95eSFlorian Westphal rcu_read_unlock(); 6174c82a95eSFlorian Westphal return ret; 618ba7d49b1SJiri Pirko } 619ba7d49b1SJiri Pirko 620ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 621ba7d49b1SJiri Pirko const struct net_device *dev) 622ba7d49b1SJiri Pirko { 623ba7d49b1SJiri Pirko struct net_device *master_dev; 624ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 625ba7d49b1SJiri Pirko struct nlattr *slave_data; 626ba7d49b1SJiri Pirko int err; 627ba7d49b1SJiri Pirko 628ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 629ba7d49b1SJiri Pirko if (!master_dev) 630ba7d49b1SJiri Pirko return 0; 631ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 632ba7d49b1SJiri Pirko if (!ops) 633ba7d49b1SJiri Pirko return 0; 634ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 635ba7d49b1SJiri Pirko return -EMSGSIZE; 636ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 637ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 638ba7d49b1SJiri Pirko if (!slave_data) 639ba7d49b1SJiri Pirko return -EMSGSIZE; 640ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 641ba7d49b1SJiri Pirko if (err < 0) 642ba7d49b1SJiri Pirko goto err_cancel_slave_data; 643ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 644ba7d49b1SJiri Pirko } 645ba7d49b1SJiri Pirko return 0; 646ba7d49b1SJiri Pirko 647ba7d49b1SJiri Pirko err_cancel_slave_data: 648ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 649ba7d49b1SJiri Pirko return err; 650ba7d49b1SJiri Pirko } 651ba7d49b1SJiri Pirko 652ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 653ba7d49b1SJiri Pirko const struct net_device *dev) 65438f7b870SPatrick McHardy { 65538f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 656ba7d49b1SJiri Pirko struct nlattr *data; 657ba7d49b1SJiri Pirko int err; 658ba7d49b1SJiri Pirko 659ba7d49b1SJiri Pirko if (!ops) 660ba7d49b1SJiri Pirko return 0; 661ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 662ba7d49b1SJiri Pirko return -EMSGSIZE; 663ba7d49b1SJiri Pirko if (ops->fill_xstats) { 664ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 665ba7d49b1SJiri Pirko if (err < 0) 666ba7d49b1SJiri Pirko return err; 667ba7d49b1SJiri Pirko } 668ba7d49b1SJiri Pirko if (ops->fill_info) { 669ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 670ba7d49b1SJiri Pirko if (data == NULL) 671ba7d49b1SJiri Pirko return -EMSGSIZE; 672ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 673ba7d49b1SJiri Pirko if (err < 0) 674ba7d49b1SJiri Pirko goto err_cancel_data; 675ba7d49b1SJiri Pirko nla_nest_end(skb, data); 676ba7d49b1SJiri Pirko } 677ba7d49b1SJiri Pirko return 0; 678ba7d49b1SJiri Pirko 679ba7d49b1SJiri Pirko err_cancel_data: 680ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 681ba7d49b1SJiri Pirko return err; 682ba7d49b1SJiri Pirko } 683ba7d49b1SJiri Pirko 684ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 685ba7d49b1SJiri Pirko { 686ba7d49b1SJiri Pirko struct nlattr *linkinfo; 68738f7b870SPatrick McHardy int err = -EMSGSIZE; 68838f7b870SPatrick McHardy 689ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 69038f7b870SPatrick McHardy if (linkinfo == NULL) 69138f7b870SPatrick McHardy goto out; 69238f7b870SPatrick McHardy 693ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 69438f7b870SPatrick McHardy if (err < 0) 69538f7b870SPatrick McHardy goto err_cancel_link; 696ba7d49b1SJiri Pirko 697ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 69838f7b870SPatrick McHardy if (err < 0) 699ba7d49b1SJiri Pirko goto err_cancel_link; 70038f7b870SPatrick McHardy 70138f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 70238f7b870SPatrick McHardy return 0; 70338f7b870SPatrick McHardy 70438f7b870SPatrick McHardy err_cancel_link: 70538f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 70638f7b870SPatrick McHardy out: 70738f7b870SPatrick McHardy return err; 70838f7b870SPatrick McHardy } 70938f7b870SPatrick McHardy 71095c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7111da177e4SLinus Torvalds { 71297c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7131da177e4SLinus Torvalds 714cfdf0d9aSYajun Deng return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 71797c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7182942e900SThomas Graf { 71997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72097c53cacSDenis V. Lunev 7212942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7222942e900SThomas Graf } 723e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7242942e900SThomas Graf 7251ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 72697676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 72797676b6bSThomas Graf { 72897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72997676b6bSThomas Graf 730f9b282b3SYajun Deng nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 73197676b6bSThomas Graf } 732e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 73397676b6bSThomas Graf 73497c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 73597676b6bSThomas Graf { 73697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73797c53cacSDenis V. Lunev 73897676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 73997676b6bSThomas Graf } 740e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 74197676b6bSThomas Graf 7421da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7431da177e4SLinus Torvalds { 7442d7202bfSThomas Graf struct nlattr *mx; 7452d7202bfSThomas Graf int i, valid = 0; 7461da177e4SLinus Torvalds 747c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 748c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 749c22a133aSDavid Ahern return 0; 750c22a133aSDavid Ahern 751ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(skb, RTA_METRICS); 7522d7202bfSThomas Graf if (mx == NULL) 7532d7202bfSThomas Graf return -ENOBUFS; 7542d7202bfSThomas Graf 7551da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7562d7202bfSThomas Graf if (metrics[i]) { 757ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 758ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 759ea697639SDaniel Borkmann 760ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 761ea697639SDaniel Borkmann if (!name) 762ea697639SDaniel Borkmann continue; 763ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 764ea697639SDaniel Borkmann goto nla_put_failure; 765c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 766c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 767c3a8d947SDaniel Borkmann 768f8edcd12SPhil Sutter if (!user_features) 769f8edcd12SPhil Sutter continue; 770c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 771c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 772c3a8d947SDaniel Borkmann goto nla_put_failure; 773ea697639SDaniel Borkmann } else { 774a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 775a6574349SDavid S. Miller goto nla_put_failure; 7761da177e4SLinus Torvalds } 777ea697639SDaniel Borkmann valid++; 778ea697639SDaniel Borkmann } 7792d7202bfSThomas Graf } 7801da177e4SLinus Torvalds 781a57d27fcSDavid S. Miller if (!valid) { 782a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 783a57d27fcSDavid S. Miller return 0; 784a57d27fcSDavid S. Miller } 7852d7202bfSThomas Graf 7862d7202bfSThomas Graf return nla_nest_end(skb, mx); 7872d7202bfSThomas Graf 7882d7202bfSThomas Graf nla_put_failure: 789bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 790bc3ed28cSThomas Graf return -EMSGSIZE; 7911da177e4SLinus Torvalds } 792e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 7931da177e4SLinus Torvalds 794e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 79587a50699SDavid S. Miller long expires, u32 error) 796e3703b3dSThomas Graf { 797e3703b3dSThomas Graf struct rta_cacheinfo ci = { 798e3703b3dSThomas Graf .rta_error = error, 799e3703b3dSThomas Graf .rta_id = id, 800e3703b3dSThomas Graf }; 801e3703b3dSThomas Graf 8023940746dSDavid Ahern if (dst) { 8033940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8043940746dSDavid Ahern ci.rta_used = dst->__use; 8053940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8063940746dSDavid Ahern } 8078253947eSLi Wei if (expires) { 8088253947eSLi Wei unsigned long clock; 809e3703b3dSThomas Graf 8108253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8118253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8128253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8138253947eSLi Wei } 814e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 815e3703b3dSThomas Graf } 816e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8171da177e4SLinus Torvalds 81893b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 819b00055aaSStefan Rompf { 820b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 821b00055aaSStefan Rompf 822b00055aaSStefan Rompf switch (transition) { 823b00055aaSStefan Rompf case IF_OPER_UP: 824b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 825eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 826b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 827eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 828b00055aaSStefan Rompf operstate = IF_OPER_UP; 829b00055aaSStefan Rompf break; 830b00055aaSStefan Rompf 831eec517cdSAndrew Lunn case IF_OPER_TESTING: 832eec517cdSAndrew Lunn if (operstate == IF_OPER_UP || 833eec517cdSAndrew Lunn operstate == IF_OPER_UNKNOWN) 834eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 835eec517cdSAndrew Lunn break; 836eec517cdSAndrew Lunn 837b00055aaSStefan Rompf case IF_OPER_DORMANT: 838b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 839b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 840b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 841b00055aaSStefan Rompf break; 8423ff50b79SStephen Hemminger } 843b00055aaSStefan Rompf 844b00055aaSStefan Rompf if (dev->operstate != operstate) { 845fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 846b00055aaSStefan Rompf dev->operstate = operstate; 847fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 848b00055aaSStefan Rompf netdev_state_change(dev); 84993b2d4a2SDavid S. Miller } 850b00055aaSStefan Rompf } 851b00055aaSStefan Rompf 852b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 853b1beb681SJiri Benc { 854b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 855b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 856b1beb681SJiri Benc } 857b1beb681SJiri Benc 8583729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8593729d502SPatrick McHardy const struct ifinfomsg *ifm) 8603729d502SPatrick McHardy { 8613729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8623729d502SPatrick McHardy 8633729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8643729d502SPatrick McHardy if (ifm->ifi_change) 8653729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 866b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8673729d502SPatrick McHardy 8683729d502SPatrick McHardy return flags; 8693729d502SPatrick McHardy } 8703729d502SPatrick McHardy 871b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 872be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8731da177e4SLinus Torvalds { 874b60c5115SThomas Graf a->rx_packets = b->rx_packets; 875b60c5115SThomas Graf a->tx_packets = b->tx_packets; 876b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 877b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 878b60c5115SThomas Graf a->rx_errors = b->rx_errors; 879b60c5115SThomas Graf a->tx_errors = b->tx_errors; 880b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 881b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 882b60c5115SThomas Graf 883b60c5115SThomas Graf a->multicast = b->multicast; 884b60c5115SThomas Graf a->collisions = b->collisions; 885b60c5115SThomas Graf 886b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 887b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 888b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 889b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 890b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 891b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 892b60c5115SThomas Graf 893b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 894b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 895b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 896b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 897b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 898b60c5115SThomas Graf 899b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 900b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9016e7333d3SJarod Wilson 9026e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 90310708f37SJan Engelhardt } 90410708f37SJan Engelhardt 905c02db8c6SChris Wright /* All VF info */ 906115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 907115c9b81SGreg Rose u32 ext_filter_mask) 908ebc08a6fSWilliams, Mitch A { 9099af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 910c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9117e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 912045de01aSScott Feldman size += num_vfs * 9137e75f74aSSabrina Dubroca (nla_total_size(0) + 9147e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 91575345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9167e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9177e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 91879aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 91979aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 920ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9217e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 922945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 92301a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9243b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9257e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9263b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 927343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9283b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 929343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9303b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 931343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9323b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 933343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9343b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 935343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9363b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 937343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 938c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 939c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 940c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 941c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 942dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 943c02db8c6SChris Wright return size; 944c02db8c6SChris Wright } else 945ebc08a6fSWilliams, Mitch A return 0; 946ebc08a6fSWilliams, Mitch A } 947ebc08a6fSWilliams, Mitch A 948c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 949c53864fdSDavid Gibson u32 ext_filter_mask) 95057b61080SScott Feldman { 95157b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 95257b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 95357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 95457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 95557b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 95657b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 95757b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 95857b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 95957b61080SScott Feldman + port_size; 96057b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 96157b61080SScott Feldman + port_size; 96257b61080SScott Feldman 963c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 964c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 96557b61080SScott Feldman return 0; 96657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 96757b61080SScott Feldman return port_self_size + vf_ports_size + 96857b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 96957b61080SScott Feldman else 97057b61080SScott Feldman return port_self_size; 97157b61080SScott Feldman } 97257b61080SScott Feldman 973b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 974d1fdd913SBrenden Blanco { 975b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 97658038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 977a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9784f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 979d1fdd913SBrenden Blanco 980d1fdd913SBrenden Blanco return xdp_size; 981d1fdd913SBrenden Blanco } 982d1fdd913SBrenden Blanco 98388f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 98488f4fb0cSJiri Pirko { 98588f4fb0cSJiri Pirko struct netdev_name_node *name_node; 98688f4fb0cSJiri Pirko size_t size; 98788f4fb0cSJiri Pirko 98888f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 98988f4fb0cSJiri Pirko return 0; 99088f4fb0cSJiri Pirko size = nla_total_size(0); 99188f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 99288f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 99388f4fb0cSJiri Pirko return size; 99488f4fb0cSJiri Pirko } 99588f4fb0cSJiri Pirko 996829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 997829eb208SRoopa Prabhu { 998829eb208SRoopa Prabhu size_t size = nla_total_size(1); 999829eb208SRoopa Prabhu 1000829eb208SRoopa Prabhu if (dev->proto_down_reason) 1001829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1002829eb208SRoopa Prabhu 1003829eb208SRoopa Prabhu return size; 1004829eb208SRoopa Prabhu } 1005829eb208SRoopa Prabhu 1006115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1007115c9b81SGreg Rose u32 ext_filter_mask) 1008339bf98fSThomas Graf { 1009339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1010339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10110b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1012339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1013270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1014339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 101535c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1016339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1017339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1018339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1019339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1020339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1021339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1022339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10239a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1024edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 102576ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 102676ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10276919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10286919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1029eac1b93cSCoco Li + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 1030339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 103138f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10322d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1033d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1034db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1035115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1036115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1037115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1038c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1039f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1040b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 104182f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 104288d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1043c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1044b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10453d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10466621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 104738e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1048829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 10497e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1050b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1051b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10523e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10533e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 105488f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1055f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 105679e1ad14SJiri Benc + 0; 1057339bf98fSThomas Graf } 1058339bf98fSThomas Graf 105957b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 106057b61080SScott Feldman { 106157b61080SScott Feldman struct nlattr *vf_ports; 106257b61080SScott Feldman struct nlattr *vf_port; 106357b61080SScott Feldman int vf; 106457b61080SScott Feldman int err; 106557b61080SScott Feldman 1066ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 106757b61080SScott Feldman if (!vf_ports) 106857b61080SScott Feldman return -EMSGSIZE; 106957b61080SScott Feldman 107057b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1071ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 10728ca94183SScott Feldman if (!vf_port) 10738ca94183SScott Feldman goto nla_put_failure; 1074a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1075a6574349SDavid S. Miller goto nla_put_failure; 107657b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10778ca94183SScott Feldman if (err == -EMSGSIZE) 10788ca94183SScott Feldman goto nla_put_failure; 107957b61080SScott Feldman if (err) { 108057b61080SScott Feldman nla_nest_cancel(skb, vf_port); 108157b61080SScott Feldman continue; 108257b61080SScott Feldman } 108357b61080SScott Feldman nla_nest_end(skb, vf_port); 108457b61080SScott Feldman } 108557b61080SScott Feldman 108657b61080SScott Feldman nla_nest_end(skb, vf_ports); 108757b61080SScott Feldman 108857b61080SScott Feldman return 0; 10898ca94183SScott Feldman 10908ca94183SScott Feldman nla_put_failure: 10918ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10928ca94183SScott Feldman return -EMSGSIZE; 109357b61080SScott Feldman } 109457b61080SScott Feldman 109557b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 109657b61080SScott Feldman { 109757b61080SScott Feldman struct nlattr *port_self; 109857b61080SScott Feldman int err; 109957b61080SScott Feldman 1100ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 110157b61080SScott Feldman if (!port_self) 110257b61080SScott Feldman return -EMSGSIZE; 110357b61080SScott Feldman 110457b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 110557b61080SScott Feldman if (err) { 110657b61080SScott Feldman nla_nest_cancel(skb, port_self); 11078ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 110857b61080SScott Feldman } 110957b61080SScott Feldman 111057b61080SScott Feldman nla_nest_end(skb, port_self); 111157b61080SScott Feldman 111257b61080SScott Feldman return 0; 111357b61080SScott Feldman } 111457b61080SScott Feldman 1115c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1116c53864fdSDavid Gibson u32 ext_filter_mask) 111757b61080SScott Feldman { 111857b61080SScott Feldman int err; 111957b61080SScott Feldman 1120c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1121c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 112257b61080SScott Feldman return 0; 112357b61080SScott Feldman 112457b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 112557b61080SScott Feldman if (err) 112657b61080SScott Feldman return err; 112757b61080SScott Feldman 112857b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 112957b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 113057b61080SScott Feldman if (err) 113157b61080SScott Feldman return err; 113257b61080SScott Feldman } 113357b61080SScott Feldman 113457b61080SScott Feldman return 0; 113557b61080SScott Feldman } 113657b61080SScott Feldman 113766cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 113866cae9edSJiri Pirko { 113966cae9edSJiri Pirko int err; 114002637fceSJiri Pirko struct netdev_phys_item_id ppid; 114166cae9edSJiri Pirko 114266cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 114366cae9edSJiri Pirko if (err) { 114466cae9edSJiri Pirko if (err == -EOPNOTSUPP) 114566cae9edSJiri Pirko return 0; 114666cae9edSJiri Pirko return err; 114766cae9edSJiri Pirko } 114866cae9edSJiri Pirko 114966cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 115066cae9edSJiri Pirko return -EMSGSIZE; 115166cae9edSJiri Pirko 115266cae9edSJiri Pirko return 0; 115366cae9edSJiri Pirko } 115466cae9edSJiri Pirko 1155db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1156db24a904SDavid Ahern { 1157db24a904SDavid Ahern char name[IFNAMSIZ]; 1158db24a904SDavid Ahern int err; 1159db24a904SDavid Ahern 1160db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1161db24a904SDavid Ahern if (err) { 1162db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1163db24a904SDavid Ahern return 0; 1164db24a904SDavid Ahern return err; 1165db24a904SDavid Ahern } 1166db24a904SDavid Ahern 116777ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1168db24a904SDavid Ahern return -EMSGSIZE; 1169db24a904SDavid Ahern 1170db24a904SDavid Ahern return 0; 1171db24a904SDavid Ahern } 1172db24a904SDavid Ahern 117382f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 117482f28412SJiri Pirko { 1175bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 117682f28412SJiri Pirko int err; 117782f28412SJiri Pirko 1178bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 117982f28412SJiri Pirko if (err) { 118082f28412SJiri Pirko if (err == -EOPNOTSUPP) 118182f28412SJiri Pirko return 0; 118282f28412SJiri Pirko return err; 118382f28412SJiri Pirko } 118482f28412SJiri Pirko 1185bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 118682f28412SJiri Pirko return -EMSGSIZE; 118782f28412SJiri Pirko 118882f28412SJiri Pirko return 0; 118982f28412SJiri Pirko } 119082f28412SJiri Pirko 1191b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1192b22b941bSHannes Frederic Sowa struct net_device *dev) 1193b22b941bSHannes Frederic Sowa { 1194550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1195b22b941bSHannes Frederic Sowa struct nlattr *attr; 1196b22b941bSHannes Frederic Sowa 119758414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 119858414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1199b22b941bSHannes Frederic Sowa if (!attr) 1200b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1201b22b941bSHannes Frederic Sowa 1202550bce59SRoopa Prabhu sp = nla_data(attr); 1203550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1204550bce59SRoopa Prabhu 1205550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1206550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1207550bce59SRoopa Prabhu if (!attr) 1208550bce59SRoopa Prabhu return -EMSGSIZE; 1209550bce59SRoopa Prabhu 1210550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1211b22b941bSHannes Frederic Sowa 1212b22b941bSHannes Frederic Sowa return 0; 1213b22b941bSHannes Frederic Sowa } 1214b22b941bSHannes Frederic Sowa 1215b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1216b22b941bSHannes Frederic Sowa struct net_device *dev, 1217b22b941bSHannes Frederic Sowa int vfs_num, 1218b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1219b22b941bSHannes Frederic Sowa { 1220b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 122179aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1222b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 122379aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1224b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1225b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1226b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1227b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1228b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1229b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1230b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 123175345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1232b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 123330aad417SDanit Goldberg struct ifla_vf_guid node_guid; 123430aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1235b22b941bSHannes Frederic Sowa 12360eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12370eed9cf5SMintz, Yuval 1238b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1239b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1240b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1241b22b941bSHannes Frederic Sowa * didn't report anything. 1242b22b941bSHannes Frederic Sowa */ 1243b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1244b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1245b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1246b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1247b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1248b22b941bSHannes Frederic Sowa */ 1249b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 125079aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 125179aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1252b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1253b22b941bSHannes Frederic Sowa return 0; 1254b22b941bSHannes Frederic Sowa 1255775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 12569fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 12579fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1258775f4f05SDan Carpenter 1259b22b941bSHannes Frederic Sowa vf_mac.vf = 1260b22b941bSHannes Frederic Sowa vf_vlan.vf = 126179aab093SMoshe Shemesh vf_vlan_info.vf = 1262b22b941bSHannes Frederic Sowa vf_rate.vf = 1263b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1264b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1265b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1266b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 12679aed6ae0SDanit Goldberg vf_trust.vf = 12689aed6ae0SDanit Goldberg node_guid.vf = 12699aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1270b22b941bSHannes Frederic Sowa 1271b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 127275345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1273b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1274b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 127579aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 127679aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 127779aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1278b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1279b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1280b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1281b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1282b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1283b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1284b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1285ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 128679aab093SMoshe Shemesh if (!vf) 128779aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1288b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 128975345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1290b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1291b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1292b22b941bSHannes Frederic Sowa &vf_rate) || 1293b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1294b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1295b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1296b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1297b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1298b22b941bSHannes Frederic Sowa &vf_linkstate) || 1299b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1300b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1301b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1302b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1303b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 130479aab093SMoshe Shemesh goto nla_put_vf_failure; 130530aad417SDanit Goldberg 130630aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 130730aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 130830aad417SDanit Goldberg &port_guid)) { 130930aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 131030aad417SDanit Goldberg &node_guid) || 131130aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 131230aad417SDanit Goldberg &port_guid)) 131330aad417SDanit Goldberg goto nla_put_vf_failure; 131430aad417SDanit Goldberg } 1315ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 131679aab093SMoshe Shemesh if (!vfvlanlist) 131779aab093SMoshe Shemesh goto nla_put_vf_failure; 131879aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 131979aab093SMoshe Shemesh &vf_vlan_info)) { 132079aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 132179aab093SMoshe Shemesh goto nla_put_vf_failure; 132279aab093SMoshe Shemesh } 132379aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1324b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1325b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1326b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1327b22b941bSHannes Frederic Sowa &vf_stats); 1328ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 132979aab093SMoshe Shemesh if (!vfstats) 133079aab093SMoshe Shemesh goto nla_put_vf_failure; 1331343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1332343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1333343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1334343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1335343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1336343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1337343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1338343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1339343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1340343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1341343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1342c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1343c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1344c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1345c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1346c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 134779aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 134879aab093SMoshe Shemesh goto nla_put_vf_failure; 134979aab093SMoshe Shemesh } 1350b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1351b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1352b22b941bSHannes Frederic Sowa return 0; 135379aab093SMoshe Shemesh 135479aab093SMoshe Shemesh nla_put_vf_failure: 135579aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 135679aab093SMoshe Shemesh nla_put_vfinfo_failure: 135779aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 135879aab093SMoshe Shemesh return -EMSGSIZE; 1359b22b941bSHannes Frederic Sowa } 1360b22b941bSHannes Frederic Sowa 1361250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1362250fc3dfSFlorian Westphal struct net_device *dev, 1363250fc3dfSFlorian Westphal u32 ext_filter_mask) 1364250fc3dfSFlorian Westphal { 1365250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1366250fc3dfSFlorian Westphal int i, num_vfs; 1367250fc3dfSFlorian Westphal 1368250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1369250fc3dfSFlorian Westphal return 0; 1370250fc3dfSFlorian Westphal 1371250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1372250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1373250fc3dfSFlorian Westphal return -EMSGSIZE; 1374250fc3dfSFlorian Westphal 1375250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1376250fc3dfSFlorian Westphal return 0; 1377250fc3dfSFlorian Westphal 1378ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1379250fc3dfSFlorian Westphal if (!vfinfo) 1380250fc3dfSFlorian Westphal return -EMSGSIZE; 1381250fc3dfSFlorian Westphal 1382250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1383250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1384250fc3dfSFlorian Westphal return -EMSGSIZE; 1385250fc3dfSFlorian Westphal } 1386250fc3dfSFlorian Westphal 1387250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1388250fc3dfSFlorian Westphal return 0; 1389250fc3dfSFlorian Westphal } 1390250fc3dfSFlorian Westphal 1391b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1392b22b941bSHannes Frederic Sowa { 13935f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13945f8e4474SKangjie Lu 13955f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13965f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13975f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13985f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13995f8e4474SKangjie Lu map.irq = dev->irq; 14005f8e4474SKangjie Lu map.dma = dev->dma; 14015f8e4474SKangjie Lu map.port = dev->if_port; 14025f8e4474SKangjie Lu 1403270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1404b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1405b22b941bSHannes Frederic Sowa 1406b22b941bSHannes Frederic Sowa return 0; 1407b22b941bSHannes Frederic Sowa } 1408b22b941bSHannes Frederic Sowa 1409a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1410d67b9cd2SDaniel Borkmann { 141158038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1412d67b9cd2SDaniel Borkmann 1413d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1414d67b9cd2SDaniel Borkmann 141558038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1416a25717d2SJakub Kicinski if (!generic_xdp_prog) 1417a25717d2SJakub Kicinski return 0; 1418a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 141958038695SMartin KaFai Lau } 1420ce158e58SJakub Kicinski 1421a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1422a25717d2SJakub Kicinski { 14237f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1424a25717d2SJakub Kicinski } 1425118b4aa2SJakub Kicinski 1426a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1427a25717d2SJakub Kicinski { 14287f0a8382SAndrii Nakryiko return dev_xdp_prog_id(dev, XDP_MODE_HW); 1429a25717d2SJakub Kicinski } 1430a25717d2SJakub Kicinski 1431a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1432a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1433a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1434a25717d2SJakub Kicinski { 1435a25717d2SJakub Kicinski u32 curr_id; 1436a25717d2SJakub Kicinski int err; 1437a25717d2SJakub Kicinski 1438a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1439a25717d2SJakub Kicinski if (!curr_id) 1440a25717d2SJakub Kicinski return 0; 1441a25717d2SJakub Kicinski 1442a25717d2SJakub Kicinski *prog_id = curr_id; 1443a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1444a25717d2SJakub Kicinski if (err) 1445a25717d2SJakub Kicinski return err; 1446a25717d2SJakub Kicinski 1447a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1448a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1449a25717d2SJakub Kicinski else 1450a25717d2SJakub Kicinski *mode = tgt_mode; 1451a25717d2SJakub Kicinski 1452a25717d2SJakub Kicinski return 0; 1453d67b9cd2SDaniel Borkmann } 1454d67b9cd2SDaniel Borkmann 1455d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1456d1fdd913SBrenden Blanco { 1457d1fdd913SBrenden Blanco struct nlattr *xdp; 145858038695SMartin KaFai Lau u32 prog_id; 1459d1fdd913SBrenden Blanco int err; 14604f91da26SJakub Kicinski u8 mode; 1461d1fdd913SBrenden Blanco 1462ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1463d1fdd913SBrenden Blanco if (!xdp) 1464d1fdd913SBrenden Blanco return -EMSGSIZE; 1465b5cdae32SDavid S. Miller 1466a25717d2SJakub Kicinski prog_id = 0; 1467a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1468202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1469202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1470202aabe8SJakub Kicinski if (err) 1471a25717d2SJakub Kicinski goto err_cancel; 1472202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1473202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1474202aabe8SJakub Kicinski if (err) 1475a25717d2SJakub Kicinski goto err_cancel; 1476202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1477202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1478202aabe8SJakub Kicinski if (err) 1479a25717d2SJakub Kicinski goto err_cancel; 1480a25717d2SJakub Kicinski 14814f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1482d1fdd913SBrenden Blanco if (err) 1483d1fdd913SBrenden Blanco goto err_cancel; 1484d1fdd913SBrenden Blanco 1485a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 148658038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 148758038695SMartin KaFai Lau if (err) 148858038695SMartin KaFai Lau goto err_cancel; 148958038695SMartin KaFai Lau } 149058038695SMartin KaFai Lau 1491d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1492d1fdd913SBrenden Blanco return 0; 1493d1fdd913SBrenden Blanco 1494d1fdd913SBrenden Blanco err_cancel: 1495d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1496d1fdd913SBrenden Blanco return err; 1497d1fdd913SBrenden Blanco } 1498d1fdd913SBrenden Blanco 14993d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15003d3ea5afSVlad Yasevich { 15013d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15023d3ea5afSVlad Yasevich 15033d3ea5afSVlad Yasevich switch (event) { 15043d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15053d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15063d3ea5afSVlad Yasevich break; 15073d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15083d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15093d3ea5afSVlad Yasevich break; 15103d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15113d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15123d3ea5afSVlad Yasevich break; 15133d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15143d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15153d3ea5afSVlad Yasevich break; 15163d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15173d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15183d3ea5afSVlad Yasevich break; 15193d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15203d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15213d3ea5afSVlad Yasevich break; 15223d3ea5afSVlad Yasevich default: 15233d3ea5afSVlad Yasevich break; 15243d3ea5afSVlad Yasevich } 15253d3ea5afSVlad Yasevich 15263d3ea5afSVlad Yasevich return rtnl_event_type; 15273d3ea5afSVlad Yasevich } 15283d3ea5afSVlad Yasevich 152979110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 153079110a04SFlorian Westphal { 153179110a04SFlorian Westphal const struct net_device *upper_dev; 153279110a04SFlorian Westphal int ret = 0; 153379110a04SFlorian Westphal 153479110a04SFlorian Westphal rcu_read_lock(); 153579110a04SFlorian Westphal 153679110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 153779110a04SFlorian Westphal if (upper_dev) 153879110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 153979110a04SFlorian Westphal 154079110a04SFlorian Westphal rcu_read_unlock(); 154179110a04SFlorian Westphal return ret; 154279110a04SFlorian Westphal } 154379110a04SFlorian Westphal 1544feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1545feadc4b6SSabrina Dubroca bool force) 154679110a04SFlorian Westphal { 154779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 154879110a04SFlorian Westphal 1549feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 155079110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1551feadc4b6SSabrina Dubroca 1552feadc4b6SSabrina Dubroca return 0; 155379110a04SFlorian Westphal } 155479110a04SFlorian Westphal 15556c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15566c557001SFlorian Westphal struct net_device *dev) 15576c557001SFlorian Westphal { 15586c557001SFlorian Westphal char buf[IFALIASZ]; 15596c557001SFlorian Westphal int ret; 15606c557001SFlorian Westphal 15616c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15626c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15636c557001SFlorian Westphal } 15646c557001SFlorian Westphal 1565b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 156679e1ad14SJiri Benc const struct net_device *dev, 1567d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1568b1e66b9aSFlorian Westphal { 1569feadc4b6SSabrina Dubroca bool put_iflink = false; 1570feadc4b6SSabrina Dubroca 1571b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1572b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1573b1e66b9aSFlorian Westphal 1574b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1575d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1576b1e66b9aSFlorian Westphal 1577b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1578b1e66b9aSFlorian Westphal return -EMSGSIZE; 1579feadc4b6SSabrina Dubroca 1580feadc4b6SSabrina Dubroca put_iflink = true; 1581b1e66b9aSFlorian Westphal } 1582b1e66b9aSFlorian Westphal } 1583b1e66b9aSFlorian Westphal 1584feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1585b1e66b9aSFlorian Westphal } 1586b1e66b9aSFlorian Westphal 1587070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1588070cbf5bSFlorian Westphal const struct net_device *dev, 1589070cbf5bSFlorian Westphal u32 ext_filter_mask) 1590070cbf5bSFlorian Westphal { 1591070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1592070cbf5bSFlorian Westphal struct nlattr *af_spec; 1593070cbf5bSFlorian Westphal 1594ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1595070cbf5bSFlorian Westphal if (!af_spec) 1596070cbf5bSFlorian Westphal return -EMSGSIZE; 1597070cbf5bSFlorian Westphal 15985fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1599070cbf5bSFlorian Westphal struct nlattr *af; 1600070cbf5bSFlorian Westphal int err; 1601070cbf5bSFlorian Westphal 1602070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1603070cbf5bSFlorian Westphal continue; 1604070cbf5bSFlorian Westphal 1605ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1606070cbf5bSFlorian Westphal if (!af) 1607070cbf5bSFlorian Westphal return -EMSGSIZE; 1608070cbf5bSFlorian Westphal 1609070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1610070cbf5bSFlorian Westphal /* 1611070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1612070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1613070cbf5bSFlorian Westphal * means we should trim the attribute header and 1614070cbf5bSFlorian Westphal * continue. 1615070cbf5bSFlorian Westphal */ 1616070cbf5bSFlorian Westphal if (err == -ENODATA) 1617070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1618070cbf5bSFlorian Westphal else if (err < 0) 1619070cbf5bSFlorian Westphal return -EMSGSIZE; 1620070cbf5bSFlorian Westphal 1621070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1622070cbf5bSFlorian Westphal } 1623070cbf5bSFlorian Westphal 1624070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1625070cbf5bSFlorian Westphal return 0; 1626070cbf5bSFlorian Westphal } 1627070cbf5bSFlorian Westphal 162888f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 162988f4fb0cSJiri Pirko const struct net_device *dev) 163088f4fb0cSJiri Pirko { 163188f4fb0cSJiri Pirko struct netdev_name_node *name_node; 163288f4fb0cSJiri Pirko int count = 0; 163388f4fb0cSJiri Pirko 163488f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 163588f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 163688f4fb0cSJiri Pirko return -EMSGSIZE; 163788f4fb0cSJiri Pirko count++; 163888f4fb0cSJiri Pirko } 163988f4fb0cSJiri Pirko return count; 164088f4fb0cSJiri Pirko } 164188f4fb0cSJiri Pirko 164288f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 164388f4fb0cSJiri Pirko const struct net_device *dev) 164488f4fb0cSJiri Pirko { 164588f4fb0cSJiri Pirko struct nlattr *prop_list; 164688f4fb0cSJiri Pirko int ret; 164788f4fb0cSJiri Pirko 164888f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 164988f4fb0cSJiri Pirko if (!prop_list) 165088f4fb0cSJiri Pirko return -EMSGSIZE; 165188f4fb0cSJiri Pirko 165288f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 165388f4fb0cSJiri Pirko if (ret <= 0) 165488f4fb0cSJiri Pirko goto nest_cancel; 165588f4fb0cSJiri Pirko 165688f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 165788f4fb0cSJiri Pirko return 0; 165888f4fb0cSJiri Pirko 165988f4fb0cSJiri Pirko nest_cancel: 166088f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 166188f4fb0cSJiri Pirko return ret; 166288f4fb0cSJiri Pirko } 166388f4fb0cSJiri Pirko 1664829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1665829eb208SRoopa Prabhu const struct net_device *dev) 1666829eb208SRoopa Prabhu { 1667829eb208SRoopa Prabhu struct nlattr *pr; 1668829eb208SRoopa Prabhu u32 preason; 1669829eb208SRoopa Prabhu 1670829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1671829eb208SRoopa Prabhu goto nla_put_failure; 1672829eb208SRoopa Prabhu 1673829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1674829eb208SRoopa Prabhu if (!preason) 1675829eb208SRoopa Prabhu return 0; 1676829eb208SRoopa Prabhu 1677829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1678829eb208SRoopa Prabhu if (!pr) 1679829eb208SRoopa Prabhu return -EMSGSIZE; 1680829eb208SRoopa Prabhu 1681829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1682829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1683829eb208SRoopa Prabhu goto nla_put_failure; 1684829eb208SRoopa Prabhu } 1685829eb208SRoopa Prabhu 1686829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1687829eb208SRoopa Prabhu return 0; 1688829eb208SRoopa Prabhu 1689829eb208SRoopa Prabhu nla_put_failure: 1690829eb208SRoopa Prabhu return -EMSGSIZE; 1691829eb208SRoopa Prabhu } 1692829eb208SRoopa Prabhu 169379e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 169479e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1695575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 16963d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 169738e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1698d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1699b60c5115SThomas Graf { 1700b60c5115SThomas Graf struct ifinfomsg *ifm; 17011da177e4SLinus Torvalds struct nlmsghdr *nlh; 17021da177e4SLinus Torvalds 17032907c35fSEric Dumazet ASSERT_RTNL(); 1704b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1705b60c5115SThomas Graf if (nlh == NULL) 170626932566SPatrick McHardy return -EMSGSIZE; 17071da177e4SLinus Torvalds 1708b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1709b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1710b60c5115SThomas Graf ifm->__ifi_pad = 0; 1711b60c5115SThomas Graf ifm->ifi_type = dev->type; 1712b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1713b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1714b60c5115SThomas Graf ifm->ifi_change = change; 17151da177e4SLinus Torvalds 17167e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 171779e1ad14SJiri Benc goto nla_put_failure; 171879e1ad14SJiri Benc 1719a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1720a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1721a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1722a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1723a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1724a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 17253e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 17263e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1727a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1728edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 172976ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1730c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1731c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1732eac1b93cSCoco Li nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) || 17331d69c2b3SMark A. Greer #ifdef CONFIG_RPS 173476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 17351d69c2b3SMark A. Greer #endif 173679110a04SFlorian Westphal put_master_ifindex(skb, dev) || 17379a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1738a6574349SDavid S. Miller (dev->qdisc && 1739a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 17406c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 17412d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1742b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1743b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1744b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1745b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1746b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1747b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1748a6574349SDavid S. Miller goto nla_put_failure; 17490b815a1aSStephen Hemminger 1750829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1751829eb208SRoopa Prabhu goto nla_put_failure; 1752829eb208SRoopa Prabhu 17533d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 17543d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 17553d3ea5afSVlad Yasevich goto nla_put_failure; 17563d3ea5afSVlad Yasevich } 17573d3ea5afSVlad Yasevich 1758b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1759a6574349SDavid S. Miller goto nla_put_failure; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if (dev->addr_len) { 1762a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1763a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1764a6574349SDavid S. Miller goto nla_put_failure; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 176766cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 176866cae9edSJiri Pirko goto nla_put_failure; 176966cae9edSJiri Pirko 1770db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1771db24a904SDavid Ahern goto nla_put_failure; 1772db24a904SDavid Ahern 177382f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 177482f28412SJiri Pirko goto nla_put_failure; 177582f28412SJiri Pirko 1776b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1777b60c5115SThomas Graf goto nla_put_failure; 1778b60c5115SThomas Graf 1779250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1780a6574349SDavid S. Miller goto nla_put_failure; 178157b61080SScott Feldman 1782c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 178357b61080SScott Feldman goto nla_put_failure; 178457b61080SScott Feldman 1785d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1786d1fdd913SBrenden Blanco goto nla_put_failure; 1787d1fdd913SBrenden Blanco 1788ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 178938f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 179038f7b870SPatrick McHardy goto nla_put_failure; 179138f7b870SPatrick McHardy } 179238f7b870SPatrick McHardy 1793d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1794d37512a2SNicolas Dichtel goto nla_put_failure; 1795d37512a2SNicolas Dichtel 17966621dd29SNicolas Dichtel if (new_nsid && 17976621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 17986621dd29SNicolas Dichtel goto nla_put_failure; 179938e01b30SNicolas Dichtel if (new_ifindex && 180038e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 180138e01b30SNicolas Dichtel goto nla_put_failure; 180238e01b30SNicolas Dichtel 1803f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1804f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1805f74877a5SMichal Kubecek goto nla_put_failure; 18066621dd29SNicolas Dichtel 18075fa85a09SFlorian Westphal rcu_read_lock(); 1808070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 18095fa85a09SFlorian Westphal goto nla_put_failure_rcu; 18105fa85a09SFlorian Westphal rcu_read_unlock(); 1811f8ff182cSThomas Graf 181288f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 181388f4fb0cSJiri Pirko goto nla_put_failure; 181488f4fb0cSJiri Pirko 181500e77ed8SJohannes Berg if (dev->dev.parent && 181600e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_NAME, 181700e77ed8SJohannes Berg dev_name(dev->dev.parent))) 181800e77ed8SJohannes Berg goto nla_put_failure; 181900e77ed8SJohannes Berg 182000e77ed8SJohannes Berg if (dev->dev.parent && dev->dev.parent->bus && 182100e77ed8SJohannes Berg nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 182200e77ed8SJohannes Berg dev->dev.parent->bus->name)) 182300e77ed8SJohannes Berg goto nla_put_failure; 182400e77ed8SJohannes Berg 1825053c095aSJohannes Berg nlmsg_end(skb, nlh); 1826053c095aSJohannes Berg return 0; 1827b60c5115SThomas Graf 18285fa85a09SFlorian Westphal nla_put_failure_rcu: 18295fa85a09SFlorian Westphal rcu_read_unlock(); 1830b60c5115SThomas Graf nla_put_failure: 183126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 183226932566SPatrick McHardy return -EMSGSIZE; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 1835f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1836f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1837f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1838f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1839f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1840f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1841f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1842f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1843f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1844f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1845f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1846f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1847f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1848f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1849f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1850f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 18512459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 18522459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 18532459b4c6SNicolas Dichtel */ 18542459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1855f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1856f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1857f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1858f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1859f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1860f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1861f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1862f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 186346e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 186446e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 186502637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 18662d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 186782f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1868317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 186988d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1870d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 18713d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1872db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 18737e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1874b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1875b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 18763e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 18773e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 187836fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 187936fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 188036fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1881f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1882829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 18837e4a5131SAndrei Vagin [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 188488b71053SJohannes Berg [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 1885eac1b93cSCoco Li [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 1886f7b12606SJiri Pirko }; 1887f7b12606SJiri Pirko 1888f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1889f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1890f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1891f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1892f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1893f7b12606SJiri Pirko }; 1894f7b12606SJiri Pirko 1895f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1896364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 189775345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1898364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 189979aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1900364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1901364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1902364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1903364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 190401a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 19053b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1906dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1907cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1908cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 19093b766cd8SEran Ben Elisha }; 19103b766cd8SEran Ben Elisha 1911f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1912f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1913f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1914f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1915f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1916f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1917f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1918f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1919f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1920f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1921025331dfSDaniel Borkmann 1922025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1923025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1924025331dfSDaniel Borkmann * combination with structs. 1925025331dfSDaniel Borkmann */ 1926025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1927025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1928f7b12606SJiri Pirko }; 1929f7b12606SJiri Pirko 1930d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 193192234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 1932d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 193392234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 1934d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 193585de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 193658038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1937d1fdd913SBrenden Blanco }; 1938d1fdd913SBrenden Blanco 1939dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1940dc599f76SDavid Ahern { 1941dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1942dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1943dc599f76SDavid Ahern 19448cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 1945dc599f76SDavid Ahern return NULL; 1946dc599f76SDavid Ahern 1947dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1948dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1949dc599f76SDavid Ahern 1950872f6903SFrancis Laniel nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1951dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1952dc599f76SDavid Ahern } 1953dc599f76SDavid Ahern 1954dc599f76SDavid Ahern return ops; 1955dc599f76SDavid Ahern } 1956dc599f76SDavid Ahern 1957dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1958dc599f76SDavid Ahern { 1959dc599f76SDavid Ahern struct net_device *master; 1960dc599f76SDavid Ahern 1961dc599f76SDavid Ahern if (!master_idx) 1962dc599f76SDavid Ahern return false; 1963dc599f76SDavid Ahern 1964dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1965d3432bf1SLahav Schlesinger 1966d3432bf1SLahav Schlesinger /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 1967d3432bf1SLahav Schlesinger * another invalid value for ifindex to denote "no master". 1968d3432bf1SLahav Schlesinger */ 1969d3432bf1SLahav Schlesinger if (master_idx == -1) 1970d3432bf1SLahav Schlesinger return !!master; 1971d3432bf1SLahav Schlesinger 1972dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1973dc599f76SDavid Ahern return true; 1974dc599f76SDavid Ahern 1975dc599f76SDavid Ahern return false; 1976dc599f76SDavid Ahern } 1977dc599f76SDavid Ahern 1978dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1979dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1980dc599f76SDavid Ahern { 1981dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1982dc599f76SDavid Ahern return true; 1983dc599f76SDavid Ahern 1984dc599f76SDavid Ahern return false; 1985dc599f76SDavid Ahern } 1986dc599f76SDavid Ahern 1987dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1988dc599f76SDavid Ahern int master_idx, 1989dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1990dc599f76SDavid Ahern { 1991dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1992dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1993dc599f76SDavid Ahern return true; 1994dc599f76SDavid Ahern 1995dc599f76SDavid Ahern return false; 1996dc599f76SDavid Ahern } 1997dc599f76SDavid Ahern 1998c383edc4SChristian Brauner /** 1999c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2000c383edc4SChristian Brauner * @sk: netlink socket 2001c383edc4SChristian Brauner * @netnsid: network namespace identifier 2002c383edc4SChristian Brauner * 2003c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 2004c383edc4SChristian Brauner * pointer on failure. 2005c383edc4SChristian Brauner */ 2006c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 200779e1ad14SJiri Benc { 200879e1ad14SJiri Benc struct net *net; 200979e1ad14SJiri Benc 2010f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 201179e1ad14SJiri Benc if (!net) 201279e1ad14SJiri Benc return ERR_PTR(-EINVAL); 201379e1ad14SJiri Benc 201479e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 201579e1ad14SJiri Benc * the user namespace owning the target net ns. 201679e1ad14SJiri Benc */ 2017f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 201879e1ad14SJiri Benc put_net(net); 201979e1ad14SJiri Benc return ERR_PTR(-EACCES); 202079e1ad14SJiri Benc } 202179e1ad14SJiri Benc return net; 202279e1ad14SJiri Benc } 2023c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 202479e1ad14SJiri Benc 2025905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2026905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2027905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2028905cf0abSDavid Ahern { 2029905cf0abSDavid Ahern int hdrlen; 2030905cf0abSDavid Ahern 2031905cf0abSDavid Ahern if (strict_check) { 2032905cf0abSDavid Ahern struct ifinfomsg *ifm; 2033905cf0abSDavid Ahern 2034905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2035905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2036905cf0abSDavid Ahern return -EINVAL; 2037905cf0abSDavid Ahern } 2038905cf0abSDavid Ahern 2039905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2040905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2041905cf0abSDavid Ahern ifm->ifi_change) { 2042905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2043905cf0abSDavid Ahern return -EINVAL; 2044905cf0abSDavid Ahern } 2045905cf0abSDavid Ahern if (ifm->ifi_index) { 2046905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2047905cf0abSDavid Ahern return -EINVAL; 2048905cf0abSDavid Ahern } 2049905cf0abSDavid Ahern 20508cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 20518cb08174SJohannes Berg IFLA_MAX, ifla_policy, 20528cb08174SJohannes Berg extack); 2053905cf0abSDavid Ahern } 2054905cf0abSDavid Ahern 2055905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2056905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2057905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2058905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2059905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2060905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2061905cf0abSDavid Ahern */ 2062905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2063905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2064905cf0abSDavid Ahern 20658cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 20668cb08174SJohannes Berg extack); 2067905cf0abSDavid Ahern } 2068905cf0abSDavid Ahern 2069b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 20701da177e4SLinus Torvalds { 2071905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2072905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 20733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 207479e1ad14SJiri Benc struct net *tgt_net = net; 20757c28bd0bSEric Dumazet int h, s_h; 20767c28bd0bSEric Dumazet int idx = 0, s_idx; 20771da177e4SLinus Torvalds struct net_device *dev; 20787c28bd0bSEric Dumazet struct hlist_head *head; 2079115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2080115c9b81SGreg Rose u32 ext_filter_mask = 0; 2081dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2082dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2083dc599f76SDavid Ahern int master_idx = 0; 208479e1ad14SJiri Benc int netnsid = -1; 2085905cf0abSDavid Ahern int err, i; 20861da177e4SLinus Torvalds 20877c28bd0bSEric Dumazet s_h = cb->args[0]; 20887c28bd0bSEric Dumazet s_idx = cb->args[1]; 20897c28bd0bSEric Dumazet 2090905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2091905cf0abSDavid Ahern if (err < 0) { 2092905cf0abSDavid Ahern if (cb->strict_check) 2093905cf0abSDavid Ahern return err; 2094e5eca6d4SMichal Schmidt 2095905cf0abSDavid Ahern goto walk_entries; 209679e1ad14SJiri Benc } 209779e1ad14SJiri Benc 2098905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2099905cf0abSDavid Ahern if (!tb[i]) 2100905cf0abSDavid Ahern continue; 2101dc599f76SDavid Ahern 2102905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2103905cf0abSDavid Ahern switch (i) { 2104905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2105905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2106905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2107905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2108905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2109905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2110905cf0abSDavid Ahern } 2111905cf0abSDavid Ahern break; 2112905cf0abSDavid Ahern case IFLA_EXT_MASK: 2113905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2114905cf0abSDavid Ahern break; 2115905cf0abSDavid Ahern case IFLA_MASTER: 2116905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2117905cf0abSDavid Ahern break; 2118905cf0abSDavid Ahern case IFLA_LINKINFO: 2119905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2120905cf0abSDavid Ahern break; 2121905cf0abSDavid Ahern default: 2122905cf0abSDavid Ahern if (cb->strict_check) { 2123905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2124905cf0abSDavid Ahern return -EINVAL; 2125905cf0abSDavid Ahern } 2126905cf0abSDavid Ahern } 2127905cf0abSDavid Ahern } 2128dc599f76SDavid Ahern 2129dc599f76SDavid Ahern if (master_idx || kind_ops) 2130dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2131115c9b81SGreg Rose 2132905cf0abSDavid Ahern walk_entries: 21337c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 21347562f876SPavel Emelianov idx = 0; 213579e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2136cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2137dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 21383f0ae05dSZhang Shengju goto cont; 21391da177e4SLinus Torvalds if (idx < s_idx) 21407562f876SPavel Emelianov goto cont; 214179e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 214279e1ad14SJiri Benc RTM_NEWLINK, 214315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2144905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 214538e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2146d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2147973462bbSDavid Gibson 2148f6c5775fSDavid Ahern if (err < 0) { 2149f6c5775fSDavid Ahern if (likely(skb->len)) 21507c28bd0bSEric Dumazet goto out; 21514e985adaSThomas Graf 2152f6c5775fSDavid Ahern goto out_err; 2153f6c5775fSDavid Ahern } 21547562f876SPavel Emelianov cont: 21557562f876SPavel Emelianov idx++; 21561da177e4SLinus Torvalds } 21577c28bd0bSEric Dumazet } 21587c28bd0bSEric Dumazet out: 2159f6c5775fSDavid Ahern err = skb->len; 2160f6c5775fSDavid Ahern out_err: 21617c28bd0bSEric Dumazet cb->args[1] = idx; 21627c28bd0bSEric Dumazet cb->args[0] = h; 2163a9ecb0cbSzhang kai cb->seq = tgt_net->dev_base_seq; 2164d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 216579e1ad14SJiri Benc if (netnsid >= 0) 216679e1ad14SJiri Benc put_net(tgt_net); 21671da177e4SLinus Torvalds 2168f6c5775fSDavid Ahern return err; 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 2171fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2172fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2173f7b12606SJiri Pirko { 21748cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 21758cb08174SJohannes Berg exterr); 2176f7b12606SJiri Pirko } 2177f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 217857b61080SScott Feldman 217981adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 218081adee47SEric W. Biederman { 218181adee47SEric W. Biederman struct net *net; 218281adee47SEric W. Biederman /* Examine the link attributes and figure out which 218381adee47SEric W. Biederman * network namespace we are talking about. 218481adee47SEric W. Biederman */ 218581adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 218681adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2187f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2188f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 218981adee47SEric W. Biederman else 219081adee47SEric W. Biederman net = get_net(src_net); 219181adee47SEric W. Biederman return net; 219281adee47SEric W. Biederman } 219381adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 219481adee47SEric W. Biederman 21957c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 21967c4f63baSChristian Brauner * examining the link attributes in the following order: 21977c4f63baSChristian Brauner * 21987c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 21997c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 22007e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 22017c4f63baSChristian Brauner */ 22027c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 22037c4f63baSChristian Brauner struct nlattr *tb[]) 22047c4f63baSChristian Brauner { 22057c4f63baSChristian Brauner struct net *net; 22067c4f63baSChristian Brauner 22077c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 22087c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 22097c4f63baSChristian Brauner 22107e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 22117c4f63baSChristian Brauner return get_net(src_net); 22127c4f63baSChristian Brauner 22137e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 22147c4f63baSChristian Brauner if (!net) 22157c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 22167c4f63baSChristian Brauner 22177c4f63baSChristian Brauner return net; 22187c4f63baSChristian Brauner } 22197c4f63baSChristian Brauner 22207c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 22217c4f63baSChristian Brauner struct net *src_net, 22227c4f63baSChristian Brauner struct nlattr *tb[], int cap) 22237c4f63baSChristian Brauner { 22247c4f63baSChristian Brauner struct net *net; 22257c4f63baSChristian Brauner 22267c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 22277c4f63baSChristian Brauner if (IS_ERR(net)) 22287c4f63baSChristian Brauner return net; 22297c4f63baSChristian Brauner 22307c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 22317c4f63baSChristian Brauner put_net(net); 22327c4f63baSChristian Brauner return ERR_PTR(-EPERM); 22337c4f63baSChristian Brauner } 22347c4f63baSChristian Brauner 22357c4f63baSChristian Brauner return net; 22367c4f63baSChristian Brauner } 22377c4f63baSChristian Brauner 22384ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 22394ff66caeSChristian Brauner * potentially referring to different network namespaces. 22404ff66caeSChristian Brauner */ 22414ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 22424ff66caeSChristian Brauner struct netlink_ext_ack *extack, 22434ff66caeSChristian Brauner bool netns_id_only) 22444ff66caeSChristian Brauner { 22454ff66caeSChristian Brauner 22464ff66caeSChristian Brauner if (netns_id_only) { 22474ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 22484ff66caeSChristian Brauner return 0; 22494ff66caeSChristian Brauner 22504ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 22514ff66caeSChristian Brauner return -EOPNOTSUPP; 22524ff66caeSChristian Brauner } 22534ff66caeSChristian Brauner 22547e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 22554ff66caeSChristian Brauner goto invalid_attr; 22564ff66caeSChristian Brauner 22577e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 22584ff66caeSChristian Brauner goto invalid_attr; 22594ff66caeSChristian Brauner 22607e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 22614ff66caeSChristian Brauner goto invalid_attr; 22624ff66caeSChristian Brauner 22634ff66caeSChristian Brauner return 0; 22644ff66caeSChristian Brauner 22654ff66caeSChristian Brauner invalid_attr: 22664ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 22674ff66caeSChristian Brauner return -EINVAL; 22684ff66caeSChristian Brauner } 22694ff66caeSChristian Brauner 22708679c31eSRocco Yue static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 22718679c31eSRocco Yue struct netlink_ext_ack *extack) 22721840bb13SThomas Graf { 22731840bb13SThomas Graf if (dev) { 22741840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 22751840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 22761840bb13SThomas Graf return -EINVAL; 22771840bb13SThomas Graf 22781840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 22791840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 22801840bb13SThomas Graf return -EINVAL; 22811840bb13SThomas Graf } 22821840bb13SThomas Graf 2283cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2284cf7afbfeSThomas Graf struct nlattr *af; 2285cf7afbfeSThomas Graf int rem, err; 2286cf7afbfeSThomas Graf 2287cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2288cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2289cf7afbfeSThomas Graf 22905fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 2291a100243dSCong Wang if (!af_ops) 2292cf7afbfeSThomas Graf return -EAFNOSUPPORT; 2293cf7afbfeSThomas Graf 2294a100243dSCong Wang if (!af_ops->set_link_af) 2295cf7afbfeSThomas Graf return -EOPNOTSUPP; 2296cf7afbfeSThomas Graf 2297cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 22988679c31eSRocco Yue err = af_ops->validate_link_af(dev, af, extack); 2299a100243dSCong Wang if (err < 0) 2300cf7afbfeSThomas Graf return err; 2301cf7afbfeSThomas Graf } 2302cf7afbfeSThomas Graf } 2303cf7afbfeSThomas Graf } 2304cf7afbfeSThomas Graf 2305eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2306eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2307eac1b93cSCoco Li 2308eac1b93cSCoco Li if (gro_max_size > GRO_MAX_SIZE) { 2309eac1b93cSCoco Li NL_SET_ERR_MSG(extack, "too big gro_max_size"); 2310eac1b93cSCoco Li return -EINVAL; 2311eac1b93cSCoco Li } 2312eac1b93cSCoco Li } 23131840bb13SThomas Graf return 0; 23141840bb13SThomas Graf } 23151840bb13SThomas Graf 2316cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2317cc8e27ccSEli Cohen int guid_type) 2318cc8e27ccSEli Cohen { 2319cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2320cc8e27ccSEli Cohen 2321cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2322cc8e27ccSEli Cohen } 2323cc8e27ccSEli Cohen 2324cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2325cc8e27ccSEli Cohen { 2326cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2327cc8e27ccSEli Cohen return -EOPNOTSUPP; 2328cc8e27ccSEli Cohen 2329cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2330cc8e27ccSEli Cohen } 2331cc8e27ccSEli Cohen 23324f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2333c02db8c6SChris Wright { 2334c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 23354f7d2cdfSDaniel Borkmann int err = -EINVAL; 2336c02db8c6SChris Wright 23374f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 23384f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 23394f7d2cdfSDaniel Borkmann 2340ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2341ff08ddbaSDan Carpenter return -EINVAL; 2342c02db8c6SChris Wright err = -EOPNOTSUPP; 2343c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2344c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2345c02db8c6SChris Wright ivm->mac); 23464f7d2cdfSDaniel Borkmann if (err < 0) 23474f7d2cdfSDaniel Borkmann return err; 2348c02db8c6SChris Wright } 23494f7d2cdfSDaniel Borkmann 23504f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 23514f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 23524f7d2cdfSDaniel Borkmann 2353ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2354ff08ddbaSDan Carpenter return -EINVAL; 2355c02db8c6SChris Wright err = -EOPNOTSUPP; 2356c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 23574f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 235879aab093SMoshe Shemesh ivv->qos, 235979aab093SMoshe Shemesh htons(ETH_P_8021Q)); 236079aab093SMoshe Shemesh if (err < 0) 236179aab093SMoshe Shemesh return err; 236279aab093SMoshe Shemesh } 236379aab093SMoshe Shemesh 236479aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 236579aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 236679aab093SMoshe Shemesh struct nlattr *attr; 236779aab093SMoshe Shemesh int rem, len = 0; 236879aab093SMoshe Shemesh 236979aab093SMoshe Shemesh err = -EOPNOTSUPP; 237079aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 237179aab093SMoshe Shemesh return err; 237279aab093SMoshe Shemesh 237379aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 237479aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 237579aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 237679aab093SMoshe Shemesh return -EINVAL; 237779aab093SMoshe Shemesh } 237879aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 237979aab093SMoshe Shemesh return -EOPNOTSUPP; 238079aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 238179aab093SMoshe Shemesh 238279aab093SMoshe Shemesh len++; 238379aab093SMoshe Shemesh } 2384fa34cd94SArnd Bergmann if (len == 0) 2385fa34cd94SArnd Bergmann return -EINVAL; 2386fa34cd94SArnd Bergmann 2387ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2388ff08ddbaSDan Carpenter return -EINVAL; 238979aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 239079aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 23914f7d2cdfSDaniel Borkmann if (err < 0) 23924f7d2cdfSDaniel Borkmann return err; 2393c02db8c6SChris Wright } 23944f7d2cdfSDaniel Borkmann 23954f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 23964f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2397ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 23984f7d2cdfSDaniel Borkmann 2399ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2400ff08ddbaSDan Carpenter return -EINVAL; 2401c02db8c6SChris Wright err = -EOPNOTSUPP; 2402ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 24034f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 24044f7d2cdfSDaniel Borkmann if (err < 0) 24054f7d2cdfSDaniel Borkmann return err; 24064f7d2cdfSDaniel Borkmann 2407ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2408ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2409ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2410ed616689SSucheta Chakraborty ivf.min_tx_rate, 2411c02db8c6SChris Wright ivt->rate); 24124f7d2cdfSDaniel Borkmann if (err < 0) 24134f7d2cdfSDaniel Borkmann return err; 2414c02db8c6SChris Wright } 24154f7d2cdfSDaniel Borkmann 24164f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 24174f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 24184f7d2cdfSDaniel Borkmann 2419ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2420ff08ddbaSDan Carpenter return -EINVAL; 2421ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2422ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2423ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2424ed616689SSucheta Chakraborty ivt->min_tx_rate, 2425ed616689SSucheta Chakraborty ivt->max_tx_rate); 24264f7d2cdfSDaniel Borkmann if (err < 0) 24274f7d2cdfSDaniel Borkmann return err; 2428ed616689SSucheta Chakraborty } 24294f7d2cdfSDaniel Borkmann 24304f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 24314f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 24324f7d2cdfSDaniel Borkmann 2433ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2434ff08ddbaSDan Carpenter return -EINVAL; 24355f8444a3SGreg Rose err = -EOPNOTSUPP; 24365f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 24375f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 24385f8444a3SGreg Rose ivs->setting); 24394f7d2cdfSDaniel Borkmann if (err < 0) 24404f7d2cdfSDaniel Borkmann return err; 24415f8444a3SGreg Rose } 24424f7d2cdfSDaniel Borkmann 24434f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 24444f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 24454f7d2cdfSDaniel Borkmann 2446ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2447ff08ddbaSDan Carpenter return -EINVAL; 24481d8faf48SRony Efraim err = -EOPNOTSUPP; 24491d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 24501d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 24511d8faf48SRony Efraim ivl->link_state); 24524f7d2cdfSDaniel Borkmann if (err < 0) 24534f7d2cdfSDaniel Borkmann return err; 24541d8faf48SRony Efraim } 24554f7d2cdfSDaniel Borkmann 24564f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 245701a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 245801a3d796SVlad Zolotarov 245901a3d796SVlad Zolotarov err = -EOPNOTSUPP; 24604f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2461ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2462ff08ddbaSDan Carpenter return -EINVAL; 246301a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 24644f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 246501a3d796SVlad Zolotarov ivrssq_en->setting); 24664f7d2cdfSDaniel Borkmann if (err < 0) 24674f7d2cdfSDaniel Borkmann return err; 246801a3d796SVlad Zolotarov } 24694f7d2cdfSDaniel Borkmann 2470dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2471dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2472dd461d6aSHiroshi Shimamoto 2473ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2474ff08ddbaSDan Carpenter return -EINVAL; 2475dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2476dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2477dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2478dd461d6aSHiroshi Shimamoto if (err < 0) 2479dd461d6aSHiroshi Shimamoto return err; 2480dd461d6aSHiroshi Shimamoto } 2481dd461d6aSHiroshi Shimamoto 2482cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2483cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2484cc8e27ccSEli Cohen 2485ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2486ff08ddbaSDan Carpenter return -EINVAL; 2487cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2488cc8e27ccSEli Cohen return -EOPNOTSUPP; 2489cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2490cc8e27ccSEli Cohen } 2491cc8e27ccSEli Cohen 2492cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2493cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2494cc8e27ccSEli Cohen 2495ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2496ff08ddbaSDan Carpenter return -EINVAL; 2497cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2498cc8e27ccSEli Cohen return -EOPNOTSUPP; 2499cc8e27ccSEli Cohen 2500cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2501cc8e27ccSEli Cohen } 2502cc8e27ccSEli Cohen 2503c02db8c6SChris Wright return err; 2504c02db8c6SChris Wright } 2505c02db8c6SChris Wright 250633eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 250733eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2508fbaec0eaSJiri Pirko { 2509898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2510fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2511fbaec0eaSJiri Pirko int err; 2512fbaec0eaSJiri Pirko 2513898e5061SJiri Pirko if (upper_dev) { 2514898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2515fbaec0eaSJiri Pirko return 0; 2516898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2517fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2518898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2519fbaec0eaSJiri Pirko if (err) 2520fbaec0eaSJiri Pirko return err; 2521fbaec0eaSJiri Pirko } else { 2522fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2523fbaec0eaSJiri Pirko } 2524fbaec0eaSJiri Pirko } 2525fbaec0eaSJiri Pirko 2526fbaec0eaSJiri Pirko if (ifindex) { 2527898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2528898e5061SJiri Pirko if (!upper_dev) 2529fbaec0eaSJiri Pirko return -EINVAL; 2530898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2531fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 253233eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2533fbaec0eaSJiri Pirko if (err) 2534fbaec0eaSJiri Pirko return err; 2535fbaec0eaSJiri Pirko } else { 2536fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2537fbaec0eaSJiri Pirko } 2538fbaec0eaSJiri Pirko } 2539fbaec0eaSJiri Pirko return 0; 2540fbaec0eaSJiri Pirko } 2541fbaec0eaSJiri Pirko 2542829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2543829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2544829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2545829eb208SRoopa Prabhu }; 2546829eb208SRoopa Prabhu 2547829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2548829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2549829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2550829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2551829eb208SRoopa Prabhu { 2552829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2553829eb208SRoopa Prabhu unsigned long mask = 0; 2554829eb208SRoopa Prabhu u32 value; 2555829eb208SRoopa Prabhu bool proto_down; 2556829eb208SRoopa Prabhu int err; 2557829eb208SRoopa Prabhu 25582106efdaSJakub Kicinski if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) { 2559829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2560829eb208SRoopa Prabhu return -EOPNOTSUPP; 2561829eb208SRoopa Prabhu } 2562829eb208SRoopa Prabhu 2563829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2564829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2565829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2566829eb208SRoopa Prabhu nl_proto_down_reason, 2567829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2568829eb208SRoopa Prabhu NULL); 2569829eb208SRoopa Prabhu if (err < 0) 2570829eb208SRoopa Prabhu return err; 2571829eb208SRoopa Prabhu 2572829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2573829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2574829eb208SRoopa Prabhu return -EINVAL; 2575829eb208SRoopa Prabhu } 2576829eb208SRoopa Prabhu 2577829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2578829eb208SRoopa Prabhu 2579829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2580829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2581829eb208SRoopa Prabhu 2582829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2583829eb208SRoopa Prabhu } 2584829eb208SRoopa Prabhu 2585829eb208SRoopa Prabhu if (nl_proto_down) { 2586829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2587829eb208SRoopa Prabhu 2588d467d0bcSZheng Yongjun /* Don't turn off protodown if there are active reasons */ 2589829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2590829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2591829eb208SRoopa Prabhu return -EBUSY; 2592829eb208SRoopa Prabhu } 2593829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2594829eb208SRoopa Prabhu proto_down); 2595829eb208SRoopa Prabhu if (err) 2596829eb208SRoopa Prabhu return err; 2597829eb208SRoopa Prabhu } 2598829eb208SRoopa Prabhu 2599829eb208SRoopa Prabhu return 0; 2600829eb208SRoopa Prabhu } 2601829eb208SRoopa Prabhu 260290c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2603ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2604ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 260590f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 260690f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2607ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 260890c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 26090157f60cSPatrick McHardy { 2610d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 26110157f60cSPatrick McHardy int err; 26120157f60cSPatrick McHardy 26138679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 2614644c7eebSEric Dumazet if (err < 0) 2615644c7eebSEric Dumazet return err; 2616644c7eebSEric Dumazet 26177e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 261896a6b93bSAndrey Ignatov const char *pat = ifname && ifname[0] ? ifname : NULL; 2619eeb85a14SAndrei Vagin struct net *net; 2620eeb85a14SAndrei Vagin int new_ifindex; 2621eeb85a14SAndrei Vagin 2622eeb85a14SAndrei Vagin net = rtnl_link_get_net_capable(skb, dev_net(dev), 26237c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2624d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2625d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2626d8a5ec67SEric W. Biederman goto errout; 2627d8a5ec67SEric W. Biederman } 26287c4f63baSChristian Brauner 2629eeb85a14SAndrei Vagin if (tb[IFLA_NEW_IFINDEX]) 2630eeb85a14SAndrei Vagin new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]); 2631eeb85a14SAndrei Vagin else 2632eeb85a14SAndrei Vagin new_ifindex = 0; 2633eeb85a14SAndrei Vagin 263496a6b93bSAndrey Ignatov err = __dev_change_net_namespace(dev, net, pat, new_ifindex); 2635d8a5ec67SEric W. Biederman put_net(net); 2636d8a5ec67SEric W. Biederman if (err) 2637d8a5ec67SEric W. Biederman goto errout; 263890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2639d8a5ec67SEric W. Biederman } 2640d8a5ec67SEric W. Biederman 26410157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 26420157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 26430157f60cSPatrick McHardy struct ifmap k_map; 26440157f60cSPatrick McHardy 2645d314774cSStephen Hemminger if (!ops->ndo_set_config) { 26460157f60cSPatrick McHardy err = -EOPNOTSUPP; 26470157f60cSPatrick McHardy goto errout; 26480157f60cSPatrick McHardy } 26490157f60cSPatrick McHardy 26500157f60cSPatrick McHardy if (!netif_device_present(dev)) { 26510157f60cSPatrick McHardy err = -ENODEV; 26520157f60cSPatrick McHardy goto errout; 26530157f60cSPatrick McHardy } 26540157f60cSPatrick McHardy 26550157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 26560157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 26570157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 26580157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 26590157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 26600157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 26610157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 26620157f60cSPatrick McHardy 2663d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 26640157f60cSPatrick McHardy if (err < 0) 26650157f60cSPatrick McHardy goto errout; 26660157f60cSPatrick McHardy 2667ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 26680157f60cSPatrick McHardy } 26690157f60cSPatrick McHardy 26700157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 26710157f60cSPatrick McHardy struct sockaddr *sa; 26720157f60cSPatrick McHardy int len; 26730157f60cSPatrick McHardy 2674153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2675153711f9SWANG Cong sizeof(*sa)); 26760157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 26770157f60cSPatrick McHardy if (!sa) { 26780157f60cSPatrick McHardy err = -ENOMEM; 26790157f60cSPatrick McHardy goto errout; 26800157f60cSPatrick McHardy } 26810157f60cSPatrick McHardy sa->sa_family = dev->type; 26820157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 26830157f60cSPatrick McHardy dev->addr_len); 26843b23a32aSCong Wang err = dev_set_mac_address_user(dev, sa, extack); 26850157f60cSPatrick McHardy kfree(sa); 26860157f60cSPatrick McHardy if (err) 26870157f60cSPatrick McHardy goto errout; 268890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26890157f60cSPatrick McHardy } 26900157f60cSPatrick McHardy 26910157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 26927a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 26930157f60cSPatrick McHardy if (err < 0) 26940157f60cSPatrick McHardy goto errout; 269590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26960157f60cSPatrick McHardy } 26970157f60cSPatrick McHardy 2698cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2699cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2700ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2701cbda10faSVlad Dogaru } 2702cbda10faSVlad Dogaru 27030157f60cSPatrick McHardy /* 27040157f60cSPatrick McHardy * Interface selected by interface index but interface 27050157f60cSPatrick McHardy * name provided implies that a name change has been 27060157f60cSPatrick McHardy * requested. 27070157f60cSPatrick McHardy */ 27080157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 27090157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 27100157f60cSPatrick McHardy if (err < 0) 27110157f60cSPatrick McHardy goto errout; 271290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27130157f60cSPatrick McHardy } 27140157f60cSPatrick McHardy 27150b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 27160b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 27170b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 27180b815a1aSStephen Hemminger if (err < 0) 27190b815a1aSStephen Hemminger goto errout; 2720ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27210b815a1aSStephen Hemminger } 27220b815a1aSStephen Hemminger 27230157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 27240157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2725e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 27260157f60cSPatrick McHardy } 27270157f60cSPatrick McHardy 27280157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2729567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2730567c5e13SPetr Machata extack); 27315f9021cfSJohannes Berg if (err < 0) 27325f9021cfSJohannes Berg goto errout; 27330157f60cSPatrick McHardy } 27340157f60cSPatrick McHardy 2735fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 273633eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2737fbaec0eaSJiri Pirko if (err) 2738fbaec0eaSJiri Pirko goto errout; 273990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2740fbaec0eaSJiri Pirko } 2741fbaec0eaSJiri Pirko 27429a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 27439a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 27449a57247fSJiri Pirko if (err) 27459a57247fSJiri Pirko goto errout; 274690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27479a57247fSJiri Pirko } 27489a57247fSJiri Pirko 27495d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 27500cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 27515d1180fcSNicolas Dichtel 27526a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 27536a643ddbSCong Wang if (err) 275408294a26SJason Wang goto errout; 27552d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 275608294a26SJason Wang } 27570157f60cSPatrick McHardy 275846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 275946e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 276046e6b992SStephen Hemminger 276146e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 276246e6b992SStephen Hemminger err = -EINVAL; 276346e6b992SStephen Hemminger goto errout; 276446e6b992SStephen Hemminger } 276546e6b992SStephen Hemminger 276646e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 276746e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 276846e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 276946e6b992SStephen Hemminger } 277046e6b992SStephen Hemminger } 277146e6b992SStephen Hemminger 277246e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 277346e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 277446e6b992SStephen Hemminger 277546e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 277646e6b992SStephen Hemminger err = -EINVAL; 277746e6b992SStephen Hemminger goto errout; 277846e6b992SStephen Hemminger } 277946e6b992SStephen Hemminger 278046e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 27816d872df3SEric Dumazet netif_set_gso_max_segs(dev, max_segs); 278246e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 278346e6b992SStephen Hemminger } 278446e6b992SStephen Hemminger } 278546e6b992SStephen Hemminger 2786eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) { 2787eac1b93cSCoco Li u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 2788eac1b93cSCoco Li 2789eac1b93cSCoco Li if (dev->gro_max_size ^ gro_max_size) { 2790eac1b93cSCoco Li netif_set_gro_max_size(dev, gro_max_size); 2791eac1b93cSCoco Li status |= DO_SETLINK_MODIFIED; 2792eac1b93cSCoco Li } 2793eac1b93cSCoco Li } 2794eac1b93cSCoco Li 27950157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 279693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 27970157f60cSPatrick McHardy 27980157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 27991889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 28001889b0e7SNicolas Dichtel 2801fd888e85SSebastian Andrzej Siewior write_lock(&dev_base_lock); 28021889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2803ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 28041889b0e7SNicolas Dichtel dev->link_mode = value; 2805fd888e85SSebastian Andrzej Siewior write_unlock(&dev_base_lock); 28060157f60cSPatrick McHardy } 28070157f60cSPatrick McHardy 2808c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 28094f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2810c02db8c6SChris Wright struct nlattr *attr; 2811c02db8c6SChris Wright int rem; 28124f7d2cdfSDaniel Borkmann 2813c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 28144f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 28154f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2816253683bbSDavid Howells err = -EINVAL; 2817c02db8c6SChris Wright goto errout; 2818253683bbSDavid Howells } 28198cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 28208cb08174SJohannes Berg attr, 28218cb08174SJohannes Berg ifla_vf_policy, 28228cb08174SJohannes Berg NULL); 28234f7d2cdfSDaniel Borkmann if (err < 0) 28244f7d2cdfSDaniel Borkmann goto errout; 28254f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2826ebc08a6fSWilliams, Mitch A if (err < 0) 2827ebc08a6fSWilliams, Mitch A goto errout; 2828ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2829ebc08a6fSWilliams, Mitch A } 2830ebc08a6fSWilliams, Mitch A } 28310157f60cSPatrick McHardy err = 0; 28320157f60cSPatrick McHardy 283357b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 283457b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 283557b61080SScott Feldman struct nlattr *attr; 283657b61080SScott Feldman int vf; 283757b61080SScott Feldman int rem; 283857b61080SScott Feldman 283957b61080SScott Feldman err = -EOPNOTSUPP; 284057b61080SScott Feldman if (!ops->ndo_set_vf_port) 284157b61080SScott Feldman goto errout; 284257b61080SScott Feldman 284357b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2844035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2845035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2846035d210fSDaniel Borkmann err = -EINVAL; 2847035d210fSDaniel Borkmann goto errout; 2848035d210fSDaniel Borkmann } 28498cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 28508cb08174SJohannes Berg attr, 28518cb08174SJohannes Berg ifla_port_policy, 28528cb08174SJohannes Berg NULL); 285357b61080SScott Feldman if (err < 0) 285457b61080SScott Feldman goto errout; 285557b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 285657b61080SScott Feldman err = -EOPNOTSUPP; 285757b61080SScott Feldman goto errout; 285857b61080SScott Feldman } 285957b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 286057b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 286157b61080SScott Feldman if (err < 0) 286257b61080SScott Feldman goto errout; 2863ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 286457b61080SScott Feldman } 286557b61080SScott Feldman } 286657b61080SScott Feldman err = 0; 286757b61080SScott Feldman 286857b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 286957b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 287057b61080SScott Feldman 28718cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 28728cb08174SJohannes Berg tb[IFLA_PORT_SELF], 28738cb08174SJohannes Berg ifla_port_policy, NULL); 287457b61080SScott Feldman if (err < 0) 287557b61080SScott Feldman goto errout; 287657b61080SScott Feldman 287757b61080SScott Feldman err = -EOPNOTSUPP; 287857b61080SScott Feldman if (ops->ndo_set_vf_port) 287957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 288057b61080SScott Feldman if (err < 0) 288157b61080SScott Feldman goto errout; 2882ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 288357b61080SScott Feldman } 2884f8ff182cSThomas Graf 2885f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2886f8ff182cSThomas Graf struct nlattr *af; 2887f8ff182cSThomas Graf int rem; 2888f8ff182cSThomas Graf 2889f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2890f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2891f8ff182cSThomas Graf 2892058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2893f8ff182cSThomas Graf 28943583a4e8SStephen Hemminger err = af_ops->set_link_af(dev, af, extack); 2895a100243dSCong Wang if (err < 0) 2896f8ff182cSThomas Graf goto errout; 2897f8ff182cSThomas Graf 2898ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2899f8ff182cSThomas Graf } 2900f8ff182cSThomas Graf } 290157b61080SScott Feldman err = 0; 290257b61080SScott Feldman 2903829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 2904829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 2905829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 290688d6378bSAnuradha Karuppiah if (err) 290788d6378bSAnuradha Karuppiah goto errout; 290888d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 290988d6378bSAnuradha Karuppiah } 291088d6378bSAnuradha Karuppiah 2911d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2912d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 291385de8576SDaniel Borkmann u32 xdp_flags = 0; 2914d1fdd913SBrenden Blanco 29158cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 29168cb08174SJohannes Berg tb[IFLA_XDP], 2917fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2918d1fdd913SBrenden Blanco if (err < 0) 2919d1fdd913SBrenden Blanco goto errout; 2920d1fdd913SBrenden Blanco 292158038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2922262d8625SBrenden Blanco err = -EINVAL; 2923262d8625SBrenden Blanco goto errout; 2924262d8625SBrenden Blanco } 292585de8576SDaniel Borkmann 292685de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 292785de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 292885de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 292985de8576SDaniel Borkmann err = -EINVAL; 293085de8576SDaniel Borkmann goto errout; 293185de8576SDaniel Borkmann } 2932ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 29330489df9aSDaniel Borkmann err = -EINVAL; 29340489df9aSDaniel Borkmann goto errout; 29350489df9aSDaniel Borkmann } 293685de8576SDaniel Borkmann } 293785de8576SDaniel Borkmann 2938d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 293992234c8fSToke Høiland-Jørgensen int expected_fd = -1; 294092234c8fSToke Høiland-Jørgensen 294192234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 294292234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 294392234c8fSToke Høiland-Jørgensen err = -EINVAL; 294492234c8fSToke Høiland-Jørgensen goto errout; 294592234c8fSToke Høiland-Jørgensen } 294692234c8fSToke Høiland-Jørgensen expected_fd = 294792234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 294892234c8fSToke Høiland-Jørgensen } 294992234c8fSToke Høiland-Jørgensen 2950ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 295185de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 295292234c8fSToke Høiland-Jørgensen expected_fd, 295385de8576SDaniel Borkmann xdp_flags); 2954d1fdd913SBrenden Blanco if (err) 2955d1fdd913SBrenden Blanco goto errout; 2956d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2957d1fdd913SBrenden Blanco } 2958d1fdd913SBrenden Blanco } 2959d1fdd913SBrenden Blanco 29600157f60cSPatrick McHardy errout: 2961ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 296264ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2963ba998906SNicolas Dichtel netdev_state_change(dev); 2964ba998906SNicolas Dichtel 2965ba998906SNicolas Dichtel if (err < 0) 2966e87cc472SJoe 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", 2967e87cc472SJoe Perches dev->name); 2968ba998906SNicolas Dichtel } 29690157f60cSPatrick McHardy 29700157f60cSPatrick McHardy return err; 29710157f60cSPatrick McHardy } 29720157f60cSPatrick McHardy 2973cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 2974cc6090e9SJiri Pirko struct nlattr *ifname_attr, 297576c9ac0eSJiri Pirko struct nlattr *altifname_attr, 2976cc6090e9SJiri Pirko char *ifname) 2977cc6090e9SJiri Pirko { 297876c9ac0eSJiri Pirko char buffer[ALTIFNAMSIZ]; 2979cc6090e9SJiri Pirko 2980cc6090e9SJiri Pirko if (!ifname) { 2981cc6090e9SJiri Pirko ifname = buffer; 2982cc6090e9SJiri Pirko if (ifname_attr) 2983872f6903SFrancis Laniel nla_strscpy(ifname, ifname_attr, IFNAMSIZ); 298476c9ac0eSJiri Pirko else if (altifname_attr) 2985872f6903SFrancis Laniel nla_strscpy(ifname, altifname_attr, ALTIFNAMSIZ); 2986cc6090e9SJiri Pirko else 2987cc6090e9SJiri Pirko return NULL; 2988cc6090e9SJiri Pirko } 2989cc6090e9SJiri Pirko 2990cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 2991cc6090e9SJiri Pirko } 2992cc6090e9SJiri Pirko 2993c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2994c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2995da5e0494SThomas Graf { 29963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2997da5e0494SThomas Graf struct ifinfomsg *ifm; 2998da5e0494SThomas Graf struct net_device *dev; 29990157f60cSPatrick McHardy int err; 3000da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 30011da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 30021da177e4SLinus Torvalds 30038cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 30048cb08174SJohannes Berg ifla_policy, extack); 3005da5e0494SThomas Graf if (err < 0) 3006da5e0494SThomas Graf goto errout; 30071da177e4SLinus Torvalds 30084ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 30094ff66caeSChristian Brauner if (err < 0) 30104ff66caeSChristian Brauner goto errout; 30114ff66caeSChristian Brauner 30125176f91eSThomas Graf if (tb[IFLA_IFNAME]) 3013872f6903SFrancis Laniel nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 301478e5b891SPatrick McHardy else 301578e5b891SPatrick McHardy ifname[0] = '\0'; 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds err = -EINVAL; 3018da5e0494SThomas Graf ifm = nlmsg_data(nlh); 301951055be8SPatrick McHardy if (ifm->ifi_index > 0) 3020a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 302176c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 302276c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 3023da5e0494SThomas Graf else 3024da5e0494SThomas Graf goto errout; 30251da177e4SLinus Torvalds 3026da5e0494SThomas Graf if (dev == NULL) { 3027da5e0494SThomas Graf err = -ENODEV; 3028da5e0494SThomas Graf goto errout; 3029da5e0494SThomas Graf } 30301da177e4SLinus Torvalds 3031ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 3032da5e0494SThomas Graf errout: 30331da177e4SLinus Torvalds return err; 30341da177e4SLinus Torvalds } 30351da177e4SLinus Torvalds 303666400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 303766400d54SWANG Cong { 303866400d54SWANG Cong struct net_device *dev, *aux; 303966400d54SWANG Cong LIST_HEAD(list_kill); 304066400d54SWANG Cong bool found = false; 304166400d54SWANG Cong 304266400d54SWANG Cong if (!group) 304366400d54SWANG Cong return -EPERM; 304466400d54SWANG Cong 304566400d54SWANG Cong for_each_netdev(net, dev) { 304666400d54SWANG Cong if (dev->group == group) { 304766400d54SWANG Cong const struct rtnl_link_ops *ops; 304866400d54SWANG Cong 304966400d54SWANG Cong found = true; 305066400d54SWANG Cong ops = dev->rtnl_link_ops; 305166400d54SWANG Cong if (!ops || !ops->dellink) 305266400d54SWANG Cong return -EOPNOTSUPP; 305366400d54SWANG Cong } 305466400d54SWANG Cong } 305566400d54SWANG Cong 305666400d54SWANG Cong if (!found) 305766400d54SWANG Cong return -ENODEV; 305866400d54SWANG Cong 305966400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 306066400d54SWANG Cong if (dev->group == group) { 306166400d54SWANG Cong const struct rtnl_link_ops *ops; 306266400d54SWANG Cong 306366400d54SWANG Cong ops = dev->rtnl_link_ops; 306466400d54SWANG Cong ops->dellink(dev, &list_kill); 306566400d54SWANG Cong } 306666400d54SWANG Cong } 306766400d54SWANG Cong unregister_netdevice_many(&list_kill); 306866400d54SWANG Cong 306966400d54SWANG Cong return 0; 307066400d54SWANG Cong } 307166400d54SWANG Cong 3072614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 3073614732eaSThomas Graf { 3074614732eaSThomas Graf const struct rtnl_link_ops *ops; 3075614732eaSThomas Graf LIST_HEAD(list_kill); 3076614732eaSThomas Graf 3077614732eaSThomas Graf ops = dev->rtnl_link_ops; 3078614732eaSThomas Graf if (!ops || !ops->dellink) 3079614732eaSThomas Graf return -EOPNOTSUPP; 3080614732eaSThomas Graf 3081614732eaSThomas Graf ops->dellink(dev, &list_kill); 3082614732eaSThomas Graf unregister_netdevice_many(&list_kill); 3083614732eaSThomas Graf 3084614732eaSThomas Graf return 0; 3085614732eaSThomas Graf } 3086614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3087614732eaSThomas Graf 3088c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3089c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 309038f7b870SPatrick McHardy { 30913b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3092b61ad68aSChristian Brauner struct net *tgt_net = net; 3093b61ad68aSChristian Brauner struct net_device *dev = NULL; 309438f7b870SPatrick McHardy struct ifinfomsg *ifm; 309538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 309638f7b870SPatrick McHardy int err; 3097b61ad68aSChristian Brauner int netnsid = -1; 309838f7b870SPatrick McHardy 30998cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31008cb08174SJohannes Berg ifla_policy, extack); 310138f7b870SPatrick McHardy if (err < 0) 310238f7b870SPatrick McHardy return err; 310338f7b870SPatrick McHardy 31044ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31054ff66caeSChristian Brauner if (err < 0) 31064ff66caeSChristian Brauner return err; 31074ff66caeSChristian Brauner 31087e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 31097e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3110c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3111b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3112b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3113b61ad68aSChristian Brauner } 3114b61ad68aSChristian Brauner 3115b61ad68aSChristian Brauner err = -EINVAL; 311638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 311738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3118b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 311976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 312076c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 312176c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 312266400d54SWANG Cong else if (tb[IFLA_GROUP]) 3123b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 312438f7b870SPatrick McHardy else 3125b61ad68aSChristian Brauner goto out; 312638f7b870SPatrick McHardy 3127b61ad68aSChristian Brauner if (!dev) { 3128b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 3129b61ad68aSChristian Brauner err = -ENODEV; 313038f7b870SPatrick McHardy 3131b61ad68aSChristian Brauner goto out; 3132b61ad68aSChristian Brauner } 3133b61ad68aSChristian Brauner 3134b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 3135b61ad68aSChristian Brauner 3136b61ad68aSChristian Brauner out: 3137b61ad68aSChristian Brauner if (netnsid >= 0) 3138b61ad68aSChristian Brauner put_net(tgt_net); 3139b61ad68aSChristian Brauner 3140b61ad68aSChristian Brauner return err; 314138f7b870SPatrick McHardy } 314238f7b870SPatrick McHardy 31433729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 31443729d502SPatrick McHardy { 31453729d502SPatrick McHardy unsigned int old_flags; 31463729d502SPatrick McHardy int err; 31473729d502SPatrick McHardy 31483729d502SPatrick McHardy old_flags = dev->flags; 31493729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 31506d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 31516d040321SPetr Machata NULL); 31523729d502SPatrick McHardy if (err < 0) 31533729d502SPatrick McHardy return err; 31543729d502SPatrick McHardy } 31553729d502SPatrick McHardy 31568d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 315756a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 31588d356b89SRoopa Prabhu } else { 31593729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 3160a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 31618d356b89SRoopa Prabhu } 31623729d502SPatrick McHardy return 0; 31633729d502SPatrick McHardy } 31643729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 31653729d502SPatrick McHardy 3166d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3167d0522f1cSDavid Ahern unsigned char name_assign_type, 3168d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3169d0522f1cSDavid Ahern struct nlattr *tb[], 3170d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3171e7199288SPavel Emelianov { 3172e7199288SPavel Emelianov struct net_device *dev; 3173d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3174d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3175e7199288SPavel Emelianov 317676ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 317776ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 317876ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3179d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 318076ff5cc9SJiri Pirko 318176ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 318276ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 318376ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3184d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3185efacb309Sstephen hemminger 3186d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3187d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 31880e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3189d0522f1cSDavid Ahern } 31900e1d6ecaSEric Dumazet 3191d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3192d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 31930e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3194d0522f1cSDavid Ahern } 31950e1d6ecaSEric Dumazet 31968c713dc9SJohannes Berg if (ops->alloc) { 31978c713dc9SJohannes Berg dev = ops->alloc(tb, ifname, name_assign_type, 31988c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 31998c713dc9SJohannes Berg if (IS_ERR(dev)) 32008c713dc9SJohannes Berg return dev; 32018c713dc9SJohannes Berg } else { 32028c713dc9SJohannes Berg dev = alloc_netdev_mqs(ops->priv_size, ifname, 32038c713dc9SJohannes Berg name_assign_type, ops->setup, 32048c713dc9SJohannes Berg num_tx_queues, num_rx_queues); 32058c713dc9SJohannes Berg } 32068c713dc9SJohannes Berg 3207e7199288SPavel Emelianov if (!dev) 3208d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3209e7199288SPavel Emelianov 321081adee47SEric W. Biederman dev_net_set(dev, net); 321181adee47SEric W. Biederman dev->rtnl_link_ops = ops; 32123729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 321381adee47SEric W. Biederman 3214d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3215d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3216d836f5c6SEric Dumazet int err; 3217d836f5c6SEric Dumazet 3218d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3219d836f5c6SEric Dumazet if (err) { 3220d836f5c6SEric Dumazet free_netdev(dev); 3221d836f5c6SEric Dumazet return ERR_PTR(err); 3222d836f5c6SEric Dumazet } 3223d836f5c6SEric Dumazet dev->mtu = mtu; 3224d836f5c6SEric Dumazet } 32252afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3226efd38f75SJakub Kicinski __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3227e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 32282afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 32292afb9b53SJiri Pirko } 3230e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3231e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3232e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3233e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3234e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3235e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 323693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3237e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3238e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3239ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3240ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 324146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 324246e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 324346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 32446d872df3SEric Dumazet netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3245eac1b93cSCoco Li if (tb[IFLA_GRO_MAX_SIZE]) 3246eac1b93cSCoco Li netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 3247e7199288SPavel Emelianov 3248e7199288SPavel Emelianov return dev; 3249e7199288SPavel Emelianov } 3250e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3251e7199288SPavel Emelianov 325290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 325390f62cf3SEric W. Biederman struct net *net, int group, 3254e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3255ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3256e7ed828fSVlad Dogaru struct nlattr **tb) 3257e7ed828fSVlad Dogaru { 3258d079535dSWANG Cong struct net_device *dev, *aux; 3259e7ed828fSVlad Dogaru int err; 3260e7ed828fSVlad Dogaru 3261d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3262e7ed828fSVlad Dogaru if (dev->group == group) { 3263ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 3264e7ed828fSVlad Dogaru if (err < 0) 3265e7ed828fSVlad Dogaru return err; 3266e7ed828fSVlad Dogaru } 3267e7ed828fSVlad Dogaru } 3268e7ed828fSVlad Dogaru 3269e7ed828fSVlad Dogaru return 0; 3270e7ed828fSVlad Dogaru } 3271e7ed828fSVlad Dogaru 3272a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3273a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 327438f7b870SPatrick McHardy { 3275420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3276420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 3277420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3278c6f6f244SEric Dumazet const struct rtnl_link_ops *m_ops; 3279c6f6f244SEric Dumazet struct net_device *master_dev; 32803b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 328138f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 328238f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 3283420d0318SJakub Kicinski struct net *dest_net, *link_net; 3284420d0318SJakub Kicinski struct nlattr **slave_data; 3285420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3286420d0318SJakub Kicinski struct net_device *dev; 3287420d0318SJakub Kicinski struct ifinfomsg *ifm; 3288420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 3289420d0318SJakub Kicinski struct nlattr **data; 329038f7b870SPatrick McHardy int err; 329138f7b870SPatrick McHardy 329295a5afcaSJohannes Berg #ifdef CONFIG_MODULES 329338f7b870SPatrick McHardy replay: 32948072f085SThomas Graf #endif 32958cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32968cb08174SJohannes Berg ifla_policy, extack); 329738f7b870SPatrick McHardy if (err < 0) 329838f7b870SPatrick McHardy return err; 329938f7b870SPatrick McHardy 33004ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 33014ff66caeSChristian Brauner if (err < 0) 33024ff66caeSChristian Brauner return err; 33034ff66caeSChristian Brauner 330438f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 3305872f6903SFrancis Laniel nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 330638f7b870SPatrick McHardy else 330738f7b870SPatrick McHardy ifname[0] = '\0'; 330838f7b870SPatrick McHardy 330938f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 331038f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3311881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 331276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 331376c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 331438f7b870SPatrick McHardy else 331538f7b870SPatrick McHardy dev = NULL; 331638f7b870SPatrick McHardy 3317c6f6f244SEric Dumazet master_dev = NULL; 3318c6f6f244SEric Dumazet m_ops = NULL; 3319ba7d49b1SJiri Pirko if (dev) { 3320ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3321ba7d49b1SJiri Pirko if (master_dev) 3322ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3323ba7d49b1SJiri Pirko } 3324ba7d49b1SJiri Pirko 33258679c31eSRocco Yue err = validate_linkmsg(dev, tb, extack); 3326e0d087afSEric Dumazet if (err < 0) 33271840bb13SThomas Graf return err; 33281840bb13SThomas Graf 332938f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 33308cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 33318cb08174SJohannes Berg tb[IFLA_LINKINFO], 33328cb08174SJohannes Berg ifla_info_policy, NULL); 333338f7b870SPatrick McHardy if (err < 0) 333438f7b870SPatrick McHardy return err; 333538f7b870SPatrick McHardy } else 333638f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 333738f7b870SPatrick McHardy 333838f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 3339872f6903SFrancis Laniel nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 334038f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 334138f7b870SPatrick McHardy } else { 334238f7b870SPatrick McHardy kind[0] = '\0'; 334338f7b870SPatrick McHardy ops = NULL; 334438f7b870SPatrick McHardy } 334538f7b870SPatrick McHardy 3346420d0318SJakub Kicinski data = NULL; 334738f7b870SPatrick McHardy if (ops) { 3348ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3349ccf8dbcdSKees Cook return -EINVAL; 3350ccf8dbcdSKees Cook 335138f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 33528cb08174SJohannes Berg err = nla_parse_nested_deprecated(attr, ops->maxtype, 335338f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 335468d57f3bSDavid Ahern ops->policy, extack); 335538f7b870SPatrick McHardy if (err < 0) 335638f7b870SPatrick McHardy return err; 335738f7b870SPatrick McHardy data = attr; 335838f7b870SPatrick McHardy } 335938f7b870SPatrick McHardy if (ops->validate) { 3360a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 336138f7b870SPatrick McHardy if (err < 0) 336238f7b870SPatrick McHardy return err; 336338f7b870SPatrick McHardy } 336438f7b870SPatrick McHardy } 336538f7b870SPatrick McHardy 3366420d0318SJakub Kicinski slave_data = NULL; 3367ba7d49b1SJiri Pirko if (m_ops) { 3368af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3369ccf8dbcdSKees Cook return -EINVAL; 3370ccf8dbcdSKees Cook 3371ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3372ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 33738cb08174SJohannes Berg err = nla_parse_nested_deprecated(slave_attr, 33748cb08174SJohannes Berg m_ops->slave_maxtype, 3375ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 33768cb08174SJohannes Berg m_ops->slave_policy, 33778cb08174SJohannes Berg extack); 3378ba7d49b1SJiri Pirko if (err < 0) 3379ba7d49b1SJiri Pirko return err; 3380ba7d49b1SJiri Pirko slave_data = slave_attr; 3381ba7d49b1SJiri Pirko } 3382ba7d49b1SJiri Pirko } 3383ba7d49b1SJiri Pirko 338438f7b870SPatrick McHardy if (dev) { 338590c325e3SNicolas Dichtel int status = 0; 338638f7b870SPatrick McHardy 338738f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 338838f7b870SPatrick McHardy return -EEXIST; 338938f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 339038f7b870SPatrick McHardy return -EOPNOTSUPP; 339138f7b870SPatrick McHardy 339238f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 339338f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 339438f7b870SPatrick McHardy !ops->changelink) 339538f7b870SPatrick McHardy return -EOPNOTSUPP; 339638f7b870SPatrick McHardy 3397ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 339838f7b870SPatrick McHardy if (err < 0) 339938f7b870SPatrick McHardy return err; 3400ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 340138f7b870SPatrick McHardy } 340238f7b870SPatrick McHardy 3403ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3404ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3405ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3406ba7d49b1SJiri Pirko 3407420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3408420d0318SJakub Kicinski slave_data, extack); 3409ba7d49b1SJiri Pirko if (err < 0) 3410ba7d49b1SJiri Pirko return err; 3411ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3412ba7d49b1SJiri Pirko } 3413ba7d49b1SJiri Pirko 3414420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 341538f7b870SPatrick McHardy } 341638f7b870SPatrick McHardy 3417ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3418ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 341990f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3420ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3421ddf9f970SJakub Kicinski ifm, extack, tb); 342238f7b870SPatrick McHardy return -ENODEV; 3423ffa934f1SPatrick McHardy } 342438f7b870SPatrick McHardy 3425160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 342638f7b870SPatrick McHardy return -EOPNOTSUPP; 342738f7b870SPatrick McHardy 342838f7b870SPatrick McHardy if (!ops) { 342995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 343038f7b870SPatrick McHardy if (kind[0]) { 343138f7b870SPatrick McHardy __rtnl_unlock(); 343238f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 343338f7b870SPatrick McHardy rtnl_lock(); 343438f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 343538f7b870SPatrick McHardy if (ops) 343638f7b870SPatrick McHardy goto replay; 343738f7b870SPatrick McHardy } 343838f7b870SPatrick McHardy #endif 343968d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 344038f7b870SPatrick McHardy return -EOPNOTSUPP; 344138f7b870SPatrick McHardy } 344238f7b870SPatrick McHardy 34438c713dc9SJohannes Berg if (!ops->alloc && !ops->setup) 3444b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3445b0ab2fabSJiri Pirko 34465517750fSTom Gundersen if (!ifname[0]) { 344738f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 34485517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 34495517750fSTom Gundersen } 345038f7b870SPatrick McHardy 34515bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 345213ad1774SEric W. Biederman if (IS_ERR(dest_net)) 345313ad1774SEric W. Biederman return PTR_ERR(dest_net); 345413ad1774SEric W. Biederman 3455317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3456317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3457317f4810SNicolas Dichtel 3458317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3459317f4810SNicolas Dichtel if (!link_net) { 346068d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3461317f4810SNicolas Dichtel err = -EINVAL; 3462317f4810SNicolas Dichtel goto out; 3463317f4810SNicolas Dichtel } 346406615bedSEric W. Biederman err = -EPERM; 346506615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 346606615bedSEric W. Biederman goto out; 3467420d0318SJakub Kicinski } else { 3468420d0318SJakub Kicinski link_net = NULL; 3469317f4810SNicolas Dichtel } 3470317f4810SNicolas Dichtel 3471317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3472d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 34739c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3474e7199288SPavel Emelianov err = PTR_ERR(dev); 34759c7dafbfSPavel Emelyanov goto out; 34769c7dafbfSPavel Emelyanov } 34779c7dafbfSPavel Emelyanov 34789c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 34799c7dafbfSPavel Emelyanov 3480c269a24cSJakub Kicinski if (ops->newlink) 3481420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 3482c269a24cSJakub Kicinski else 34832d85cba2SPatrick McHardy err = register_netdevice(dev); 3484fce9b9beSDan Carpenter if (err < 0) { 348538f7b870SPatrick McHardy free_netdev(dev); 34863729d502SPatrick McHardy goto out; 3487fce9b9beSDan Carpenter } 3488c269a24cSJakub Kicinski 34893729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 349043638900SDavid S. Miller if (err < 0) 349143638900SDavid S. Miller goto out_unregister; 349243638900SDavid S. Miller if (link_net) { 34930854fa82SAndrei Vagin err = dev_change_net_namespace(dev, dest_net, ifname); 349443638900SDavid S. Miller if (err < 0) 349543638900SDavid S. Miller goto out_unregister; 349643638900SDavid S. Miller } 3497160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3498420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3499160ca014STheuns Verwoerd if (err) 3500160ca014STheuns Verwoerd goto out_unregister; 3501160ca014STheuns Verwoerd } 350243638900SDavid S. Miller out: 350343638900SDavid S. Miller if (link_net) 350443638900SDavid S. Miller put_net(link_net); 350543638900SDavid S. Miller put_net(dest_net); 350643638900SDavid S. Miller return err; 350743638900SDavid S. Miller out_unregister: 35087afb8886SWANG Cong if (ops->newlink) { 35097afb8886SWANG Cong LIST_HEAD(list_kill); 35107afb8886SWANG Cong 35117afb8886SWANG Cong ops->dellink(dev, &list_kill); 35127afb8886SWANG Cong unregister_netdevice_many(&list_kill); 35137afb8886SWANG Cong } else { 35143729d502SPatrick McHardy unregister_netdevice(dev); 35157afb8886SWANG Cong } 3516317f4810SNicolas Dichtel goto out; 3517317f4810SNicolas Dichtel } 351838f7b870SPatrick McHardy 3519a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3520a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3521a2939745SJakub Kicinski { 3522a2939745SJakub Kicinski struct nlattr **attr; 3523a2939745SJakub Kicinski int ret; 3524a2939745SJakub Kicinski 3525a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3526a2939745SJakub Kicinski if (!attr) 3527a2939745SJakub Kicinski return -ENOMEM; 3528a2939745SJakub Kicinski 3529a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3530a2939745SJakub Kicinski kfree(attr); 3531a2939745SJakub Kicinski return ret; 3532a2939745SJakub Kicinski } 3533a2939745SJakub Kicinski 35349b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 35359b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 35369b3757b0SJakub Kicinski struct nlattr **tb, 35379b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 35389b3757b0SJakub Kicinski { 35399b3757b0SJakub Kicinski struct ifinfomsg *ifm; 35409b3757b0SJakub Kicinski int i, err; 35419b3757b0SJakub Kicinski 35429b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 35439b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 35449b3757b0SJakub Kicinski return -EINVAL; 35459b3757b0SJakub Kicinski } 35469b3757b0SJakub Kicinski 35479b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 35488cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35498cb08174SJohannes Berg ifla_policy, extack); 35509b3757b0SJakub Kicinski 35519b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 35529b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 35539b3757b0SJakub Kicinski ifm->ifi_change) { 35549b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 35559b3757b0SJakub Kicinski return -EINVAL; 35569b3757b0SJakub Kicinski } 35579b3757b0SJakub Kicinski 35588cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 35598cb08174SJohannes Berg ifla_policy, extack); 35609b3757b0SJakub Kicinski if (err) 35619b3757b0SJakub Kicinski return err; 35629b3757b0SJakub Kicinski 35639b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 35649b3757b0SJakub Kicinski if (!tb[i]) 35659b3757b0SJakub Kicinski continue; 35669b3757b0SJakub Kicinski 35679b3757b0SJakub Kicinski switch (i) { 35689b3757b0SJakub Kicinski case IFLA_IFNAME: 356976c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 35709b3757b0SJakub Kicinski case IFLA_EXT_MASK: 35719b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 35729b3757b0SJakub Kicinski break; 35739b3757b0SJakub Kicinski default: 35749b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 35759b3757b0SJakub Kicinski return -EINVAL; 35769b3757b0SJakub Kicinski } 35779b3757b0SJakub Kicinski } 35789b3757b0SJakub Kicinski 35799b3757b0SJakub Kicinski return 0; 35809b3757b0SJakub Kicinski } 35819b3757b0SJakub Kicinski 3582c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3583c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3584711e2c33SJean Tourrilhes { 35853b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 358679e1ad14SJiri Benc struct net *tgt_net = net; 3587b60c5115SThomas Graf struct ifinfomsg *ifm; 3588b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3589b60c5115SThomas Graf struct net_device *dev = NULL; 3590b60c5115SThomas Graf struct sk_buff *nskb; 359179e1ad14SJiri Benc int netnsid = -1; 3592339bf98fSThomas Graf int err; 3593115c9b81SGreg Rose u32 ext_filter_mask = 0; 3594711e2c33SJean Tourrilhes 35959b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3596b60c5115SThomas Graf if (err < 0) 35979918f230SEric Sesterhenn return err; 3598b60c5115SThomas Graf 35994ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 36004ff66caeSChristian Brauner if (err < 0) 36014ff66caeSChristian Brauner return err; 36024ff66caeSChristian Brauner 36037e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 36047e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3605c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 360679e1ad14SJiri Benc if (IS_ERR(tgt_net)) 360779e1ad14SJiri Benc return PTR_ERR(tgt_net); 360879e1ad14SJiri Benc } 360979e1ad14SJiri Benc 3610115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3611115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3612115c9b81SGreg Rose 361379e1ad14SJiri Benc err = -EINVAL; 3614b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3615a3d12891SEric Dumazet if (ifm->ifi_index > 0) 361679e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 361776c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 361876c9ac0eSJiri Pirko dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], 361976c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3620a3d12891SEric Dumazet else 362179e1ad14SJiri Benc goto out; 3622b60c5115SThomas Graf 362379e1ad14SJiri Benc err = -ENODEV; 3624a3d12891SEric Dumazet if (dev == NULL) 362579e1ad14SJiri Benc goto out; 3626a3d12891SEric Dumazet 362779e1ad14SJiri Benc err = -ENOBUFS; 3628115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3629a3d12891SEric Dumazet if (nskb == NULL) 363079e1ad14SJiri Benc goto out; 3631711e2c33SJean Tourrilhes 363279e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 363379e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 363479e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3635d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 363626932566SPatrick McHardy if (err < 0) { 363726932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 363826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 363926932566SPatrick McHardy kfree_skb(nskb); 3640a3d12891SEric Dumazet } else 364115e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 364279e1ad14SJiri Benc out: 364379e1ad14SJiri Benc if (netnsid >= 0) 364479e1ad14SJiri Benc put_net(tgt_net); 3645b60c5115SThomas Graf 3646711e2c33SJean Tourrilhes return err; 3647711e2c33SJean Tourrilhes } 3648711e2c33SJean Tourrilhes 364936fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 365036fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 365136fbf1e5SJiri Pirko { 365236fbf1e5SJiri Pirko char *alt_ifname; 365336fbf1e5SJiri Pirko int err; 365436fbf1e5SJiri Pirko 365536fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 365636fbf1e5SJiri Pirko if (err) 365736fbf1e5SJiri Pirko return err; 365836fbf1e5SJiri Pirko 365944bfa9c5SEric Dumazet alt_ifname = nla_strdup(attr, GFP_KERNEL); 366036fbf1e5SJiri Pirko if (!alt_ifname) 366136fbf1e5SJiri Pirko return -ENOMEM; 366244bfa9c5SEric Dumazet 366344bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 366436fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 366544bfa9c5SEric Dumazet if (!err) 366644bfa9c5SEric Dumazet alt_ifname = NULL; 366736fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 366836fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 366936fbf1e5SJiri Pirko } else { 367044bfa9c5SEric Dumazet WARN_ON_ONCE(1); 367144bfa9c5SEric Dumazet err = -EINVAL; 367236fbf1e5SJiri Pirko } 367336fbf1e5SJiri Pirko 367444bfa9c5SEric Dumazet kfree(alt_ifname); 367544bfa9c5SEric Dumazet if (!err) 367636fbf1e5SJiri Pirko *changed = true; 367744bfa9c5SEric Dumazet return err; 367836fbf1e5SJiri Pirko } 367936fbf1e5SJiri Pirko 368036fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 368136fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 368236fbf1e5SJiri Pirko { 368336fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 368436fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 368536fbf1e5SJiri Pirko struct net_device *dev; 368636fbf1e5SJiri Pirko struct ifinfomsg *ifm; 368736fbf1e5SJiri Pirko bool changed = false; 368836fbf1e5SJiri Pirko struct nlattr *attr; 368936fbf1e5SJiri Pirko int err, rem; 369036fbf1e5SJiri Pirko 369136fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 369236fbf1e5SJiri Pirko if (err) 369336fbf1e5SJiri Pirko return err; 369436fbf1e5SJiri Pirko 369536fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 369636fbf1e5SJiri Pirko if (err) 369736fbf1e5SJiri Pirko return err; 369836fbf1e5SJiri Pirko 369936fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3700cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 370136fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 370276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 370376c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 370476c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3705cc6090e9SJiri Pirko else 370636fbf1e5SJiri Pirko return -EINVAL; 370736fbf1e5SJiri Pirko 370836fbf1e5SJiri Pirko if (!dev) 370936fbf1e5SJiri Pirko return -ENODEV; 371036fbf1e5SJiri Pirko 371136fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 371236fbf1e5SJiri Pirko return 0; 371336fbf1e5SJiri Pirko 371436fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 371536fbf1e5SJiri Pirko switch (nla_type(attr)) { 371636fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 371736fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 371836fbf1e5SJiri Pirko if (err) 371936fbf1e5SJiri Pirko return err; 372036fbf1e5SJiri Pirko break; 372136fbf1e5SJiri Pirko } 372236fbf1e5SJiri Pirko } 372336fbf1e5SJiri Pirko 372436fbf1e5SJiri Pirko if (changed) 372536fbf1e5SJiri Pirko netdev_state_change(dev); 372636fbf1e5SJiri Pirko return 0; 372736fbf1e5SJiri Pirko } 372836fbf1e5SJiri Pirko 372936fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 373036fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 373136fbf1e5SJiri Pirko { 373236fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 373336fbf1e5SJiri Pirko } 373436fbf1e5SJiri Pirko 373536fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 373636fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 373736fbf1e5SJiri Pirko { 373836fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 373936fbf1e5SJiri Pirko } 374036fbf1e5SJiri Pirko 3741ebfe3c51SDi Zhu static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3742c7ac8679SGreg Rose { 3743115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3744ebfe3c51SDi Zhu size_t min_ifinfo_dump_size = 0; 3745115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3746115c9b81SGreg Rose u32 ext_filter_mask = 0; 3747ebfe3c51SDi Zhu struct net_device *dev; 3748e5eca6d4SMichal Schmidt int hdrlen; 3749115c9b81SGreg Rose 3750e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3751e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3752e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3753e5eca6d4SMichal Schmidt 37548cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3755115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3756115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3757a4b64fbeSEric Dumazet } 3758115c9b81SGreg Rose 3759115c9b81SGreg Rose if (!ext_filter_mask) 3760115c9b81SGreg Rose return NLMSG_GOODSIZE; 3761115c9b81SGreg Rose /* 3762115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3763115c9b81SGreg Rose * buffer size based upon the filter mask. 3764115c9b81SGreg Rose */ 37656853dd48SFlorian Westphal rcu_read_lock(); 37666853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3767ebfe3c51SDi Zhu min_ifinfo_dump_size = max(min_ifinfo_dump_size, 3768ebfe3c51SDi Zhu if_nlmsg_size(dev, ext_filter_mask)); 3769115c9b81SGreg Rose } 37706853dd48SFlorian Westphal rcu_read_unlock(); 3771115c9b81SGreg Rose 377293af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3773c7ac8679SGreg Rose } 3774c7ac8679SGreg Rose 377542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 37761da177e4SLinus Torvalds { 37771da177e4SLinus Torvalds int idx; 37781da177e4SLinus Torvalds int s_idx = cb->family; 377987ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3780c63586dcSDavid Ahern int ret = 0; 37811da177e4SLinus Torvalds 37821da177e4SLinus Torvalds if (s_idx == 0) 37831da177e4SLinus Torvalds s_idx = 1; 37846853dd48SFlorian Westphal 378525239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 378651e13685SJakub Kicinski struct rtnl_link __rcu **tab; 3787addf9b90SFlorian Westphal struct rtnl_link *link; 37886853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 37896853dd48SFlorian Westphal 37901da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 37911da177e4SLinus Torvalds continue; 37926853dd48SFlorian Westphal 3793addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 37941da177e4SLinus Torvalds continue; 37956853dd48SFlorian Westphal 3796addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3797addf9b90SFlorian Westphal if (!tab) 3798addf9b90SFlorian Westphal continue; 3799addf9b90SFlorian Westphal 380051e13685SJakub Kicinski link = rcu_dereference_rtnl(tab[type]); 3801addf9b90SFlorian Westphal if (!link) 3802addf9b90SFlorian Westphal continue; 3803addf9b90SFlorian Westphal 3804addf9b90SFlorian Westphal dumpit = link->dumpit; 38056853dd48SFlorian Westphal if (!dumpit) 38066853dd48SFlorian Westphal continue; 38076853dd48SFlorian Westphal 38080465277fSNicolas Dichtel if (idx > s_idx) { 38091da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 38100465277fSNicolas Dichtel cb->prev_seq = 0; 38110465277fSNicolas Dichtel cb->seq = 0; 38120465277fSNicolas Dichtel } 3813c63586dcSDavid Ahern ret = dumpit(skb, cb); 38145e1acb4aSAlexey Kodanev if (ret) 38151da177e4SLinus Torvalds break; 38161da177e4SLinus Torvalds } 38171da177e4SLinus Torvalds cb->family = idx; 38181da177e4SLinus Torvalds 3819c63586dcSDavid Ahern return skb->len ? : ret; 38201da177e4SLinus Torvalds } 38211da177e4SLinus Torvalds 3822395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 38233d3ea5afSVlad Yasevich unsigned int change, 382438e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 382538e01b30SNicolas Dichtel int new_ifindex) 38261da177e4SLinus Torvalds { 3827c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 38281da177e4SLinus Torvalds struct sk_buff *skb; 38290ec6d3f4SThomas Graf int err = -ENOBUFS; 38301da177e4SLinus Torvalds 383150af5969Sluo penghao skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 38320ec6d3f4SThomas Graf if (skb == NULL) 38330ec6d3f4SThomas Graf goto errout; 38341da177e4SLinus Torvalds 383579e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 383679e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 3837d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 383826932566SPatrick McHardy if (err < 0) { 383926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 384026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 384126932566SPatrick McHardy kfree_skb(skb); 384226932566SPatrick McHardy goto errout; 384326932566SPatrick McHardy } 3844395eea6cSMahesh Bandewar return skb; 38450ec6d3f4SThomas Graf errout: 38460ec6d3f4SThomas Graf if (err < 0) 38474b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3848395eea6cSMahesh Bandewar return NULL; 3849395eea6cSMahesh Bandewar } 3850395eea6cSMahesh Bandewar 3851395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3852395eea6cSMahesh Bandewar { 3853395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3854395eea6cSMahesh Bandewar 3855395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3856395eea6cSMahesh Bandewar } 3857395eea6cSMahesh Bandewar 38583d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 38593d3ea5afSVlad Yasevich unsigned int change, u32 event, 386038e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3861395eea6cSMahesh Bandewar { 3862395eea6cSMahesh Bandewar struct sk_buff *skb; 3863395eea6cSMahesh Bandewar 3864ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3865ed2a80abSNicolas Dichtel return; 3866ed2a80abSNicolas Dichtel 386738e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 386838e01b30SNicolas Dichtel new_ifindex); 3869395eea6cSMahesh Bandewar if (skb) 3870395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 38711da177e4SLinus Torvalds } 38723d3ea5afSVlad Yasevich 38733d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 38743d3ea5afSVlad Yasevich gfp_t flags) 38753d3ea5afSVlad Yasevich { 387638e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 387738e01b30SNicolas Dichtel NULL, 0); 38783d3ea5afSVlad Yasevich } 38791da177e4SLinus Torvalds 38806621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 388138e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 38826621dd29SNicolas Dichtel { 38836621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 388438e01b30SNicolas Dichtel new_nsid, new_ifindex); 38856621dd29SNicolas Dichtel } 38866621dd29SNicolas Dichtel 3887d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3888d83b0603SJohn Fastabend struct net_device *dev, 38891e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 38901c104a6bSNicolas Dichtel int type, unsigned int flags, 3891b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3892d83b0603SJohn Fastabend { 3893d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3894d83b0603SJohn Fastabend struct ndmsg *ndm; 3895d83b0603SJohn Fastabend 38961c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3897d83b0603SJohn Fastabend if (!nlh) 3898d83b0603SJohn Fastabend return -EMSGSIZE; 3899d83b0603SJohn Fastabend 3900d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3901d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3902d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3903d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3904d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3905d83b0603SJohn Fastabend ndm->ndm_type = 0; 3906d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3907b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3908d83b0603SJohn Fastabend 3909d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3910d83b0603SJohn Fastabend goto nla_put_failure; 39111e53d5bbSHubert Sokolowski if (vid) 39121e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 39131e53d5bbSHubert Sokolowski goto nla_put_failure; 3914d83b0603SJohn Fastabend 3915053c095aSJohannes Berg nlmsg_end(skb, nlh); 3916053c095aSJohannes Berg return 0; 3917d83b0603SJohn Fastabend 3918d83b0603SJohn Fastabend nla_put_failure: 3919d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3920d83b0603SJohn Fastabend return -EMSGSIZE; 3921d83b0603SJohn Fastabend } 3922d83b0603SJohn Fastabend 39233ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 39243ff661c3SJohn Fastabend { 3925f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3926f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3927f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3928f82ef3e1SSabrina Dubroca 0; 39293ff661c3SJohn Fastabend } 39303ff661c3SJohn Fastabend 3931b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3932b3379041SHubert Sokolowski u16 ndm_state) 39333ff661c3SJohn Fastabend { 39343ff661c3SJohn Fastabend struct net *net = dev_net(dev); 39353ff661c3SJohn Fastabend struct sk_buff *skb; 39363ff661c3SJohn Fastabend int err = -ENOBUFS; 39373ff661c3SJohn Fastabend 39383ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 39393ff661c3SJohn Fastabend if (!skb) 39403ff661c3SJohn Fastabend goto errout; 39413ff661c3SJohn Fastabend 39421e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3943b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 39443ff661c3SJohn Fastabend if (err < 0) { 39453ff661c3SJohn Fastabend kfree_skb(skb); 39463ff661c3SJohn Fastabend goto errout; 39473ff661c3SJohn Fastabend } 39483ff661c3SJohn Fastabend 39493ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 39503ff661c3SJohn Fastabend return; 39513ff661c3SJohn Fastabend errout: 39523ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 39533ff661c3SJohn Fastabend } 39543ff661c3SJohn Fastabend 3955a986967eSBart Van Assche /* 3956090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3957090096bfSVlad Yasevich */ 3958090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3959090096bfSVlad Yasevich struct nlattr *tb[], 3960090096bfSVlad Yasevich struct net_device *dev, 3961f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3962090096bfSVlad Yasevich u16 flags) 3963090096bfSVlad Yasevich { 3964090096bfSVlad Yasevich int err = -EINVAL; 3965090096bfSVlad Yasevich 3966090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3967090096bfSVlad Yasevich * implement its own handler for this. 3968090096bfSVlad Yasevich */ 3969090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 397078ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 3971090096bfSVlad Yasevich return err; 3972090096bfSVlad Yasevich } 3973090096bfSVlad Yasevich 397465891feaSOr Gerlitz if (vid) { 397523ac0b42SVladimir Oltean netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 397665891feaSOr Gerlitz return err; 397765891feaSOr Gerlitz } 397865891feaSOr Gerlitz 3979090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3980090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3981090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3982090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3983090096bfSVlad Yasevich 3984090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3985090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3986090096bfSVlad Yasevich err = 0; 3987090096bfSVlad Yasevich 3988090096bfSVlad Yasevich return err; 3989090096bfSVlad Yasevich } 3990090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3991090096bfSVlad Yasevich 3992b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3993b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3994f6f6424bSJiri Pirko { 3995f6f6424bSJiri Pirko u16 vid = 0; 3996f6f6424bSJiri Pirko 3997f6f6424bSJiri Pirko if (vlan_attr) { 3998f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3999b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4000f6f6424bSJiri Pirko return -EINVAL; 4001f6f6424bSJiri Pirko } 4002f6f6424bSJiri Pirko 4003f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 4004f6f6424bSJiri Pirko 4005f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 4006b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 4007f6f6424bSJiri Pirko return -EINVAL; 4008f6f6424bSJiri Pirko } 4009f6f6424bSJiri Pirko } 4010f6f6424bSJiri Pirko *p_vid = vid; 4011f6f6424bSJiri Pirko return 0; 4012f6f6424bSJiri Pirko } 4013f6f6424bSJiri Pirko 4014c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4015c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 401677162022SJohn Fastabend { 401777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 401877162022SJohn Fastabend struct ndmsg *ndm; 401977162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 402077162022SJohn Fastabend struct net_device *dev; 402177162022SJohn Fastabend u8 *addr; 4022f6f6424bSJiri Pirko u16 vid; 402377162022SJohn Fastabend int err; 402477162022SJohn Fastabend 40258cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 40268cb08174SJohannes Berg extack); 402777162022SJohn Fastabend if (err < 0) 402877162022SJohn Fastabend return err; 402977162022SJohn Fastabend 403077162022SJohn Fastabend ndm = nlmsg_data(nlh); 403177162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4032b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 403377162022SJohn Fastabend return -EINVAL; 403477162022SJohn Fastabend } 403577162022SJohn Fastabend 403677162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 403777162022SJohn Fastabend if (dev == NULL) { 4038b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 403977162022SJohn Fastabend return -ENODEV; 404077162022SJohn Fastabend } 404177162022SJohn Fastabend 404277162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4043b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 404477162022SJohn Fastabend return -EINVAL; 404577162022SJohn Fastabend } 404677162022SJohn Fastabend 4047da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4048da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4049da715775SIdo Schimmel return -EINVAL; 4050da715775SIdo Schimmel } 4051da715775SIdo Schimmel 405277162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 405377162022SJohn Fastabend 4054b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4055f6f6424bSJiri Pirko if (err) 4056f6f6424bSJiri Pirko return err; 4057f6f6424bSJiri Pirko 405877162022SJohn Fastabend err = -EOPNOTSUPP; 405977162022SJohn Fastabend 406077162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 406177162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 40622e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4063898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4064898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4065898e5061SJiri Pirko 4066f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 406787b0984eSPetr Machata nlh->nlmsg_flags, extack); 406877162022SJohn Fastabend if (err) 406977162022SJohn Fastabend goto out; 407077162022SJohn Fastabend else 407177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 407277162022SJohn Fastabend } 407377162022SJohn Fastabend 407477162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4075090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4076090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4077090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4078f6f6424bSJiri Pirko vid, 407987b0984eSPetr Machata nlh->nlmsg_flags, 408087b0984eSPetr Machata extack); 4081090096bfSVlad Yasevich else 4082f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 408377162022SJohn Fastabend nlh->nlmsg_flags); 408477162022SJohn Fastabend 40853ff661c3SJohn Fastabend if (!err) { 4086b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4087b3379041SHubert Sokolowski ndm->ndm_state); 408877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 408977162022SJohn Fastabend } 40903ff661c3SJohn Fastabend } 409177162022SJohn Fastabend out: 409277162022SJohn Fastabend return err; 409377162022SJohn Fastabend } 409477162022SJohn Fastabend 4095a986967eSBart Van Assche /* 4096090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4097090096bfSVlad Yasevich */ 4098090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4099090096bfSVlad Yasevich struct nlattr *tb[], 4100090096bfSVlad Yasevich struct net_device *dev, 4101f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4102090096bfSVlad Yasevich { 4103c8a89c4aSAlexander Duyck int err = -EINVAL; 4104090096bfSVlad Yasevich 4105090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4106090096bfSVlad Yasevich * implement its own handler for this. 4107090096bfSVlad Yasevich */ 410864535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 410978ecc890SVladimir Oltean netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4110c8a89c4aSAlexander Duyck return err; 4111090096bfSVlad Yasevich } 4112090096bfSVlad Yasevich 4113090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4114090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4115090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4116090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4117090096bfSVlad Yasevich 4118090096bfSVlad Yasevich return err; 4119090096bfSVlad Yasevich } 4120090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4121090096bfSVlad Yasevich 4122c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4123c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 412477162022SJohn Fastabend { 412577162022SJohn Fastabend struct net *net = sock_net(skb->sk); 412677162022SJohn Fastabend struct ndmsg *ndm; 41271690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 412877162022SJohn Fastabend struct net_device *dev; 412977162022SJohn Fastabend __u8 *addr; 41307d311801SColin Ian King int err; 4131f6f6424bSJiri Pirko u16 vid; 413277162022SJohn Fastabend 413390f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 41341690be63SVlad Yasevich return -EPERM; 41351690be63SVlad Yasevich 41368cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 41378cb08174SJohannes Berg extack); 41381690be63SVlad Yasevich if (err < 0) 41391690be63SVlad Yasevich return err; 414077162022SJohn Fastabend 414177162022SJohn Fastabend ndm = nlmsg_data(nlh); 414277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4143b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 414477162022SJohn Fastabend return -EINVAL; 414577162022SJohn Fastabend } 414677162022SJohn Fastabend 414777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 414877162022SJohn Fastabend if (dev == NULL) { 4149b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 415077162022SJohn Fastabend return -ENODEV; 415177162022SJohn Fastabend } 415277162022SJohn Fastabend 41531690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4154b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 415577162022SJohn Fastabend return -EINVAL; 415677162022SJohn Fastabend } 415777162022SJohn Fastabend 4158da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4159da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4160da715775SIdo Schimmel return -EINVAL; 4161da715775SIdo Schimmel } 4162da715775SIdo Schimmel 41631690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 41641690be63SVlad Yasevich 4165b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4166f6f6424bSJiri Pirko if (err) 4167f6f6424bSJiri Pirko return err; 4168f6f6424bSJiri Pirko 416977162022SJohn Fastabend err = -EOPNOTSUPP; 417077162022SJohn Fastabend 417177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 417277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 41732e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4174898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4175898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 417677162022SJohn Fastabend 4177898e5061SJiri Pirko if (ops->ndo_fdb_del) 4178f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 417977162022SJohn Fastabend 418077162022SJohn Fastabend if (err) 418177162022SJohn Fastabend goto out; 418277162022SJohn Fastabend else 418377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 418477162022SJohn Fastabend } 418577162022SJohn Fastabend 418677162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4187090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 4188090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 4189f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 4190f6f6424bSJiri Pirko vid); 4191090096bfSVlad Yasevich else 4192f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 419377162022SJohn Fastabend 41943ff661c3SJohn Fastabend if (!err) { 4195b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4196b3379041SHubert Sokolowski ndm->ndm_state); 419777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 419877162022SJohn Fastabend } 41993ff661c3SJohn Fastabend } 420077162022SJohn Fastabend out: 420177162022SJohn Fastabend return err; 420277162022SJohn Fastabend } 420377162022SJohn Fastabend 4204d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4205d83b0603SJohn Fastabend struct netlink_callback *cb, 4206d83b0603SJohn Fastabend struct net_device *dev, 4207d83b0603SJohn Fastabend int *idx, 4208d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4209d83b0603SJohn Fastabend { 4210d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4211d83b0603SJohn Fastabend int err; 421215e47304SEric W. Biederman u32 portid, seq; 4213d83b0603SJohn Fastabend 421415e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4215d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4216d83b0603SJohn Fastabend 4217d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4218d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4219d83b0603SJohn Fastabend goto skip; 4220d83b0603SJohn Fastabend 42211e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4222a7a558feSJohn Fastabend portid, seq, 42231c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4224b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4225d83b0603SJohn Fastabend if (err < 0) 4226d83b0603SJohn Fastabend return err; 4227d83b0603SJohn Fastabend skip: 4228d83b0603SJohn Fastabend *idx += 1; 4229d83b0603SJohn Fastabend } 4230d83b0603SJohn Fastabend return 0; 4231d83b0603SJohn Fastabend } 4232d83b0603SJohn Fastabend 4233d83b0603SJohn Fastabend /** 42342c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4235a986967eSBart Van Assche * @skb: socket buffer to store message in 4236a986967eSBart Van Assche * @cb: netlink callback 4237d83b0603SJohn Fastabend * @dev: netdevice 4238a986967eSBart Van Assche * @filter_dev: ignored 4239a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4240d83b0603SJohn Fastabend * 4241d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 424291f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4243d83b0603SJohn Fastabend */ 4244d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4245d83b0603SJohn Fastabend struct netlink_callback *cb, 4246d83b0603SJohn Fastabend struct net_device *dev, 42475d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4248d297653dSRoopa Prabhu int *idx) 4249d83b0603SJohn Fastabend { 4250d83b0603SJohn Fastabend int err; 4251d83b0603SJohn Fastabend 425268883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 425368883893SEric Dumazet return -EINVAL; 425468883893SEric Dumazet 4255d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4256d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4257d83b0603SJohn Fastabend if (err) 4258d83b0603SJohn Fastabend goto out; 42592934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4260d83b0603SJohn Fastabend out: 4261d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4262d297653dSRoopa Prabhu return err; 4263d83b0603SJohn Fastabend } 4264d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4265d83b0603SJohn Fastabend 42668c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 42678c6e137fSDavid Ahern int *br_idx, int *brport_idx, 42688c6e137fSDavid Ahern struct netlink_ext_ack *extack) 42698c6e137fSDavid Ahern { 42708c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 42718c6e137fSDavid Ahern struct ndmsg *ndm; 42728c6e137fSDavid Ahern int err, i; 42738c6e137fSDavid Ahern 42748c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 42758c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 42768c6e137fSDavid Ahern return -EINVAL; 42778c6e137fSDavid Ahern } 42788c6e137fSDavid Ahern 42798c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 42808c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 42818c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 42828b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 42838c6e137fSDavid Ahern return -EINVAL; 42848c6e137fSDavid Ahern } 42858c6e137fSDavid Ahern 42868cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 42878cb08174SJohannes Berg NDA_MAX, NULL, extack); 42888c6e137fSDavid Ahern if (err < 0) 42898c6e137fSDavid Ahern return err; 42908c6e137fSDavid Ahern 42918c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 42928c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 42938c6e137fSDavid Ahern if (!tb[i]) 42948c6e137fSDavid Ahern continue; 42958c6e137fSDavid Ahern 42968c6e137fSDavid Ahern switch (i) { 42978c6e137fSDavid Ahern case NDA_IFINDEX: 42988c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 42998c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 43008c6e137fSDavid Ahern return -EINVAL; 43018c6e137fSDavid Ahern } 43028c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 43038c6e137fSDavid Ahern break; 43048c6e137fSDavid Ahern case NDA_MASTER: 43058c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 43068c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 43078c6e137fSDavid Ahern return -EINVAL; 43088c6e137fSDavid Ahern } 43098c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 43108c6e137fSDavid Ahern break; 43118c6e137fSDavid Ahern default: 43128c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 43138c6e137fSDavid Ahern return -EINVAL; 43148c6e137fSDavid Ahern } 43158c6e137fSDavid Ahern } 43168c6e137fSDavid Ahern 43178c6e137fSDavid Ahern return 0; 43188c6e137fSDavid Ahern } 43198c6e137fSDavid Ahern 43208dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 43218dfbda19SDavid Ahern int *br_idx, int *brport_idx, 43228dfbda19SDavid Ahern struct netlink_ext_ack *extack) 43238dfbda19SDavid Ahern { 43248dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 43258dfbda19SDavid Ahern int err; 43268dfbda19SDavid Ahern 43278dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 43288dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 43298dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 43308dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 43318dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 43328dfbda19SDavid Ahern * with an optional attribute. 43338dfbda19SDavid Ahern */ 43348dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 43358dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 43368dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 43374565d7e5SDavid Ahern struct ifinfomsg *ifm; 43384565d7e5SDavid Ahern 43398cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 43408cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 43418cb08174SJohannes Berg extack); 43428dfbda19SDavid Ahern if (err < 0) { 43438dfbda19SDavid Ahern return -EINVAL; 43448dfbda19SDavid Ahern } else if (err == 0) { 43458dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 43468dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 43478dfbda19SDavid Ahern } 43488dfbda19SDavid Ahern 43494565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 43508dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 43518dfbda19SDavid Ahern } 43528dfbda19SDavid Ahern return 0; 43538dfbda19SDavid Ahern } 43548dfbda19SDavid Ahern 435577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 435677162022SJohn Fastabend { 435777162022SJohn Fastabend struct net_device *dev; 43585e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 43595e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 43605e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 43615e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4362d297653dSRoopa Prabhu struct hlist_head *head; 43635e6d2435SJamal Hadi Salim int brport_idx = 0; 43645e6d2435SJamal Hadi Salim int br_idx = 0; 4365d297653dSRoopa Prabhu int h, s_h; 4366d297653dSRoopa Prabhu int idx = 0, s_idx; 4367d297653dSRoopa Prabhu int err = 0; 4368d297653dSRoopa Prabhu int fidx = 0; 436977162022SJohn Fastabend 43708c6e137fSDavid Ahern if (cb->strict_check) 43718c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 43728c6e137fSDavid Ahern cb->extack); 43738c6e137fSDavid Ahern else 43748dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 43758dfbda19SDavid Ahern cb->extack); 43768dfbda19SDavid Ahern if (err < 0) 43778dfbda19SDavid Ahern return err; 43785e6d2435SJamal Hadi Salim 43795e6d2435SJamal Hadi Salim if (br_idx) { 43805e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 43815e6d2435SJamal Hadi Salim if (!br_dev) 43825e6d2435SJamal Hadi Salim return -ENODEV; 43835e6d2435SJamal Hadi Salim 4384898e5061SJiri Pirko ops = br_dev->netdev_ops; 43855e6d2435SJamal Hadi Salim } 43865e6d2435SJamal Hadi Salim 4387d297653dSRoopa Prabhu s_h = cb->args[0]; 4388d297653dSRoopa Prabhu s_idx = cb->args[1]; 4389d297653dSRoopa Prabhu 4390d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4391d297653dSRoopa Prabhu idx = 0; 4392d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4393d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4394d297653dSRoopa Prabhu 43955e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 43965e6d2435SJamal Hadi Salim continue; 43975e6d2435SJamal Hadi Salim 43985e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 43992e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 44005e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 44015e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 44025e6d2435SJamal Hadi Salim } 44035e6d2435SJamal Hadi Salim } else { 44045e6d2435SJamal Hadi Salim if (dev != br_dev && 44052e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 44065e6d2435SJamal Hadi Salim continue; 44075e6d2435SJamal Hadi Salim 44085e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 4409254ec036SKyungrok Chung !netif_is_bridge_master(dev)) 44105e6d2435SJamal Hadi Salim continue; 44115e6d2435SJamal Hadi Salim cops = ops; 44125e6d2435SJamal Hadi Salim } 44135e6d2435SJamal Hadi Salim 4414d297653dSRoopa Prabhu if (idx < s_idx) 4415d297653dSRoopa Prabhu goto cont; 4416d297653dSRoopa Prabhu 44172e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4418d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4419d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4420d297653dSRoopa Prabhu br_dev, dev, 4421d297653dSRoopa Prabhu &fidx); 4422d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4423d297653dSRoopa Prabhu goto out; 442477162022SJohn Fastabend } 4425d297653dSRoopa Prabhu } 442677162022SJohn Fastabend 44275e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4428d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4429d297653dSRoopa Prabhu dev, NULL, 4430d297653dSRoopa Prabhu &fidx); 44316cb69742SHubert Sokolowski else 4432d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4433d297653dSRoopa Prabhu &fidx); 4434d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4435d297653dSRoopa Prabhu goto out; 44365e6d2435SJamal Hadi Salim 44375e6d2435SJamal Hadi Salim cops = NULL; 4438d297653dSRoopa Prabhu 4439d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4440d297653dSRoopa Prabhu cb->args[2] = 0; 4441d297653dSRoopa Prabhu fidx = 0; 4442d297653dSRoopa Prabhu cont: 4443d297653dSRoopa Prabhu idx++; 4444d297653dSRoopa Prabhu } 444577162022SJohn Fastabend } 444677162022SJohn Fastabend 4447d297653dSRoopa Prabhu out: 4448d297653dSRoopa Prabhu cb->args[0] = h; 4449d297653dSRoopa Prabhu cb->args[1] = idx; 4450d297653dSRoopa Prabhu cb->args[2] = fidx; 4451d297653dSRoopa Prabhu 445277162022SJohn Fastabend return skb->len; 445377162022SJohn Fastabend } 445477162022SJohn Fastabend 44555b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 44565b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 44575b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 44585b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 44595b2f94b2SRoopa Prabhu { 44605b2f94b2SRoopa Prabhu struct ndmsg *ndm; 44615b2f94b2SRoopa Prabhu int err, i; 44625b2f94b2SRoopa Prabhu 44635b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 44645b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 44655b2f94b2SRoopa Prabhu return -EINVAL; 44665b2f94b2SRoopa Prabhu } 44675b2f94b2SRoopa Prabhu 44685b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 44695b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 44705b2f94b2SRoopa Prabhu ndm->ndm_type) { 44715b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 44725b2f94b2SRoopa Prabhu return -EINVAL; 44735b2f94b2SRoopa Prabhu } 44745b2f94b2SRoopa Prabhu 44755b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 44765b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 44775b2f94b2SRoopa Prabhu return -EINVAL; 44785b2f94b2SRoopa Prabhu } 44795b2f94b2SRoopa Prabhu 44808cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 44818cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 44825b2f94b2SRoopa Prabhu if (err < 0) 44835b2f94b2SRoopa Prabhu return err; 44845b2f94b2SRoopa Prabhu 44855b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 44865b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 44875b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 44885b2f94b2SRoopa Prabhu if (!tb[i]) 44895b2f94b2SRoopa Prabhu continue; 44905b2f94b2SRoopa Prabhu 44915b2f94b2SRoopa Prabhu switch (i) { 44925b2f94b2SRoopa Prabhu case NDA_MASTER: 44935b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 44945b2f94b2SRoopa Prabhu break; 44955b2f94b2SRoopa Prabhu case NDA_LLADDR: 44965b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 44975b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 44985b2f94b2SRoopa Prabhu return -EINVAL; 44995b2f94b2SRoopa Prabhu } 45005b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 45015b2f94b2SRoopa Prabhu break; 45025b2f94b2SRoopa Prabhu case NDA_VLAN: 45035b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 45045b2f94b2SRoopa Prabhu if (err) 45055b2f94b2SRoopa Prabhu return err; 45065b2f94b2SRoopa Prabhu break; 45075b2f94b2SRoopa Prabhu case NDA_VNI: 45085b2f94b2SRoopa Prabhu break; 45095b2f94b2SRoopa Prabhu default: 45105b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 45115b2f94b2SRoopa Prabhu return -EINVAL; 45125b2f94b2SRoopa Prabhu } 45135b2f94b2SRoopa Prabhu } 45145b2f94b2SRoopa Prabhu 45155b2f94b2SRoopa Prabhu return 0; 45165b2f94b2SRoopa Prabhu } 45175b2f94b2SRoopa Prabhu 45185b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 45195b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 45205b2f94b2SRoopa Prabhu { 45215b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 45225b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 45235b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 45245b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 45255b2f94b2SRoopa Prabhu struct sk_buff *skb; 45265b2f94b2SRoopa Prabhu int brport_idx = 0; 45275b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 45285b2f94b2SRoopa Prabhu int br_idx = 0; 45295b2f94b2SRoopa Prabhu u8 *addr = NULL; 45305b2f94b2SRoopa Prabhu u16 vid = 0; 45315b2f94b2SRoopa Prabhu int err; 45325b2f94b2SRoopa Prabhu 45335b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 45345b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 45355b2f94b2SRoopa Prabhu if (err < 0) 45365b2f94b2SRoopa Prabhu return err; 45375b2f94b2SRoopa Prabhu 4538f989d03eSNikolay Aleksandrov if (!addr) { 4539f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4540f989d03eSNikolay Aleksandrov return -EINVAL; 4541f989d03eSNikolay Aleksandrov } 4542f989d03eSNikolay Aleksandrov 45435b2f94b2SRoopa Prabhu if (brport_idx) { 45445b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 45455b2f94b2SRoopa Prabhu if (!dev) { 45465b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 45475b2f94b2SRoopa Prabhu return -ENODEV; 45485b2f94b2SRoopa Prabhu } 45495b2f94b2SRoopa Prabhu } 45505b2f94b2SRoopa Prabhu 45515b2f94b2SRoopa Prabhu if (br_idx) { 45525b2f94b2SRoopa Prabhu if (dev) { 45535b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 45545b2f94b2SRoopa Prabhu return -EINVAL; 45555b2f94b2SRoopa Prabhu } 45565b2f94b2SRoopa Prabhu 45575b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 45585b2f94b2SRoopa Prabhu if (!br_dev) { 45595b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 45605b2f94b2SRoopa Prabhu return -EINVAL; 45615b2f94b2SRoopa Prabhu } 45625b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 45635b2f94b2SRoopa Prabhu } 45645b2f94b2SRoopa Prabhu 45655b2f94b2SRoopa Prabhu if (dev) { 45665b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 45672e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 45685b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 45695b2f94b2SRoopa Prabhu return -EINVAL; 45705b2f94b2SRoopa Prabhu } 45715b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 45725b2f94b2SRoopa Prabhu if (!br_dev) { 45735b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 45745b2f94b2SRoopa Prabhu return -EINVAL; 45755b2f94b2SRoopa Prabhu } 45765b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 45775b2f94b2SRoopa Prabhu } else { 45785b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 45795b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 45805b2f94b2SRoopa Prabhu return -EINVAL; 45815b2f94b2SRoopa Prabhu } 45825b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 45835b2f94b2SRoopa Prabhu } 45845b2f94b2SRoopa Prabhu } 45855b2f94b2SRoopa Prabhu 45865b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 45875b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 45885b2f94b2SRoopa Prabhu return -ENODEV; 45895b2f94b2SRoopa Prabhu } 45905b2f94b2SRoopa Prabhu 45915b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 45925b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 45935b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 45945b2f94b2SRoopa Prabhu } 45955b2f94b2SRoopa Prabhu 45965b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 45975b2f94b2SRoopa Prabhu if (!skb) 45985b2f94b2SRoopa Prabhu return -ENOBUFS; 45995b2f94b2SRoopa Prabhu 46005b2f94b2SRoopa Prabhu if (br_dev) 46015b2f94b2SRoopa Prabhu dev = br_dev; 46025b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 46035b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 46045b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 46055b2f94b2SRoopa Prabhu if (err) 46065b2f94b2SRoopa Prabhu goto out; 46075b2f94b2SRoopa Prabhu 46085b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 46095b2f94b2SRoopa Prabhu out: 46105b2f94b2SRoopa Prabhu kfree_skb(skb); 46115b2f94b2SRoopa Prabhu return err; 46125b2f94b2SRoopa Prabhu } 46135b2f94b2SRoopa Prabhu 46142c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 46152c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 46162c3c031cSScott Feldman { 46172c3c031cSScott Feldman if (mask & flag) 46182c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 46192c3c031cSScott Feldman return 0; 46202c3c031cSScott Feldman } 46212c3c031cSScott Feldman 4622815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 46232c3c031cSScott Feldman struct net_device *dev, u16 mode, 46247d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 46257d4f8d87SScott Feldman u32 filter_mask, 46267d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 46277d4f8d87SScott Feldman struct net_device *dev, 46287d4f8d87SScott Feldman u32 filter_mask)) 4629815cccbfSJohn Fastabend { 4630815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4631815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4632815cccbfSJohn Fastabend struct nlattr *br_afspec; 46332c3c031cSScott Feldman struct nlattr *protinfo; 4634815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4635898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 46367d4f8d87SScott Feldman int err = 0; 4637815cccbfSJohn Fastabend 463846c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4639815cccbfSJohn Fastabend if (nlh == NULL) 4640815cccbfSJohn Fastabend return -EMSGSIZE; 4641815cccbfSJohn Fastabend 4642815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4643815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4644815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4645815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4646815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4647815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4648815cccbfSJohn Fastabend ifm->ifi_change = 0; 4649815cccbfSJohn Fastabend 4650815cccbfSJohn Fastabend 4651815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4652815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4653815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4654898e5061SJiri Pirko (br_dev && 4655898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4656815cccbfSJohn Fastabend (dev->addr_len && 4657815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4658a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4659a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4660815cccbfSJohn Fastabend goto nla_put_failure; 4661815cccbfSJohn Fastabend 4662ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4663815cccbfSJohn Fastabend if (!br_afspec) 4664815cccbfSJohn Fastabend goto nla_put_failure; 4665815cccbfSJohn Fastabend 46661d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4667815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4668815cccbfSJohn Fastabend goto nla_put_failure; 4669815cccbfSJohn Fastabend } 46701d460b98SRoopa Prabhu 46711d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 46721d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 46731d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 46741d460b98SRoopa Prabhu goto nla_put_failure; 46751d460b98SRoopa Prabhu } 46761d460b98SRoopa Prabhu } 46777d4f8d87SScott Feldman if (vlan_fill) { 46787d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 46797d4f8d87SScott Feldman if (err) { 46807d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 46817d4f8d87SScott Feldman goto nla_put_failure; 46827d4f8d87SScott Feldman } 46837d4f8d87SScott Feldman } 4684815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4685815cccbfSJohn Fastabend 4686ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 46872c3c031cSScott Feldman if (!protinfo) 46882c3c031cSScott Feldman goto nla_put_failure; 46892c3c031cSScott Feldman 46902c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 46912c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 46922c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46932c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 46942c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46952c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 46962c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 46972c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46982c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 46992c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47002c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 47012c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47022c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 47032c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 47042c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 47052c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4706583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4707583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4708583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4709583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4710583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 47112c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 47122c3c031cSScott Feldman goto nla_put_failure; 47132c3c031cSScott Feldman } 47142c3c031cSScott Feldman 47152c3c031cSScott Feldman nla_nest_end(skb, protinfo); 47162c3c031cSScott Feldman 4717053c095aSJohannes Berg nlmsg_end(skb, nlh); 4718053c095aSJohannes Berg return 0; 4719815cccbfSJohn Fastabend nla_put_failure: 4720815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 47217d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4722815cccbfSJohn Fastabend } 47237d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4724815cccbfSJohn Fastabend 47252d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 47262d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 47272d011be8SDavid Ahern struct netlink_ext_ack *extack) 47282d011be8SDavid Ahern { 47292d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 47302d011be8SDavid Ahern int err, i; 47312d011be8SDavid Ahern 47322d011be8SDavid Ahern if (strict_check) { 47332d011be8SDavid Ahern struct ifinfomsg *ifm; 47342d011be8SDavid Ahern 47352d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 47362d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 47372d011be8SDavid Ahern return -EINVAL; 47382d011be8SDavid Ahern } 47392d011be8SDavid Ahern 47402d011be8SDavid Ahern ifm = nlmsg_data(nlh); 47412d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 47422d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 47432d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 47442d011be8SDavid Ahern return -EINVAL; 47452d011be8SDavid Ahern } 47462d011be8SDavid Ahern 47478cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 47488cb08174SJohannes Berg sizeof(struct ifinfomsg), 47498cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 47508cb08174SJohannes Berg extack); 47512d011be8SDavid Ahern } else { 47528cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 47538cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 47548cb08174SJohannes Berg extack); 47552d011be8SDavid Ahern } 47562d011be8SDavid Ahern if (err < 0) 47572d011be8SDavid Ahern return err; 47582d011be8SDavid Ahern 47592d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 47602d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 47612d011be8SDavid Ahern if (!tb[i]) 47622d011be8SDavid Ahern continue; 47632d011be8SDavid Ahern 47642d011be8SDavid Ahern switch (i) { 47652d011be8SDavid Ahern case IFLA_EXT_MASK: 47662d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 47672d011be8SDavid Ahern break; 47682d011be8SDavid Ahern default: 47692d011be8SDavid Ahern if (strict_check) { 47702d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 47712d011be8SDavid Ahern return -EINVAL; 47722d011be8SDavid Ahern } 47732d011be8SDavid Ahern } 47742d011be8SDavid Ahern } 47752d011be8SDavid Ahern 47762d011be8SDavid Ahern return 0; 47772d011be8SDavid Ahern } 47782d011be8SDavid Ahern 4779e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4780e5a55a89SJohn Fastabend { 47812d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4782e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4783e5a55a89SJohn Fastabend struct net_device *dev; 4784e5a55a89SJohn Fastabend int idx = 0; 4785e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 47862d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 47876cbdceebSVlad Yasevich u32 filter_mask = 0; 4788d64f69b0SRoopa Prabhu int err; 47896cbdceebSVlad Yasevich 47902d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 47912d011be8SDavid Ahern cb->extack); 47922d011be8SDavid Ahern if (err < 0 && cb->strict_check) 47932d011be8SDavid Ahern return err; 4794e5a55a89SJohn Fastabend 4795e5a55a89SJohn Fastabend rcu_read_lock(); 4796e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4797e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4798898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4799e5a55a89SJohn Fastabend 4800898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4801d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4802d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4803d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4804d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4805f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4806f6c5775fSDavid Ahern if (likely(skb->len)) 4807e5a55a89SJohn Fastabend break; 4808f6c5775fSDavid Ahern 4809f6c5775fSDavid Ahern goto out_err; 4810f6c5775fSDavid Ahern } 4811d64f69b0SRoopa Prabhu } 4812e5a55a89SJohn Fastabend idx++; 4813e5a55a89SJohn Fastabend } 4814e5a55a89SJohn Fastabend 4815e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4816d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4817d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4818d64f69b0SRoopa Prabhu seq, dev, 481946c264daSNicolas Dichtel filter_mask, 4820d64f69b0SRoopa Prabhu NLM_F_MULTI); 4821f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4822f6c5775fSDavid Ahern if (likely(skb->len)) 4823e5a55a89SJohn Fastabend break; 4824f6c5775fSDavid Ahern 4825f6c5775fSDavid Ahern goto out_err; 4826f6c5775fSDavid Ahern } 4827d64f69b0SRoopa Prabhu } 4828e5a55a89SJohn Fastabend idx++; 4829e5a55a89SJohn Fastabend } 4830e5a55a89SJohn Fastabend } 4831f6c5775fSDavid Ahern err = skb->len; 4832f6c5775fSDavid Ahern out_err: 4833e5a55a89SJohn Fastabend rcu_read_unlock(); 4834e5a55a89SJohn Fastabend cb->args[0] = idx; 4835e5a55a89SJohn Fastabend 4836f6c5775fSDavid Ahern return err; 4837e5a55a89SJohn Fastabend } 4838e5a55a89SJohn Fastabend 48392469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 48402469ffd7SJohn Fastabend { 48412469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 48422469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 48432469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 48442469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 48452469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 48462469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 48472469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 48482469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 48492469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 48502469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 48512469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 48522469ffd7SJohn Fastabend } 48532469ffd7SJohn Fastabend 485402dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 48552469ffd7SJohn Fastabend { 48562469ffd7SJohn Fastabend struct net *net = dev_net(dev); 48572469ffd7SJohn Fastabend struct sk_buff *skb; 48582469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 48592469ffd7SJohn Fastabend 486002dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 486102dba438SRoopa Prabhu return 0; 486202dba438SRoopa Prabhu 48632469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 48642469ffd7SJohn Fastabend if (!skb) { 48652469ffd7SJohn Fastabend err = -ENOMEM; 48662469ffd7SJohn Fastabend goto errout; 48672469ffd7SJohn Fastabend } 48682469ffd7SJohn Fastabend 486946c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4870c38e01b8SJohn Fastabend if (err < 0) 4871c38e01b8SJohn Fastabend goto errout; 48722469ffd7SJohn Fastabend 4873d2e381c4SIdo Schimmel /* Notification info is only filled for bridge ports, not the bridge 4874d2e381c4SIdo Schimmel * device itself. Therefore, a zero notification length is valid and 4875d2e381c4SIdo Schimmel * should not result in an error. 4876d2e381c4SIdo Schimmel */ 4877d2e381c4SIdo Schimmel if (!skb->len) 487859ccaaaaSRoopa Prabhu goto errout; 487959ccaaaaSRoopa Prabhu 48802469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 48812469ffd7SJohn Fastabend return 0; 48822469ffd7SJohn Fastabend errout: 48832469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 48842469ffd7SJohn Fastabend kfree_skb(skb); 488559ccaaaaSRoopa Prabhu if (err) 48862469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 48872469ffd7SJohn Fastabend return err; 48882469ffd7SJohn Fastabend } 48892469ffd7SJohn Fastabend 4890c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4891c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4892e5a55a89SJohn Fastabend { 4893e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4894e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4895e5a55a89SJohn Fastabend struct net_device *dev; 48962469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 48972469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 48984de8b413SRosen, Rami u16 flags = 0; 4899c38e01b8SJohn Fastabend bool have_flags = false; 4900e5a55a89SJohn Fastabend 4901e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4902e5a55a89SJohn Fastabend return -EINVAL; 4903e5a55a89SJohn Fastabend 4904e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4905e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4906e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4907e5a55a89SJohn Fastabend 4908e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4909e5a55a89SJohn Fastabend if (!dev) { 4910b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4911e5a55a89SJohn Fastabend return -ENODEV; 4912e5a55a89SJohn Fastabend } 4913e5a55a89SJohn Fastabend 49142469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 49152469ffd7SJohn Fastabend if (br_spec) { 49162469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 49172469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 49186e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 49196e8d1c55SThomas Graf return -EINVAL; 49206e8d1c55SThomas Graf 4921c38e01b8SJohn Fastabend have_flags = true; 49222469ffd7SJohn Fastabend flags = nla_get_u16(attr); 49232469ffd7SJohn Fastabend break; 49242469ffd7SJohn Fastabend } 49252469ffd7SJohn Fastabend } 49262469ffd7SJohn Fastabend } 49272469ffd7SJohn Fastabend 49282469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4929898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4930898e5061SJiri Pirko 4931898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 49322469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4933e5a55a89SJohn Fastabend goto out; 4934e5a55a89SJohn Fastabend } 4935e5a55a89SJohn Fastabend 49362fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 49372fd527b7SPetr Machata extack); 49382469ffd7SJohn Fastabend if (err) 49392469ffd7SJohn Fastabend goto out; 49402469ffd7SJohn Fastabend 49412469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 49422469ffd7SJohn Fastabend } 49432469ffd7SJohn Fastabend 49442469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 49452469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 49462469ffd7SJohn Fastabend err = -EOPNOTSUPP; 49472469ffd7SJohn Fastabend else 4948add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 49492fd527b7SPetr Machata flags, 49502fd527b7SPetr Machata extack); 495102dba438SRoopa Prabhu if (!err) { 49522469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 495302dba438SRoopa Prabhu 495402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 495502dba438SRoopa Prabhu * change 495602dba438SRoopa Prabhu */ 495702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 495802dba438SRoopa Prabhu } 49592469ffd7SJohn Fastabend } 49602469ffd7SJohn Fastabend 4961c38e01b8SJohn Fastabend if (have_flags) 49622469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4963e5a55a89SJohn Fastabend out: 4964e5a55a89SJohn Fastabend return err; 4965e5a55a89SJohn Fastabend } 4966e5a55a89SJohn Fastabend 4967c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4968c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4969407af329SVlad Yasevich { 4970407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4971407af329SVlad Yasevich struct ifinfomsg *ifm; 4972407af329SVlad Yasevich struct net_device *dev; 4973407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4974407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 49754de8b413SRosen, Rami u16 flags = 0; 4976407af329SVlad Yasevich bool have_flags = false; 4977407af329SVlad Yasevich 4978407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4979407af329SVlad Yasevich return -EINVAL; 4980407af329SVlad Yasevich 4981407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4982407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4983407af329SVlad Yasevich return -EPFNOSUPPORT; 4984407af329SVlad Yasevich 4985407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4986407af329SVlad Yasevich if (!dev) { 4987b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4988407af329SVlad Yasevich return -ENODEV; 4989407af329SVlad Yasevich } 4990407af329SVlad Yasevich 4991407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4992407af329SVlad Yasevich if (br_spec) { 4993407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4994407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 49956e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 49966e8d1c55SThomas Graf return -EINVAL; 49976e8d1c55SThomas Graf 4998407af329SVlad Yasevich have_flags = true; 4999407af329SVlad Yasevich flags = nla_get_u16(attr); 5000407af329SVlad Yasevich break; 5001407af329SVlad Yasevich } 5002407af329SVlad Yasevich } 5003407af329SVlad Yasevich } 5004407af329SVlad Yasevich 5005407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5006407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5007407af329SVlad Yasevich 5008407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5009407af329SVlad Yasevich err = -EOPNOTSUPP; 5010407af329SVlad Yasevich goto out; 5011407af329SVlad Yasevich } 5012407af329SVlad Yasevich 5013add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5014407af329SVlad Yasevich if (err) 5015407af329SVlad Yasevich goto out; 5016407af329SVlad Yasevich 5017407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 5018407af329SVlad Yasevich } 5019407af329SVlad Yasevich 5020407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 5021407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 5022407af329SVlad Yasevich err = -EOPNOTSUPP; 5023407af329SVlad Yasevich else 5024add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5025add511b3SRoopa Prabhu flags); 5026407af329SVlad Yasevich 502702dba438SRoopa Prabhu if (!err) { 5028407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 502902dba438SRoopa Prabhu 503002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 503102dba438SRoopa Prabhu * change 503202dba438SRoopa Prabhu */ 503302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 503402dba438SRoopa Prabhu } 5035407af329SVlad Yasevich } 5036407af329SVlad Yasevich 5037407af329SVlad Yasevich if (have_flags) 5038407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5039407af329SVlad Yasevich out: 5040407af329SVlad Yasevich return err; 5041407af329SVlad Yasevich } 5042407af329SVlad Yasevich 5043e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5044e8872a25SNikolay Aleksandrov { 5045e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5046e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5047e8872a25SNikolay Aleksandrov } 5048e8872a25SNikolay Aleksandrov 504969ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 505069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 505169ae6ad2SNogah Frankel { 505269ae6ad2SNogah Frankel switch (attr_id) { 505369ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 505469ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 505569ae6ad2SNogah Frankel } 505669ae6ad2SNogah Frankel 505769ae6ad2SNogah Frankel return 0; 505869ae6ad2SNogah Frankel } 505969ae6ad2SNogah Frankel 506069ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 506169ae6ad2SNogah Frankel int *prividx) 506269ae6ad2SNogah Frankel { 506369ae6ad2SNogah Frankel struct nlattr *attr = NULL; 506469ae6ad2SNogah Frankel int attr_id, size; 506569ae6ad2SNogah Frankel void *attr_data; 506669ae6ad2SNogah Frankel int err; 506769ae6ad2SNogah Frankel 506869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 506969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 507069ae6ad2SNogah Frankel return -ENODATA; 507169ae6ad2SNogah Frankel 507269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 507369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 507469ae6ad2SNogah Frankel if (attr_id < *prividx) 507569ae6ad2SNogah Frankel continue; 507669ae6ad2SNogah Frankel 507769ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 507869ae6ad2SNogah Frankel if (!size) 507969ae6ad2SNogah Frankel continue; 508069ae6ad2SNogah Frankel 50813df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 508269ae6ad2SNogah Frankel continue; 508369ae6ad2SNogah Frankel 508469ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 508569ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 508669ae6ad2SNogah Frankel if (!attr) 508769ae6ad2SNogah Frankel goto nla_put_failure; 508869ae6ad2SNogah Frankel 508969ae6ad2SNogah Frankel attr_data = nla_data(attr); 509069ae6ad2SNogah Frankel memset(attr_data, 0, size); 509169ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 509269ae6ad2SNogah Frankel attr_data); 509369ae6ad2SNogah Frankel if (err) 509469ae6ad2SNogah Frankel goto get_offload_stats_failure; 509569ae6ad2SNogah Frankel } 509669ae6ad2SNogah Frankel 509769ae6ad2SNogah Frankel if (!attr) 509869ae6ad2SNogah Frankel return -ENODATA; 509969ae6ad2SNogah Frankel 510069ae6ad2SNogah Frankel *prividx = 0; 510169ae6ad2SNogah Frankel return 0; 510269ae6ad2SNogah Frankel 510369ae6ad2SNogah Frankel nla_put_failure: 510469ae6ad2SNogah Frankel err = -EMSGSIZE; 510569ae6ad2SNogah Frankel get_offload_stats_failure: 510669ae6ad2SNogah Frankel *prividx = attr_id; 510769ae6ad2SNogah Frankel return err; 510869ae6ad2SNogah Frankel } 510969ae6ad2SNogah Frankel 511069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 511169ae6ad2SNogah Frankel { 511269ae6ad2SNogah Frankel int nla_size = 0; 511369ae6ad2SNogah Frankel int attr_id; 511469ae6ad2SNogah Frankel int size; 511569ae6ad2SNogah Frankel 511669ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 511769ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 511869ae6ad2SNogah Frankel return 0; 511969ae6ad2SNogah Frankel 512069ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 512169ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 51223df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 512369ae6ad2SNogah Frankel continue; 512469ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 512569ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 512669ae6ad2SNogah Frankel } 512769ae6ad2SNogah Frankel 512869ae6ad2SNogah Frankel if (nla_size != 0) 512969ae6ad2SNogah Frankel nla_size += nla_total_size(0); 513069ae6ad2SNogah Frankel 513169ae6ad2SNogah Frankel return nla_size; 513269ae6ad2SNogah Frankel } 513369ae6ad2SNogah Frankel 513410c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 513510c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 5136e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 5137e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 513810c9ead9SRoopa Prabhu { 513910c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 514010c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 514110c9ead9SRoopa Prabhu struct nlattr *attr; 5142e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 514369ae6ad2SNogah Frankel int err; 514410c9ead9SRoopa Prabhu 514510c9ead9SRoopa Prabhu ASSERT_RTNL(); 514610c9ead9SRoopa Prabhu 514710c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 514810c9ead9SRoopa Prabhu if (!nlh) 514910c9ead9SRoopa Prabhu return -EMSGSIZE; 515010c9ead9SRoopa Prabhu 515110c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5152ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5153ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5154ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 515510c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 515610c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 515710c9ead9SRoopa Prabhu 5158e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 515910c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 516010c9ead9SRoopa Prabhu 516158414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 516258414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 516358414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 516410c9ead9SRoopa Prabhu if (!attr) 516510c9ead9SRoopa Prabhu goto nla_put_failure; 516610c9ead9SRoopa Prabhu 516710c9ead9SRoopa Prabhu sp = nla_data(attr); 516810c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 516910c9ead9SRoopa Prabhu } 517010c9ead9SRoopa Prabhu 517197a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 517297a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 517397a47facSNikolay Aleksandrov 517497a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 517597a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5176ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 517797a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 517897a47facSNikolay Aleksandrov if (!attr) 517997a47facSNikolay Aleksandrov goto nla_put_failure; 518097a47facSNikolay Aleksandrov 518180e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 518280e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 518380e73cc5SNikolay Aleksandrov if (err) 518480e73cc5SNikolay Aleksandrov goto nla_put_failure; 518580e73cc5SNikolay Aleksandrov *idxattr = 0; 518680e73cc5SNikolay Aleksandrov } 518780e73cc5SNikolay Aleksandrov } 518880e73cc5SNikolay Aleksandrov 518980e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 519080e73cc5SNikolay Aleksandrov *idxattr)) { 519180e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 519280e73cc5SNikolay Aleksandrov const struct net_device *master; 519380e73cc5SNikolay Aleksandrov 519480e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 519580e73cc5SNikolay Aleksandrov if (master) 519680e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 519780e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 519880e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5199ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 520080e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 520180e73cc5SNikolay Aleksandrov if (!attr) 520280e73cc5SNikolay Aleksandrov goto nla_put_failure; 520380e73cc5SNikolay Aleksandrov 520480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 520597a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 520697a47facSNikolay Aleksandrov if (err) 520797a47facSNikolay Aleksandrov goto nla_put_failure; 520897a47facSNikolay Aleksandrov *idxattr = 0; 520997a47facSNikolay Aleksandrov } 521097a47facSNikolay Aleksandrov } 521197a47facSNikolay Aleksandrov 521269ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 521369ae6ad2SNogah Frankel *idxattr)) { 521469ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5215ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5216ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 521769ae6ad2SNogah Frankel if (!attr) 521869ae6ad2SNogah Frankel goto nla_put_failure; 521969ae6ad2SNogah Frankel 522069ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 522169ae6ad2SNogah Frankel if (err == -ENODATA) 522269ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 522369ae6ad2SNogah Frankel else 522469ae6ad2SNogah Frankel nla_nest_end(skb, attr); 522569ae6ad2SNogah Frankel 522669ae6ad2SNogah Frankel if (err && err != -ENODATA) 522769ae6ad2SNogah Frankel goto nla_put_failure; 522869ae6ad2SNogah Frankel *idxattr = 0; 522969ae6ad2SNogah Frankel } 523069ae6ad2SNogah Frankel 5231aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5232aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5233aefb4d4aSRobert Shearman 5234aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5235ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5236aefb4d4aSRobert Shearman if (!attr) 5237aefb4d4aSRobert Shearman goto nla_put_failure; 5238aefb4d4aSRobert Shearman 52395fa85a09SFlorian Westphal rcu_read_lock(); 52405fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5241aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5242aefb4d4aSRobert Shearman struct nlattr *af; 5243aefb4d4aSRobert Shearman int err; 5244aefb4d4aSRobert Shearman 5245ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5246ae0be8deSMichal Kubecek af_ops->family); 52475fa85a09SFlorian Westphal if (!af) { 52485fa85a09SFlorian Westphal rcu_read_unlock(); 5249aefb4d4aSRobert Shearman goto nla_put_failure; 52505fa85a09SFlorian Westphal } 5251aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5252aefb4d4aSRobert Shearman 52535fa85a09SFlorian Westphal if (err == -ENODATA) { 5254aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 52555fa85a09SFlorian Westphal } else if (err < 0) { 52565fa85a09SFlorian Westphal rcu_read_unlock(); 5257aefb4d4aSRobert Shearman goto nla_put_failure; 52585fa85a09SFlorian Westphal } 5259aefb4d4aSRobert Shearman 5260aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5261aefb4d4aSRobert Shearman } 5262aefb4d4aSRobert Shearman } 52635fa85a09SFlorian Westphal rcu_read_unlock(); 5264aefb4d4aSRobert Shearman 5265aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5266aefb4d4aSRobert Shearman 5267aefb4d4aSRobert Shearman *idxattr = 0; 5268aefb4d4aSRobert Shearman } 5269aefb4d4aSRobert Shearman 527010c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 527110c9ead9SRoopa Prabhu 527210c9ead9SRoopa Prabhu return 0; 527310c9ead9SRoopa Prabhu 527410c9ead9SRoopa Prabhu nla_put_failure: 5275e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5276e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 527710c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5278e8872a25SNikolay Aleksandrov else 5279e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 528010c9ead9SRoopa Prabhu 528110c9ead9SRoopa Prabhu return -EMSGSIZE; 528210c9ead9SRoopa Prabhu } 528310c9ead9SRoopa Prabhu 528410c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 528510c9ead9SRoopa Prabhu u32 filter_mask) 528610c9ead9SRoopa Prabhu { 5287d3436799SEric Dumazet size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 528810c9ead9SRoopa Prabhu 5289e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 529010c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 529110c9ead9SRoopa Prabhu 529297a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 529397a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 529480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 529597a47facSNikolay Aleksandrov 529697a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 529780e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 529880e73cc5SNikolay Aleksandrov attr)); 529997a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 530097a47facSNikolay Aleksandrov size += nla_total_size(0); 530197a47facSNikolay Aleksandrov } 530297a47facSNikolay Aleksandrov } 530397a47facSNikolay Aleksandrov 530480e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 530580e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 530680e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 530780e73cc5SNikolay Aleksandrov const struct net_device *master; 530880e73cc5SNikolay Aleksandrov 530980e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 531080e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 531180e73cc5SNikolay Aleksandrov if (master) 531280e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 531380e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 531480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 531580e73cc5SNikolay Aleksandrov 531680e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 531780e73cc5SNikolay Aleksandrov attr)); 531880e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 531980e73cc5SNikolay Aleksandrov size += nla_total_size(0); 532080e73cc5SNikolay Aleksandrov } 532180e73cc5SNikolay Aleksandrov } 532280e73cc5SNikolay Aleksandrov 532369ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 532469ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 532569ae6ad2SNogah Frankel 5326aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5327aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5328aefb4d4aSRobert Shearman 5329aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5330aefb4d4aSRobert Shearman size += nla_total_size(0); 5331aefb4d4aSRobert Shearman 53325fa85a09SFlorian Westphal rcu_read_lock(); 53335fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5334aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5335aefb4d4aSRobert Shearman size += nla_total_size( 5336aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5337aefb4d4aSRobert Shearman 5338aefb4d4aSRobert Shearman /* for AF_* */ 5339aefb4d4aSRobert Shearman size += nla_total_size(0); 5340aefb4d4aSRobert Shearman } 5341aefb4d4aSRobert Shearman } 53425fa85a09SFlorian Westphal rcu_read_unlock(); 5343aefb4d4aSRobert Shearman } 5344aefb4d4aSRobert Shearman 534510c9ead9SRoopa Prabhu return size; 534610c9ead9SRoopa Prabhu } 534710c9ead9SRoopa Prabhu 534851bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 534951bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 535051bc860dSJakub Kicinski { 535151bc860dSJakub Kicinski struct if_stats_msg *ifsm; 535251bc860dSJakub Kicinski 535369f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 535451bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 535551bc860dSJakub Kicinski return -EINVAL; 535651bc860dSJakub Kicinski } 535751bc860dSJakub Kicinski 535851bc860dSJakub Kicinski if (!strict_check) 535951bc860dSJakub Kicinski return 0; 536051bc860dSJakub Kicinski 536151bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 536251bc860dSJakub Kicinski 536351bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 536451bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 536551bc860dSJakub Kicinski */ 536651bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 536751bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 536851bc860dSJakub Kicinski return -EINVAL; 536951bc860dSJakub Kicinski } 537051bc860dSJakub Kicinski if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 537151bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 537251bc860dSJakub Kicinski return -EINVAL; 537351bc860dSJakub Kicinski } 53746300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 53756300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 53766300acb2SJakub Kicinski return -EINVAL; 53776300acb2SJakub Kicinski } 537851bc860dSJakub Kicinski 537951bc860dSJakub Kicinski return 0; 538051bc860dSJakub Kicinski } 538151bc860dSJakub Kicinski 5382c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5383c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 538410c9ead9SRoopa Prabhu { 538510c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 538610c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5387e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5388e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 538910c9ead9SRoopa Prabhu struct sk_buff *nskb; 539010c9ead9SRoopa Prabhu u32 filter_mask; 539110c9ead9SRoopa Prabhu int err; 539210c9ead9SRoopa Prabhu 539351bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 539451bc860dSJakub Kicinski false, extack); 539551bc860dSJakub Kicinski if (err) 539651bc860dSJakub Kicinski return err; 53974775cc1fSMathias Krause 539810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 539910c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 540010c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 540110c9ead9SRoopa Prabhu else 540210c9ead9SRoopa Prabhu return -EINVAL; 540310c9ead9SRoopa Prabhu 540410c9ead9SRoopa Prabhu if (!dev) 540510c9ead9SRoopa Prabhu return -ENODEV; 540610c9ead9SRoopa Prabhu 540710c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 5408*22b67d17SPetr Machata if (!filter_mask) { 5409*22b67d17SPetr Machata NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 541010c9ead9SRoopa Prabhu return -EINVAL; 5411*22b67d17SPetr Machata } 541210c9ead9SRoopa Prabhu 541310c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 541410c9ead9SRoopa Prabhu if (!nskb) 541510c9ead9SRoopa Prabhu return -ENOBUFS; 541610c9ead9SRoopa Prabhu 541710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 541810c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5419e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 542010c9ead9SRoopa Prabhu if (err < 0) { 542110c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 542210c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 542310c9ead9SRoopa Prabhu kfree_skb(nskb); 542410c9ead9SRoopa Prabhu } else { 542510c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 542610c9ead9SRoopa Prabhu } 542710c9ead9SRoopa Prabhu 542810c9ead9SRoopa Prabhu return err; 542910c9ead9SRoopa Prabhu } 543010c9ead9SRoopa Prabhu 543110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 543210c9ead9SRoopa Prabhu { 5433841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5434e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 543510c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 543610c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5437e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5438e8872a25SNikolay Aleksandrov struct hlist_head *head; 5439e8872a25SNikolay Aleksandrov struct net_device *dev; 544010c9ead9SRoopa Prabhu u32 filter_mask = 0; 5441e8872a25SNikolay Aleksandrov int idx = 0; 544210c9ead9SRoopa Prabhu 544310c9ead9SRoopa Prabhu s_h = cb->args[0]; 544410c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5445e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5446e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 544710c9ead9SRoopa Prabhu 544810c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 544910c9ead9SRoopa Prabhu 545051bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 545151bc860dSJakub Kicinski if (err) 545251bc860dSJakub Kicinski return err; 54534775cc1fSMathias Krause 545410c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 5455841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 5456841891ecSDavid Ahern if (!filter_mask) { 5457841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5458841891ecSDavid Ahern return -EINVAL; 5459841891ecSDavid Ahern } 546010c9ead9SRoopa Prabhu 546110c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 546210c9ead9SRoopa Prabhu idx = 0; 546310c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 546410c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 546510c9ead9SRoopa Prabhu if (idx < s_idx) 546610c9ead9SRoopa Prabhu goto cont; 546710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 546810c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 546910c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 5470e8872a25SNikolay Aleksandrov flags, filter_mask, 5471e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 547210c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 547310c9ead9SRoopa Prabhu * we're in trouble 547410c9ead9SRoopa Prabhu */ 547510c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 547610c9ead9SRoopa Prabhu 547710c9ead9SRoopa Prabhu if (err < 0) 547810c9ead9SRoopa Prabhu goto out; 5479e8872a25SNikolay Aleksandrov s_prividx = 0; 5480e8872a25SNikolay Aleksandrov s_idxattr = 0; 548110c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 548210c9ead9SRoopa Prabhu cont: 548310c9ead9SRoopa Prabhu idx++; 548410c9ead9SRoopa Prabhu } 548510c9ead9SRoopa Prabhu } 548610c9ead9SRoopa Prabhu out: 5487e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5488e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 548910c9ead9SRoopa Prabhu cb->args[1] = idx; 549010c9ead9SRoopa Prabhu cb->args[0] = h; 549110c9ead9SRoopa Prabhu 549210c9ead9SRoopa Prabhu return skb->len; 549310c9ead9SRoopa Prabhu } 549410c9ead9SRoopa Prabhu 54951da177e4SLinus Torvalds /* Process one rtnetlink message. */ 54961da177e4SLinus Torvalds 54972d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 54982d4bc933SJohannes Berg struct netlink_ext_ack *extack) 54991da177e4SLinus Torvalds { 55003b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5501addf9b90SFlorian Westphal struct rtnl_link *link; 5502e4202511SFlorian Westphal struct module *owner; 55036853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5504e2849863SThomas Graf rtnl_doit_func doit; 550562256f98SFlorian Westphal unsigned int flags; 5506617cfc75SAlexander Kuleshov int kind; 55071da177e4SLinus Torvalds int family; 55081da177e4SLinus Torvalds int type; 55091da177e4SLinus Torvalds 55101da177e4SLinus Torvalds type = nlh->nlmsg_type; 55111da177e4SLinus Torvalds if (type > RTM_MAX) 5512038890feSThomas Graf return -EOPNOTSUPP; 55131da177e4SLinus Torvalds 55141da177e4SLinus Torvalds type -= RTM_BASE; 55151da177e4SLinus Torvalds 55161da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5517573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 55181da177e4SLinus Torvalds return 0; 55191da177e4SLinus Torvalds 5520573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 55211da177e4SLinus Torvalds kind = type&3; 55221da177e4SLinus Torvalds 552390f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 55241d00a4ebSThomas Graf return -EPERM; 55251da177e4SLinus Torvalds 55266853dd48SFlorian Westphal rcu_read_lock(); 5527b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 552897c53cacSDenis V. Lunev struct sock *rtnl; 5529e2849863SThomas Graf rtnl_dumpit_func dumpit; 5530ebfe3c51SDi Zhu u32 min_dump_alloc = 0; 55311da177e4SLinus Torvalds 5532addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5533addf9b90SFlorian Westphal if (!link || !link->dumpit) { 55346853dd48SFlorian Westphal family = PF_UNSPEC; 5535addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5536addf9b90SFlorian Westphal if (!link || !link->dumpit) 55376853dd48SFlorian Westphal goto err_unlock; 55386853dd48SFlorian Westphal } 5539e4202511SFlorian Westphal owner = link->owner; 5540addf9b90SFlorian Westphal dumpit = link->dumpit; 55416853dd48SFlorian Westphal 55425c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5543e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 55441da177e4SLinus Torvalds 5545e4202511SFlorian Westphal err = 0; 5546e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5547e4202511SFlorian Westphal if (!try_module_get(owner)) 5548e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5549e4202511SFlorian Westphal 55506853dd48SFlorian Westphal rcu_read_unlock(); 55516853dd48SFlorian Westphal 555297c53cacSDenis V. Lunev rtnl = net->rtnl; 5553e4202511SFlorian Westphal if (err == 0) { 555480d326faSPablo Neira Ayuso struct netlink_dump_control c = { 555580d326faSPablo Neira Ayuso .dump = dumpit, 555680d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5557e4202511SFlorian Westphal .module = owner, 555880d326faSPablo Neira Ayuso }; 555980d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5560e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5561e4202511SFlorian Westphal * module if dump is still in progress. 5562e4202511SFlorian Westphal */ 5563e4202511SFlorian Westphal module_put(owner); 556480d326faSPablo Neira Ayuso } 55652907c35fSEric Dumazet return err; 55661da177e4SLinus Torvalds } 55671da177e4SLinus Torvalds 5568addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5569addf9b90SFlorian Westphal if (!link || !link->doit) { 55708caa38b5SFlorian Westphal family = PF_UNSPEC; 5571addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 5572addf9b90SFlorian Westphal if (!link || !link->doit) 5573addf9b90SFlorian Westphal goto out_unlock; 55748caa38b5SFlorian Westphal } 55758caa38b5SFlorian Westphal 5576e4202511SFlorian Westphal owner = link->owner; 5577e4202511SFlorian Westphal if (!try_module_get(owner)) { 5578e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5579e4202511SFlorian Westphal goto out_unlock; 5580e4202511SFlorian Westphal } 5581e4202511SFlorian Westphal 5582addf9b90SFlorian Westphal flags = link->flags; 558362256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5584addf9b90SFlorian Westphal doit = link->doit; 558562256f98SFlorian Westphal rcu_read_unlock(); 558662256f98SFlorian Westphal if (doit) 558762256f98SFlorian Westphal err = doit(skb, nlh, extack); 5588e4202511SFlorian Westphal module_put(owner); 558962256f98SFlorian Westphal return err; 559062256f98SFlorian Westphal } 55916853dd48SFlorian Westphal rcu_read_unlock(); 55926853dd48SFlorian Westphal 55930cc09020SFlorian Westphal rtnl_lock(); 5594addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5595addf9b90SFlorian Westphal if (link && link->doit) 5596addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 55970cc09020SFlorian Westphal rtnl_unlock(); 5598addf9b90SFlorian Westphal 5599e4202511SFlorian Westphal module_put(owner); 5600e4202511SFlorian Westphal 5601addf9b90SFlorian Westphal return err; 5602addf9b90SFlorian Westphal 5603addf9b90SFlorian Westphal out_unlock: 5604addf9b90SFlorian Westphal rcu_read_unlock(); 56050cc09020SFlorian Westphal return err; 56060cc09020SFlorian Westphal 56070cc09020SFlorian Westphal err_unlock: 56086853dd48SFlorian Westphal rcu_read_unlock(); 56090cc09020SFlorian Westphal return -EOPNOTSUPP; 56101da177e4SLinus Torvalds } 56111da177e4SLinus Torvalds 5612cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 56131da177e4SLinus Torvalds { 5614cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 56151da177e4SLinus Torvalds } 56161da177e4SLinus Torvalds 56175f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 56185f729eaaSJulien Gomes { 56195f729eaaSJulien Gomes switch (group) { 56205f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 56215f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 56225f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 56235f729eaaSJulien Gomes return -EPERM; 56245f729eaaSJulien Gomes break; 56255f729eaaSJulien Gomes } 56265f729eaaSJulien Gomes return 0; 56275f729eaaSJulien Gomes } 56285f729eaaSJulien Gomes 56291da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 56301da177e4SLinus Torvalds { 5631351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5632e9dc8653SEric W. Biederman 56331da177e4SLinus Torvalds switch (event) { 56345138e86fSVlad Yasevich case NETDEV_REBOOT: 56358a212589SXin Long case NETDEV_CHANGEMTU: 56363753654eSDavid Ahern case NETDEV_CHANGEADDR: 56375138e86fSVlad Yasevich case NETDEV_CHANGENAME: 56385138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 56395138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 5640e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 56415138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 5642dc709f37SXin Long case NETDEV_CHANGEUPPER: 56435138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 56445138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 5645eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 5646ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 56473d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 564838e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 56491da177e4SLinus Torvalds break; 56501da177e4SLinus Torvalds default: 56511da177e4SLinus Torvalds break; 56521da177e4SLinus Torvalds } 56531da177e4SLinus Torvalds return NOTIFY_DONE; 56541da177e4SLinus Torvalds } 56551da177e4SLinus Torvalds 56561da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 56571da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 56581da177e4SLinus Torvalds }; 56591da177e4SLinus Torvalds 566097c53cacSDenis V. Lunev 56612c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 566297c53cacSDenis V. Lunev { 566397c53cacSDenis V. Lunev struct sock *sk; 5664a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 5665a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 5666a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 5667a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 56689785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 56695f729eaaSJulien Gomes .bind = rtnetlink_bind, 5670a31f2d17SPablo Neira Ayuso }; 5671a31f2d17SPablo Neira Ayuso 56729f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 567397c53cacSDenis V. Lunev if (!sk) 567497c53cacSDenis V. Lunev return -ENOMEM; 567597c53cacSDenis V. Lunev net->rtnl = sk; 567697c53cacSDenis V. Lunev return 0; 567797c53cacSDenis V. Lunev } 567897c53cacSDenis V. Lunev 56792c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 568097c53cacSDenis V. Lunev { 5681b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 568297c53cacSDenis V. Lunev net->rtnl = NULL; 568397c53cacSDenis V. Lunev } 568497c53cacSDenis V. Lunev 568597c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 568697c53cacSDenis V. Lunev .init = rtnetlink_net_init, 568797c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 568897c53cacSDenis V. Lunev }; 568997c53cacSDenis V. Lunev 56901da177e4SLinus Torvalds void __init rtnetlink_init(void) 56911da177e4SLinus Torvalds { 569297c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 56931da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 569497c53cacSDenis V. Lunev 56951da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 5696340d17fcSThomas Graf 5697c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5698b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 5699b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5700b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5701b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5702687ad8ccSThomas Graf 5703b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5704b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5705b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 570677162022SJohn Fastabend 570736fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 570836fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 570936fbf1e5SJiri Pirko 5710b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5711b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 57125b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5713e5a55a89SJohn Fastabend 5714b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5715b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5716b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 571710c9ead9SRoopa Prabhu 571810c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5719b97bac64SFlorian Westphal 0); 57201da177e4SLinus Torvalds } 5721