11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixes: 161da177e4SLinus Torvalds * Vitaly E. Lavrov RTA_OK arithmetics was wrong. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 19ee5d032fSJakub Kicinski #include <linux/bitops.h> 201da177e4SLinus Torvalds #include <linux/errno.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/types.h> 231da177e4SLinus Torvalds #include <linux/socket.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 251da177e4SLinus Torvalds #include <linux/timer.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/sockios.h> 281da177e4SLinus Torvalds #include <linux/net.h> 291da177e4SLinus Torvalds #include <linux/fcntl.h> 301da177e4SLinus Torvalds #include <linux/mm.h> 311da177e4SLinus Torvalds #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/capability.h> 341da177e4SLinus Torvalds #include <linux/skbuff.h> 351da177e4SLinus Torvalds #include <linux/init.h> 361da177e4SLinus Torvalds #include <linux/security.h> 376756ae4bSStephen Hemminger #include <linux/mutex.h> 381823730fSThomas Graf #include <linux/if_addr.h> 3977162022SJohn Fastabend #include <linux/if_bridge.h> 40f6f6424bSJiri Pirko #include <linux/if_vlan.h> 41ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 4277162022SJohn Fastabend #include <linux/etherdevice.h> 4358038695SMartin KaFai Lau #include <linux/bpf.h> 441da177e4SLinus Torvalds 457c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #include <net/ip.h> 501da177e4SLinus Torvalds #include <net/protocol.h> 511da177e4SLinus Torvalds #include <net/arp.h> 521da177e4SLinus Torvalds #include <net/route.h> 531da177e4SLinus Torvalds #include <net/udp.h> 54ea697639SDaniel Borkmann #include <net/tcp.h> 551da177e4SLinus Torvalds #include <net/sock.h> 561da177e4SLinus Torvalds #include <net/pkt_sched.h> 5714c0b97dSThomas Graf #include <net/fib_rules.h> 58e2849863SThomas Graf #include <net/rtnetlink.h> 5930ffee84SJohannes Berg #include <net/net_namespace.h> 601da177e4SLinus Torvalds 61a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 62ccf8dbcdSKees Cook #define RTNL_SLAVE_MAX_TYPE 36 63ccf8dbcdSKees Cook 64e0d087afSEric Dumazet struct rtnl_link { 65e2849863SThomas Graf rtnl_doit_func doit; 66e2849863SThomas Graf rtnl_dumpit_func dumpit; 67e4202511SFlorian Westphal struct module *owner; 6862256f98SFlorian Westphal unsigned int flags; 69addf9b90SFlorian Westphal struct rcu_head rcu; 70e2849863SThomas Graf }; 71e2849863SThomas Graf 726756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds void rtnl_lock(void) 751da177e4SLinus Torvalds { 766756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 771da177e4SLinus Torvalds } 78e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 791da177e4SLinus Torvalds 8079ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 8179ffdfc6SKirill Tkhai { 8279ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 8379ffdfc6SKirill Tkhai } 8479ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8579ffdfc6SKirill Tkhai 861b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 871b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 881b5c5493SEric Dumazet { 891b5c5493SEric Dumazet if (head && tail) { 901b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 911b5c5493SEric Dumazet defer_kfree_skb_list = head; 921b5c5493SEric Dumazet } 931b5c5493SEric Dumazet } 941b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 951b5c5493SEric Dumazet 966756ae4bSStephen Hemminger void __rtnl_unlock(void) 971da177e4SLinus Torvalds { 981b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 991b5c5493SEric Dumazet 1001b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 1011b5c5493SEric Dumazet 1026756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1031b5c5493SEric Dumazet 1041b5c5493SEric Dumazet while (head) { 1051b5c5493SEric Dumazet struct sk_buff *next = head->next; 1061b5c5493SEric Dumazet 1071b5c5493SEric Dumazet kfree_skb(head); 1081b5c5493SEric Dumazet cond_resched(); 1091b5c5493SEric Dumazet head = next; 1101b5c5493SEric Dumazet } 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds void rtnl_unlock(void) 1141da177e4SLinus Torvalds { 11558ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1161da177e4SLinus Torvalds netdev_run_todo(); 1171da177e4SLinus Torvalds } 118e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1191da177e4SLinus Torvalds 1206756ae4bSStephen Hemminger int rtnl_trylock(void) 1216756ae4bSStephen Hemminger { 1226756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1236756ae4bSStephen Hemminger } 124e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1256756ae4bSStephen Hemminger 126c9c1014bSPatrick McHardy int rtnl_is_locked(void) 127c9c1014bSPatrick McHardy { 128c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 129c9c1014bSPatrick McHardy } 130e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 131c9c1014bSPatrick McHardy 1326f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1336f99528eSVlad Buslov { 1346f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1356f99528eSVlad Buslov } 1366f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1376f99528eSVlad Buslov 138a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1390cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 140a898def2SPaul E. McKenney { 141a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 142a898def2SPaul E. McKenney } 143a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 144a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 145a898def2SPaul E. McKenney 146b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 147e2849863SThomas Graf 148e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 149e2849863SThomas Graf { 150e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 151e2849863SThomas Graf 152e2849863SThomas Graf /* 153e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 154e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 155e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 156e2849863SThomas Graf */ 157e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 158e2849863SThomas Graf 159e2849863SThomas Graf return msgindex; 160e2849863SThomas Graf } 161e2849863SThomas Graf 162addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 163addf9b90SFlorian Westphal { 164addf9b90SFlorian Westphal struct rtnl_link **tab; 165addf9b90SFlorian Westphal 166addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 167addf9b90SFlorian Westphal protocol = PF_UNSPEC; 168addf9b90SFlorian Westphal 169addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 170addf9b90SFlorian Westphal if (!tab) 171addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 172addf9b90SFlorian Westphal 173addf9b90SFlorian Westphal return tab[msgtype]; 174addf9b90SFlorian Westphal } 175addf9b90SFlorian Westphal 176e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 177e4202511SFlorian Westphal int protocol, int msgtype, 178c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 179b97bac64SFlorian Westphal unsigned int flags) 180e2849863SThomas Graf { 181b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 182b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 183e2849863SThomas Graf int msgindex; 184addf9b90SFlorian Westphal int ret = -ENOBUFS; 185e2849863SThomas Graf 18625239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 187e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 188e2849863SThomas Graf 189addf9b90SFlorian Westphal rtnl_lock(); 190addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 191e2849863SThomas Graf if (tab == NULL) { 192addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 193addf9b90SFlorian Westphal if (!tab) 194addf9b90SFlorian Westphal goto unlock; 195e2849863SThomas Graf 196addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1976853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 198e2849863SThomas Graf } 199e2849863SThomas Graf 200addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 201addf9b90SFlorian Westphal if (old) { 202addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 203addf9b90SFlorian Westphal if (!link) 204addf9b90SFlorian Westphal goto unlock; 205addf9b90SFlorian Westphal } else { 206addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 207addf9b90SFlorian Westphal if (!link) 208addf9b90SFlorian Westphal goto unlock; 209addf9b90SFlorian Westphal } 210e2849863SThomas Graf 211e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 212e4202511SFlorian Westphal link->owner = owner; 213e4202511SFlorian Westphal 214addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 215addf9b90SFlorian Westphal if (doit) 216addf9b90SFlorian Westphal link->doit = doit; 217addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 218addf9b90SFlorian Westphal if (dumpit) 219addf9b90SFlorian Westphal link->dumpit = dumpit; 220addf9b90SFlorian Westphal 221addf9b90SFlorian Westphal link->flags |= flags; 222addf9b90SFlorian Westphal 223addf9b90SFlorian Westphal /* publish protocol:msgtype */ 224addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 225addf9b90SFlorian Westphal ret = 0; 226addf9b90SFlorian Westphal if (old) 227addf9b90SFlorian Westphal kfree_rcu(old, rcu); 228addf9b90SFlorian Westphal unlock: 229addf9b90SFlorian Westphal rtnl_unlock(); 230addf9b90SFlorian Westphal return ret; 231e2849863SThomas Graf } 232e4202511SFlorian Westphal 233e4202511SFlorian Westphal /** 234e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 235e4202511SFlorian Westphal * 236e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 237e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 238e4202511SFlorian Westphal * @msgtype: rtnetlink message type 239e4202511SFlorian Westphal * @doit: Function pointer called for each request message 240e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 241e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 242e4202511SFlorian Westphal * 243e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 244e4202511SFlorian Westphal */ 245e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 246e4202511SFlorian Westphal int protocol, int msgtype, 247e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 248e4202511SFlorian Westphal unsigned int flags) 249e4202511SFlorian Westphal { 250e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 251e4202511SFlorian Westphal doit, dumpit, flags); 252e4202511SFlorian Westphal } 253e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 254e4202511SFlorian Westphal 255e4202511SFlorian Westphal /** 25616feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 257e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 258e4202511SFlorian Westphal * @msgtype: rtnetlink message type 259e4202511SFlorian Westphal * @doit: Function pointer called for each request message 260e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 261e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 262e4202511SFlorian Westphal * 263e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 264e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 265e4202511SFlorian Westphal * specified protocol family and message type is received. 266e4202511SFlorian Westphal * 267e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 268e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 269e4202511SFlorian Westphal * family exists. 270e2849863SThomas Graf */ 271e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 272c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 273b97bac64SFlorian Westphal unsigned int flags) 274e2849863SThomas Graf { 27516feebcfSFlorian Westphal int err; 27616feebcfSFlorian Westphal 27716feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27816feebcfSFlorian Westphal flags); 27916feebcfSFlorian Westphal if (err) 28016feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 28116feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 282e2849863SThomas Graf } 283e2849863SThomas Graf 284e2849863SThomas Graf /** 285e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 286e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 287e2849863SThomas Graf * @msgtype: rtnetlink message type 288e2849863SThomas Graf * 289e2849863SThomas Graf * Returns 0 on success or a negative error code. 290e2849863SThomas Graf */ 291e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 292e2849863SThomas Graf { 293addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 294e2849863SThomas Graf int msgindex; 295e2849863SThomas Graf 29625239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 297e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 298e2849863SThomas Graf 2996853dd48SFlorian Westphal rtnl_lock(); 300addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 301addf9b90SFlorian Westphal if (!tab) { 3026853dd48SFlorian Westphal rtnl_unlock(); 303e2849863SThomas Graf return -ENOENT; 3046853dd48SFlorian Westphal } 305e2849863SThomas Graf 306addf9b90SFlorian Westphal link = tab[msgindex]; 307addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 3086853dd48SFlorian Westphal rtnl_unlock(); 309e2849863SThomas Graf 310addf9b90SFlorian Westphal kfree_rcu(link, rcu); 311addf9b90SFlorian Westphal 312e2849863SThomas Graf return 0; 313e2849863SThomas Graf } 314e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 315e2849863SThomas Graf 316e2849863SThomas Graf /** 317e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 318e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 319e2849863SThomas Graf * 320e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 321e2849863SThomas Graf * of a certain protocol family. 322e2849863SThomas Graf */ 323e2849863SThomas Graf void rtnl_unregister_all(int protocol) 324e2849863SThomas Graf { 325addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 326addf9b90SFlorian Westphal int msgindex; 327019a3169SFlorian Westphal 32825239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 329e2849863SThomas Graf 330019a3169SFlorian Westphal rtnl_lock(); 331addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 332f707ef61SSabrina Dubroca if (!tab) { 333f707ef61SSabrina Dubroca rtnl_unlock(); 334f707ef61SSabrina Dubroca return; 335f707ef61SSabrina Dubroca } 3366853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 337addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 338addf9b90SFlorian Westphal link = tab[msgindex]; 339addf9b90SFlorian Westphal if (!link) 340addf9b90SFlorian Westphal continue; 341addf9b90SFlorian Westphal 342addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 343addf9b90SFlorian Westphal kfree_rcu(link, rcu); 344addf9b90SFlorian Westphal } 345019a3169SFlorian Westphal rtnl_unlock(); 346019a3169SFlorian Westphal 3476853dd48SFlorian Westphal synchronize_net(); 3486853dd48SFlorian Westphal 349addf9b90SFlorian Westphal kfree(tab); 350e2849863SThomas Graf } 351e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3521da177e4SLinus Torvalds 35338f7b870SPatrick McHardy static LIST_HEAD(link_ops); 35438f7b870SPatrick McHardy 355c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 356c63044f0SEric Dumazet { 357c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 358c63044f0SEric Dumazet 359c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 360c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 361c63044f0SEric Dumazet return ops; 362c63044f0SEric Dumazet } 363c63044f0SEric Dumazet return NULL; 364c63044f0SEric Dumazet } 365c63044f0SEric Dumazet 36638f7b870SPatrick McHardy /** 36738f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 36838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 36938f7b870SPatrick McHardy * 37038f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 37138f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 37238f7b870SPatrick McHardy * must be called before registering the devices. 37338f7b870SPatrick McHardy * 37438f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 37538f7b870SPatrick McHardy */ 37638f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 37738f7b870SPatrick McHardy { 378c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 379c63044f0SEric Dumazet return -EEXIST; 380c63044f0SEric Dumazet 381b0ab2fabSJiri Pirko /* The check for setup is here because if ops 382b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 383b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 384b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 385b0ab2fabSJiri Pirko */ 386b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 38723289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3882d85cba2SPatrick McHardy 38938f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 39038f7b870SPatrick McHardy return 0; 39138f7b870SPatrick McHardy } 39238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 39338f7b870SPatrick McHardy 39438f7b870SPatrick McHardy /** 39538f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 39638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 39738f7b870SPatrick McHardy * 39838f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 39938f7b870SPatrick McHardy */ 40038f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 40138f7b870SPatrick McHardy { 40238f7b870SPatrick McHardy int err; 40338f7b870SPatrick McHardy 404ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 405ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 406ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 407ccf8dbcdSKees Cook return -EINVAL; 408ccf8dbcdSKees Cook 40938f7b870SPatrick McHardy rtnl_lock(); 41038f7b870SPatrick McHardy err = __rtnl_link_register(ops); 41138f7b870SPatrick McHardy rtnl_unlock(); 41238f7b870SPatrick McHardy return err; 41338f7b870SPatrick McHardy } 41438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 41538f7b870SPatrick McHardy 416669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 417669f87baSPavel Emelyanov { 418669f87baSPavel Emelyanov struct net_device *dev; 41923289a37SEric Dumazet LIST_HEAD(list_kill); 42023289a37SEric Dumazet 421669f87baSPavel Emelyanov for_each_netdev(net, dev) { 42223289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 42323289a37SEric Dumazet ops->dellink(dev, &list_kill); 424669f87baSPavel Emelyanov } 42523289a37SEric Dumazet unregister_netdevice_many(&list_kill); 426669f87baSPavel Emelyanov } 427669f87baSPavel Emelyanov 42838f7b870SPatrick McHardy /** 42938f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 43038f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 43138f7b870SPatrick McHardy * 432554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 433554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 434554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 43538f7b870SPatrick McHardy */ 43638f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 43738f7b870SPatrick McHardy { 438881d966bSEric W. Biederman struct net *net; 4392d85cba2SPatrick McHardy 440881d966bSEric W. Biederman for_each_net(net) { 441669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 442881d966bSEric W. Biederman } 44338f7b870SPatrick McHardy list_del(&ops->list); 44438f7b870SPatrick McHardy } 44538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 44638f7b870SPatrick McHardy 447200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 448200b916fSCong Wang * devices unregistering in any network namespace. 449200b916fSCong Wang */ 450200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 451200b916fSCong Wang { 452200b916fSCong Wang struct net *net; 453200b916fSCong Wang bool unregistering; 454ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 455200b916fSCong Wang 456ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 457200b916fSCong Wang for (;;) { 458200b916fSCong Wang unregistering = false; 459200b916fSCong Wang rtnl_lock(); 460f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 461f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 462f0b07bb1SKirill Tkhai */ 463200b916fSCong Wang for_each_net(net) { 464200b916fSCong Wang if (net->dev_unreg_count > 0) { 465200b916fSCong Wang unregistering = true; 466200b916fSCong Wang break; 467200b916fSCong Wang } 468200b916fSCong Wang } 469200b916fSCong Wang if (!unregistering) 470200b916fSCong Wang break; 471200b916fSCong Wang __rtnl_unlock(); 472ff960a73SPeter Zijlstra 473ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 474200b916fSCong Wang } 475ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 476200b916fSCong Wang } 477200b916fSCong Wang 47838f7b870SPatrick McHardy /** 47938f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 48038f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 48138f7b870SPatrick McHardy */ 48238f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 48338f7b870SPatrick McHardy { 4848518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4854420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 486200b916fSCong Wang rtnl_lock_unregistering_all(); 48738f7b870SPatrick McHardy __rtnl_link_unregister(ops); 48838f7b870SPatrick McHardy rtnl_unlock(); 4894420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 49038f7b870SPatrick McHardy } 49138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 49238f7b870SPatrick McHardy 493ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 494ba7d49b1SJiri Pirko { 495ba7d49b1SJiri Pirko struct net_device *master_dev; 496ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4978515ae38SFlorian Westphal size_t size = 0; 498ba7d49b1SJiri Pirko 4998515ae38SFlorian Westphal rcu_read_lock(); 5008515ae38SFlorian Westphal 5018515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 502ba7d49b1SJiri Pirko if (!master_dev) 5038515ae38SFlorian Westphal goto out; 5048515ae38SFlorian Westphal 505ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5066049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5078515ae38SFlorian Westphal goto out; 508ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5098515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 510ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5118515ae38SFlorian Westphal 5128515ae38SFlorian Westphal out: 5138515ae38SFlorian Westphal rcu_read_unlock(); 5148515ae38SFlorian Westphal return size; 515ba7d49b1SJiri Pirko } 516ba7d49b1SJiri Pirko 51738f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 51838f7b870SPatrick McHardy { 51938f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 52038f7b870SPatrick McHardy size_t size; 52138f7b870SPatrick McHardy 52238f7b870SPatrick McHardy if (!ops) 52338f7b870SPatrick McHardy return 0; 52438f7b870SPatrick McHardy 525369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 526369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 52738f7b870SPatrick McHardy 52838f7b870SPatrick McHardy if (ops->get_size) 52938f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 530369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 53138f7b870SPatrick McHardy ops->get_size(dev); 53238f7b870SPatrick McHardy 53338f7b870SPatrick McHardy if (ops->get_xstats_size) 534369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 535369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 53638f7b870SPatrick McHardy 537ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 538ba7d49b1SJiri Pirko 53938f7b870SPatrick McHardy return size; 54038f7b870SPatrick McHardy } 54138f7b870SPatrick McHardy 542f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 543f8ff182cSThomas Graf 544f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 545f8ff182cSThomas Graf { 546f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 547f8ff182cSThomas Graf 5485fa85a09SFlorian Westphal list_for_each_entry_rcu(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) { 637ba7d49b1SJiri Pirko slave_data = nla_nest_start(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) { 669ba7d49b1SJiri Pirko data = nla_nest_start(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 68938f7b870SPatrick McHardy linkinfo = nla_nest_start(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 int err = 0; 7141da177e4SLinus Torvalds 715ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 7161da177e4SLinus Torvalds if (echo) 71763354797SReshetova, Elena refcount_inc(&skb->users); 7181da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 7191da177e4SLinus Torvalds if (echo) 7201da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 7211da177e4SLinus Torvalds return err; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 72497c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7252942e900SThomas Graf { 72697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72797c53cacSDenis V. Lunev 7282942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7292942e900SThomas Graf } 730e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7312942e900SThomas Graf 7321ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 73397676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 73497676b6bSThomas Graf { 73597c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73697676b6bSThomas Graf int report = 0; 73797676b6bSThomas Graf 73897676b6bSThomas Graf if (nlh) 73997676b6bSThomas Graf report = nlmsg_report(nlh); 74097676b6bSThomas Graf 7411ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 74297676b6bSThomas Graf } 743e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 74497676b6bSThomas Graf 74597c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 74697676b6bSThomas Graf { 74797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 74897c53cacSDenis V. Lunev 74997676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 75097676b6bSThomas Graf } 751e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 75297676b6bSThomas Graf 7531da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7541da177e4SLinus Torvalds { 7552d7202bfSThomas Graf struct nlattr *mx; 7562d7202bfSThomas Graf int i, valid = 0; 7571da177e4SLinus Torvalds 7582d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 7592d7202bfSThomas Graf if (mx == NULL) 7602d7202bfSThomas Graf return -ENOBUFS; 7612d7202bfSThomas Graf 7621da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7632d7202bfSThomas Graf if (metrics[i]) { 764ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 765ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 766ea697639SDaniel Borkmann 767ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 768ea697639SDaniel Borkmann if (!name) 769ea697639SDaniel Borkmann continue; 770ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 771ea697639SDaniel Borkmann goto nla_put_failure; 772c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 773c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 774c3a8d947SDaniel Borkmann 775f8edcd12SPhil Sutter if (!user_features) 776f8edcd12SPhil Sutter continue; 777c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 778c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 779c3a8d947SDaniel Borkmann goto nla_put_failure; 780ea697639SDaniel Borkmann } else { 781a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 782a6574349SDavid S. Miller goto nla_put_failure; 7831da177e4SLinus Torvalds } 784ea697639SDaniel Borkmann valid++; 785ea697639SDaniel Borkmann } 7862d7202bfSThomas Graf } 7871da177e4SLinus Torvalds 788a57d27fcSDavid S. Miller if (!valid) { 789a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 790a57d27fcSDavid S. Miller return 0; 791a57d27fcSDavid S. Miller } 7922d7202bfSThomas Graf 7932d7202bfSThomas Graf return nla_nest_end(skb, mx); 7942d7202bfSThomas Graf 7952d7202bfSThomas Graf nla_put_failure: 796bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 797bc3ed28cSThomas Graf return -EMSGSIZE; 7981da177e4SLinus Torvalds } 799e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8001da177e4SLinus Torvalds 801e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 80287a50699SDavid S. Miller long expires, u32 error) 803e3703b3dSThomas Graf { 804e3703b3dSThomas Graf struct rta_cacheinfo ci = { 805e3703b3dSThomas Graf .rta_error = error, 806e3703b3dSThomas Graf .rta_id = id, 807e3703b3dSThomas Graf }; 808e3703b3dSThomas Graf 8093940746dSDavid Ahern if (dst) { 8103940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8113940746dSDavid Ahern ci.rta_used = dst->__use; 8123940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8133940746dSDavid Ahern } 8148253947eSLi Wei if (expires) { 8158253947eSLi Wei unsigned long clock; 816e3703b3dSThomas Graf 8178253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8188253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8198253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8208253947eSLi Wei } 821e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 822e3703b3dSThomas Graf } 823e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8241da177e4SLinus Torvalds 82593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 826b00055aaSStefan Rompf { 827b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 828b00055aaSStefan Rompf 829b00055aaSStefan Rompf switch (transition) { 830b00055aaSStefan Rompf case IF_OPER_UP: 831b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 832b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 833b00055aaSStefan Rompf !netif_dormant(dev)) 834b00055aaSStefan Rompf operstate = IF_OPER_UP; 835b00055aaSStefan Rompf break; 836b00055aaSStefan Rompf 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) { 845b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 846b00055aaSStefan Rompf dev->operstate = operstate; 847b00055aaSStefan Rompf write_unlock_bh(&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)) + 9157e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9167e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 91779aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 91879aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 919ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9207e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 921945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 92201a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9233b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9247e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9253b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 926343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9273b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 928343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9293b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 930343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9313b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 932343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9333b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 934343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9353b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 936343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 937c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 938c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 939c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 940c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 941dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 942c02db8c6SChris Wright return size; 943c02db8c6SChris Wright } else 944ebc08a6fSWilliams, Mitch A return 0; 945ebc08a6fSWilliams, Mitch A } 946ebc08a6fSWilliams, Mitch A 947c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 948c53864fdSDavid Gibson u32 ext_filter_mask) 94957b61080SScott Feldman { 95057b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 95157b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 95257b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 95357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 95457b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 95557b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 95657b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 95757b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 95857b61080SScott Feldman + port_size; 95957b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 96057b61080SScott Feldman + port_size; 96157b61080SScott Feldman 962c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 963c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 96457b61080SScott Feldman return 0; 96557b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 96657b61080SScott Feldman return port_self_size + vf_ports_size + 96757b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 96857b61080SScott Feldman else 96957b61080SScott Feldman return port_self_size; 97057b61080SScott Feldman } 97157b61080SScott Feldman 972b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 973d1fdd913SBrenden Blanco { 974b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 97558038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 976a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9774f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 978d1fdd913SBrenden Blanco 979d1fdd913SBrenden Blanco return xdp_size; 980d1fdd913SBrenden Blanco } 981d1fdd913SBrenden Blanco 982115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 983115c9b81SGreg Rose u32 ext_filter_mask) 984339bf98fSThomas Graf { 985339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 986339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9870b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 988339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 989270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 990339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 99135c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 992339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 993339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 994339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 995339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 996339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 997339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 998339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9999a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1000edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 100176ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 100276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10036919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10046919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1005339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 100638f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10072d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1008d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1009db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1010115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1011115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1012115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1013c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1014f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1015b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 101682f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 101788d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1018c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1019b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10203d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10216621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 102238e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 102303ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 10247e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1025b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1026b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10273e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10283e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 102979e1ad14SJiri Benc + 0; 1030339bf98fSThomas Graf } 1031339bf98fSThomas Graf 103257b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 103357b61080SScott Feldman { 103457b61080SScott Feldman struct nlattr *vf_ports; 103557b61080SScott Feldman struct nlattr *vf_port; 103657b61080SScott Feldman int vf; 103757b61080SScott Feldman int err; 103857b61080SScott Feldman 103957b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 104057b61080SScott Feldman if (!vf_ports) 104157b61080SScott Feldman return -EMSGSIZE; 104257b61080SScott Feldman 104357b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 104457b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10458ca94183SScott Feldman if (!vf_port) 10468ca94183SScott Feldman goto nla_put_failure; 1047a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1048a6574349SDavid S. Miller goto nla_put_failure; 104957b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10508ca94183SScott Feldman if (err == -EMSGSIZE) 10518ca94183SScott Feldman goto nla_put_failure; 105257b61080SScott Feldman if (err) { 105357b61080SScott Feldman nla_nest_cancel(skb, vf_port); 105457b61080SScott Feldman continue; 105557b61080SScott Feldman } 105657b61080SScott Feldman nla_nest_end(skb, vf_port); 105757b61080SScott Feldman } 105857b61080SScott Feldman 105957b61080SScott Feldman nla_nest_end(skb, vf_ports); 106057b61080SScott Feldman 106157b61080SScott Feldman return 0; 10628ca94183SScott Feldman 10638ca94183SScott Feldman nla_put_failure: 10648ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10658ca94183SScott Feldman return -EMSGSIZE; 106657b61080SScott Feldman } 106757b61080SScott Feldman 106857b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 106957b61080SScott Feldman { 107057b61080SScott Feldman struct nlattr *port_self; 107157b61080SScott Feldman int err; 107257b61080SScott Feldman 107357b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 107457b61080SScott Feldman if (!port_self) 107557b61080SScott Feldman return -EMSGSIZE; 107657b61080SScott Feldman 107757b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 107857b61080SScott Feldman if (err) { 107957b61080SScott Feldman nla_nest_cancel(skb, port_self); 10808ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 108157b61080SScott Feldman } 108257b61080SScott Feldman 108357b61080SScott Feldman nla_nest_end(skb, port_self); 108457b61080SScott Feldman 108557b61080SScott Feldman return 0; 108657b61080SScott Feldman } 108757b61080SScott Feldman 1088c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1089c53864fdSDavid Gibson u32 ext_filter_mask) 109057b61080SScott Feldman { 109157b61080SScott Feldman int err; 109257b61080SScott Feldman 1093c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1094c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 109557b61080SScott Feldman return 0; 109657b61080SScott Feldman 109757b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 109857b61080SScott Feldman if (err) 109957b61080SScott Feldman return err; 110057b61080SScott Feldman 110157b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 110257b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 110357b61080SScott Feldman if (err) 110457b61080SScott Feldman return err; 110557b61080SScott Feldman } 110657b61080SScott Feldman 110757b61080SScott Feldman return 0; 110857b61080SScott Feldman } 110957b61080SScott Feldman 111066cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 111166cae9edSJiri Pirko { 111266cae9edSJiri Pirko int err; 111302637fceSJiri Pirko struct netdev_phys_item_id ppid; 111466cae9edSJiri Pirko 111566cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 111666cae9edSJiri Pirko if (err) { 111766cae9edSJiri Pirko if (err == -EOPNOTSUPP) 111866cae9edSJiri Pirko return 0; 111966cae9edSJiri Pirko return err; 112066cae9edSJiri Pirko } 112166cae9edSJiri Pirko 112266cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 112366cae9edSJiri Pirko return -EMSGSIZE; 112466cae9edSJiri Pirko 112566cae9edSJiri Pirko return 0; 112666cae9edSJiri Pirko } 112766cae9edSJiri Pirko 1128db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1129db24a904SDavid Ahern { 1130db24a904SDavid Ahern char name[IFNAMSIZ]; 1131db24a904SDavid Ahern int err; 1132db24a904SDavid Ahern 1133db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1134db24a904SDavid Ahern if (err) { 1135db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1136db24a904SDavid Ahern return 0; 1137db24a904SDavid Ahern return err; 1138db24a904SDavid Ahern } 1139db24a904SDavid Ahern 114077ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1141db24a904SDavid Ahern return -EMSGSIZE; 1142db24a904SDavid Ahern 1143db24a904SDavid Ahern return 0; 1144db24a904SDavid Ahern } 1145db24a904SDavid Ahern 114682f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 114782f28412SJiri Pirko { 1148*bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 114982f28412SJiri Pirko int err; 115082f28412SJiri Pirko 1151*bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 115282f28412SJiri Pirko if (err) { 115382f28412SJiri Pirko if (err == -EOPNOTSUPP) 115482f28412SJiri Pirko return 0; 115582f28412SJiri Pirko return err; 115682f28412SJiri Pirko } 115782f28412SJiri Pirko 1158*bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 115982f28412SJiri Pirko return -EMSGSIZE; 116082f28412SJiri Pirko 116182f28412SJiri Pirko return 0; 116282f28412SJiri Pirko } 116382f28412SJiri Pirko 1164b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1165b22b941bSHannes Frederic Sowa struct net_device *dev) 1166b22b941bSHannes Frederic Sowa { 1167550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1168b22b941bSHannes Frederic Sowa struct nlattr *attr; 1169b22b941bSHannes Frederic Sowa 117058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 117158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1172b22b941bSHannes Frederic Sowa if (!attr) 1173b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1174b22b941bSHannes Frederic Sowa 1175550bce59SRoopa Prabhu sp = nla_data(attr); 1176550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1177550bce59SRoopa Prabhu 1178550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1179550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1180550bce59SRoopa Prabhu if (!attr) 1181550bce59SRoopa Prabhu return -EMSGSIZE; 1182550bce59SRoopa Prabhu 1183550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1184b22b941bSHannes Frederic Sowa 1185b22b941bSHannes Frederic Sowa return 0; 1186b22b941bSHannes Frederic Sowa } 1187b22b941bSHannes Frederic Sowa 1188b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1189b22b941bSHannes Frederic Sowa struct net_device *dev, 1190b22b941bSHannes Frederic Sowa int vfs_num, 1191b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1192b22b941bSHannes Frederic Sowa { 1193b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 119479aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1195b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 119679aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1197b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1198b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1199b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1200b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1201b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1202b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1203b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1204b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1205b22b941bSHannes Frederic Sowa 12060eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12070eed9cf5SMintz, Yuval 1208b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1209b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1210b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1211b22b941bSHannes Frederic Sowa * didn't report anything. 1212b22b941bSHannes Frederic Sowa */ 1213b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1214b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1215b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1216b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1217b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1218b22b941bSHannes Frederic Sowa */ 1219b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 122079aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 122179aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1222b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1223b22b941bSHannes Frederic Sowa return 0; 1224b22b941bSHannes Frederic Sowa 1225775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1226775f4f05SDan Carpenter 1227b22b941bSHannes Frederic Sowa vf_mac.vf = 1228b22b941bSHannes Frederic Sowa vf_vlan.vf = 122979aab093SMoshe Shemesh vf_vlan_info.vf = 1230b22b941bSHannes Frederic Sowa vf_rate.vf = 1231b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1232b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1233b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1234b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1235b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1236b22b941bSHannes Frederic Sowa 1237b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1238b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1239b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 124079aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 124179aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 124279aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1243b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1244b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1245b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1246b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1247b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1248b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1249b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1250b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 125179aab093SMoshe Shemesh if (!vf) 125279aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1253b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1254b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1255b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1256b22b941bSHannes Frederic Sowa &vf_rate) || 1257b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1258b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1259b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1260b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1261b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1262b22b941bSHannes Frederic Sowa &vf_linkstate) || 1263b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1264b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1265b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1266b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1267b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 126879aab093SMoshe Shemesh goto nla_put_vf_failure; 126979aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 127079aab093SMoshe Shemesh if (!vfvlanlist) 127179aab093SMoshe Shemesh goto nla_put_vf_failure; 127279aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 127379aab093SMoshe Shemesh &vf_vlan_info)) { 127479aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 127579aab093SMoshe Shemesh goto nla_put_vf_failure; 127679aab093SMoshe Shemesh } 127779aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1278b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1279b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1280b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1281b22b941bSHannes Frederic Sowa &vf_stats); 1282b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 128379aab093SMoshe Shemesh if (!vfstats) 128479aab093SMoshe Shemesh goto nla_put_vf_failure; 1285343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1286343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1287343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1288343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1289343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1290343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1291343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1292343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1293343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1294343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1295343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1296c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1297c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1298c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1299c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1300c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 130179aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 130279aab093SMoshe Shemesh goto nla_put_vf_failure; 130379aab093SMoshe Shemesh } 1304b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1305b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1306b22b941bSHannes Frederic Sowa return 0; 130779aab093SMoshe Shemesh 130879aab093SMoshe Shemesh nla_put_vf_failure: 130979aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 131079aab093SMoshe Shemesh nla_put_vfinfo_failure: 131179aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 131279aab093SMoshe Shemesh return -EMSGSIZE; 1313b22b941bSHannes Frederic Sowa } 1314b22b941bSHannes Frederic Sowa 1315250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1316250fc3dfSFlorian Westphal struct net_device *dev, 1317250fc3dfSFlorian Westphal u32 ext_filter_mask) 1318250fc3dfSFlorian Westphal { 1319250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1320250fc3dfSFlorian Westphal int i, num_vfs; 1321250fc3dfSFlorian Westphal 1322250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1323250fc3dfSFlorian Westphal return 0; 1324250fc3dfSFlorian Westphal 1325250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1326250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1327250fc3dfSFlorian Westphal return -EMSGSIZE; 1328250fc3dfSFlorian Westphal 1329250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1330250fc3dfSFlorian Westphal return 0; 1331250fc3dfSFlorian Westphal 1332250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1333250fc3dfSFlorian Westphal if (!vfinfo) 1334250fc3dfSFlorian Westphal return -EMSGSIZE; 1335250fc3dfSFlorian Westphal 1336250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1337250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1338250fc3dfSFlorian Westphal return -EMSGSIZE; 1339250fc3dfSFlorian Westphal } 1340250fc3dfSFlorian Westphal 1341250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1342250fc3dfSFlorian Westphal return 0; 1343250fc3dfSFlorian Westphal } 1344250fc3dfSFlorian Westphal 1345b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1346b22b941bSHannes Frederic Sowa { 13475f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13485f8e4474SKangjie Lu 13495f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13505f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13515f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13525f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13535f8e4474SKangjie Lu map.irq = dev->irq; 13545f8e4474SKangjie Lu map.dma = dev->dma; 13555f8e4474SKangjie Lu map.port = dev->if_port; 13565f8e4474SKangjie Lu 1357270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1358b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1359b22b941bSHannes Frederic Sowa 1360b22b941bSHannes Frederic Sowa return 0; 1361b22b941bSHannes Frederic Sowa } 1362b22b941bSHannes Frederic Sowa 1363a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1364d67b9cd2SDaniel Borkmann { 136558038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1366d67b9cd2SDaniel Borkmann 1367d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1368d67b9cd2SDaniel Borkmann 136958038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1370a25717d2SJakub Kicinski if (!generic_xdp_prog) 1371a25717d2SJakub Kicinski return 0; 1372a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 137358038695SMartin KaFai Lau } 1374ce158e58SJakub Kicinski 1375a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1376a25717d2SJakub Kicinski { 1377a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1378a25717d2SJakub Kicinski } 1379118b4aa2SJakub Kicinski 1380a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1381a25717d2SJakub Kicinski { 1382a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1383a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1384a25717d2SJakub Kicinski } 1385a25717d2SJakub Kicinski 1386a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1387a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1388a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1389a25717d2SJakub Kicinski { 1390a25717d2SJakub Kicinski u32 curr_id; 1391a25717d2SJakub Kicinski int err; 1392a25717d2SJakub Kicinski 1393a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1394a25717d2SJakub Kicinski if (!curr_id) 1395a25717d2SJakub Kicinski return 0; 1396a25717d2SJakub Kicinski 1397a25717d2SJakub Kicinski *prog_id = curr_id; 1398a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1399a25717d2SJakub Kicinski if (err) 1400a25717d2SJakub Kicinski return err; 1401a25717d2SJakub Kicinski 1402a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1403a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1404a25717d2SJakub Kicinski else 1405a25717d2SJakub Kicinski *mode = tgt_mode; 1406a25717d2SJakub Kicinski 1407a25717d2SJakub Kicinski return 0; 1408d67b9cd2SDaniel Borkmann } 1409d67b9cd2SDaniel Borkmann 1410d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1411d1fdd913SBrenden Blanco { 1412d1fdd913SBrenden Blanco struct nlattr *xdp; 141358038695SMartin KaFai Lau u32 prog_id; 1414d1fdd913SBrenden Blanco int err; 14154f91da26SJakub Kicinski u8 mode; 1416d1fdd913SBrenden Blanco 1417d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1418d1fdd913SBrenden Blanco if (!xdp) 1419d1fdd913SBrenden Blanco return -EMSGSIZE; 1420b5cdae32SDavid S. Miller 1421a25717d2SJakub Kicinski prog_id = 0; 1422a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1423202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1424202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1425202aabe8SJakub Kicinski if (err) 1426a25717d2SJakub Kicinski goto err_cancel; 1427202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1428202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1429202aabe8SJakub Kicinski if (err) 1430a25717d2SJakub Kicinski goto err_cancel; 1431202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1432202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1433202aabe8SJakub Kicinski if (err) 1434a25717d2SJakub Kicinski goto err_cancel; 1435a25717d2SJakub Kicinski 14364f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1437d1fdd913SBrenden Blanco if (err) 1438d1fdd913SBrenden Blanco goto err_cancel; 1439d1fdd913SBrenden Blanco 1440a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 144158038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 144258038695SMartin KaFai Lau if (err) 144358038695SMartin KaFai Lau goto err_cancel; 144458038695SMartin KaFai Lau } 144558038695SMartin KaFai Lau 1446d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1447d1fdd913SBrenden Blanco return 0; 1448d1fdd913SBrenden Blanco 1449d1fdd913SBrenden Blanco err_cancel: 1450d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1451d1fdd913SBrenden Blanco return err; 1452d1fdd913SBrenden Blanco } 1453d1fdd913SBrenden Blanco 14543d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14553d3ea5afSVlad Yasevich { 14563d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14573d3ea5afSVlad Yasevich 14583d3ea5afSVlad Yasevich switch (event) { 14593d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14603d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14613d3ea5afSVlad Yasevich break; 14623d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14633d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14643d3ea5afSVlad Yasevich break; 14653d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14673d3ea5afSVlad Yasevich break; 14683d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14703d3ea5afSVlad Yasevich break; 14713d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14733d3ea5afSVlad Yasevich break; 14743d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14753d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14763d3ea5afSVlad Yasevich break; 14773d3ea5afSVlad Yasevich default: 14783d3ea5afSVlad Yasevich break; 14793d3ea5afSVlad Yasevich } 14803d3ea5afSVlad Yasevich 14813d3ea5afSVlad Yasevich return rtnl_event_type; 14823d3ea5afSVlad Yasevich } 14833d3ea5afSVlad Yasevich 148479110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 148579110a04SFlorian Westphal { 148679110a04SFlorian Westphal const struct net_device *upper_dev; 148779110a04SFlorian Westphal int ret = 0; 148879110a04SFlorian Westphal 148979110a04SFlorian Westphal rcu_read_lock(); 149079110a04SFlorian Westphal 149179110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 149279110a04SFlorian Westphal if (upper_dev) 149379110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 149479110a04SFlorian Westphal 149579110a04SFlorian Westphal rcu_read_unlock(); 149679110a04SFlorian Westphal return ret; 149779110a04SFlorian Westphal } 149879110a04SFlorian Westphal 149979110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 150079110a04SFlorian Westphal { 150179110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 150279110a04SFlorian Westphal 150379110a04SFlorian Westphal if (dev->ifindex == ifindex) 150479110a04SFlorian Westphal return 0; 150579110a04SFlorian Westphal 150679110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 150779110a04SFlorian Westphal } 150879110a04SFlorian Westphal 15096c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15106c557001SFlorian Westphal struct net_device *dev) 15116c557001SFlorian Westphal { 15126c557001SFlorian Westphal char buf[IFALIASZ]; 15136c557001SFlorian Westphal int ret; 15146c557001SFlorian Westphal 15156c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15166c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15176c557001SFlorian Westphal } 15186c557001SFlorian Westphal 1519b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 152079e1ad14SJiri Benc const struct net_device *dev, 152179e1ad14SJiri Benc struct net *src_net) 1522b1e66b9aSFlorian Westphal { 1523b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1524b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1525b1e66b9aSFlorian Westphal 1526b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 152779e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1528b1e66b9aSFlorian Westphal 1529b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1530b1e66b9aSFlorian Westphal return -EMSGSIZE; 1531b1e66b9aSFlorian Westphal } 1532b1e66b9aSFlorian Westphal } 1533b1e66b9aSFlorian Westphal 1534b1e66b9aSFlorian Westphal return 0; 1535b1e66b9aSFlorian Westphal } 1536b1e66b9aSFlorian Westphal 1537070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1538070cbf5bSFlorian Westphal const struct net_device *dev, 1539070cbf5bSFlorian Westphal u32 ext_filter_mask) 1540070cbf5bSFlorian Westphal { 1541070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1542070cbf5bSFlorian Westphal struct nlattr *af_spec; 1543070cbf5bSFlorian Westphal 1544070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1545070cbf5bSFlorian Westphal if (!af_spec) 1546070cbf5bSFlorian Westphal return -EMSGSIZE; 1547070cbf5bSFlorian Westphal 15485fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1549070cbf5bSFlorian Westphal struct nlattr *af; 1550070cbf5bSFlorian Westphal int err; 1551070cbf5bSFlorian Westphal 1552070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1553070cbf5bSFlorian Westphal continue; 1554070cbf5bSFlorian Westphal 1555070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1556070cbf5bSFlorian Westphal if (!af) 1557070cbf5bSFlorian Westphal return -EMSGSIZE; 1558070cbf5bSFlorian Westphal 1559070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1560070cbf5bSFlorian Westphal /* 1561070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1562070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1563070cbf5bSFlorian Westphal * means we should trim the attribute header and 1564070cbf5bSFlorian Westphal * continue. 1565070cbf5bSFlorian Westphal */ 1566070cbf5bSFlorian Westphal if (err == -ENODATA) 1567070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1568070cbf5bSFlorian Westphal else if (err < 0) 1569070cbf5bSFlorian Westphal return -EMSGSIZE; 1570070cbf5bSFlorian Westphal 1571070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1572070cbf5bSFlorian Westphal } 1573070cbf5bSFlorian Westphal 1574070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1575070cbf5bSFlorian Westphal return 0; 1576070cbf5bSFlorian Westphal } 1577070cbf5bSFlorian Westphal 157879e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 157979e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1580575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15813d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 158238e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 158338e01b30SNicolas Dichtel int tgt_netnsid) 1584b60c5115SThomas Graf { 1585b60c5115SThomas Graf struct ifinfomsg *ifm; 15861da177e4SLinus Torvalds struct nlmsghdr *nlh; 15871da177e4SLinus Torvalds 15882907c35fSEric Dumazet ASSERT_RTNL(); 1589b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1590b60c5115SThomas Graf if (nlh == NULL) 159126932566SPatrick McHardy return -EMSGSIZE; 15921da177e4SLinus Torvalds 1593b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1594b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1595b60c5115SThomas Graf ifm->__ifi_pad = 0; 1596b60c5115SThomas Graf ifm->ifi_type = dev->type; 1597b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1598b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1599b60c5115SThomas Graf ifm->ifi_change = change; 16001da177e4SLinus Torvalds 16017e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 160279e1ad14SJiri Benc goto nla_put_failure; 160379e1ad14SJiri Benc 1604a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1605a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1606a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1607a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1608a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1609a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 16103e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 16113e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1612a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1613edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 161476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1615c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1616c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 16171d69c2b3SMark A. Greer #ifdef CONFIG_RPS 161876ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 16191d69c2b3SMark A. Greer #endif 162079110a04SFlorian Westphal nla_put_iflink(skb, dev) || 162179110a04SFlorian Westphal put_master_ifindex(skb, dev) || 16229a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1623a6574349SDavid S. Miller (dev->qdisc && 1624a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 16256c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 16262d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1627b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1628b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1629b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1630b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1631b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1632b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1633b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1634a6574349SDavid S. Miller goto nla_put_failure; 16350b815a1aSStephen Hemminger 16363d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 16373d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 16383d3ea5afSVlad Yasevich goto nla_put_failure; 16393d3ea5afSVlad Yasevich } 16403d3ea5afSVlad Yasevich 1641b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1642a6574349SDavid S. Miller goto nla_put_failure; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds if (dev->addr_len) { 1645a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1646a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1647a6574349SDavid S. Miller goto nla_put_failure; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 165066cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 165166cae9edSJiri Pirko goto nla_put_failure; 165266cae9edSJiri Pirko 1653db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1654db24a904SDavid Ahern goto nla_put_failure; 1655db24a904SDavid Ahern 165682f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 165782f28412SJiri Pirko goto nla_put_failure; 165882f28412SJiri Pirko 1659b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1660b60c5115SThomas Graf goto nla_put_failure; 1661b60c5115SThomas Graf 1662250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1663a6574349SDavid S. Miller goto nla_put_failure; 166457b61080SScott Feldman 1665c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 166657b61080SScott Feldman goto nla_put_failure; 166757b61080SScott Feldman 1668d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1669d1fdd913SBrenden Blanco goto nla_put_failure; 1670d1fdd913SBrenden Blanco 1671ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 167238f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 167338f7b870SPatrick McHardy goto nla_put_failure; 167438f7b870SPatrick McHardy } 167538f7b870SPatrick McHardy 167679e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1677d37512a2SNicolas Dichtel goto nla_put_failure; 1678d37512a2SNicolas Dichtel 16796621dd29SNicolas Dichtel if (new_nsid && 16806621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16816621dd29SNicolas Dichtel goto nla_put_failure; 168238e01b30SNicolas Dichtel if (new_ifindex && 168338e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 168438e01b30SNicolas Dichtel goto nla_put_failure; 168538e01b30SNicolas Dichtel 16866621dd29SNicolas Dichtel 16875fa85a09SFlorian Westphal rcu_read_lock(); 1688070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16895fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16905fa85a09SFlorian Westphal rcu_read_unlock(); 1691f8ff182cSThomas Graf 1692053c095aSJohannes Berg nlmsg_end(skb, nlh); 1693053c095aSJohannes Berg return 0; 1694b60c5115SThomas Graf 16955fa85a09SFlorian Westphal nla_put_failure_rcu: 16965fa85a09SFlorian Westphal rcu_read_unlock(); 1697b60c5115SThomas Graf nla_put_failure: 169826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 169926932566SPatrick McHardy return -EMSGSIZE; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 1702f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1703f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1704f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1705f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1706f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1707f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1708f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1709f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1710f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1711f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1712f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1713f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1714f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1715f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1716f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1717f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 17182459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 17192459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 17202459b4c6SNicolas Dichtel */ 17212459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1722f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1723f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1724f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1725f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1726f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1727f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1728f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1729f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 173046e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 173146e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 173202637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 17332d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 173482f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1735317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 173688d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1737d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 17383d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1739db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 17407e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1741b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1742b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 17433e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 17443e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 1745f7b12606SJiri Pirko }; 1746f7b12606SJiri Pirko 1747f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1748f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1749f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1750f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1751f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1752f7b12606SJiri Pirko }; 1753f7b12606SJiri Pirko 1754f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1755364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1756364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 175779aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1758364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1759364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1760364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1761364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 176201a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17633b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1764dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1765cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1766cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17673b766cd8SEran Ben Elisha }; 17683b766cd8SEran Ben Elisha 1769f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1770f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1771f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1772f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1773f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1774f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1775f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1776f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1777f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1778f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1779025331dfSDaniel Borkmann 1780025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1781025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1782025331dfSDaniel Borkmann * combination with structs. 1783025331dfSDaniel Borkmann */ 1784025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1785025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1786f7b12606SJiri Pirko }; 1787f7b12606SJiri Pirko 1788d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1789d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1790d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 179185de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 179258038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1793d1fdd913SBrenden Blanco }; 1794d1fdd913SBrenden Blanco 1795dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1796dc599f76SDavid Ahern { 1797dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1798dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1799dc599f76SDavid Ahern 1800fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1801fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1802dc599f76SDavid Ahern return NULL; 1803dc599f76SDavid Ahern 1804dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1805dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1806dc599f76SDavid Ahern 1807dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1808dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1809dc599f76SDavid Ahern } 1810dc599f76SDavid Ahern 1811dc599f76SDavid Ahern return ops; 1812dc599f76SDavid Ahern } 1813dc599f76SDavid Ahern 1814dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1815dc599f76SDavid Ahern { 1816dc599f76SDavid Ahern struct net_device *master; 1817dc599f76SDavid Ahern 1818dc599f76SDavid Ahern if (!master_idx) 1819dc599f76SDavid Ahern return false; 1820dc599f76SDavid Ahern 1821dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1822dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1823dc599f76SDavid Ahern return true; 1824dc599f76SDavid Ahern 1825dc599f76SDavid Ahern return false; 1826dc599f76SDavid Ahern } 1827dc599f76SDavid Ahern 1828dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1829dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1830dc599f76SDavid Ahern { 1831dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1832dc599f76SDavid Ahern return true; 1833dc599f76SDavid Ahern 1834dc599f76SDavid Ahern return false; 1835dc599f76SDavid Ahern } 1836dc599f76SDavid Ahern 1837dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1838dc599f76SDavid Ahern int master_idx, 1839dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1840dc599f76SDavid Ahern { 1841dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1842dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1843dc599f76SDavid Ahern return true; 1844dc599f76SDavid Ahern 1845dc599f76SDavid Ahern return false; 1846dc599f76SDavid Ahern } 1847dc599f76SDavid Ahern 1848c383edc4SChristian Brauner /** 1849c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 1850c383edc4SChristian Brauner * @sk: netlink socket 1851c383edc4SChristian Brauner * @netnsid: network namespace identifier 1852c383edc4SChristian Brauner * 1853c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 1854c383edc4SChristian Brauner * pointer on failure. 1855c383edc4SChristian Brauner */ 1856c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 185779e1ad14SJiri Benc { 185879e1ad14SJiri Benc struct net *net; 185979e1ad14SJiri Benc 1860f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 186179e1ad14SJiri Benc if (!net) 186279e1ad14SJiri Benc return ERR_PTR(-EINVAL); 186379e1ad14SJiri Benc 186479e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 186579e1ad14SJiri Benc * the user namespace owning the target net ns. 186679e1ad14SJiri Benc */ 1867f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 186879e1ad14SJiri Benc put_net(net); 186979e1ad14SJiri Benc return ERR_PTR(-EACCES); 187079e1ad14SJiri Benc } 187179e1ad14SJiri Benc return net; 187279e1ad14SJiri Benc } 1873c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 187479e1ad14SJiri Benc 1875905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 1876905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 1877905cf0abSDavid Ahern struct netlink_ext_ack *extack) 1878905cf0abSDavid Ahern { 1879905cf0abSDavid Ahern int hdrlen; 1880905cf0abSDavid Ahern 1881905cf0abSDavid Ahern if (strict_check) { 1882905cf0abSDavid Ahern struct ifinfomsg *ifm; 1883905cf0abSDavid Ahern 1884905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 1885905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 1886905cf0abSDavid Ahern return -EINVAL; 1887905cf0abSDavid Ahern } 1888905cf0abSDavid Ahern 1889905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 1890905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 1891905cf0abSDavid Ahern ifm->ifi_change) { 1892905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 1893905cf0abSDavid Ahern return -EINVAL; 1894905cf0abSDavid Ahern } 1895905cf0abSDavid Ahern if (ifm->ifi_index) { 1896905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 1897905cf0abSDavid Ahern return -EINVAL; 1898905cf0abSDavid Ahern } 1899905cf0abSDavid Ahern 1900905cf0abSDavid Ahern return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 1901905cf0abSDavid Ahern ifla_policy, extack); 1902905cf0abSDavid Ahern } 1903905cf0abSDavid Ahern 1904905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 1905905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1906905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1907905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 1908905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1909905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 1910905cf0abSDavid Ahern */ 1911905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 1912905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1913905cf0abSDavid Ahern 1914905cf0abSDavid Ahern return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack); 1915905cf0abSDavid Ahern } 1916905cf0abSDavid Ahern 1917b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 19181da177e4SLinus Torvalds { 1919905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 1920905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 19213b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 192279e1ad14SJiri Benc struct net *tgt_net = net; 19237c28bd0bSEric Dumazet int h, s_h; 19247c28bd0bSEric Dumazet int idx = 0, s_idx; 19251da177e4SLinus Torvalds struct net_device *dev; 19267c28bd0bSEric Dumazet struct hlist_head *head; 1927115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1928115c9b81SGreg Rose u32 ext_filter_mask = 0; 1929dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1930dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1931dc599f76SDavid Ahern int master_idx = 0; 193279e1ad14SJiri Benc int netnsid = -1; 1933905cf0abSDavid Ahern int err, i; 19341da177e4SLinus Torvalds 19357c28bd0bSEric Dumazet s_h = cb->args[0]; 19367c28bd0bSEric Dumazet s_idx = cb->args[1]; 19377c28bd0bSEric Dumazet 1938905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 1939905cf0abSDavid Ahern if (err < 0) { 1940905cf0abSDavid Ahern if (cb->strict_check) 1941905cf0abSDavid Ahern return err; 1942e5eca6d4SMichal Schmidt 1943905cf0abSDavid Ahern goto walk_entries; 194479e1ad14SJiri Benc } 194579e1ad14SJiri Benc 1946905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 1947905cf0abSDavid Ahern if (!tb[i]) 1948905cf0abSDavid Ahern continue; 1949dc599f76SDavid Ahern 1950905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 1951905cf0abSDavid Ahern switch (i) { 1952905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 1953905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 1954905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 1955905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 1956905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 1957905cf0abSDavid Ahern return PTR_ERR(tgt_net); 1958905cf0abSDavid Ahern } 1959905cf0abSDavid Ahern break; 1960905cf0abSDavid Ahern case IFLA_EXT_MASK: 1961905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 1962905cf0abSDavid Ahern break; 1963905cf0abSDavid Ahern case IFLA_MASTER: 1964905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 1965905cf0abSDavid Ahern break; 1966905cf0abSDavid Ahern case IFLA_LINKINFO: 1967905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 1968905cf0abSDavid Ahern break; 1969905cf0abSDavid Ahern default: 1970905cf0abSDavid Ahern if (cb->strict_check) { 1971905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 1972905cf0abSDavid Ahern return -EINVAL; 1973905cf0abSDavid Ahern } 1974905cf0abSDavid Ahern } 1975905cf0abSDavid Ahern } 1976dc599f76SDavid Ahern 1977dc599f76SDavid Ahern if (master_idx || kind_ops) 1978dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1979115c9b81SGreg Rose 1980905cf0abSDavid Ahern walk_entries: 19817c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 19827562f876SPavel Emelianov idx = 0; 198379e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1984cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1985dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 19863f0ae05dSZhang Shengju goto cont; 19871da177e4SLinus Torvalds if (idx < s_idx) 19887562f876SPavel Emelianov goto cont; 198979e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 199079e1ad14SJiri Benc RTM_NEWLINK, 199115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 1992905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 199338e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 199479e1ad14SJiri Benc netnsid); 1995973462bbSDavid Gibson 1996f6c5775fSDavid Ahern if (err < 0) { 1997f6c5775fSDavid Ahern if (likely(skb->len)) 19987c28bd0bSEric Dumazet goto out; 19994e985adaSThomas Graf 2000f6c5775fSDavid Ahern goto out_err; 2001f6c5775fSDavid Ahern } 20027562f876SPavel Emelianov cont: 20037562f876SPavel Emelianov idx++; 20041da177e4SLinus Torvalds } 20057c28bd0bSEric Dumazet } 20067c28bd0bSEric Dumazet out: 2007f6c5775fSDavid Ahern err = skb->len; 2008f6c5775fSDavid Ahern out_err: 20097c28bd0bSEric Dumazet cb->args[1] = idx; 20107c28bd0bSEric Dumazet cb->args[0] = h; 2011d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 2012d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 201379e1ad14SJiri Benc if (netnsid >= 0) 201479e1ad14SJiri Benc put_net(tgt_net); 20151da177e4SLinus Torvalds 2016f6c5775fSDavid Ahern return err; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 2019fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2020fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2021f7b12606SJiri Pirko { 2022fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 2023f7b12606SJiri Pirko } 2024f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 202557b61080SScott Feldman 202681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 202781adee47SEric W. Biederman { 202881adee47SEric W. Biederman struct net *net; 202981adee47SEric W. Biederman /* Examine the link attributes and figure out which 203081adee47SEric W. Biederman * network namespace we are talking about. 203181adee47SEric W. Biederman */ 203281adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 203381adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2034f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2035f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 203681adee47SEric W. Biederman else 203781adee47SEric W. Biederman net = get_net(src_net); 203881adee47SEric W. Biederman return net; 203981adee47SEric W. Biederman } 204081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 204181adee47SEric W. Biederman 20427c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 20437c4f63baSChristian Brauner * examining the link attributes in the following order: 20447c4f63baSChristian Brauner * 20457c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 20467c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 20477e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 20487c4f63baSChristian Brauner */ 20497c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 20507c4f63baSChristian Brauner struct nlattr *tb[]) 20517c4f63baSChristian Brauner { 20527c4f63baSChristian Brauner struct net *net; 20537c4f63baSChristian Brauner 20547c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 20557c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 20567c4f63baSChristian Brauner 20577e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 20587c4f63baSChristian Brauner return get_net(src_net); 20597c4f63baSChristian Brauner 20607e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 20617c4f63baSChristian Brauner if (!net) 20627c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 20637c4f63baSChristian Brauner 20647c4f63baSChristian Brauner return net; 20657c4f63baSChristian Brauner } 20667c4f63baSChristian Brauner 20677c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 20687c4f63baSChristian Brauner struct net *src_net, 20697c4f63baSChristian Brauner struct nlattr *tb[], int cap) 20707c4f63baSChristian Brauner { 20717c4f63baSChristian Brauner struct net *net; 20727c4f63baSChristian Brauner 20737c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 20747c4f63baSChristian Brauner if (IS_ERR(net)) 20757c4f63baSChristian Brauner return net; 20767c4f63baSChristian Brauner 20777c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 20787c4f63baSChristian Brauner put_net(net); 20797c4f63baSChristian Brauner return ERR_PTR(-EPERM); 20807c4f63baSChristian Brauner } 20817c4f63baSChristian Brauner 20827c4f63baSChristian Brauner return net; 20837c4f63baSChristian Brauner } 20847c4f63baSChristian Brauner 20854ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 20864ff66caeSChristian Brauner * potentially referring to different network namespaces. 20874ff66caeSChristian Brauner */ 20884ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 20894ff66caeSChristian Brauner struct netlink_ext_ack *extack, 20904ff66caeSChristian Brauner bool netns_id_only) 20914ff66caeSChristian Brauner { 20924ff66caeSChristian Brauner 20934ff66caeSChristian Brauner if (netns_id_only) { 20944ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 20954ff66caeSChristian Brauner return 0; 20964ff66caeSChristian Brauner 20974ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 20984ff66caeSChristian Brauner return -EOPNOTSUPP; 20994ff66caeSChristian Brauner } 21004ff66caeSChristian Brauner 21017e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 21024ff66caeSChristian Brauner goto invalid_attr; 21034ff66caeSChristian Brauner 21047e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 21054ff66caeSChristian Brauner goto invalid_attr; 21064ff66caeSChristian Brauner 21077e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 21084ff66caeSChristian Brauner goto invalid_attr; 21094ff66caeSChristian Brauner 21104ff66caeSChristian Brauner return 0; 21114ff66caeSChristian Brauner 21124ff66caeSChristian Brauner invalid_attr: 21134ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 21144ff66caeSChristian Brauner return -EINVAL; 21154ff66caeSChristian Brauner } 21164ff66caeSChristian Brauner 21171840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 21181840bb13SThomas Graf { 21191840bb13SThomas Graf if (dev) { 21201840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 21211840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 21221840bb13SThomas Graf return -EINVAL; 21231840bb13SThomas Graf 21241840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 21251840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 21261840bb13SThomas Graf return -EINVAL; 21271840bb13SThomas Graf } 21281840bb13SThomas Graf 2129cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2130cf7afbfeSThomas Graf struct nlattr *af; 2131cf7afbfeSThomas Graf int rem, err; 2132cf7afbfeSThomas Graf 2133cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2134cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2135cf7afbfeSThomas Graf 21365fa85a09SFlorian Westphal rcu_read_lock(); 21375fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 21385fa85a09SFlorian Westphal if (!af_ops) { 21395fa85a09SFlorian Westphal rcu_read_unlock(); 2140cf7afbfeSThomas Graf return -EAFNOSUPPORT; 21415fa85a09SFlorian Westphal } 2142cf7afbfeSThomas Graf 21435fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 21445fa85a09SFlorian Westphal rcu_read_unlock(); 2145cf7afbfeSThomas Graf return -EOPNOTSUPP; 21465fa85a09SFlorian Westphal } 2147cf7afbfeSThomas Graf 2148cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 21496d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 21505fa85a09SFlorian Westphal if (err < 0) { 21515fa85a09SFlorian Westphal rcu_read_unlock(); 2152cf7afbfeSThomas Graf return err; 2153cf7afbfeSThomas Graf } 2154cf7afbfeSThomas Graf } 21555fa85a09SFlorian Westphal 21565fa85a09SFlorian Westphal rcu_read_unlock(); 21575fa85a09SFlorian Westphal } 2158cf7afbfeSThomas Graf } 2159cf7afbfeSThomas Graf 21601840bb13SThomas Graf return 0; 21611840bb13SThomas Graf } 21621840bb13SThomas Graf 2163cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2164cc8e27ccSEli Cohen int guid_type) 2165cc8e27ccSEli Cohen { 2166cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2167cc8e27ccSEli Cohen 2168cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2169cc8e27ccSEli Cohen } 2170cc8e27ccSEli Cohen 2171cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2172cc8e27ccSEli Cohen { 2173cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2174cc8e27ccSEli Cohen return -EOPNOTSUPP; 2175cc8e27ccSEli Cohen 2176cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2177cc8e27ccSEli Cohen } 2178cc8e27ccSEli Cohen 21794f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2180c02db8c6SChris Wright { 2181c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 21824f7d2cdfSDaniel Borkmann int err = -EINVAL; 2183c02db8c6SChris Wright 21844f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 21854f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 21864f7d2cdfSDaniel Borkmann 2187c02db8c6SChris Wright err = -EOPNOTSUPP; 2188c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2189c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2190c02db8c6SChris Wright ivm->mac); 21914f7d2cdfSDaniel Borkmann if (err < 0) 21924f7d2cdfSDaniel Borkmann return err; 2193c02db8c6SChris Wright } 21944f7d2cdfSDaniel Borkmann 21954f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 21964f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 21974f7d2cdfSDaniel Borkmann 2198c02db8c6SChris Wright err = -EOPNOTSUPP; 2199c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 22004f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 220179aab093SMoshe Shemesh ivv->qos, 220279aab093SMoshe Shemesh htons(ETH_P_8021Q)); 220379aab093SMoshe Shemesh if (err < 0) 220479aab093SMoshe Shemesh return err; 220579aab093SMoshe Shemesh } 220679aab093SMoshe Shemesh 220779aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 220879aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 220979aab093SMoshe Shemesh struct nlattr *attr; 221079aab093SMoshe Shemesh int rem, len = 0; 221179aab093SMoshe Shemesh 221279aab093SMoshe Shemesh err = -EOPNOTSUPP; 221379aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 221479aab093SMoshe Shemesh return err; 221579aab093SMoshe Shemesh 221679aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 221779aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 221879aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 221979aab093SMoshe Shemesh return -EINVAL; 222079aab093SMoshe Shemesh } 222179aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 222279aab093SMoshe Shemesh return -EOPNOTSUPP; 222379aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 222479aab093SMoshe Shemesh 222579aab093SMoshe Shemesh len++; 222679aab093SMoshe Shemesh } 2227fa34cd94SArnd Bergmann if (len == 0) 2228fa34cd94SArnd Bergmann return -EINVAL; 2229fa34cd94SArnd Bergmann 223079aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 223179aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 22324f7d2cdfSDaniel Borkmann if (err < 0) 22334f7d2cdfSDaniel Borkmann return err; 2234c02db8c6SChris Wright } 22354f7d2cdfSDaniel Borkmann 22364f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 22374f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2238ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 22394f7d2cdfSDaniel Borkmann 2240c02db8c6SChris Wright err = -EOPNOTSUPP; 2241ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 22424f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 22434f7d2cdfSDaniel Borkmann if (err < 0) 22444f7d2cdfSDaniel Borkmann return err; 22454f7d2cdfSDaniel Borkmann 2246ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2247ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2248ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2249ed616689SSucheta Chakraborty ivf.min_tx_rate, 2250c02db8c6SChris Wright ivt->rate); 22514f7d2cdfSDaniel Borkmann if (err < 0) 22524f7d2cdfSDaniel Borkmann return err; 2253c02db8c6SChris Wright } 22544f7d2cdfSDaniel Borkmann 22554f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 22564f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 22574f7d2cdfSDaniel Borkmann 2258ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2259ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2260ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2261ed616689SSucheta Chakraborty ivt->min_tx_rate, 2262ed616689SSucheta Chakraborty ivt->max_tx_rate); 22634f7d2cdfSDaniel Borkmann if (err < 0) 22644f7d2cdfSDaniel Borkmann return err; 2265ed616689SSucheta Chakraborty } 22664f7d2cdfSDaniel Borkmann 22674f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 22684f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 22694f7d2cdfSDaniel Borkmann 22705f8444a3SGreg Rose err = -EOPNOTSUPP; 22715f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 22725f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 22735f8444a3SGreg Rose ivs->setting); 22744f7d2cdfSDaniel Borkmann if (err < 0) 22754f7d2cdfSDaniel Borkmann return err; 22765f8444a3SGreg Rose } 22774f7d2cdfSDaniel Borkmann 22784f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 22794f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 22804f7d2cdfSDaniel Borkmann 22811d8faf48SRony Efraim err = -EOPNOTSUPP; 22821d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 22831d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 22841d8faf48SRony Efraim ivl->link_state); 22854f7d2cdfSDaniel Borkmann if (err < 0) 22864f7d2cdfSDaniel Borkmann return err; 22871d8faf48SRony Efraim } 22884f7d2cdfSDaniel Borkmann 22894f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 229001a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 229101a3d796SVlad Zolotarov 229201a3d796SVlad Zolotarov err = -EOPNOTSUPP; 22934f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 229401a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 22954f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 229601a3d796SVlad Zolotarov ivrssq_en->setting); 22974f7d2cdfSDaniel Borkmann if (err < 0) 22984f7d2cdfSDaniel Borkmann return err; 229901a3d796SVlad Zolotarov } 23004f7d2cdfSDaniel Borkmann 2301dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2302dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2303dd461d6aSHiroshi Shimamoto 2304dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2305dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2306dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2307dd461d6aSHiroshi Shimamoto if (err < 0) 2308dd461d6aSHiroshi Shimamoto return err; 2309dd461d6aSHiroshi Shimamoto } 2310dd461d6aSHiroshi Shimamoto 2311cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2312cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2313cc8e27ccSEli Cohen 2314cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2315cc8e27ccSEli Cohen return -EOPNOTSUPP; 2316cc8e27ccSEli Cohen 2317cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2318cc8e27ccSEli Cohen } 2319cc8e27ccSEli Cohen 2320cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2321cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2322cc8e27ccSEli Cohen 2323cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2324cc8e27ccSEli Cohen return -EOPNOTSUPP; 2325cc8e27ccSEli Cohen 2326cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2327cc8e27ccSEli Cohen } 2328cc8e27ccSEli Cohen 2329c02db8c6SChris Wright return err; 2330c02db8c6SChris Wright } 2331c02db8c6SChris Wright 233233eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 233333eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2334fbaec0eaSJiri Pirko { 2335898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2336fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2337fbaec0eaSJiri Pirko int err; 2338fbaec0eaSJiri Pirko 2339898e5061SJiri Pirko if (upper_dev) { 2340898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2341fbaec0eaSJiri Pirko return 0; 2342898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2343fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2344898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2345fbaec0eaSJiri Pirko if (err) 2346fbaec0eaSJiri Pirko return err; 2347fbaec0eaSJiri Pirko } else { 2348fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2349fbaec0eaSJiri Pirko } 2350fbaec0eaSJiri Pirko } 2351fbaec0eaSJiri Pirko 2352fbaec0eaSJiri Pirko if (ifindex) { 2353898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2354898e5061SJiri Pirko if (!upper_dev) 2355fbaec0eaSJiri Pirko return -EINVAL; 2356898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2357fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 235833eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2359fbaec0eaSJiri Pirko if (err) 2360fbaec0eaSJiri Pirko return err; 2361fbaec0eaSJiri Pirko } else { 2362fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2363fbaec0eaSJiri Pirko } 2364fbaec0eaSJiri Pirko } 2365fbaec0eaSJiri Pirko return 0; 2366fbaec0eaSJiri Pirko } 2367fbaec0eaSJiri Pirko 236890c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2369ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2370ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 237190f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 237290f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2373ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 237490c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 23750157f60cSPatrick McHardy { 2376d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 23770157f60cSPatrick McHardy int err; 23780157f60cSPatrick McHardy 2379644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2380644c7eebSEric Dumazet if (err < 0) 2381644c7eebSEric Dumazet return err; 2382644c7eebSEric Dumazet 23837e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 23847c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 23857c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2386d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2387d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2388d8a5ec67SEric W. Biederman goto errout; 2389d8a5ec67SEric W. Biederman } 23907c4f63baSChristian Brauner 2391d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2392d8a5ec67SEric W. Biederman put_net(net); 2393d8a5ec67SEric W. Biederman if (err) 2394d8a5ec67SEric W. Biederman goto errout; 239590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2396d8a5ec67SEric W. Biederman } 2397d8a5ec67SEric W. Biederman 23980157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 23990157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 24000157f60cSPatrick McHardy struct ifmap k_map; 24010157f60cSPatrick McHardy 2402d314774cSStephen Hemminger if (!ops->ndo_set_config) { 24030157f60cSPatrick McHardy err = -EOPNOTSUPP; 24040157f60cSPatrick McHardy goto errout; 24050157f60cSPatrick McHardy } 24060157f60cSPatrick McHardy 24070157f60cSPatrick McHardy if (!netif_device_present(dev)) { 24080157f60cSPatrick McHardy err = -ENODEV; 24090157f60cSPatrick McHardy goto errout; 24100157f60cSPatrick McHardy } 24110157f60cSPatrick McHardy 24120157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 24130157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 24140157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 24150157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 24160157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 24170157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 24180157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 24190157f60cSPatrick McHardy 2420d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 24210157f60cSPatrick McHardy if (err < 0) 24220157f60cSPatrick McHardy goto errout; 24230157f60cSPatrick McHardy 2424ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24250157f60cSPatrick McHardy } 24260157f60cSPatrick McHardy 24270157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 24280157f60cSPatrick McHardy struct sockaddr *sa; 24290157f60cSPatrick McHardy int len; 24300157f60cSPatrick McHardy 2431153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2432153711f9SWANG Cong sizeof(*sa)); 24330157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 24340157f60cSPatrick McHardy if (!sa) { 24350157f60cSPatrick McHardy err = -ENOMEM; 24360157f60cSPatrick McHardy goto errout; 24370157f60cSPatrick McHardy } 24380157f60cSPatrick McHardy sa->sa_family = dev->type; 24390157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 24400157f60cSPatrick McHardy dev->addr_len); 24413a37a963SPetr Machata err = dev_set_mac_address(dev, sa, extack); 24420157f60cSPatrick McHardy kfree(sa); 24430157f60cSPatrick McHardy if (err) 24440157f60cSPatrick McHardy goto errout; 244590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24460157f60cSPatrick McHardy } 24470157f60cSPatrick McHardy 24480157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 24497a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 24500157f60cSPatrick McHardy if (err < 0) 24510157f60cSPatrick McHardy goto errout; 245290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24530157f60cSPatrick McHardy } 24540157f60cSPatrick McHardy 2455cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2456cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2457ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2458cbda10faSVlad Dogaru } 2459cbda10faSVlad Dogaru 24600157f60cSPatrick McHardy /* 24610157f60cSPatrick McHardy * Interface selected by interface index but interface 24620157f60cSPatrick McHardy * name provided implies that a name change has been 24630157f60cSPatrick McHardy * requested. 24640157f60cSPatrick McHardy */ 24650157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 24660157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 24670157f60cSPatrick McHardy if (err < 0) 24680157f60cSPatrick McHardy goto errout; 246990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24700157f60cSPatrick McHardy } 24710157f60cSPatrick McHardy 24720b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 24730b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 24740b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 24750b815a1aSStephen Hemminger if (err < 0) 24760b815a1aSStephen Hemminger goto errout; 2477ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24780b815a1aSStephen Hemminger } 24790b815a1aSStephen Hemminger 24800157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 24810157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2482e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 24830157f60cSPatrick McHardy } 24840157f60cSPatrick McHardy 24850157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2486567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2487567c5e13SPetr Machata extack); 24885f9021cfSJohannes Berg if (err < 0) 24895f9021cfSJohannes Berg goto errout; 24900157f60cSPatrick McHardy } 24910157f60cSPatrick McHardy 2492fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 249333eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2494fbaec0eaSJiri Pirko if (err) 2495fbaec0eaSJiri Pirko goto errout; 249690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2497fbaec0eaSJiri Pirko } 2498fbaec0eaSJiri Pirko 24999a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 25009a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 25019a57247fSJiri Pirko if (err) 25029a57247fSJiri Pirko goto errout; 250390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25049a57247fSJiri Pirko } 25059a57247fSJiri Pirko 25065d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 25070cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 25085d1180fcSNicolas Dichtel 25096a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 25106a643ddbSCong Wang if (err) 251108294a26SJason Wang goto errout; 25122d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 251308294a26SJason Wang } 25140157f60cSPatrick McHardy 251546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 251646e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 251746e6b992SStephen Hemminger 251846e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 251946e6b992SStephen Hemminger err = -EINVAL; 252046e6b992SStephen Hemminger goto errout; 252146e6b992SStephen Hemminger } 252246e6b992SStephen Hemminger 252346e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 252446e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 252546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 252646e6b992SStephen Hemminger } 252746e6b992SStephen Hemminger } 252846e6b992SStephen Hemminger 252946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 253046e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 253146e6b992SStephen Hemminger 253246e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 253346e6b992SStephen Hemminger err = -EINVAL; 253446e6b992SStephen Hemminger goto errout; 253546e6b992SStephen Hemminger } 253646e6b992SStephen Hemminger 253746e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 253846e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 253946e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 254046e6b992SStephen Hemminger } 254146e6b992SStephen Hemminger } 254246e6b992SStephen Hemminger 25430157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 254493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 25450157f60cSPatrick McHardy 25460157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 25471889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 25481889b0e7SNicolas Dichtel 25490157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 25501889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2551ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 25521889b0e7SNicolas Dichtel dev->link_mode = value; 255393b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 25540157f60cSPatrick McHardy } 25550157f60cSPatrick McHardy 2556c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 25574f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2558c02db8c6SChris Wright struct nlattr *attr; 2559c02db8c6SChris Wright int rem; 25604f7d2cdfSDaniel Borkmann 2561c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 25624f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 25634f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2564253683bbSDavid Howells err = -EINVAL; 2565c02db8c6SChris Wright goto errout; 2566253683bbSDavid Howells } 25674f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2568fceb6435SJohannes Berg ifla_vf_policy, NULL); 25694f7d2cdfSDaniel Borkmann if (err < 0) 25704f7d2cdfSDaniel Borkmann goto errout; 25714f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2572ebc08a6fSWilliams, Mitch A if (err < 0) 2573ebc08a6fSWilliams, Mitch A goto errout; 2574ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2575ebc08a6fSWilliams, Mitch A } 2576ebc08a6fSWilliams, Mitch A } 25770157f60cSPatrick McHardy err = 0; 25780157f60cSPatrick McHardy 257957b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 258057b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 258157b61080SScott Feldman struct nlattr *attr; 258257b61080SScott Feldman int vf; 258357b61080SScott Feldman int rem; 258457b61080SScott Feldman 258557b61080SScott Feldman err = -EOPNOTSUPP; 258657b61080SScott Feldman if (!ops->ndo_set_vf_port) 258757b61080SScott Feldman goto errout; 258857b61080SScott Feldman 258957b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2590035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2591035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2592035d210fSDaniel Borkmann err = -EINVAL; 2593035d210fSDaniel Borkmann goto errout; 2594035d210fSDaniel Borkmann } 2595035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2596fceb6435SJohannes Berg ifla_port_policy, NULL); 259757b61080SScott Feldman if (err < 0) 259857b61080SScott Feldman goto errout; 259957b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 260057b61080SScott Feldman err = -EOPNOTSUPP; 260157b61080SScott Feldman goto errout; 260257b61080SScott Feldman } 260357b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 260457b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 260557b61080SScott Feldman if (err < 0) 260657b61080SScott Feldman goto errout; 2607ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 260857b61080SScott Feldman } 260957b61080SScott Feldman } 261057b61080SScott Feldman err = 0; 261157b61080SScott Feldman 261257b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 261357b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 261457b61080SScott Feldman 261557b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2616fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2617fceb6435SJohannes Berg NULL); 261857b61080SScott Feldman if (err < 0) 261957b61080SScott Feldman goto errout; 262057b61080SScott Feldman 262157b61080SScott Feldman err = -EOPNOTSUPP; 262257b61080SScott Feldman if (ops->ndo_set_vf_port) 262357b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 262457b61080SScott Feldman if (err < 0) 262557b61080SScott Feldman goto errout; 2626ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 262757b61080SScott Feldman } 2628f8ff182cSThomas Graf 2629f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2630f8ff182cSThomas Graf struct nlattr *af; 2631f8ff182cSThomas Graf int rem; 2632f8ff182cSThomas Graf 2633f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2634f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2635f8ff182cSThomas Graf 26365fa85a09SFlorian Westphal rcu_read_lock(); 26375fa85a09SFlorian Westphal 2638058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2639f8ff182cSThomas Graf 2640cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 26415fa85a09SFlorian Westphal if (err < 0) { 26425fa85a09SFlorian Westphal rcu_read_unlock(); 2643f8ff182cSThomas Graf goto errout; 26445fa85a09SFlorian Westphal } 2645f8ff182cSThomas Graf 26465fa85a09SFlorian Westphal rcu_read_unlock(); 2647ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2648f8ff182cSThomas Graf } 2649f8ff182cSThomas Graf } 265057b61080SScott Feldman err = 0; 265157b61080SScott Feldman 265288d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 265388d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 265488d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 265588d6378bSAnuradha Karuppiah if (err) 265688d6378bSAnuradha Karuppiah goto errout; 265788d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 265888d6378bSAnuradha Karuppiah } 265988d6378bSAnuradha Karuppiah 2660d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2661d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 266285de8576SDaniel Borkmann u32 xdp_flags = 0; 2663d1fdd913SBrenden Blanco 2664d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2665fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2666d1fdd913SBrenden Blanco if (err < 0) 2667d1fdd913SBrenden Blanco goto errout; 2668d1fdd913SBrenden Blanco 266958038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2670262d8625SBrenden Blanco err = -EINVAL; 2671262d8625SBrenden Blanco goto errout; 2672262d8625SBrenden Blanco } 267385de8576SDaniel Borkmann 267485de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 267585de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 267685de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 267785de8576SDaniel Borkmann err = -EINVAL; 267885de8576SDaniel Borkmann goto errout; 267985de8576SDaniel Borkmann } 2680ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 26810489df9aSDaniel Borkmann err = -EINVAL; 26820489df9aSDaniel Borkmann goto errout; 26830489df9aSDaniel Borkmann } 268485de8576SDaniel Borkmann } 268585de8576SDaniel Borkmann 2686d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2687ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 268885de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 268985de8576SDaniel Borkmann xdp_flags); 2690d1fdd913SBrenden Blanco if (err) 2691d1fdd913SBrenden Blanco goto errout; 2692d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2693d1fdd913SBrenden Blanco } 2694d1fdd913SBrenden Blanco } 2695d1fdd913SBrenden Blanco 26960157f60cSPatrick McHardy errout: 2697ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 269864ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2699ba998906SNicolas Dichtel netdev_state_change(dev); 2700ba998906SNicolas Dichtel 2701ba998906SNicolas Dichtel if (err < 0) 2702e87cc472SJoe 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", 2703e87cc472SJoe Perches dev->name); 2704ba998906SNicolas Dichtel } 27050157f60cSPatrick McHardy 27060157f60cSPatrick McHardy return err; 27070157f60cSPatrick McHardy } 27080157f60cSPatrick McHardy 2709c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2710c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2711da5e0494SThomas Graf { 27123b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2713da5e0494SThomas Graf struct ifinfomsg *ifm; 2714da5e0494SThomas Graf struct net_device *dev; 27150157f60cSPatrick McHardy int err; 2716da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 27171da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 27181da177e4SLinus Torvalds 2719c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2720c21ef3e3SDavid Ahern extack); 2721da5e0494SThomas Graf if (err < 0) 2722da5e0494SThomas Graf goto errout; 27231da177e4SLinus Torvalds 27244ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 27254ff66caeSChristian Brauner if (err < 0) 27264ff66caeSChristian Brauner goto errout; 27274ff66caeSChristian Brauner 27285176f91eSThomas Graf if (tb[IFLA_IFNAME]) 27295176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 273078e5b891SPatrick McHardy else 273178e5b891SPatrick McHardy ifname[0] = '\0'; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds err = -EINVAL; 2734da5e0494SThomas Graf ifm = nlmsg_data(nlh); 273551055be8SPatrick McHardy if (ifm->ifi_index > 0) 2736a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2737da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2738a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2739da5e0494SThomas Graf else 2740da5e0494SThomas Graf goto errout; 27411da177e4SLinus Torvalds 2742da5e0494SThomas Graf if (dev == NULL) { 2743da5e0494SThomas Graf err = -ENODEV; 2744da5e0494SThomas Graf goto errout; 2745da5e0494SThomas Graf } 27461da177e4SLinus Torvalds 2747ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2748da5e0494SThomas Graf errout: 27491da177e4SLinus Torvalds return err; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds 275266400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 275366400d54SWANG Cong { 275466400d54SWANG Cong struct net_device *dev, *aux; 275566400d54SWANG Cong LIST_HEAD(list_kill); 275666400d54SWANG Cong bool found = false; 275766400d54SWANG Cong 275866400d54SWANG Cong if (!group) 275966400d54SWANG Cong return -EPERM; 276066400d54SWANG Cong 276166400d54SWANG Cong for_each_netdev(net, dev) { 276266400d54SWANG Cong if (dev->group == group) { 276366400d54SWANG Cong const struct rtnl_link_ops *ops; 276466400d54SWANG Cong 276566400d54SWANG Cong found = true; 276666400d54SWANG Cong ops = dev->rtnl_link_ops; 276766400d54SWANG Cong if (!ops || !ops->dellink) 276866400d54SWANG Cong return -EOPNOTSUPP; 276966400d54SWANG Cong } 277066400d54SWANG Cong } 277166400d54SWANG Cong 277266400d54SWANG Cong if (!found) 277366400d54SWANG Cong return -ENODEV; 277466400d54SWANG Cong 277566400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 277666400d54SWANG Cong if (dev->group == group) { 277766400d54SWANG Cong const struct rtnl_link_ops *ops; 277866400d54SWANG Cong 277966400d54SWANG Cong ops = dev->rtnl_link_ops; 278066400d54SWANG Cong ops->dellink(dev, &list_kill); 278166400d54SWANG Cong } 278266400d54SWANG Cong } 278366400d54SWANG Cong unregister_netdevice_many(&list_kill); 278466400d54SWANG Cong 278566400d54SWANG Cong return 0; 278666400d54SWANG Cong } 278766400d54SWANG Cong 2788614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2789614732eaSThomas Graf { 2790614732eaSThomas Graf const struct rtnl_link_ops *ops; 2791614732eaSThomas Graf LIST_HEAD(list_kill); 2792614732eaSThomas Graf 2793614732eaSThomas Graf ops = dev->rtnl_link_ops; 2794614732eaSThomas Graf if (!ops || !ops->dellink) 2795614732eaSThomas Graf return -EOPNOTSUPP; 2796614732eaSThomas Graf 2797614732eaSThomas Graf ops->dellink(dev, &list_kill); 2798614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2799614732eaSThomas Graf 2800614732eaSThomas Graf return 0; 2801614732eaSThomas Graf } 2802614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2803614732eaSThomas Graf 2804c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2805c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 280638f7b870SPatrick McHardy { 28073b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2808b61ad68aSChristian Brauner struct net *tgt_net = net; 2809b61ad68aSChristian Brauner struct net_device *dev = NULL; 281038f7b870SPatrick McHardy struct ifinfomsg *ifm; 281138f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 281238f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 281338f7b870SPatrick McHardy int err; 2814b61ad68aSChristian Brauner int netnsid = -1; 281538f7b870SPatrick McHardy 2816c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 281738f7b870SPatrick McHardy if (err < 0) 281838f7b870SPatrick McHardy return err; 281938f7b870SPatrick McHardy 28204ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 28214ff66caeSChristian Brauner if (err < 0) 28224ff66caeSChristian Brauner return err; 28234ff66caeSChristian Brauner 282438f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 282538f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 282638f7b870SPatrick McHardy 28277e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 28287e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 2829c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 2830b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2831b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2832b61ad68aSChristian Brauner } 2833b61ad68aSChristian Brauner 2834b61ad68aSChristian Brauner err = -EINVAL; 283538f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 283638f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2837b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 283838f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2839b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 284066400d54SWANG Cong else if (tb[IFLA_GROUP]) 2841b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 284238f7b870SPatrick McHardy else 2843b61ad68aSChristian Brauner goto out; 284438f7b870SPatrick McHardy 2845b61ad68aSChristian Brauner if (!dev) { 2846b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2847b61ad68aSChristian Brauner err = -ENODEV; 284838f7b870SPatrick McHardy 2849b61ad68aSChristian Brauner goto out; 2850b61ad68aSChristian Brauner } 2851b61ad68aSChristian Brauner 2852b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2853b61ad68aSChristian Brauner 2854b61ad68aSChristian Brauner out: 2855b61ad68aSChristian Brauner if (netnsid >= 0) 2856b61ad68aSChristian Brauner put_net(tgt_net); 2857b61ad68aSChristian Brauner 2858b61ad68aSChristian Brauner return err; 285938f7b870SPatrick McHardy } 286038f7b870SPatrick McHardy 28613729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 28623729d502SPatrick McHardy { 28633729d502SPatrick McHardy unsigned int old_flags; 28643729d502SPatrick McHardy int err; 28653729d502SPatrick McHardy 28663729d502SPatrick McHardy old_flags = dev->flags; 28673729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 28686d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 28696d040321SPetr Machata NULL); 28703729d502SPatrick McHardy if (err < 0) 28713729d502SPatrick McHardy return err; 28723729d502SPatrick McHardy } 28733729d502SPatrick McHardy 28748d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 287556a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 28768d356b89SRoopa Prabhu } else { 28773729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2878a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 28798d356b89SRoopa Prabhu } 28803729d502SPatrick McHardy return 0; 28813729d502SPatrick McHardy } 28823729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 28833729d502SPatrick McHardy 2884d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 2885d0522f1cSDavid Ahern unsigned char name_assign_type, 2886d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 2887d0522f1cSDavid Ahern struct nlattr *tb[], 2888d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 2889e7199288SPavel Emelianov { 2890e7199288SPavel Emelianov struct net_device *dev; 2891d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2892d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2893e7199288SPavel Emelianov 289476ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 289576ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 289676ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2897d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 289876ff5cc9SJiri Pirko 289976ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 290076ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 290176ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2902d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2903efacb309Sstephen hemminger 2904d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 2905d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 29060e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2907d0522f1cSDavid Ahern } 29080e1d6ecaSEric Dumazet 2909d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 2910d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 29110e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2912d0522f1cSDavid Ahern } 29130e1d6ecaSEric Dumazet 29145517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2915c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2916e7199288SPavel Emelianov if (!dev) 2917d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2918e7199288SPavel Emelianov 291981adee47SEric W. Biederman dev_net_set(dev, net); 292081adee47SEric W. Biederman dev->rtnl_link_ops = ops; 29213729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 292281adee47SEric W. Biederman 2923e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2924e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 29252afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2926e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2927e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 29282afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 29292afb9b53SJiri Pirko } 2930e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2931e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2932e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2933e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2934e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2935e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 293693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2937e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2938e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2939ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2940ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 294146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 294246e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 294346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2944a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2945e7199288SPavel Emelianov 2946e7199288SPavel Emelianov return dev; 2947e7199288SPavel Emelianov } 2948e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2949e7199288SPavel Emelianov 295090f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 295190f62cf3SEric W. Biederman struct net *net, int group, 2952e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2953ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2954e7ed828fSVlad Dogaru struct nlattr **tb) 2955e7ed828fSVlad Dogaru { 2956d079535dSWANG Cong struct net_device *dev, *aux; 2957e7ed828fSVlad Dogaru int err; 2958e7ed828fSVlad Dogaru 2959d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2960e7ed828fSVlad Dogaru if (dev->group == group) { 2961ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2962e7ed828fSVlad Dogaru if (err < 0) 2963e7ed828fSVlad Dogaru return err; 2964e7ed828fSVlad Dogaru } 2965e7ed828fSVlad Dogaru } 2966e7ed828fSVlad Dogaru 2967e7ed828fSVlad Dogaru return 0; 2968e7ed828fSVlad Dogaru } 2969e7ed828fSVlad Dogaru 2970a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2971a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 297238f7b870SPatrick McHardy { 2973420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2974420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 2975420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 2976420d0318SJakub Kicinski const struct rtnl_link_ops *m_ops = NULL; 2977420d0318SJakub Kicinski struct net_device *master_dev = NULL; 29783b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 297938f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 298038f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 2981420d0318SJakub Kicinski struct net *dest_net, *link_net; 2982420d0318SJakub Kicinski struct nlattr **slave_data; 2983420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 2984420d0318SJakub Kicinski struct net_device *dev; 2985420d0318SJakub Kicinski struct ifinfomsg *ifm; 2986420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 2987420d0318SJakub Kicinski struct nlattr **data; 298838f7b870SPatrick McHardy int err; 298938f7b870SPatrick McHardy 299095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 299138f7b870SPatrick McHardy replay: 29928072f085SThomas Graf #endif 2993c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 299438f7b870SPatrick McHardy if (err < 0) 299538f7b870SPatrick McHardy return err; 299638f7b870SPatrick McHardy 29974ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 29984ff66caeSChristian Brauner if (err < 0) 29994ff66caeSChristian Brauner return err; 30004ff66caeSChristian Brauner 300138f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 300238f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 300338f7b870SPatrick McHardy else 300438f7b870SPatrick McHardy ifname[0] = '\0'; 300538f7b870SPatrick McHardy 300638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 300738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3008881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3009e7ed828fSVlad Dogaru else { 3010e7ed828fSVlad Dogaru if (ifname[0]) 3011881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 301238f7b870SPatrick McHardy else 301338f7b870SPatrick McHardy dev = NULL; 3014e7ed828fSVlad Dogaru } 301538f7b870SPatrick McHardy 3016ba7d49b1SJiri Pirko if (dev) { 3017ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3018ba7d49b1SJiri Pirko if (master_dev) 3019ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3020ba7d49b1SJiri Pirko } 3021ba7d49b1SJiri Pirko 3022e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 3023e0d087afSEric Dumazet if (err < 0) 30241840bb13SThomas Graf return err; 30251840bb13SThomas Graf 302638f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 302738f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 3028fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 3029fceb6435SJohannes Berg NULL); 303038f7b870SPatrick McHardy if (err < 0) 303138f7b870SPatrick McHardy return err; 303238f7b870SPatrick McHardy } else 303338f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 303438f7b870SPatrick McHardy 303538f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 303638f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 303738f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 303838f7b870SPatrick McHardy } else { 303938f7b870SPatrick McHardy kind[0] = '\0'; 304038f7b870SPatrick McHardy ops = NULL; 304138f7b870SPatrick McHardy } 304238f7b870SPatrick McHardy 3043420d0318SJakub Kicinski data = NULL; 304438f7b870SPatrick McHardy if (ops) { 3045ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3046ccf8dbcdSKees Cook return -EINVAL; 3047ccf8dbcdSKees Cook 304838f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 304938f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 305038f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 305168d57f3bSDavid Ahern ops->policy, extack); 305238f7b870SPatrick McHardy if (err < 0) 305338f7b870SPatrick McHardy return err; 305438f7b870SPatrick McHardy data = attr; 305538f7b870SPatrick McHardy } 305638f7b870SPatrick McHardy if (ops->validate) { 3057a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 305838f7b870SPatrick McHardy if (err < 0) 305938f7b870SPatrick McHardy return err; 306038f7b870SPatrick McHardy } 306138f7b870SPatrick McHardy } 306238f7b870SPatrick McHardy 3063420d0318SJakub Kicinski slave_data = NULL; 3064ba7d49b1SJiri Pirko if (m_ops) { 3065af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3066ccf8dbcdSKees Cook return -EINVAL; 3067ccf8dbcdSKees Cook 3068ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3069ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3070420d0318SJakub Kicinski err = nla_parse_nested(slave_attr, m_ops->slave_maxtype, 3071ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 3072420d0318SJakub Kicinski m_ops->slave_policy, extack); 3073ba7d49b1SJiri Pirko if (err < 0) 3074ba7d49b1SJiri Pirko return err; 3075ba7d49b1SJiri Pirko slave_data = slave_attr; 3076ba7d49b1SJiri Pirko } 3077ba7d49b1SJiri Pirko } 3078ba7d49b1SJiri Pirko 307938f7b870SPatrick McHardy if (dev) { 308090c325e3SNicolas Dichtel int status = 0; 308138f7b870SPatrick McHardy 308238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 308338f7b870SPatrick McHardy return -EEXIST; 308438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 308538f7b870SPatrick McHardy return -EOPNOTSUPP; 308638f7b870SPatrick McHardy 308738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 308838f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 308938f7b870SPatrick McHardy !ops->changelink) 309038f7b870SPatrick McHardy return -EOPNOTSUPP; 309138f7b870SPatrick McHardy 3092ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 309338f7b870SPatrick McHardy if (err < 0) 309438f7b870SPatrick McHardy return err; 3095ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 309638f7b870SPatrick McHardy } 309738f7b870SPatrick McHardy 3098ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3099ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3100ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3101ba7d49b1SJiri Pirko 3102420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3103420d0318SJakub Kicinski slave_data, extack); 3104ba7d49b1SJiri Pirko if (err < 0) 3105ba7d49b1SJiri Pirko return err; 3106ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3107ba7d49b1SJiri Pirko } 3108ba7d49b1SJiri Pirko 3109420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 311038f7b870SPatrick McHardy } 311138f7b870SPatrick McHardy 3112ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3113ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 311490f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3115ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3116ddf9f970SJakub Kicinski ifm, extack, tb); 311738f7b870SPatrick McHardy return -ENODEV; 3118ffa934f1SPatrick McHardy } 311938f7b870SPatrick McHardy 3120160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 312138f7b870SPatrick McHardy return -EOPNOTSUPP; 312238f7b870SPatrick McHardy 312338f7b870SPatrick McHardy if (!ops) { 312495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 312538f7b870SPatrick McHardy if (kind[0]) { 312638f7b870SPatrick McHardy __rtnl_unlock(); 312738f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 312838f7b870SPatrick McHardy rtnl_lock(); 312938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 313038f7b870SPatrick McHardy if (ops) 313138f7b870SPatrick McHardy goto replay; 313238f7b870SPatrick McHardy } 313338f7b870SPatrick McHardy #endif 313468d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 313538f7b870SPatrick McHardy return -EOPNOTSUPP; 313638f7b870SPatrick McHardy } 313738f7b870SPatrick McHardy 3138b0ab2fabSJiri Pirko if (!ops->setup) 3139b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3140b0ab2fabSJiri Pirko 31415517750fSTom Gundersen if (!ifname[0]) { 314238f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 31435517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 31445517750fSTom Gundersen } 314538f7b870SPatrick McHardy 31465bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 314713ad1774SEric W. Biederman if (IS_ERR(dest_net)) 314813ad1774SEric W. Biederman return PTR_ERR(dest_net); 314913ad1774SEric W. Biederman 3150317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3151317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3152317f4810SNicolas Dichtel 3153317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3154317f4810SNicolas Dichtel if (!link_net) { 315568d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3156317f4810SNicolas Dichtel err = -EINVAL; 3157317f4810SNicolas Dichtel goto out; 3158317f4810SNicolas Dichtel } 315906615bedSEric W. Biederman err = -EPERM; 316006615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 316106615bedSEric W. Biederman goto out; 3162420d0318SJakub Kicinski } else { 3163420d0318SJakub Kicinski link_net = NULL; 3164317f4810SNicolas Dichtel } 3165317f4810SNicolas Dichtel 3166317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3167d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 31689c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3169e7199288SPavel Emelianov err = PTR_ERR(dev); 31709c7dafbfSPavel Emelyanov goto out; 31719c7dafbfSPavel Emelyanov } 31729c7dafbfSPavel Emelyanov 31739c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 31749c7dafbfSPavel Emelyanov 31750e0eee24SCong Wang if (ops->newlink) { 3176420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 31770e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3178e51fb152SCong Wang * and unregister it on failure after registration 3179e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 31800e0eee24SCong Wang */ 3181e51fb152SCong Wang if (err < 0) { 3182e51fb152SCong Wang /* If device is not registered at all, free it now */ 3183e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3184e51fb152SCong Wang free_netdev(dev); 31850e0eee24SCong Wang goto out; 3186e51fb152SCong Wang } 31870e0eee24SCong Wang } else { 31882d85cba2SPatrick McHardy err = register_netdevice(dev); 3189fce9b9beSDan Carpenter if (err < 0) { 319038f7b870SPatrick McHardy free_netdev(dev); 31913729d502SPatrick McHardy goto out; 3192fce9b9beSDan Carpenter } 31930e0eee24SCong Wang } 31943729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 319543638900SDavid S. Miller if (err < 0) 319643638900SDavid S. Miller goto out_unregister; 319743638900SDavid S. Miller if (link_net) { 319843638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 319943638900SDavid S. Miller if (err < 0) 320043638900SDavid S. Miller goto out_unregister; 320143638900SDavid S. Miller } 3202160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3203420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3204160ca014STheuns Verwoerd if (err) 3205160ca014STheuns Verwoerd goto out_unregister; 3206160ca014STheuns Verwoerd } 320743638900SDavid S. Miller out: 320843638900SDavid S. Miller if (link_net) 320943638900SDavid S. Miller put_net(link_net); 321043638900SDavid S. Miller put_net(dest_net); 321143638900SDavid S. Miller return err; 321243638900SDavid S. Miller out_unregister: 32137afb8886SWANG Cong if (ops->newlink) { 32147afb8886SWANG Cong LIST_HEAD(list_kill); 32157afb8886SWANG Cong 32167afb8886SWANG Cong ops->dellink(dev, &list_kill); 32177afb8886SWANG Cong unregister_netdevice_many(&list_kill); 32187afb8886SWANG Cong } else { 32193729d502SPatrick McHardy unregister_netdevice(dev); 32207afb8886SWANG Cong } 3221317f4810SNicolas Dichtel goto out; 3222317f4810SNicolas Dichtel } 322338f7b870SPatrick McHardy 3224a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3225a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3226a2939745SJakub Kicinski { 3227a2939745SJakub Kicinski struct nlattr **attr; 3228a2939745SJakub Kicinski int ret; 3229a2939745SJakub Kicinski 3230a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3231a2939745SJakub Kicinski if (!attr) 3232a2939745SJakub Kicinski return -ENOMEM; 3233a2939745SJakub Kicinski 3234a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3235a2939745SJakub Kicinski kfree(attr); 3236a2939745SJakub Kicinski return ret; 3237a2939745SJakub Kicinski } 3238a2939745SJakub Kicinski 32399b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 32409b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 32419b3757b0SJakub Kicinski struct nlattr **tb, 32429b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 32439b3757b0SJakub Kicinski { 32449b3757b0SJakub Kicinski struct ifinfomsg *ifm; 32459b3757b0SJakub Kicinski int i, err; 32469b3757b0SJakub Kicinski 32479b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 32489b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 32499b3757b0SJakub Kicinski return -EINVAL; 32509b3757b0SJakub Kicinski } 32519b3757b0SJakub Kicinski 32529b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 32539b3757b0SJakub Kicinski return nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 32549b3757b0SJakub Kicinski extack); 32559b3757b0SJakub Kicinski 32569b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 32579b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 32589b3757b0SJakub Kicinski ifm->ifi_change) { 32599b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 32609b3757b0SJakub Kicinski return -EINVAL; 32619b3757b0SJakub Kicinski } 32629b3757b0SJakub Kicinski 32639b3757b0SJakub Kicinski err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 32649b3757b0SJakub Kicinski extack); 32659b3757b0SJakub Kicinski if (err) 32669b3757b0SJakub Kicinski return err; 32679b3757b0SJakub Kicinski 32689b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 32699b3757b0SJakub Kicinski if (!tb[i]) 32709b3757b0SJakub Kicinski continue; 32719b3757b0SJakub Kicinski 32729b3757b0SJakub Kicinski switch (i) { 32739b3757b0SJakub Kicinski case IFLA_IFNAME: 32749b3757b0SJakub Kicinski case IFLA_EXT_MASK: 32759b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 32769b3757b0SJakub Kicinski break; 32779b3757b0SJakub Kicinski default: 32789b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 32799b3757b0SJakub Kicinski return -EINVAL; 32809b3757b0SJakub Kicinski } 32819b3757b0SJakub Kicinski } 32829b3757b0SJakub Kicinski 32839b3757b0SJakub Kicinski return 0; 32849b3757b0SJakub Kicinski } 32859b3757b0SJakub Kicinski 3286c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3287c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3288711e2c33SJean Tourrilhes { 32893b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 329079e1ad14SJiri Benc struct net *tgt_net = net; 3291b60c5115SThomas Graf struct ifinfomsg *ifm; 3292a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3293b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3294b60c5115SThomas Graf struct net_device *dev = NULL; 3295b60c5115SThomas Graf struct sk_buff *nskb; 329679e1ad14SJiri Benc int netnsid = -1; 3297339bf98fSThomas Graf int err; 3298115c9b81SGreg Rose u32 ext_filter_mask = 0; 3299711e2c33SJean Tourrilhes 33009b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3301b60c5115SThomas Graf if (err < 0) 33029918f230SEric Sesterhenn return err; 3303b60c5115SThomas Graf 33044ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 33054ff66caeSChristian Brauner if (err < 0) 33064ff66caeSChristian Brauner return err; 33074ff66caeSChristian Brauner 33087e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 33097e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3310c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 331179e1ad14SJiri Benc if (IS_ERR(tgt_net)) 331279e1ad14SJiri Benc return PTR_ERR(tgt_net); 331379e1ad14SJiri Benc } 331479e1ad14SJiri Benc 3315a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3316a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3317a3d12891SEric Dumazet 3318115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3319115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3320115c9b81SGreg Rose 332179e1ad14SJiri Benc err = -EINVAL; 3322b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3323a3d12891SEric Dumazet if (ifm->ifi_index > 0) 332479e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3325a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 332679e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3327a3d12891SEric Dumazet else 332879e1ad14SJiri Benc goto out; 3329b60c5115SThomas Graf 333079e1ad14SJiri Benc err = -ENODEV; 3331a3d12891SEric Dumazet if (dev == NULL) 333279e1ad14SJiri Benc goto out; 3333a3d12891SEric Dumazet 333479e1ad14SJiri Benc err = -ENOBUFS; 3335115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3336a3d12891SEric Dumazet if (nskb == NULL) 333779e1ad14SJiri Benc goto out; 3338711e2c33SJean Tourrilhes 333979e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 334079e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 334179e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 334238e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 334326932566SPatrick McHardy if (err < 0) { 334426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 334526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 334626932566SPatrick McHardy kfree_skb(nskb); 3347a3d12891SEric Dumazet } else 334815e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 334979e1ad14SJiri Benc out: 335079e1ad14SJiri Benc if (netnsid >= 0) 335179e1ad14SJiri Benc put_net(tgt_net); 3352b60c5115SThomas Graf 3353711e2c33SJean Tourrilhes return err; 3354711e2c33SJean Tourrilhes } 3355711e2c33SJean Tourrilhes 3356115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3357c7ac8679SGreg Rose { 3358115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3359115c9b81SGreg Rose struct net_device *dev; 3360115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3361115c9b81SGreg Rose u32 ext_filter_mask = 0; 3362115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3363e5eca6d4SMichal Schmidt int hdrlen; 3364115c9b81SGreg Rose 3365e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3366e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3367e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3368e5eca6d4SMichal Schmidt 3369fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3370115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3371115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3372a4b64fbeSEric Dumazet } 3373115c9b81SGreg Rose 3374115c9b81SGreg Rose if (!ext_filter_mask) 3375115c9b81SGreg Rose return NLMSG_GOODSIZE; 3376115c9b81SGreg Rose /* 3377115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3378115c9b81SGreg Rose * buffer size based upon the filter mask. 3379115c9b81SGreg Rose */ 33806853dd48SFlorian Westphal rcu_read_lock(); 33816853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3382115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3383115c9b81SGreg Rose if_nlmsg_size(dev, 3384115c9b81SGreg Rose ext_filter_mask)); 3385115c9b81SGreg Rose } 33866853dd48SFlorian Westphal rcu_read_unlock(); 3387115c9b81SGreg Rose 338893af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3389c7ac8679SGreg Rose } 3390c7ac8679SGreg Rose 339142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 33921da177e4SLinus Torvalds { 33931da177e4SLinus Torvalds int idx; 33941da177e4SLinus Torvalds int s_idx = cb->family; 339587ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3396c63586dcSDavid Ahern int ret = 0; 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds if (s_idx == 0) 33991da177e4SLinus Torvalds s_idx = 1; 34006853dd48SFlorian Westphal 340125239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3402addf9b90SFlorian Westphal struct rtnl_link **tab; 3403addf9b90SFlorian Westphal struct rtnl_link *link; 34046853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 34056853dd48SFlorian Westphal 34061da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 34071da177e4SLinus Torvalds continue; 34086853dd48SFlorian Westphal 3409addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 34101da177e4SLinus Torvalds continue; 34116853dd48SFlorian Westphal 3412addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3413addf9b90SFlorian Westphal if (!tab) 3414addf9b90SFlorian Westphal continue; 3415addf9b90SFlorian Westphal 3416addf9b90SFlorian Westphal link = tab[type]; 3417addf9b90SFlorian Westphal if (!link) 3418addf9b90SFlorian Westphal continue; 3419addf9b90SFlorian Westphal 3420addf9b90SFlorian Westphal dumpit = link->dumpit; 34216853dd48SFlorian Westphal if (!dumpit) 34226853dd48SFlorian Westphal continue; 34236853dd48SFlorian Westphal 34240465277fSNicolas Dichtel if (idx > s_idx) { 34251da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 34260465277fSNicolas Dichtel cb->prev_seq = 0; 34270465277fSNicolas Dichtel cb->seq = 0; 34280465277fSNicolas Dichtel } 3429c63586dcSDavid Ahern ret = dumpit(skb, cb); 34305e1acb4aSAlexey Kodanev if (ret) 34311da177e4SLinus Torvalds break; 34321da177e4SLinus Torvalds } 34331da177e4SLinus Torvalds cb->family = idx; 34341da177e4SLinus Torvalds 3435c63586dcSDavid Ahern return skb->len ? : ret; 34361da177e4SLinus Torvalds } 34371da177e4SLinus Torvalds 3438395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 34393d3ea5afSVlad Yasevich unsigned int change, 344038e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 344138e01b30SNicolas Dichtel int new_ifindex) 34421da177e4SLinus Torvalds { 3443c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 34441da177e4SLinus Torvalds struct sk_buff *skb; 34450ec6d3f4SThomas Graf int err = -ENOBUFS; 3446c7ac8679SGreg Rose size_t if_info_size; 34471da177e4SLinus Torvalds 34487f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 34490ec6d3f4SThomas Graf if (skb == NULL) 34500ec6d3f4SThomas Graf goto errout; 34511da177e4SLinus Torvalds 345279e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 345379e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 345438e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 345526932566SPatrick McHardy if (err < 0) { 345626932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 345726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 345826932566SPatrick McHardy kfree_skb(skb); 345926932566SPatrick McHardy goto errout; 346026932566SPatrick McHardy } 3461395eea6cSMahesh Bandewar return skb; 34620ec6d3f4SThomas Graf errout: 34630ec6d3f4SThomas Graf if (err < 0) 34644b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3465395eea6cSMahesh Bandewar return NULL; 3466395eea6cSMahesh Bandewar } 3467395eea6cSMahesh Bandewar 3468395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3469395eea6cSMahesh Bandewar { 3470395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3471395eea6cSMahesh Bandewar 3472395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3473395eea6cSMahesh Bandewar } 3474395eea6cSMahesh Bandewar 34753d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 34763d3ea5afSVlad Yasevich unsigned int change, u32 event, 347738e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3478395eea6cSMahesh Bandewar { 3479395eea6cSMahesh Bandewar struct sk_buff *skb; 3480395eea6cSMahesh Bandewar 3481ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3482ed2a80abSNicolas Dichtel return; 3483ed2a80abSNicolas Dichtel 348438e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 348538e01b30SNicolas Dichtel new_ifindex); 3486395eea6cSMahesh Bandewar if (skb) 3487395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 34881da177e4SLinus Torvalds } 34893d3ea5afSVlad Yasevich 34903d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 34913d3ea5afSVlad Yasevich gfp_t flags) 34923d3ea5afSVlad Yasevich { 349338e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 349438e01b30SNicolas Dichtel NULL, 0); 34953d3ea5afSVlad Yasevich } 34961da177e4SLinus Torvalds 34976621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 349838e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 34996621dd29SNicolas Dichtel { 35006621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 350138e01b30SNicolas Dichtel new_nsid, new_ifindex); 35026621dd29SNicolas Dichtel } 35036621dd29SNicolas Dichtel 3504d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3505d83b0603SJohn Fastabend struct net_device *dev, 35061e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 35071c104a6bSNicolas Dichtel int type, unsigned int flags, 3508b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3509d83b0603SJohn Fastabend { 3510d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3511d83b0603SJohn Fastabend struct ndmsg *ndm; 3512d83b0603SJohn Fastabend 35131c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3514d83b0603SJohn Fastabend if (!nlh) 3515d83b0603SJohn Fastabend return -EMSGSIZE; 3516d83b0603SJohn Fastabend 3517d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3518d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3519d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3520d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3521d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3522d83b0603SJohn Fastabend ndm->ndm_type = 0; 3523d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3524b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3525d83b0603SJohn Fastabend 3526d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3527d83b0603SJohn Fastabend goto nla_put_failure; 35281e53d5bbSHubert Sokolowski if (vid) 35291e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 35301e53d5bbSHubert Sokolowski goto nla_put_failure; 3531d83b0603SJohn Fastabend 3532053c095aSJohannes Berg nlmsg_end(skb, nlh); 3533053c095aSJohannes Berg return 0; 3534d83b0603SJohn Fastabend 3535d83b0603SJohn Fastabend nla_put_failure: 3536d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3537d83b0603SJohn Fastabend return -EMSGSIZE; 3538d83b0603SJohn Fastabend } 3539d83b0603SJohn Fastabend 35403ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 35413ff661c3SJohn Fastabend { 3542f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3543f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3544f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3545f82ef3e1SSabrina Dubroca 0; 35463ff661c3SJohn Fastabend } 35473ff661c3SJohn Fastabend 3548b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3549b3379041SHubert Sokolowski u16 ndm_state) 35503ff661c3SJohn Fastabend { 35513ff661c3SJohn Fastabend struct net *net = dev_net(dev); 35523ff661c3SJohn Fastabend struct sk_buff *skb; 35533ff661c3SJohn Fastabend int err = -ENOBUFS; 35543ff661c3SJohn Fastabend 35553ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 35563ff661c3SJohn Fastabend if (!skb) 35573ff661c3SJohn Fastabend goto errout; 35583ff661c3SJohn Fastabend 35591e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3560b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 35613ff661c3SJohn Fastabend if (err < 0) { 35623ff661c3SJohn Fastabend kfree_skb(skb); 35633ff661c3SJohn Fastabend goto errout; 35643ff661c3SJohn Fastabend } 35653ff661c3SJohn Fastabend 35663ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 35673ff661c3SJohn Fastabend return; 35683ff661c3SJohn Fastabend errout: 35693ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 35703ff661c3SJohn Fastabend } 35713ff661c3SJohn Fastabend 3572090096bfSVlad Yasevich /** 3573090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3574090096bfSVlad Yasevich */ 3575090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3576090096bfSVlad Yasevich struct nlattr *tb[], 3577090096bfSVlad Yasevich struct net_device *dev, 3578f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3579090096bfSVlad Yasevich u16 flags) 3580090096bfSVlad Yasevich { 3581090096bfSVlad Yasevich int err = -EINVAL; 3582090096bfSVlad Yasevich 3583090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3584090096bfSVlad Yasevich * implement its own handler for this. 3585090096bfSVlad Yasevich */ 3586090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3587090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3588090096bfSVlad Yasevich return err; 3589090096bfSVlad Yasevich } 3590090096bfSVlad Yasevich 359165891feaSOr Gerlitz if (vid) { 359265891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 359365891feaSOr Gerlitz return err; 359465891feaSOr Gerlitz } 359565891feaSOr Gerlitz 3596090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3597090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3598090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3599090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3600090096bfSVlad Yasevich 3601090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3602090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3603090096bfSVlad Yasevich err = 0; 3604090096bfSVlad Yasevich 3605090096bfSVlad Yasevich return err; 3606090096bfSVlad Yasevich } 3607090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3608090096bfSVlad Yasevich 3609b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3610b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3611f6f6424bSJiri Pirko { 3612f6f6424bSJiri Pirko u16 vid = 0; 3613f6f6424bSJiri Pirko 3614f6f6424bSJiri Pirko if (vlan_attr) { 3615f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3616b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3617f6f6424bSJiri Pirko return -EINVAL; 3618f6f6424bSJiri Pirko } 3619f6f6424bSJiri Pirko 3620f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3621f6f6424bSJiri Pirko 3622f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3623b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3624f6f6424bSJiri Pirko return -EINVAL; 3625f6f6424bSJiri Pirko } 3626f6f6424bSJiri Pirko } 3627f6f6424bSJiri Pirko *p_vid = vid; 3628f6f6424bSJiri Pirko return 0; 3629f6f6424bSJiri Pirko } 3630f6f6424bSJiri Pirko 3631c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3632c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 363377162022SJohn Fastabend { 363477162022SJohn Fastabend struct net *net = sock_net(skb->sk); 363577162022SJohn Fastabend struct ndmsg *ndm; 363677162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 363777162022SJohn Fastabend struct net_device *dev; 363877162022SJohn Fastabend u8 *addr; 3639f6f6424bSJiri Pirko u16 vid; 364077162022SJohn Fastabend int err; 364177162022SJohn Fastabend 3642c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 364377162022SJohn Fastabend if (err < 0) 364477162022SJohn Fastabend return err; 364577162022SJohn Fastabend 364677162022SJohn Fastabend ndm = nlmsg_data(nlh); 364777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3648b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 364977162022SJohn Fastabend return -EINVAL; 365077162022SJohn Fastabend } 365177162022SJohn Fastabend 365277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 365377162022SJohn Fastabend if (dev == NULL) { 3654b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 365577162022SJohn Fastabend return -ENODEV; 365677162022SJohn Fastabend } 365777162022SJohn Fastabend 365877162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3659b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 366077162022SJohn Fastabend return -EINVAL; 366177162022SJohn Fastabend } 366277162022SJohn Fastabend 3663da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 3664da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 3665da715775SIdo Schimmel return -EINVAL; 3666da715775SIdo Schimmel } 3667da715775SIdo Schimmel 366877162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 366977162022SJohn Fastabend 3670b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3671f6f6424bSJiri Pirko if (err) 3672f6f6424bSJiri Pirko return err; 3673f6f6424bSJiri Pirko 367477162022SJohn Fastabend err = -EOPNOTSUPP; 367577162022SJohn Fastabend 367677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 367777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 367877162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3679898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3680898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3681898e5061SJiri Pirko 3682f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 368387b0984eSPetr Machata nlh->nlmsg_flags, extack); 368477162022SJohn Fastabend if (err) 368577162022SJohn Fastabend goto out; 368677162022SJohn Fastabend else 368777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 368877162022SJohn Fastabend } 368977162022SJohn Fastabend 369077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3691090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3692090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3693090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3694f6f6424bSJiri Pirko vid, 369587b0984eSPetr Machata nlh->nlmsg_flags, 369687b0984eSPetr Machata extack); 3697090096bfSVlad Yasevich else 3698f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 369977162022SJohn Fastabend nlh->nlmsg_flags); 370077162022SJohn Fastabend 37013ff661c3SJohn Fastabend if (!err) { 3702b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3703b3379041SHubert Sokolowski ndm->ndm_state); 370477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 370577162022SJohn Fastabend } 37063ff661c3SJohn Fastabend } 370777162022SJohn Fastabend out: 370877162022SJohn Fastabend return err; 370977162022SJohn Fastabend } 371077162022SJohn Fastabend 3711090096bfSVlad Yasevich /** 3712090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3713090096bfSVlad Yasevich */ 3714090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3715090096bfSVlad Yasevich struct nlattr *tb[], 3716090096bfSVlad Yasevich struct net_device *dev, 3717f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3718090096bfSVlad Yasevich { 3719c8a89c4aSAlexander Duyck int err = -EINVAL; 3720090096bfSVlad Yasevich 3721090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3722090096bfSVlad Yasevich * implement its own handler for this. 3723090096bfSVlad Yasevich */ 372464535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3725090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3726c8a89c4aSAlexander Duyck return err; 3727090096bfSVlad Yasevich } 3728090096bfSVlad Yasevich 3729090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3730090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3731090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3732090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3733090096bfSVlad Yasevich 3734090096bfSVlad Yasevich return err; 3735090096bfSVlad Yasevich } 3736090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3737090096bfSVlad Yasevich 3738c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3739c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 374077162022SJohn Fastabend { 374177162022SJohn Fastabend struct net *net = sock_net(skb->sk); 374277162022SJohn Fastabend struct ndmsg *ndm; 37431690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 374477162022SJohn Fastabend struct net_device *dev; 374577162022SJohn Fastabend int err = -EINVAL; 374677162022SJohn Fastabend __u8 *addr; 3747f6f6424bSJiri Pirko u16 vid; 374877162022SJohn Fastabend 374990f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 37501690be63SVlad Yasevich return -EPERM; 37511690be63SVlad Yasevich 3752c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 37531690be63SVlad Yasevich if (err < 0) 37541690be63SVlad Yasevich return err; 375577162022SJohn Fastabend 375677162022SJohn Fastabend ndm = nlmsg_data(nlh); 375777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3758b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 375977162022SJohn Fastabend return -EINVAL; 376077162022SJohn Fastabend } 376177162022SJohn Fastabend 376277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 376377162022SJohn Fastabend if (dev == NULL) { 3764b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 376577162022SJohn Fastabend return -ENODEV; 376677162022SJohn Fastabend } 376777162022SJohn Fastabend 37681690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3769b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 377077162022SJohn Fastabend return -EINVAL; 377177162022SJohn Fastabend } 377277162022SJohn Fastabend 3773da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 3774da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 3775da715775SIdo Schimmel return -EINVAL; 3776da715775SIdo Schimmel } 3777da715775SIdo Schimmel 37781690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 37791690be63SVlad Yasevich 3780b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3781f6f6424bSJiri Pirko if (err) 3782f6f6424bSJiri Pirko return err; 3783f6f6424bSJiri Pirko 378477162022SJohn Fastabend err = -EOPNOTSUPP; 378577162022SJohn Fastabend 378677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 378777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 378877162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3789898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3790898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 379177162022SJohn Fastabend 3792898e5061SJiri Pirko if (ops->ndo_fdb_del) 3793f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 379477162022SJohn Fastabend 379577162022SJohn Fastabend if (err) 379677162022SJohn Fastabend goto out; 379777162022SJohn Fastabend else 379877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 379977162022SJohn Fastabend } 380077162022SJohn Fastabend 380177162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3802090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3803090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3804f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3805f6f6424bSJiri Pirko vid); 3806090096bfSVlad Yasevich else 3807f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 380877162022SJohn Fastabend 38093ff661c3SJohn Fastabend if (!err) { 3810b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3811b3379041SHubert Sokolowski ndm->ndm_state); 381277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 381377162022SJohn Fastabend } 38143ff661c3SJohn Fastabend } 381577162022SJohn Fastabend out: 381677162022SJohn Fastabend return err; 381777162022SJohn Fastabend } 381877162022SJohn Fastabend 3819d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3820d83b0603SJohn Fastabend struct netlink_callback *cb, 3821d83b0603SJohn Fastabend struct net_device *dev, 3822d83b0603SJohn Fastabend int *idx, 3823d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3824d83b0603SJohn Fastabend { 3825d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3826d83b0603SJohn Fastabend int err; 382715e47304SEric W. Biederman u32 portid, seq; 3828d83b0603SJohn Fastabend 382915e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3830d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3831d83b0603SJohn Fastabend 3832d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3833d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3834d83b0603SJohn Fastabend goto skip; 3835d83b0603SJohn Fastabend 38361e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3837a7a558feSJohn Fastabend portid, seq, 38381c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3839b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3840d83b0603SJohn Fastabend if (err < 0) 3841d83b0603SJohn Fastabend return err; 3842d83b0603SJohn Fastabend skip: 3843d83b0603SJohn Fastabend *idx += 1; 3844d83b0603SJohn Fastabend } 3845d83b0603SJohn Fastabend return 0; 3846d83b0603SJohn Fastabend } 3847d83b0603SJohn Fastabend 3848d83b0603SJohn Fastabend /** 38492c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3850d83b0603SJohn Fastabend * @nlh: netlink message header 3851d83b0603SJohn Fastabend * @dev: netdevice 3852d83b0603SJohn Fastabend * 3853d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 385491f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3855d83b0603SJohn Fastabend */ 3856d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3857d83b0603SJohn Fastabend struct netlink_callback *cb, 3858d83b0603SJohn Fastabend struct net_device *dev, 38595d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3860d297653dSRoopa Prabhu int *idx) 3861d83b0603SJohn Fastabend { 3862d83b0603SJohn Fastabend int err; 3863d83b0603SJohn Fastabend 386468883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 386568883893SEric Dumazet return -EINVAL; 386668883893SEric Dumazet 3867d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3868d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3869d83b0603SJohn Fastabend if (err) 3870d83b0603SJohn Fastabend goto out; 38712934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3872d83b0603SJohn Fastabend out: 3873d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3874d297653dSRoopa Prabhu return err; 3875d83b0603SJohn Fastabend } 3876d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3877d83b0603SJohn Fastabend 38788c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 38798c6e137fSDavid Ahern int *br_idx, int *brport_idx, 38808c6e137fSDavid Ahern struct netlink_ext_ack *extack) 38818c6e137fSDavid Ahern { 38828c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 38838c6e137fSDavid Ahern struct ndmsg *ndm; 38848c6e137fSDavid Ahern int err, i; 38858c6e137fSDavid Ahern 38868c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 38878c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 38888c6e137fSDavid Ahern return -EINVAL; 38898c6e137fSDavid Ahern } 38908c6e137fSDavid Ahern 38918c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 38928c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 38938c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 38948c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request"); 38958c6e137fSDavid Ahern return -EINVAL; 38968c6e137fSDavid Ahern } 38978c6e137fSDavid Ahern 38988c6e137fSDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX, 38998c6e137fSDavid Ahern NULL, extack); 39008c6e137fSDavid Ahern if (err < 0) 39018c6e137fSDavid Ahern return err; 39028c6e137fSDavid Ahern 39038c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 39048c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 39058c6e137fSDavid Ahern if (!tb[i]) 39068c6e137fSDavid Ahern continue; 39078c6e137fSDavid Ahern 39088c6e137fSDavid Ahern switch (i) { 39098c6e137fSDavid Ahern case NDA_IFINDEX: 39108c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 39118c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 39128c6e137fSDavid Ahern return -EINVAL; 39138c6e137fSDavid Ahern } 39148c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 39158c6e137fSDavid Ahern break; 39168c6e137fSDavid Ahern case NDA_MASTER: 39178c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 39188c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 39198c6e137fSDavid Ahern return -EINVAL; 39208c6e137fSDavid Ahern } 39218c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 39228c6e137fSDavid Ahern break; 39238c6e137fSDavid Ahern default: 39248c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 39258c6e137fSDavid Ahern return -EINVAL; 39268c6e137fSDavid Ahern } 39278c6e137fSDavid Ahern } 39288c6e137fSDavid Ahern 39298c6e137fSDavid Ahern return 0; 39308c6e137fSDavid Ahern } 39318c6e137fSDavid Ahern 39328dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 39338dfbda19SDavid Ahern int *br_idx, int *brport_idx, 39348dfbda19SDavid Ahern struct netlink_ext_ack *extack) 39358dfbda19SDavid Ahern { 39368dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 39378dfbda19SDavid Ahern int err; 39388dfbda19SDavid Ahern 39398dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 39408dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 39418dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 39428dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 39438dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 39448dfbda19SDavid Ahern * with an optional attribute. 39458dfbda19SDavid Ahern */ 39468dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 39478dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 39488dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 39494565d7e5SDavid Ahern struct ifinfomsg *ifm; 39504565d7e5SDavid Ahern 39518dfbda19SDavid Ahern err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, 39528dfbda19SDavid Ahern ifla_policy, extack); 39538dfbda19SDavid Ahern if (err < 0) { 39548dfbda19SDavid Ahern return -EINVAL; 39558dfbda19SDavid Ahern } else if (err == 0) { 39568dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 39578dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 39588dfbda19SDavid Ahern } 39598dfbda19SDavid Ahern 39604565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 39618dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 39628dfbda19SDavid Ahern } 39638dfbda19SDavid Ahern return 0; 39648dfbda19SDavid Ahern } 39658dfbda19SDavid Ahern 396677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 396777162022SJohn Fastabend { 396877162022SJohn Fastabend struct net_device *dev; 39695e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 39705e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 39715e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 39725e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3973d297653dSRoopa Prabhu struct hlist_head *head; 39745e6d2435SJamal Hadi Salim int brport_idx = 0; 39755e6d2435SJamal Hadi Salim int br_idx = 0; 3976d297653dSRoopa Prabhu int h, s_h; 3977d297653dSRoopa Prabhu int idx = 0, s_idx; 3978d297653dSRoopa Prabhu int err = 0; 3979d297653dSRoopa Prabhu int fidx = 0; 398077162022SJohn Fastabend 39818c6e137fSDavid Ahern if (cb->strict_check) 39828c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 39838c6e137fSDavid Ahern cb->extack); 39848c6e137fSDavid Ahern else 39858dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 39868dfbda19SDavid Ahern cb->extack); 39878dfbda19SDavid Ahern if (err < 0) 39888dfbda19SDavid Ahern return err; 39895e6d2435SJamal Hadi Salim 39905e6d2435SJamal Hadi Salim if (br_idx) { 39915e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 39925e6d2435SJamal Hadi Salim if (!br_dev) 39935e6d2435SJamal Hadi Salim return -ENODEV; 39945e6d2435SJamal Hadi Salim 3995898e5061SJiri Pirko ops = br_dev->netdev_ops; 39965e6d2435SJamal Hadi Salim } 39975e6d2435SJamal Hadi Salim 3998d297653dSRoopa Prabhu s_h = cb->args[0]; 3999d297653dSRoopa Prabhu s_idx = cb->args[1]; 4000d297653dSRoopa Prabhu 4001d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4002d297653dSRoopa Prabhu idx = 0; 4003d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4004d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4005d297653dSRoopa Prabhu 40065e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 40075e6d2435SJamal Hadi Salim continue; 40085e6d2435SJamal Hadi Salim 40095e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 40105e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 40115e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 40125e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 40135e6d2435SJamal Hadi Salim } 40145e6d2435SJamal Hadi Salim } else { 40155e6d2435SJamal Hadi Salim if (dev != br_dev && 40165e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 40175e6d2435SJamal Hadi Salim continue; 40185e6d2435SJamal Hadi Salim 40195e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 40205e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 40215e6d2435SJamal Hadi Salim continue; 40225e6d2435SJamal Hadi Salim cops = ops; 40235e6d2435SJamal Hadi Salim } 40245e6d2435SJamal Hadi Salim 4025d297653dSRoopa Prabhu if (idx < s_idx) 4026d297653dSRoopa Prabhu goto cont; 4027d297653dSRoopa Prabhu 40285e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 4029d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4030d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4031d297653dSRoopa Prabhu br_dev, dev, 4032d297653dSRoopa Prabhu &fidx); 4033d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4034d297653dSRoopa Prabhu goto out; 403577162022SJohn Fastabend } 4036d297653dSRoopa Prabhu } 403777162022SJohn Fastabend 40385e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4039d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4040d297653dSRoopa Prabhu dev, NULL, 4041d297653dSRoopa Prabhu &fidx); 40426cb69742SHubert Sokolowski else 4043d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4044d297653dSRoopa Prabhu &fidx); 4045d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4046d297653dSRoopa Prabhu goto out; 40475e6d2435SJamal Hadi Salim 40485e6d2435SJamal Hadi Salim cops = NULL; 4049d297653dSRoopa Prabhu 4050d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4051d297653dSRoopa Prabhu cb->args[2] = 0; 4052d297653dSRoopa Prabhu fidx = 0; 4053d297653dSRoopa Prabhu cont: 4054d297653dSRoopa Prabhu idx++; 4055d297653dSRoopa Prabhu } 405677162022SJohn Fastabend } 405777162022SJohn Fastabend 4058d297653dSRoopa Prabhu out: 4059d297653dSRoopa Prabhu cb->args[0] = h; 4060d297653dSRoopa Prabhu cb->args[1] = idx; 4061d297653dSRoopa Prabhu cb->args[2] = fidx; 4062d297653dSRoopa Prabhu 406377162022SJohn Fastabend return skb->len; 406477162022SJohn Fastabend } 406577162022SJohn Fastabend 40665b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 40675b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 40685b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 40695b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 40705b2f94b2SRoopa Prabhu { 40715b2f94b2SRoopa Prabhu struct ndmsg *ndm; 40725b2f94b2SRoopa Prabhu int err, i; 40735b2f94b2SRoopa Prabhu 40745b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 40755b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 40765b2f94b2SRoopa Prabhu return -EINVAL; 40775b2f94b2SRoopa Prabhu } 40785b2f94b2SRoopa Prabhu 40795b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 40805b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 40815b2f94b2SRoopa Prabhu ndm->ndm_type) { 40825b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 40835b2f94b2SRoopa Prabhu return -EINVAL; 40845b2f94b2SRoopa Prabhu } 40855b2f94b2SRoopa Prabhu 40865b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 40875b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 40885b2f94b2SRoopa Prabhu return -EINVAL; 40895b2f94b2SRoopa Prabhu } 40905b2f94b2SRoopa Prabhu 40915b2f94b2SRoopa Prabhu err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX, 40925b2f94b2SRoopa Prabhu nda_policy, extack); 40935b2f94b2SRoopa Prabhu if (err < 0) 40945b2f94b2SRoopa Prabhu return err; 40955b2f94b2SRoopa Prabhu 40965b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 40975b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 40985b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 40995b2f94b2SRoopa Prabhu if (!tb[i]) 41005b2f94b2SRoopa Prabhu continue; 41015b2f94b2SRoopa Prabhu 41025b2f94b2SRoopa Prabhu switch (i) { 41035b2f94b2SRoopa Prabhu case NDA_MASTER: 41045b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 41055b2f94b2SRoopa Prabhu break; 41065b2f94b2SRoopa Prabhu case NDA_LLADDR: 41075b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 41085b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 41095b2f94b2SRoopa Prabhu return -EINVAL; 41105b2f94b2SRoopa Prabhu } 41115b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 41125b2f94b2SRoopa Prabhu break; 41135b2f94b2SRoopa Prabhu case NDA_VLAN: 41145b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 41155b2f94b2SRoopa Prabhu if (err) 41165b2f94b2SRoopa Prabhu return err; 41175b2f94b2SRoopa Prabhu break; 41185b2f94b2SRoopa Prabhu case NDA_VNI: 41195b2f94b2SRoopa Prabhu break; 41205b2f94b2SRoopa Prabhu default: 41215b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 41225b2f94b2SRoopa Prabhu return -EINVAL; 41235b2f94b2SRoopa Prabhu } 41245b2f94b2SRoopa Prabhu } 41255b2f94b2SRoopa Prabhu 41265b2f94b2SRoopa Prabhu return 0; 41275b2f94b2SRoopa Prabhu } 41285b2f94b2SRoopa Prabhu 41295b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 41305b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 41315b2f94b2SRoopa Prabhu { 41325b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 41335b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 41345b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 41355b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 41365b2f94b2SRoopa Prabhu struct sk_buff *skb; 41375b2f94b2SRoopa Prabhu int brport_idx = 0; 41385b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 41395b2f94b2SRoopa Prabhu int br_idx = 0; 41405b2f94b2SRoopa Prabhu u8 *addr = NULL; 41415b2f94b2SRoopa Prabhu u16 vid = 0; 41425b2f94b2SRoopa Prabhu int err; 41435b2f94b2SRoopa Prabhu 41445b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 41455b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 41465b2f94b2SRoopa Prabhu if (err < 0) 41475b2f94b2SRoopa Prabhu return err; 41485b2f94b2SRoopa Prabhu 4149f989d03eSNikolay Aleksandrov if (!addr) { 4150f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4151f989d03eSNikolay Aleksandrov return -EINVAL; 4152f989d03eSNikolay Aleksandrov } 4153f989d03eSNikolay Aleksandrov 41545b2f94b2SRoopa Prabhu if (brport_idx) { 41555b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 41565b2f94b2SRoopa Prabhu if (!dev) { 41575b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 41585b2f94b2SRoopa Prabhu return -ENODEV; 41595b2f94b2SRoopa Prabhu } 41605b2f94b2SRoopa Prabhu } 41615b2f94b2SRoopa Prabhu 41625b2f94b2SRoopa Prabhu if (br_idx) { 41635b2f94b2SRoopa Prabhu if (dev) { 41645b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 41655b2f94b2SRoopa Prabhu return -EINVAL; 41665b2f94b2SRoopa Prabhu } 41675b2f94b2SRoopa Prabhu 41685b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 41695b2f94b2SRoopa Prabhu if (!br_dev) { 41705b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 41715b2f94b2SRoopa Prabhu return -EINVAL; 41725b2f94b2SRoopa Prabhu } 41735b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 41745b2f94b2SRoopa Prabhu } 41755b2f94b2SRoopa Prabhu 41765b2f94b2SRoopa Prabhu if (dev) { 41775b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 41785b2f94b2SRoopa Prabhu if (!(dev->priv_flags & IFF_BRIDGE_PORT)) { 41795b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 41805b2f94b2SRoopa Prabhu return -EINVAL; 41815b2f94b2SRoopa Prabhu } 41825b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 41835b2f94b2SRoopa Prabhu if (!br_dev) { 41845b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 41855b2f94b2SRoopa Prabhu return -EINVAL; 41865b2f94b2SRoopa Prabhu } 41875b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 41885b2f94b2SRoopa Prabhu } else { 41895b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 41905b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 41915b2f94b2SRoopa Prabhu return -EINVAL; 41925b2f94b2SRoopa Prabhu } 41935b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 41945b2f94b2SRoopa Prabhu } 41955b2f94b2SRoopa Prabhu } 41965b2f94b2SRoopa Prabhu 41975b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 41985b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 41995b2f94b2SRoopa Prabhu return -ENODEV; 42005b2f94b2SRoopa Prabhu } 42015b2f94b2SRoopa Prabhu 42025b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 42035b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 42045b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 42055b2f94b2SRoopa Prabhu } 42065b2f94b2SRoopa Prabhu 42075b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 42085b2f94b2SRoopa Prabhu if (!skb) 42095b2f94b2SRoopa Prabhu return -ENOBUFS; 42105b2f94b2SRoopa Prabhu 42115b2f94b2SRoopa Prabhu if (br_dev) 42125b2f94b2SRoopa Prabhu dev = br_dev; 42135b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 42145b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 42155b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 42165b2f94b2SRoopa Prabhu if (err) 42175b2f94b2SRoopa Prabhu goto out; 42185b2f94b2SRoopa Prabhu 42195b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 42205b2f94b2SRoopa Prabhu out: 42215b2f94b2SRoopa Prabhu kfree_skb(skb); 42225b2f94b2SRoopa Prabhu return err; 42235b2f94b2SRoopa Prabhu } 42245b2f94b2SRoopa Prabhu 42252c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 42262c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 42272c3c031cSScott Feldman { 42282c3c031cSScott Feldman if (mask & flag) 42292c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 42302c3c031cSScott Feldman return 0; 42312c3c031cSScott Feldman } 42322c3c031cSScott Feldman 4233815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 42342c3c031cSScott Feldman struct net_device *dev, u16 mode, 42357d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 42367d4f8d87SScott Feldman u32 filter_mask, 42377d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 42387d4f8d87SScott Feldman struct net_device *dev, 42397d4f8d87SScott Feldman u32 filter_mask)) 4240815cccbfSJohn Fastabend { 4241815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4242815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4243815cccbfSJohn Fastabend struct nlattr *br_afspec; 42442c3c031cSScott Feldman struct nlattr *protinfo; 4245815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4246898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 42477d4f8d87SScott Feldman int err = 0; 4248815cccbfSJohn Fastabend 424946c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4250815cccbfSJohn Fastabend if (nlh == NULL) 4251815cccbfSJohn Fastabend return -EMSGSIZE; 4252815cccbfSJohn Fastabend 4253815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4254815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4255815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4256815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4257815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4258815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4259815cccbfSJohn Fastabend ifm->ifi_change = 0; 4260815cccbfSJohn Fastabend 4261815cccbfSJohn Fastabend 4262815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4263815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4264815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4265898e5061SJiri Pirko (br_dev && 4266898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4267815cccbfSJohn Fastabend (dev->addr_len && 4268815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4269a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4270a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4271815cccbfSJohn Fastabend goto nla_put_failure; 4272815cccbfSJohn Fastabend 4273815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 4274815cccbfSJohn Fastabend if (!br_afspec) 4275815cccbfSJohn Fastabend goto nla_put_failure; 4276815cccbfSJohn Fastabend 42771d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4278815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4279815cccbfSJohn Fastabend goto nla_put_failure; 4280815cccbfSJohn Fastabend } 42811d460b98SRoopa Prabhu 42821d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 42831d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 42841d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 42851d460b98SRoopa Prabhu goto nla_put_failure; 42861d460b98SRoopa Prabhu } 42871d460b98SRoopa Prabhu } 42887d4f8d87SScott Feldman if (vlan_fill) { 42897d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 42907d4f8d87SScott Feldman if (err) { 42917d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 42927d4f8d87SScott Feldman goto nla_put_failure; 42937d4f8d87SScott Feldman } 42947d4f8d87SScott Feldman } 4295815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4296815cccbfSJohn Fastabend 42972c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 42982c3c031cSScott Feldman if (!protinfo) 42992c3c031cSScott Feldman goto nla_put_failure; 43002c3c031cSScott Feldman 43012c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 43022c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 43032c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43042c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 43052c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43062c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 43072c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 43082c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43092c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 43102c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43112c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 43122c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43132c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 43142c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43152c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 43162c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 43172c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 43182c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 43192c3c031cSScott Feldman goto nla_put_failure; 43202c3c031cSScott Feldman } 43212c3c031cSScott Feldman 43222c3c031cSScott Feldman nla_nest_end(skb, protinfo); 43232c3c031cSScott Feldman 4324053c095aSJohannes Berg nlmsg_end(skb, nlh); 4325053c095aSJohannes Berg return 0; 4326815cccbfSJohn Fastabend nla_put_failure: 4327815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 43287d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4329815cccbfSJohn Fastabend } 43307d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4331815cccbfSJohn Fastabend 43322d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 43332d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 43342d011be8SDavid Ahern struct netlink_ext_ack *extack) 43352d011be8SDavid Ahern { 43362d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 43372d011be8SDavid Ahern int err, i; 43382d011be8SDavid Ahern 43392d011be8SDavid Ahern if (strict_check) { 43402d011be8SDavid Ahern struct ifinfomsg *ifm; 43412d011be8SDavid Ahern 43422d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 43432d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 43442d011be8SDavid Ahern return -EINVAL; 43452d011be8SDavid Ahern } 43462d011be8SDavid Ahern 43472d011be8SDavid Ahern ifm = nlmsg_data(nlh); 43482d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 43492d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 43502d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 43512d011be8SDavid Ahern return -EINVAL; 43522d011be8SDavid Ahern } 43532d011be8SDavid Ahern 43542d011be8SDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb, 43552d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 43562d011be8SDavid Ahern } else { 43572d011be8SDavid Ahern err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, 43582d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 43592d011be8SDavid Ahern } 43602d011be8SDavid Ahern if (err < 0) 43612d011be8SDavid Ahern return err; 43622d011be8SDavid Ahern 43632d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 43642d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 43652d011be8SDavid Ahern if (!tb[i]) 43662d011be8SDavid Ahern continue; 43672d011be8SDavid Ahern 43682d011be8SDavid Ahern switch (i) { 43692d011be8SDavid Ahern case IFLA_EXT_MASK: 43702d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 43712d011be8SDavid Ahern break; 43722d011be8SDavid Ahern default: 43732d011be8SDavid Ahern if (strict_check) { 43742d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 43752d011be8SDavid Ahern return -EINVAL; 43762d011be8SDavid Ahern } 43772d011be8SDavid Ahern } 43782d011be8SDavid Ahern } 43792d011be8SDavid Ahern 43802d011be8SDavid Ahern return 0; 43812d011be8SDavid Ahern } 43822d011be8SDavid Ahern 4383e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4384e5a55a89SJohn Fastabend { 43852d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4386e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4387e5a55a89SJohn Fastabend struct net_device *dev; 4388e5a55a89SJohn Fastabend int idx = 0; 4389e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 43902d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 43916cbdceebSVlad Yasevich u32 filter_mask = 0; 4392d64f69b0SRoopa Prabhu int err; 43936cbdceebSVlad Yasevich 43942d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 43952d011be8SDavid Ahern cb->extack); 43962d011be8SDavid Ahern if (err < 0 && cb->strict_check) 43972d011be8SDavid Ahern return err; 4398e5a55a89SJohn Fastabend 4399e5a55a89SJohn Fastabend rcu_read_lock(); 4400e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4401e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4402898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4403e5a55a89SJohn Fastabend 4404898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4405d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4406d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4407d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4408d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4409f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4410f6c5775fSDavid Ahern if (likely(skb->len)) 4411e5a55a89SJohn Fastabend break; 4412f6c5775fSDavid Ahern 4413f6c5775fSDavid Ahern goto out_err; 4414f6c5775fSDavid Ahern } 4415d64f69b0SRoopa Prabhu } 4416e5a55a89SJohn Fastabend idx++; 4417e5a55a89SJohn Fastabend } 4418e5a55a89SJohn Fastabend 4419e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4420d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4421d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4422d64f69b0SRoopa Prabhu seq, dev, 442346c264daSNicolas Dichtel filter_mask, 4424d64f69b0SRoopa Prabhu NLM_F_MULTI); 4425f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4426f6c5775fSDavid Ahern if (likely(skb->len)) 4427e5a55a89SJohn Fastabend break; 4428f6c5775fSDavid Ahern 4429f6c5775fSDavid Ahern goto out_err; 4430f6c5775fSDavid Ahern } 4431d64f69b0SRoopa Prabhu } 4432e5a55a89SJohn Fastabend idx++; 4433e5a55a89SJohn Fastabend } 4434e5a55a89SJohn Fastabend } 4435f6c5775fSDavid Ahern err = skb->len; 4436f6c5775fSDavid Ahern out_err: 4437e5a55a89SJohn Fastabend rcu_read_unlock(); 4438e5a55a89SJohn Fastabend cb->args[0] = idx; 4439e5a55a89SJohn Fastabend 4440f6c5775fSDavid Ahern return err; 4441e5a55a89SJohn Fastabend } 4442e5a55a89SJohn Fastabend 44432469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 44442469ffd7SJohn Fastabend { 44452469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 44462469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 44472469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 44482469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 44492469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 44502469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 44512469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 44522469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 44532469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 44542469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 44552469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 44562469ffd7SJohn Fastabend } 44572469ffd7SJohn Fastabend 445802dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 44592469ffd7SJohn Fastabend { 44602469ffd7SJohn Fastabend struct net *net = dev_net(dev); 44612469ffd7SJohn Fastabend struct sk_buff *skb; 44622469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 44632469ffd7SJohn Fastabend 446402dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 446502dba438SRoopa Prabhu return 0; 446602dba438SRoopa Prabhu 44672469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 44682469ffd7SJohn Fastabend if (!skb) { 44692469ffd7SJohn Fastabend err = -ENOMEM; 44702469ffd7SJohn Fastabend goto errout; 44712469ffd7SJohn Fastabend } 44722469ffd7SJohn Fastabend 447346c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4474c38e01b8SJohn Fastabend if (err < 0) 4475c38e01b8SJohn Fastabend goto errout; 44762469ffd7SJohn Fastabend 447759ccaaaaSRoopa Prabhu if (!skb->len) 447859ccaaaaSRoopa Prabhu goto errout; 447959ccaaaaSRoopa Prabhu 44802469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 44812469ffd7SJohn Fastabend return 0; 44822469ffd7SJohn Fastabend errout: 44832469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 44842469ffd7SJohn Fastabend kfree_skb(skb); 448559ccaaaaSRoopa Prabhu if (err) 44862469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 44872469ffd7SJohn Fastabend return err; 44882469ffd7SJohn Fastabend } 44892469ffd7SJohn Fastabend 4490c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4491c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4492e5a55a89SJohn Fastabend { 4493e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4494e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4495e5a55a89SJohn Fastabend struct net_device *dev; 44962469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 44972469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 44984de8b413SRosen, Rami u16 flags = 0; 4499c38e01b8SJohn Fastabend bool have_flags = false; 4500e5a55a89SJohn Fastabend 4501e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4502e5a55a89SJohn Fastabend return -EINVAL; 4503e5a55a89SJohn Fastabend 4504e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4505e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4506e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4507e5a55a89SJohn Fastabend 4508e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4509e5a55a89SJohn Fastabend if (!dev) { 4510b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4511e5a55a89SJohn Fastabend return -ENODEV; 4512e5a55a89SJohn Fastabend } 4513e5a55a89SJohn Fastabend 45142469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 45152469ffd7SJohn Fastabend if (br_spec) { 45162469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 45172469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 45186e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 45196e8d1c55SThomas Graf return -EINVAL; 45206e8d1c55SThomas Graf 4521c38e01b8SJohn Fastabend have_flags = true; 45222469ffd7SJohn Fastabend flags = nla_get_u16(attr); 45232469ffd7SJohn Fastabend break; 45242469ffd7SJohn Fastabend } 45252469ffd7SJohn Fastabend } 45262469ffd7SJohn Fastabend } 45272469ffd7SJohn Fastabend 45282469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4529898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4530898e5061SJiri Pirko 4531898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 45322469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4533e5a55a89SJohn Fastabend goto out; 4534e5a55a89SJohn Fastabend } 4535e5a55a89SJohn Fastabend 45362fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 45372fd527b7SPetr Machata extack); 45382469ffd7SJohn Fastabend if (err) 45392469ffd7SJohn Fastabend goto out; 45402469ffd7SJohn Fastabend 45412469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 45422469ffd7SJohn Fastabend } 45432469ffd7SJohn Fastabend 45442469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 45452469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 45462469ffd7SJohn Fastabend err = -EOPNOTSUPP; 45472469ffd7SJohn Fastabend else 4548add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 45492fd527b7SPetr Machata flags, 45502fd527b7SPetr Machata extack); 455102dba438SRoopa Prabhu if (!err) { 45522469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 455302dba438SRoopa Prabhu 455402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 455502dba438SRoopa Prabhu * change 455602dba438SRoopa Prabhu */ 455702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 455802dba438SRoopa Prabhu } 45592469ffd7SJohn Fastabend } 45602469ffd7SJohn Fastabend 4561c38e01b8SJohn Fastabend if (have_flags) 45622469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4563e5a55a89SJohn Fastabend out: 4564e5a55a89SJohn Fastabend return err; 4565e5a55a89SJohn Fastabend } 4566e5a55a89SJohn Fastabend 4567c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4568c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4569407af329SVlad Yasevich { 4570407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4571407af329SVlad Yasevich struct ifinfomsg *ifm; 4572407af329SVlad Yasevich struct net_device *dev; 4573407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4574407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 45754de8b413SRosen, Rami u16 flags = 0; 4576407af329SVlad Yasevich bool have_flags = false; 4577407af329SVlad Yasevich 4578407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4579407af329SVlad Yasevich return -EINVAL; 4580407af329SVlad Yasevich 4581407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4582407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4583407af329SVlad Yasevich return -EPFNOSUPPORT; 4584407af329SVlad Yasevich 4585407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4586407af329SVlad Yasevich if (!dev) { 4587b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4588407af329SVlad Yasevich return -ENODEV; 4589407af329SVlad Yasevich } 4590407af329SVlad Yasevich 4591407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4592407af329SVlad Yasevich if (br_spec) { 4593407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4594407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 45956e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 45966e8d1c55SThomas Graf return -EINVAL; 45976e8d1c55SThomas Graf 4598407af329SVlad Yasevich have_flags = true; 4599407af329SVlad Yasevich flags = nla_get_u16(attr); 4600407af329SVlad Yasevich break; 4601407af329SVlad Yasevich } 4602407af329SVlad Yasevich } 4603407af329SVlad Yasevich } 4604407af329SVlad Yasevich 4605407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4606407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4607407af329SVlad Yasevich 4608407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4609407af329SVlad Yasevich err = -EOPNOTSUPP; 4610407af329SVlad Yasevich goto out; 4611407af329SVlad Yasevich } 4612407af329SVlad Yasevich 4613add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4614407af329SVlad Yasevich if (err) 4615407af329SVlad Yasevich goto out; 4616407af329SVlad Yasevich 4617407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4618407af329SVlad Yasevich } 4619407af329SVlad Yasevich 4620407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4621407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4622407af329SVlad Yasevich err = -EOPNOTSUPP; 4623407af329SVlad Yasevich else 4624add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4625add511b3SRoopa Prabhu flags); 4626407af329SVlad Yasevich 462702dba438SRoopa Prabhu if (!err) { 4628407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 462902dba438SRoopa Prabhu 463002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 463102dba438SRoopa Prabhu * change 463202dba438SRoopa Prabhu */ 463302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 463402dba438SRoopa Prabhu } 4635407af329SVlad Yasevich } 4636407af329SVlad Yasevich 4637407af329SVlad Yasevich if (have_flags) 4638407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4639407af329SVlad Yasevich out: 4640407af329SVlad Yasevich return err; 4641407af329SVlad Yasevich } 4642407af329SVlad Yasevich 4643e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4644e8872a25SNikolay Aleksandrov { 4645e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4646e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4647e8872a25SNikolay Aleksandrov } 4648e8872a25SNikolay Aleksandrov 464969ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 465069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 465169ae6ad2SNogah Frankel { 465269ae6ad2SNogah Frankel switch (attr_id) { 465369ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 465469ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 465569ae6ad2SNogah Frankel } 465669ae6ad2SNogah Frankel 465769ae6ad2SNogah Frankel return 0; 465869ae6ad2SNogah Frankel } 465969ae6ad2SNogah Frankel 466069ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 466169ae6ad2SNogah Frankel int *prividx) 466269ae6ad2SNogah Frankel { 466369ae6ad2SNogah Frankel struct nlattr *attr = NULL; 466469ae6ad2SNogah Frankel int attr_id, size; 466569ae6ad2SNogah Frankel void *attr_data; 466669ae6ad2SNogah Frankel int err; 466769ae6ad2SNogah Frankel 466869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 466969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 467069ae6ad2SNogah Frankel return -ENODATA; 467169ae6ad2SNogah Frankel 467269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 467369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 467469ae6ad2SNogah Frankel if (attr_id < *prividx) 467569ae6ad2SNogah Frankel continue; 467669ae6ad2SNogah Frankel 467769ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 467869ae6ad2SNogah Frankel if (!size) 467969ae6ad2SNogah Frankel continue; 468069ae6ad2SNogah Frankel 46813df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 468269ae6ad2SNogah Frankel continue; 468369ae6ad2SNogah Frankel 468469ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 468569ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 468669ae6ad2SNogah Frankel if (!attr) 468769ae6ad2SNogah Frankel goto nla_put_failure; 468869ae6ad2SNogah Frankel 468969ae6ad2SNogah Frankel attr_data = nla_data(attr); 469069ae6ad2SNogah Frankel memset(attr_data, 0, size); 469169ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 469269ae6ad2SNogah Frankel attr_data); 469369ae6ad2SNogah Frankel if (err) 469469ae6ad2SNogah Frankel goto get_offload_stats_failure; 469569ae6ad2SNogah Frankel } 469669ae6ad2SNogah Frankel 469769ae6ad2SNogah Frankel if (!attr) 469869ae6ad2SNogah Frankel return -ENODATA; 469969ae6ad2SNogah Frankel 470069ae6ad2SNogah Frankel *prividx = 0; 470169ae6ad2SNogah Frankel return 0; 470269ae6ad2SNogah Frankel 470369ae6ad2SNogah Frankel nla_put_failure: 470469ae6ad2SNogah Frankel err = -EMSGSIZE; 470569ae6ad2SNogah Frankel get_offload_stats_failure: 470669ae6ad2SNogah Frankel *prividx = attr_id; 470769ae6ad2SNogah Frankel return err; 470869ae6ad2SNogah Frankel } 470969ae6ad2SNogah Frankel 471069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 471169ae6ad2SNogah Frankel { 471269ae6ad2SNogah Frankel int nla_size = 0; 471369ae6ad2SNogah Frankel int attr_id; 471469ae6ad2SNogah Frankel int size; 471569ae6ad2SNogah Frankel 471669ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 471769ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 471869ae6ad2SNogah Frankel return 0; 471969ae6ad2SNogah Frankel 472069ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 472169ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 47223df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 472369ae6ad2SNogah Frankel continue; 472469ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 472569ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 472669ae6ad2SNogah Frankel } 472769ae6ad2SNogah Frankel 472869ae6ad2SNogah Frankel if (nla_size != 0) 472969ae6ad2SNogah Frankel nla_size += nla_total_size(0); 473069ae6ad2SNogah Frankel 473169ae6ad2SNogah Frankel return nla_size; 473269ae6ad2SNogah Frankel } 473369ae6ad2SNogah Frankel 473410c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 473510c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4736e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4737e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 473810c9ead9SRoopa Prabhu { 473910c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 474010c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 474110c9ead9SRoopa Prabhu struct nlattr *attr; 4742e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 474369ae6ad2SNogah Frankel int err; 474410c9ead9SRoopa Prabhu 474510c9ead9SRoopa Prabhu ASSERT_RTNL(); 474610c9ead9SRoopa Prabhu 474710c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 474810c9ead9SRoopa Prabhu if (!nlh) 474910c9ead9SRoopa Prabhu return -EMSGSIZE; 475010c9ead9SRoopa Prabhu 475110c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4752ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4753ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4754ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 475510c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 475610c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 475710c9ead9SRoopa Prabhu 4758e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 475910c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 476010c9ead9SRoopa Prabhu 476158414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 476258414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 476358414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 476410c9ead9SRoopa Prabhu if (!attr) 476510c9ead9SRoopa Prabhu goto nla_put_failure; 476610c9ead9SRoopa Prabhu 476710c9ead9SRoopa Prabhu sp = nla_data(attr); 476810c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 476910c9ead9SRoopa Prabhu } 477010c9ead9SRoopa Prabhu 477197a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 477297a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 477397a47facSNikolay Aleksandrov 477497a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 477597a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 477697a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 477797a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 477897a47facSNikolay Aleksandrov if (!attr) 477997a47facSNikolay Aleksandrov goto nla_put_failure; 478097a47facSNikolay Aleksandrov 478180e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 478280e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 478380e73cc5SNikolay Aleksandrov if (err) 478480e73cc5SNikolay Aleksandrov goto nla_put_failure; 478580e73cc5SNikolay Aleksandrov *idxattr = 0; 478680e73cc5SNikolay Aleksandrov } 478780e73cc5SNikolay Aleksandrov } 478880e73cc5SNikolay Aleksandrov 478980e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 479080e73cc5SNikolay Aleksandrov *idxattr)) { 479180e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 479280e73cc5SNikolay Aleksandrov const struct net_device *master; 479380e73cc5SNikolay Aleksandrov 479480e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 479580e73cc5SNikolay Aleksandrov if (master) 479680e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 479780e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 479880e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 479980e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 480080e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 480180e73cc5SNikolay Aleksandrov if (!attr) 480280e73cc5SNikolay Aleksandrov goto nla_put_failure; 480380e73cc5SNikolay Aleksandrov 480480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 480597a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 480697a47facSNikolay Aleksandrov if (err) 480797a47facSNikolay Aleksandrov goto nla_put_failure; 480897a47facSNikolay Aleksandrov *idxattr = 0; 480997a47facSNikolay Aleksandrov } 481097a47facSNikolay Aleksandrov } 481197a47facSNikolay Aleksandrov 481269ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 481369ae6ad2SNogah Frankel *idxattr)) { 481469ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 481569ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 481669ae6ad2SNogah Frankel if (!attr) 481769ae6ad2SNogah Frankel goto nla_put_failure; 481869ae6ad2SNogah Frankel 481969ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 482069ae6ad2SNogah Frankel if (err == -ENODATA) 482169ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 482269ae6ad2SNogah Frankel else 482369ae6ad2SNogah Frankel nla_nest_end(skb, attr); 482469ae6ad2SNogah Frankel 482569ae6ad2SNogah Frankel if (err && err != -ENODATA) 482669ae6ad2SNogah Frankel goto nla_put_failure; 482769ae6ad2SNogah Frankel *idxattr = 0; 482869ae6ad2SNogah Frankel } 482969ae6ad2SNogah Frankel 4830aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4831aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4832aefb4d4aSRobert Shearman 4833aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4834aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4835aefb4d4aSRobert Shearman if (!attr) 4836aefb4d4aSRobert Shearman goto nla_put_failure; 4837aefb4d4aSRobert Shearman 48385fa85a09SFlorian Westphal rcu_read_lock(); 48395fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4840aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4841aefb4d4aSRobert Shearman struct nlattr *af; 4842aefb4d4aSRobert Shearman int err; 4843aefb4d4aSRobert Shearman 4844aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 48455fa85a09SFlorian Westphal if (!af) { 48465fa85a09SFlorian Westphal rcu_read_unlock(); 4847aefb4d4aSRobert Shearman goto nla_put_failure; 48485fa85a09SFlorian Westphal } 4849aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4850aefb4d4aSRobert Shearman 48515fa85a09SFlorian Westphal if (err == -ENODATA) { 4852aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 48535fa85a09SFlorian Westphal } else if (err < 0) { 48545fa85a09SFlorian Westphal rcu_read_unlock(); 4855aefb4d4aSRobert Shearman goto nla_put_failure; 48565fa85a09SFlorian Westphal } 4857aefb4d4aSRobert Shearman 4858aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4859aefb4d4aSRobert Shearman } 4860aefb4d4aSRobert Shearman } 48615fa85a09SFlorian Westphal rcu_read_unlock(); 4862aefb4d4aSRobert Shearman 4863aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4864aefb4d4aSRobert Shearman 4865aefb4d4aSRobert Shearman *idxattr = 0; 4866aefb4d4aSRobert Shearman } 4867aefb4d4aSRobert Shearman 486810c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 486910c9ead9SRoopa Prabhu 487010c9ead9SRoopa Prabhu return 0; 487110c9ead9SRoopa Prabhu 487210c9ead9SRoopa Prabhu nla_put_failure: 4873e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4874e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 487510c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4876e8872a25SNikolay Aleksandrov else 4877e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 487810c9ead9SRoopa Prabhu 487910c9ead9SRoopa Prabhu return -EMSGSIZE; 488010c9ead9SRoopa Prabhu } 488110c9ead9SRoopa Prabhu 488210c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 488310c9ead9SRoopa Prabhu u32 filter_mask) 488410c9ead9SRoopa Prabhu { 488510c9ead9SRoopa Prabhu size_t size = 0; 488610c9ead9SRoopa Prabhu 4887e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 488810c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 488910c9ead9SRoopa Prabhu 489097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 489197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 489280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 489397a47facSNikolay Aleksandrov 489497a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 489580e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 489680e73cc5SNikolay Aleksandrov attr)); 489797a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 489897a47facSNikolay Aleksandrov size += nla_total_size(0); 489997a47facSNikolay Aleksandrov } 490097a47facSNikolay Aleksandrov } 490197a47facSNikolay Aleksandrov 490280e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 490380e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 490480e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 490580e73cc5SNikolay Aleksandrov const struct net_device *master; 490680e73cc5SNikolay Aleksandrov 490780e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 490880e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 490980e73cc5SNikolay Aleksandrov if (master) 491080e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 491180e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 491280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 491380e73cc5SNikolay Aleksandrov 491480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 491580e73cc5SNikolay Aleksandrov attr)); 491680e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 491780e73cc5SNikolay Aleksandrov size += nla_total_size(0); 491880e73cc5SNikolay Aleksandrov } 491980e73cc5SNikolay Aleksandrov } 492080e73cc5SNikolay Aleksandrov 492169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 492269ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 492369ae6ad2SNogah Frankel 4924aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4925aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4926aefb4d4aSRobert Shearman 4927aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4928aefb4d4aSRobert Shearman size += nla_total_size(0); 4929aefb4d4aSRobert Shearman 49305fa85a09SFlorian Westphal rcu_read_lock(); 49315fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4932aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4933aefb4d4aSRobert Shearman size += nla_total_size( 4934aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4935aefb4d4aSRobert Shearman 4936aefb4d4aSRobert Shearman /* for AF_* */ 4937aefb4d4aSRobert Shearman size += nla_total_size(0); 4938aefb4d4aSRobert Shearman } 4939aefb4d4aSRobert Shearman } 49405fa85a09SFlorian Westphal rcu_read_unlock(); 4941aefb4d4aSRobert Shearman } 4942aefb4d4aSRobert Shearman 494310c9ead9SRoopa Prabhu return size; 494410c9ead9SRoopa Prabhu } 494510c9ead9SRoopa Prabhu 494651bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 494751bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 494851bc860dSJakub Kicinski { 494951bc860dSJakub Kicinski struct if_stats_msg *ifsm; 495051bc860dSJakub Kicinski 495151bc860dSJakub Kicinski if (nlh->nlmsg_len < sizeof(*ifsm)) { 495251bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 495351bc860dSJakub Kicinski return -EINVAL; 495451bc860dSJakub Kicinski } 495551bc860dSJakub Kicinski 495651bc860dSJakub Kicinski if (!strict_check) 495751bc860dSJakub Kicinski return 0; 495851bc860dSJakub Kicinski 495951bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 496051bc860dSJakub Kicinski 496151bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 496251bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 496351bc860dSJakub Kicinski */ 496451bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 496551bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 496651bc860dSJakub Kicinski return -EINVAL; 496751bc860dSJakub Kicinski } 496851bc860dSJakub Kicinski if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 496951bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 497051bc860dSJakub Kicinski return -EINVAL; 497151bc860dSJakub Kicinski } 49726300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 49736300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 49746300acb2SJakub Kicinski return -EINVAL; 49756300acb2SJakub Kicinski } 497651bc860dSJakub Kicinski 497751bc860dSJakub Kicinski return 0; 497851bc860dSJakub Kicinski } 497951bc860dSJakub Kicinski 4980c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4981c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 498210c9ead9SRoopa Prabhu { 498310c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 498410c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4985e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4986e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 498710c9ead9SRoopa Prabhu struct sk_buff *nskb; 498810c9ead9SRoopa Prabhu u32 filter_mask; 498910c9ead9SRoopa Prabhu int err; 499010c9ead9SRoopa Prabhu 499151bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 499251bc860dSJakub Kicinski false, extack); 499351bc860dSJakub Kicinski if (err) 499451bc860dSJakub Kicinski return err; 49954775cc1fSMathias Krause 499610c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 499710c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 499810c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 499910c9ead9SRoopa Prabhu else 500010c9ead9SRoopa Prabhu return -EINVAL; 500110c9ead9SRoopa Prabhu 500210c9ead9SRoopa Prabhu if (!dev) 500310c9ead9SRoopa Prabhu return -ENODEV; 500410c9ead9SRoopa Prabhu 500510c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 500610c9ead9SRoopa Prabhu if (!filter_mask) 500710c9ead9SRoopa Prabhu return -EINVAL; 500810c9ead9SRoopa Prabhu 500910c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 501010c9ead9SRoopa Prabhu if (!nskb) 501110c9ead9SRoopa Prabhu return -ENOBUFS; 501210c9ead9SRoopa Prabhu 501310c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 501410c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5015e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 501610c9ead9SRoopa Prabhu if (err < 0) { 501710c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 501810c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 501910c9ead9SRoopa Prabhu kfree_skb(nskb); 502010c9ead9SRoopa Prabhu } else { 502110c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 502210c9ead9SRoopa Prabhu } 502310c9ead9SRoopa Prabhu 502410c9ead9SRoopa Prabhu return err; 502510c9ead9SRoopa Prabhu } 502610c9ead9SRoopa Prabhu 502710c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 502810c9ead9SRoopa Prabhu { 5029841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5030e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 503110c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 503210c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5033e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5034e8872a25SNikolay Aleksandrov struct hlist_head *head; 5035e8872a25SNikolay Aleksandrov struct net_device *dev; 503610c9ead9SRoopa Prabhu u32 filter_mask = 0; 5037e8872a25SNikolay Aleksandrov int idx = 0; 503810c9ead9SRoopa Prabhu 503910c9ead9SRoopa Prabhu s_h = cb->args[0]; 504010c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5041e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5042e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 504310c9ead9SRoopa Prabhu 504410c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 504510c9ead9SRoopa Prabhu 504651bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 504751bc860dSJakub Kicinski if (err) 504851bc860dSJakub Kicinski return err; 50494775cc1fSMathias Krause 505010c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 5051841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 5052841891ecSDavid Ahern if (!filter_mask) { 5053841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5054841891ecSDavid Ahern return -EINVAL; 5055841891ecSDavid Ahern } 505610c9ead9SRoopa Prabhu 505710c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 505810c9ead9SRoopa Prabhu idx = 0; 505910c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 506010c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 506110c9ead9SRoopa Prabhu if (idx < s_idx) 506210c9ead9SRoopa Prabhu goto cont; 506310c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 506410c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 506510c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 5066e8872a25SNikolay Aleksandrov flags, filter_mask, 5067e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 506810c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 506910c9ead9SRoopa Prabhu * we're in trouble 507010c9ead9SRoopa Prabhu */ 507110c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 507210c9ead9SRoopa Prabhu 507310c9ead9SRoopa Prabhu if (err < 0) 507410c9ead9SRoopa Prabhu goto out; 5075e8872a25SNikolay Aleksandrov s_prividx = 0; 5076e8872a25SNikolay Aleksandrov s_idxattr = 0; 507710c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 507810c9ead9SRoopa Prabhu cont: 507910c9ead9SRoopa Prabhu idx++; 508010c9ead9SRoopa Prabhu } 508110c9ead9SRoopa Prabhu } 508210c9ead9SRoopa Prabhu out: 5083e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5084e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 508510c9ead9SRoopa Prabhu cb->args[1] = idx; 508610c9ead9SRoopa Prabhu cb->args[0] = h; 508710c9ead9SRoopa Prabhu 508810c9ead9SRoopa Prabhu return skb->len; 508910c9ead9SRoopa Prabhu } 509010c9ead9SRoopa Prabhu 50911da177e4SLinus Torvalds /* Process one rtnetlink message. */ 50921da177e4SLinus Torvalds 50932d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 50942d4bc933SJohannes Berg struct netlink_ext_ack *extack) 50951da177e4SLinus Torvalds { 50963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5097addf9b90SFlorian Westphal struct rtnl_link *link; 5098e4202511SFlorian Westphal struct module *owner; 50996853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5100e2849863SThomas Graf rtnl_doit_func doit; 510162256f98SFlorian Westphal unsigned int flags; 5102617cfc75SAlexander Kuleshov int kind; 51031da177e4SLinus Torvalds int family; 51041da177e4SLinus Torvalds int type; 51051da177e4SLinus Torvalds 51061da177e4SLinus Torvalds type = nlh->nlmsg_type; 51071da177e4SLinus Torvalds if (type > RTM_MAX) 5108038890feSThomas Graf return -EOPNOTSUPP; 51091da177e4SLinus Torvalds 51101da177e4SLinus Torvalds type -= RTM_BASE; 51111da177e4SLinus Torvalds 51121da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5113573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 51141da177e4SLinus Torvalds return 0; 51151da177e4SLinus Torvalds 5116573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 51171da177e4SLinus Torvalds kind = type&3; 51181da177e4SLinus Torvalds 511990f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 51201d00a4ebSThomas Graf return -EPERM; 51211da177e4SLinus Torvalds 51226853dd48SFlorian Westphal rcu_read_lock(); 5123b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 512497c53cacSDenis V. Lunev struct sock *rtnl; 5125e2849863SThomas Graf rtnl_dumpit_func dumpit; 5126c7ac8679SGreg Rose u16 min_dump_alloc = 0; 51271da177e4SLinus Torvalds 5128addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5129addf9b90SFlorian Westphal if (!link || !link->dumpit) { 51306853dd48SFlorian Westphal family = PF_UNSPEC; 5131addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5132addf9b90SFlorian Westphal if (!link || !link->dumpit) 51336853dd48SFlorian Westphal goto err_unlock; 51346853dd48SFlorian Westphal } 5135e4202511SFlorian Westphal owner = link->owner; 5136addf9b90SFlorian Westphal dumpit = link->dumpit; 51376853dd48SFlorian Westphal 51385c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5139e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 51401da177e4SLinus Torvalds 5141e4202511SFlorian Westphal err = 0; 5142e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5143e4202511SFlorian Westphal if (!try_module_get(owner)) 5144e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5145e4202511SFlorian Westphal 51466853dd48SFlorian Westphal rcu_read_unlock(); 51476853dd48SFlorian Westphal 514897c53cacSDenis V. Lunev rtnl = net->rtnl; 5149e4202511SFlorian Westphal if (err == 0) { 515080d326faSPablo Neira Ayuso struct netlink_dump_control c = { 515180d326faSPablo Neira Ayuso .dump = dumpit, 515280d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5153e4202511SFlorian Westphal .module = owner, 515480d326faSPablo Neira Ayuso }; 515580d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5156e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5157e4202511SFlorian Westphal * module if dump is still in progress. 5158e4202511SFlorian Westphal */ 5159e4202511SFlorian Westphal module_put(owner); 516080d326faSPablo Neira Ayuso } 51612907c35fSEric Dumazet return err; 51621da177e4SLinus Torvalds } 51631da177e4SLinus Torvalds 5164addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5165addf9b90SFlorian Westphal if (!link || !link->doit) { 51668caa38b5SFlorian Westphal family = PF_UNSPEC; 5167addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 5168addf9b90SFlorian Westphal if (!link || !link->doit) 5169addf9b90SFlorian Westphal goto out_unlock; 51708caa38b5SFlorian Westphal } 51718caa38b5SFlorian Westphal 5172e4202511SFlorian Westphal owner = link->owner; 5173e4202511SFlorian Westphal if (!try_module_get(owner)) { 5174e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5175e4202511SFlorian Westphal goto out_unlock; 5176e4202511SFlorian Westphal } 5177e4202511SFlorian Westphal 5178addf9b90SFlorian Westphal flags = link->flags; 517962256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5180addf9b90SFlorian Westphal doit = link->doit; 518162256f98SFlorian Westphal rcu_read_unlock(); 518262256f98SFlorian Westphal if (doit) 518362256f98SFlorian Westphal err = doit(skb, nlh, extack); 5184e4202511SFlorian Westphal module_put(owner); 518562256f98SFlorian Westphal return err; 518662256f98SFlorian Westphal } 51876853dd48SFlorian Westphal rcu_read_unlock(); 51886853dd48SFlorian Westphal 51890cc09020SFlorian Westphal rtnl_lock(); 5190addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5191addf9b90SFlorian Westphal if (link && link->doit) 5192addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 51930cc09020SFlorian Westphal rtnl_unlock(); 5194addf9b90SFlorian Westphal 5195e4202511SFlorian Westphal module_put(owner); 5196e4202511SFlorian Westphal 5197addf9b90SFlorian Westphal return err; 5198addf9b90SFlorian Westphal 5199addf9b90SFlorian Westphal out_unlock: 5200addf9b90SFlorian Westphal rcu_read_unlock(); 52010cc09020SFlorian Westphal return err; 52020cc09020SFlorian Westphal 52030cc09020SFlorian Westphal err_unlock: 52046853dd48SFlorian Westphal rcu_read_unlock(); 52050cc09020SFlorian Westphal return -EOPNOTSUPP; 52061da177e4SLinus Torvalds } 52071da177e4SLinus Torvalds 5208cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 52091da177e4SLinus Torvalds { 5210cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 52111da177e4SLinus Torvalds } 52121da177e4SLinus Torvalds 52135f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 52145f729eaaSJulien Gomes { 52155f729eaaSJulien Gomes switch (group) { 52165f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 52175f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 52185f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 52195f729eaaSJulien Gomes return -EPERM; 52205f729eaaSJulien Gomes break; 52215f729eaaSJulien Gomes } 52225f729eaaSJulien Gomes return 0; 52235f729eaaSJulien Gomes } 52245f729eaaSJulien Gomes 52251da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 52261da177e4SLinus Torvalds { 5227351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5228e9dc8653SEric W. Biederman 52291da177e4SLinus Torvalds switch (event) { 52305138e86fSVlad Yasevich case NETDEV_REBOOT: 52318a212589SXin Long case NETDEV_CHANGEMTU: 52323753654eSDavid Ahern case NETDEV_CHANGEADDR: 52335138e86fSVlad Yasevich case NETDEV_CHANGENAME: 52345138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 52355138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 5236e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 52375138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 5238dc709f37SXin Long case NETDEV_CHANGEUPPER: 52395138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 52405138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 5241eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 5242ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 52433d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 524438e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 52451da177e4SLinus Torvalds break; 52461da177e4SLinus Torvalds default: 52471da177e4SLinus Torvalds break; 52481da177e4SLinus Torvalds } 52491da177e4SLinus Torvalds return NOTIFY_DONE; 52501da177e4SLinus Torvalds } 52511da177e4SLinus Torvalds 52521da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 52531da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 52541da177e4SLinus Torvalds }; 52551da177e4SLinus Torvalds 525697c53cacSDenis V. Lunev 52572c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 525897c53cacSDenis V. Lunev { 525997c53cacSDenis V. Lunev struct sock *sk; 5260a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 5261a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 5262a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 5263a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 52649785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 52655f729eaaSJulien Gomes .bind = rtnetlink_bind, 5266a31f2d17SPablo Neira Ayuso }; 5267a31f2d17SPablo Neira Ayuso 52689f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 526997c53cacSDenis V. Lunev if (!sk) 527097c53cacSDenis V. Lunev return -ENOMEM; 527197c53cacSDenis V. Lunev net->rtnl = sk; 527297c53cacSDenis V. Lunev return 0; 527397c53cacSDenis V. Lunev } 527497c53cacSDenis V. Lunev 52752c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 527697c53cacSDenis V. Lunev { 5277b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 527897c53cacSDenis V. Lunev net->rtnl = NULL; 527997c53cacSDenis V. Lunev } 528097c53cacSDenis V. Lunev 528197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 528297c53cacSDenis V. Lunev .init = rtnetlink_net_init, 528397c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 528497c53cacSDenis V. Lunev }; 528597c53cacSDenis V. Lunev 52861da177e4SLinus Torvalds void __init rtnetlink_init(void) 52871da177e4SLinus Torvalds { 528897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 52891da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 529097c53cacSDenis V. Lunev 52911da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 5292340d17fcSThomas Graf 5293c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5294b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 5295b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5296b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5297b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5298687ad8ccSThomas Graf 5299b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5300b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5301b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 530277162022SJohn Fastabend 5303b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5304b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 53055b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5306e5a55a89SJohn Fastabend 5307b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5308b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5309b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 531010c9ead9SRoopa Prabhu 531110c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5312b97bac64SFlorian Westphal 0); 53131da177e4SLinus Torvalds } 5314