12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 121da177e4SLinus Torvalds * Vitaly E. Lavrov RTA_OK arithmetics was wrong. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15ee5d032fSJakub Kicinski #include <linux/bitops.h> 161da177e4SLinus Torvalds #include <linux/errno.h> 171da177e4SLinus Torvalds #include <linux/module.h> 181da177e4SLinus Torvalds #include <linux/types.h> 191da177e4SLinus Torvalds #include <linux/socket.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/timer.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/sockios.h> 241da177e4SLinus Torvalds #include <linux/net.h> 251da177e4SLinus Torvalds #include <linux/fcntl.h> 261da177e4SLinus Torvalds #include <linux/mm.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/interrupt.h> 291da177e4SLinus Torvalds #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/skbuff.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/security.h> 336756ae4bSStephen Hemminger #include <linux/mutex.h> 341823730fSThomas Graf #include <linux/if_addr.h> 3577162022SJohn Fastabend #include <linux/if_bridge.h> 36f6f6424bSJiri Pirko #include <linux/if_vlan.h> 37ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 3877162022SJohn Fastabend #include <linux/etherdevice.h> 3958038695SMartin KaFai Lau #include <linux/bpf.h> 401da177e4SLinus Torvalds 417c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/netdevice.h> 451da177e4SLinus Torvalds #include <net/ip.h> 461da177e4SLinus Torvalds #include <net/protocol.h> 471da177e4SLinus Torvalds #include <net/arp.h> 481da177e4SLinus Torvalds #include <net/route.h> 491da177e4SLinus Torvalds #include <net/udp.h> 50ea697639SDaniel Borkmann #include <net/tcp.h> 511da177e4SLinus Torvalds #include <net/sock.h> 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 5314c0b97dSThomas Graf #include <net/fib_rules.h> 54e2849863SThomas Graf #include <net/rtnetlink.h> 5530ffee84SJohannes Berg #include <net/net_namespace.h> 561da177e4SLinus Torvalds 57a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 58ccf8dbcdSKees Cook #define RTNL_SLAVE_MAX_TYPE 36 59ccf8dbcdSKees Cook 60e0d087afSEric Dumazet struct rtnl_link { 61e2849863SThomas Graf rtnl_doit_func doit; 62e2849863SThomas Graf rtnl_dumpit_func dumpit; 63e4202511SFlorian Westphal struct module *owner; 6462256f98SFlorian Westphal unsigned int flags; 65addf9b90SFlorian Westphal struct rcu_head rcu; 66e2849863SThomas Graf }; 67e2849863SThomas Graf 686756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds void rtnl_lock(void) 711da177e4SLinus Torvalds { 726756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 731da177e4SLinus Torvalds } 74e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 751da177e4SLinus Torvalds 7679ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 7779ffdfc6SKirill Tkhai { 7879ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 7979ffdfc6SKirill Tkhai } 8079ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8179ffdfc6SKirill Tkhai 821b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 831b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 841b5c5493SEric Dumazet { 851b5c5493SEric Dumazet if (head && tail) { 861b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 871b5c5493SEric Dumazet defer_kfree_skb_list = head; 881b5c5493SEric Dumazet } 891b5c5493SEric Dumazet } 901b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 911b5c5493SEric Dumazet 926756ae4bSStephen Hemminger void __rtnl_unlock(void) 931da177e4SLinus Torvalds { 941b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 951b5c5493SEric Dumazet 961b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 971b5c5493SEric Dumazet 986756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 991b5c5493SEric Dumazet 1001b5c5493SEric Dumazet while (head) { 1011b5c5493SEric Dumazet struct sk_buff *next = head->next; 1021b5c5493SEric Dumazet 1031b5c5493SEric Dumazet kfree_skb(head); 1041b5c5493SEric Dumazet cond_resched(); 1051b5c5493SEric Dumazet head = next; 1061b5c5493SEric Dumazet } 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds void rtnl_unlock(void) 1101da177e4SLinus Torvalds { 11158ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1121da177e4SLinus Torvalds netdev_run_todo(); 1131da177e4SLinus Torvalds } 114e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1151da177e4SLinus Torvalds 1166756ae4bSStephen Hemminger int rtnl_trylock(void) 1176756ae4bSStephen Hemminger { 1186756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1196756ae4bSStephen Hemminger } 120e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1216756ae4bSStephen Hemminger 122c9c1014bSPatrick McHardy int rtnl_is_locked(void) 123c9c1014bSPatrick McHardy { 124c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 125c9c1014bSPatrick McHardy } 126e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 127c9c1014bSPatrick McHardy 1286f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1296f99528eSVlad Buslov { 1306f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1316f99528eSVlad Buslov } 1326f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1336f99528eSVlad Buslov 134a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1350cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 136a898def2SPaul E. McKenney { 137a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 138a898def2SPaul E. McKenney } 139a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 140a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 141a898def2SPaul E. McKenney 142b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 143e2849863SThomas Graf 144e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 145e2849863SThomas Graf { 146e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 147e2849863SThomas Graf 148e2849863SThomas Graf /* 149e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 150e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 151e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 152e2849863SThomas Graf */ 153e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 154e2849863SThomas Graf 155e2849863SThomas Graf return msgindex; 156e2849863SThomas Graf } 157e2849863SThomas Graf 158addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 159addf9b90SFlorian Westphal { 160addf9b90SFlorian Westphal struct rtnl_link **tab; 161addf9b90SFlorian Westphal 162addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 163addf9b90SFlorian Westphal protocol = PF_UNSPEC; 164addf9b90SFlorian Westphal 165addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 166addf9b90SFlorian Westphal if (!tab) 167addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 168addf9b90SFlorian Westphal 169addf9b90SFlorian Westphal return tab[msgtype]; 170addf9b90SFlorian Westphal } 171addf9b90SFlorian Westphal 172e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 173e4202511SFlorian Westphal int protocol, int msgtype, 174c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 175b97bac64SFlorian Westphal unsigned int flags) 176e2849863SThomas Graf { 177b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 178b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 179e2849863SThomas Graf int msgindex; 180addf9b90SFlorian Westphal int ret = -ENOBUFS; 181e2849863SThomas Graf 18225239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 183e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 184e2849863SThomas Graf 185addf9b90SFlorian Westphal rtnl_lock(); 186addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 187e2849863SThomas Graf if (tab == NULL) { 188addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 189addf9b90SFlorian Westphal if (!tab) 190addf9b90SFlorian Westphal goto unlock; 191e2849863SThomas Graf 192addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1936853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 194e2849863SThomas Graf } 195e2849863SThomas Graf 196addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 197addf9b90SFlorian Westphal if (old) { 198addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 199addf9b90SFlorian Westphal if (!link) 200addf9b90SFlorian Westphal goto unlock; 201addf9b90SFlorian Westphal } else { 202addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 203addf9b90SFlorian Westphal if (!link) 204addf9b90SFlorian Westphal goto unlock; 205addf9b90SFlorian Westphal } 206e2849863SThomas Graf 207e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 208e4202511SFlorian Westphal link->owner = owner; 209e4202511SFlorian Westphal 210addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 211addf9b90SFlorian Westphal if (doit) 212addf9b90SFlorian Westphal link->doit = doit; 213addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 214addf9b90SFlorian Westphal if (dumpit) 215addf9b90SFlorian Westphal link->dumpit = dumpit; 216addf9b90SFlorian Westphal 217addf9b90SFlorian Westphal link->flags |= flags; 218addf9b90SFlorian Westphal 219addf9b90SFlorian Westphal /* publish protocol:msgtype */ 220addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 221addf9b90SFlorian Westphal ret = 0; 222addf9b90SFlorian Westphal if (old) 223addf9b90SFlorian Westphal kfree_rcu(old, rcu); 224addf9b90SFlorian Westphal unlock: 225addf9b90SFlorian Westphal rtnl_unlock(); 226addf9b90SFlorian Westphal return ret; 227e2849863SThomas Graf } 228e4202511SFlorian Westphal 229e4202511SFlorian Westphal /** 230e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 231e4202511SFlorian Westphal * 232e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 233e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 234e4202511SFlorian Westphal * @msgtype: rtnetlink message type 235e4202511SFlorian Westphal * @doit: Function pointer called for each request message 236e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 237e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 238e4202511SFlorian Westphal * 239e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 240e4202511SFlorian Westphal */ 241e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 242e4202511SFlorian Westphal int protocol, int msgtype, 243e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 244e4202511SFlorian Westphal unsigned int flags) 245e4202511SFlorian Westphal { 246e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 247e4202511SFlorian Westphal doit, dumpit, flags); 248e4202511SFlorian Westphal } 249e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 250e4202511SFlorian Westphal 251e4202511SFlorian Westphal /** 25216feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 253e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 254e4202511SFlorian Westphal * @msgtype: rtnetlink message type 255e4202511SFlorian Westphal * @doit: Function pointer called for each request message 256e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 257e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 258e4202511SFlorian Westphal * 259e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 260e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 261e4202511SFlorian Westphal * specified protocol family and message type is received. 262e4202511SFlorian Westphal * 263e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 264e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 265e4202511SFlorian Westphal * family exists. 266e2849863SThomas Graf */ 267e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 268c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 269b97bac64SFlorian Westphal unsigned int flags) 270e2849863SThomas Graf { 27116feebcfSFlorian Westphal int err; 27216feebcfSFlorian Westphal 27316feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27416feebcfSFlorian Westphal flags); 27516feebcfSFlorian Westphal if (err) 27616feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 27716feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 278e2849863SThomas Graf } 279e2849863SThomas Graf 280e2849863SThomas Graf /** 281e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 282e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 283e2849863SThomas Graf * @msgtype: rtnetlink message type 284e2849863SThomas Graf * 285e2849863SThomas Graf * Returns 0 on success or a negative error code. 286e2849863SThomas Graf */ 287e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 288e2849863SThomas Graf { 289addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 290e2849863SThomas Graf int msgindex; 291e2849863SThomas Graf 29225239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 293e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 294e2849863SThomas Graf 2956853dd48SFlorian Westphal rtnl_lock(); 296addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 297addf9b90SFlorian Westphal if (!tab) { 2986853dd48SFlorian Westphal rtnl_unlock(); 299e2849863SThomas Graf return -ENOENT; 3006853dd48SFlorian Westphal } 301e2849863SThomas Graf 302addf9b90SFlorian Westphal link = tab[msgindex]; 303addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 3046853dd48SFlorian Westphal rtnl_unlock(); 305e2849863SThomas Graf 306addf9b90SFlorian Westphal kfree_rcu(link, rcu); 307addf9b90SFlorian Westphal 308e2849863SThomas Graf return 0; 309e2849863SThomas Graf } 310e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 311e2849863SThomas Graf 312e2849863SThomas Graf /** 313e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 314e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 315e2849863SThomas Graf * 316e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 317e2849863SThomas Graf * of a certain protocol family. 318e2849863SThomas Graf */ 319e2849863SThomas Graf void rtnl_unregister_all(int protocol) 320e2849863SThomas Graf { 321addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 322addf9b90SFlorian Westphal int msgindex; 323019a3169SFlorian Westphal 32425239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 325e2849863SThomas Graf 326019a3169SFlorian Westphal rtnl_lock(); 327addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 328f707ef61SSabrina Dubroca if (!tab) { 329f707ef61SSabrina Dubroca rtnl_unlock(); 330f707ef61SSabrina Dubroca return; 331f707ef61SSabrina Dubroca } 3326853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 333addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 334addf9b90SFlorian Westphal link = tab[msgindex]; 335addf9b90SFlorian Westphal if (!link) 336addf9b90SFlorian Westphal continue; 337addf9b90SFlorian Westphal 338addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 339addf9b90SFlorian Westphal kfree_rcu(link, rcu); 340addf9b90SFlorian Westphal } 341019a3169SFlorian Westphal rtnl_unlock(); 342019a3169SFlorian Westphal 3436853dd48SFlorian Westphal synchronize_net(); 3446853dd48SFlorian Westphal 345addf9b90SFlorian Westphal kfree(tab); 346e2849863SThomas Graf } 347e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3481da177e4SLinus Torvalds 34938f7b870SPatrick McHardy static LIST_HEAD(link_ops); 35038f7b870SPatrick McHardy 351c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 352c63044f0SEric Dumazet { 353c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 354c63044f0SEric Dumazet 355c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 356c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 357c63044f0SEric Dumazet return ops; 358c63044f0SEric Dumazet } 359c63044f0SEric Dumazet return NULL; 360c63044f0SEric Dumazet } 361c63044f0SEric Dumazet 36238f7b870SPatrick McHardy /** 36338f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 36438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 36538f7b870SPatrick McHardy * 36638f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 36738f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 36838f7b870SPatrick McHardy * must be called before registering the devices. 36938f7b870SPatrick McHardy * 37038f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 37138f7b870SPatrick McHardy */ 37238f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 37338f7b870SPatrick McHardy { 374c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 375c63044f0SEric Dumazet return -EEXIST; 376c63044f0SEric Dumazet 377b0ab2fabSJiri Pirko /* The check for setup is here because if ops 378b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 379b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 380b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 381b0ab2fabSJiri Pirko */ 382b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 38323289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3842d85cba2SPatrick McHardy 38538f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 38638f7b870SPatrick McHardy return 0; 38738f7b870SPatrick McHardy } 38838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 38938f7b870SPatrick McHardy 39038f7b870SPatrick McHardy /** 39138f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 39238f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 39338f7b870SPatrick McHardy * 39438f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 39538f7b870SPatrick McHardy */ 39638f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 39738f7b870SPatrick McHardy { 39838f7b870SPatrick McHardy int err; 39938f7b870SPatrick McHardy 400ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 401ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 402ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 403ccf8dbcdSKees Cook return -EINVAL; 404ccf8dbcdSKees Cook 40538f7b870SPatrick McHardy rtnl_lock(); 40638f7b870SPatrick McHardy err = __rtnl_link_register(ops); 40738f7b870SPatrick McHardy rtnl_unlock(); 40838f7b870SPatrick McHardy return err; 40938f7b870SPatrick McHardy } 41038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 41138f7b870SPatrick McHardy 412669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 413669f87baSPavel Emelyanov { 414669f87baSPavel Emelyanov struct net_device *dev; 41523289a37SEric Dumazet LIST_HEAD(list_kill); 41623289a37SEric Dumazet 417669f87baSPavel Emelyanov for_each_netdev(net, dev) { 41823289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 41923289a37SEric Dumazet ops->dellink(dev, &list_kill); 420669f87baSPavel Emelyanov } 42123289a37SEric Dumazet unregister_netdevice_many(&list_kill); 422669f87baSPavel Emelyanov } 423669f87baSPavel Emelyanov 42438f7b870SPatrick McHardy /** 42538f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 42638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 42738f7b870SPatrick McHardy * 428554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 429554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 430554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 43138f7b870SPatrick McHardy */ 43238f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 43338f7b870SPatrick McHardy { 434881d966bSEric W. Biederman struct net *net; 4352d85cba2SPatrick McHardy 436881d966bSEric W. Biederman for_each_net(net) { 437669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 438881d966bSEric W. Biederman } 43938f7b870SPatrick McHardy list_del(&ops->list); 44038f7b870SPatrick McHardy } 44138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 44238f7b870SPatrick McHardy 443200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 444200b916fSCong Wang * devices unregistering in any network namespace. 445200b916fSCong Wang */ 446200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 447200b916fSCong Wang { 448200b916fSCong Wang struct net *net; 449200b916fSCong Wang bool unregistering; 450ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 451200b916fSCong Wang 452ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 453200b916fSCong Wang for (;;) { 454200b916fSCong Wang unregistering = false; 455200b916fSCong Wang rtnl_lock(); 456f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 457f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 458f0b07bb1SKirill Tkhai */ 459200b916fSCong Wang for_each_net(net) { 460200b916fSCong Wang if (net->dev_unreg_count > 0) { 461200b916fSCong Wang unregistering = true; 462200b916fSCong Wang break; 463200b916fSCong Wang } 464200b916fSCong Wang } 465200b916fSCong Wang if (!unregistering) 466200b916fSCong Wang break; 467200b916fSCong Wang __rtnl_unlock(); 468ff960a73SPeter Zijlstra 469ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 470200b916fSCong Wang } 471ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 472200b916fSCong Wang } 473200b916fSCong Wang 47438f7b870SPatrick McHardy /** 47538f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 47638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47738f7b870SPatrick McHardy */ 47838f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 47938f7b870SPatrick McHardy { 4808518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4814420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 482200b916fSCong Wang rtnl_lock_unregistering_all(); 48338f7b870SPatrick McHardy __rtnl_link_unregister(ops); 48438f7b870SPatrick McHardy rtnl_unlock(); 4854420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 48638f7b870SPatrick McHardy } 48738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 48838f7b870SPatrick McHardy 489ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 490ba7d49b1SJiri Pirko { 491ba7d49b1SJiri Pirko struct net_device *master_dev; 492ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4938515ae38SFlorian Westphal size_t size = 0; 494ba7d49b1SJiri Pirko 4958515ae38SFlorian Westphal rcu_read_lock(); 4968515ae38SFlorian Westphal 4978515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 498ba7d49b1SJiri Pirko if (!master_dev) 4998515ae38SFlorian Westphal goto out; 5008515ae38SFlorian Westphal 501ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5026049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5038515ae38SFlorian Westphal goto out; 504ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5058515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 506ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5078515ae38SFlorian Westphal 5088515ae38SFlorian Westphal out: 5098515ae38SFlorian Westphal rcu_read_unlock(); 5108515ae38SFlorian Westphal return size; 511ba7d49b1SJiri Pirko } 512ba7d49b1SJiri Pirko 51338f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 51438f7b870SPatrick McHardy { 51538f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 51638f7b870SPatrick McHardy size_t size; 51738f7b870SPatrick McHardy 51838f7b870SPatrick McHardy if (!ops) 51938f7b870SPatrick McHardy return 0; 52038f7b870SPatrick McHardy 521369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 522369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 52338f7b870SPatrick McHardy 52438f7b870SPatrick McHardy if (ops->get_size) 52538f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 526369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 52738f7b870SPatrick McHardy ops->get_size(dev); 52838f7b870SPatrick McHardy 52938f7b870SPatrick McHardy if (ops->get_xstats_size) 530369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 531369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 53238f7b870SPatrick McHardy 533ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 534ba7d49b1SJiri Pirko 53538f7b870SPatrick McHardy return size; 53638f7b870SPatrick McHardy } 53738f7b870SPatrick McHardy 538f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 539f8ff182cSThomas Graf 540f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 541f8ff182cSThomas Graf { 542f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 543f8ff182cSThomas Graf 5445fa85a09SFlorian Westphal list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 545f8ff182cSThomas Graf if (ops->family == family) 546f8ff182cSThomas Graf return ops; 547f8ff182cSThomas Graf } 548f8ff182cSThomas Graf 549f8ff182cSThomas Graf return NULL; 550f8ff182cSThomas Graf } 551f8ff182cSThomas Graf 552f8ff182cSThomas Graf /** 553f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 554f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 555f8ff182cSThomas Graf * 556f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 557f8ff182cSThomas Graf */ 5583678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 559f8ff182cSThomas Graf { 560f8ff182cSThomas Graf rtnl_lock(); 5615fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 562f8ff182cSThomas Graf rtnl_unlock(); 563f8ff182cSThomas Graf } 564f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 565f8ff182cSThomas Graf 566f8ff182cSThomas Graf /** 567f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 568f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 569f8ff182cSThomas Graf */ 570f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 571f8ff182cSThomas Graf { 572f8ff182cSThomas Graf rtnl_lock(); 5735fa85a09SFlorian Westphal list_del_rcu(&ops->list); 574f8ff182cSThomas Graf rtnl_unlock(); 5755fa85a09SFlorian Westphal 5765fa85a09SFlorian Westphal synchronize_rcu(); 577f8ff182cSThomas Graf } 578f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 579f8ff182cSThomas Graf 580b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 581b1974ed0SArad, Ronen u32 ext_filter_mask) 582f8ff182cSThomas Graf { 583f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 584f8ff182cSThomas Graf size_t size; 585f8ff182cSThomas Graf 586f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 587f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 588f8ff182cSThomas Graf 5895fa85a09SFlorian Westphal rcu_read_lock(); 5905fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 591f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 592f8ff182cSThomas Graf /* AF_* + nested data */ 593f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 594b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 595f8ff182cSThomas Graf } 596f8ff182cSThomas Graf } 5975fa85a09SFlorian Westphal rcu_read_unlock(); 598f8ff182cSThomas Graf 599f8ff182cSThomas Graf return size; 600f8ff182cSThomas Graf } 601f8ff182cSThomas Graf 602ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 603ba7d49b1SJiri Pirko { 604ba7d49b1SJiri Pirko struct net_device *master_dev; 6054c82a95eSFlorian Westphal bool ret = false; 606ba7d49b1SJiri Pirko 6074c82a95eSFlorian Westphal rcu_read_lock(); 6084c82a95eSFlorian Westphal 6094c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 610813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6114c82a95eSFlorian Westphal ret = true; 6124c82a95eSFlorian Westphal rcu_read_unlock(); 6134c82a95eSFlorian Westphal return ret; 614ba7d49b1SJiri Pirko } 615ba7d49b1SJiri Pirko 616ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 617ba7d49b1SJiri Pirko const struct net_device *dev) 618ba7d49b1SJiri Pirko { 619ba7d49b1SJiri Pirko struct net_device *master_dev; 620ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 621ba7d49b1SJiri Pirko struct nlattr *slave_data; 622ba7d49b1SJiri Pirko int err; 623ba7d49b1SJiri Pirko 624ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 625ba7d49b1SJiri Pirko if (!master_dev) 626ba7d49b1SJiri Pirko return 0; 627ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 628ba7d49b1SJiri Pirko if (!ops) 629ba7d49b1SJiri Pirko return 0; 630ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 631ba7d49b1SJiri Pirko return -EMSGSIZE; 632ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 633ae0be8deSMichal Kubecek slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 634ba7d49b1SJiri Pirko if (!slave_data) 635ba7d49b1SJiri Pirko return -EMSGSIZE; 636ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 637ba7d49b1SJiri Pirko if (err < 0) 638ba7d49b1SJiri Pirko goto err_cancel_slave_data; 639ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 640ba7d49b1SJiri Pirko } 641ba7d49b1SJiri Pirko return 0; 642ba7d49b1SJiri Pirko 643ba7d49b1SJiri Pirko err_cancel_slave_data: 644ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 645ba7d49b1SJiri Pirko return err; 646ba7d49b1SJiri Pirko } 647ba7d49b1SJiri Pirko 648ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 649ba7d49b1SJiri Pirko const struct net_device *dev) 65038f7b870SPatrick McHardy { 65138f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 652ba7d49b1SJiri Pirko struct nlattr *data; 653ba7d49b1SJiri Pirko int err; 654ba7d49b1SJiri Pirko 655ba7d49b1SJiri Pirko if (!ops) 656ba7d49b1SJiri Pirko return 0; 657ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 658ba7d49b1SJiri Pirko return -EMSGSIZE; 659ba7d49b1SJiri Pirko if (ops->fill_xstats) { 660ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 661ba7d49b1SJiri Pirko if (err < 0) 662ba7d49b1SJiri Pirko return err; 663ba7d49b1SJiri Pirko } 664ba7d49b1SJiri Pirko if (ops->fill_info) { 665ae0be8deSMichal Kubecek data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 666ba7d49b1SJiri Pirko if (data == NULL) 667ba7d49b1SJiri Pirko return -EMSGSIZE; 668ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 669ba7d49b1SJiri Pirko if (err < 0) 670ba7d49b1SJiri Pirko goto err_cancel_data; 671ba7d49b1SJiri Pirko nla_nest_end(skb, data); 672ba7d49b1SJiri Pirko } 673ba7d49b1SJiri Pirko return 0; 674ba7d49b1SJiri Pirko 675ba7d49b1SJiri Pirko err_cancel_data: 676ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 677ba7d49b1SJiri Pirko return err; 678ba7d49b1SJiri Pirko } 679ba7d49b1SJiri Pirko 680ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 681ba7d49b1SJiri Pirko { 682ba7d49b1SJiri Pirko struct nlattr *linkinfo; 68338f7b870SPatrick McHardy int err = -EMSGSIZE; 68438f7b870SPatrick McHardy 685ae0be8deSMichal Kubecek linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 68638f7b870SPatrick McHardy if (linkinfo == NULL) 68738f7b870SPatrick McHardy goto out; 68838f7b870SPatrick McHardy 689ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 69038f7b870SPatrick McHardy if (err < 0) 69138f7b870SPatrick McHardy goto err_cancel_link; 692ba7d49b1SJiri Pirko 693ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 69438f7b870SPatrick McHardy if (err < 0) 695ba7d49b1SJiri Pirko goto err_cancel_link; 69638f7b870SPatrick McHardy 69738f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 69838f7b870SPatrick McHardy return 0; 69938f7b870SPatrick McHardy 70038f7b870SPatrick McHardy err_cancel_link: 70138f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 70238f7b870SPatrick McHardy out: 70338f7b870SPatrick McHardy return err; 70438f7b870SPatrick McHardy } 70538f7b870SPatrick McHardy 70695c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7071da177e4SLinus Torvalds { 70897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7091da177e4SLinus Torvalds int err = 0; 7101da177e4SLinus Torvalds 711ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 7121da177e4SLinus Torvalds if (echo) 71363354797SReshetova, Elena refcount_inc(&skb->users); 7141da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 7151da177e4SLinus Torvalds if (echo) 7161da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 7171da177e4SLinus Torvalds return err; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 72097c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7212942e900SThomas Graf { 72297c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72397c53cacSDenis V. Lunev 7242942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7252942e900SThomas Graf } 726e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7272942e900SThomas Graf 7281ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 72997676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 73097676b6bSThomas Graf { 73197c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73297676b6bSThomas Graf int report = 0; 73397676b6bSThomas Graf 73497676b6bSThomas Graf if (nlh) 73597676b6bSThomas Graf report = nlmsg_report(nlh); 73697676b6bSThomas Graf 7371ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 73897676b6bSThomas Graf } 739e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 74097676b6bSThomas Graf 74197c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 74297676b6bSThomas Graf { 74397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 74497c53cacSDenis V. Lunev 74597676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 74697676b6bSThomas Graf } 747e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 74897676b6bSThomas Graf 7491da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7501da177e4SLinus Torvalds { 7512d7202bfSThomas Graf struct nlattr *mx; 7522d7202bfSThomas Graf int i, valid = 0; 7531da177e4SLinus Torvalds 754c22a133aSDavid Ahern /* nothing is dumped for dst_default_metrics, so just skip the loop */ 755c22a133aSDavid Ahern if (metrics == dst_default_metrics.metrics) 756c22a133aSDavid Ahern return 0; 757c22a133aSDavid Ahern 758ae0be8deSMichal Kubecek mx = nla_nest_start_noflag(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 || 832eec517cdSAndrew Lunn operstate == IF_OPER_TESTING || 833b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 834eec517cdSAndrew Lunn !netif_dormant(dev) && !netif_testing(dev)) 835b00055aaSStefan Rompf operstate = IF_OPER_UP; 836b00055aaSStefan Rompf break; 837b00055aaSStefan Rompf 838eec517cdSAndrew Lunn case IF_OPER_TESTING: 839eec517cdSAndrew Lunn if (operstate == IF_OPER_UP || 840eec517cdSAndrew Lunn operstate == IF_OPER_UNKNOWN) 841eec517cdSAndrew Lunn operstate = IF_OPER_TESTING; 842eec517cdSAndrew Lunn break; 843eec517cdSAndrew Lunn 844b00055aaSStefan Rompf case IF_OPER_DORMANT: 845b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 846b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 847b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 848b00055aaSStefan Rompf break; 8493ff50b79SStephen Hemminger } 850b00055aaSStefan Rompf 851b00055aaSStefan Rompf if (dev->operstate != operstate) { 852b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 853b00055aaSStefan Rompf dev->operstate = operstate; 854b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 855b00055aaSStefan Rompf netdev_state_change(dev); 85693b2d4a2SDavid S. Miller } 857b00055aaSStefan Rompf } 858b00055aaSStefan Rompf 859b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 860b1beb681SJiri Benc { 861b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 862b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 863b1beb681SJiri Benc } 864b1beb681SJiri Benc 8653729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8663729d502SPatrick McHardy const struct ifinfomsg *ifm) 8673729d502SPatrick McHardy { 8683729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8693729d502SPatrick McHardy 8703729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8713729d502SPatrick McHardy if (ifm->ifi_change) 8723729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 873b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8743729d502SPatrick McHardy 8753729d502SPatrick McHardy return flags; 8763729d502SPatrick McHardy } 8773729d502SPatrick McHardy 878b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 879be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8801da177e4SLinus Torvalds { 881b60c5115SThomas Graf a->rx_packets = b->rx_packets; 882b60c5115SThomas Graf a->tx_packets = b->tx_packets; 883b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 884b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 885b60c5115SThomas Graf a->rx_errors = b->rx_errors; 886b60c5115SThomas Graf a->tx_errors = b->tx_errors; 887b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 888b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 889b60c5115SThomas Graf 890b60c5115SThomas Graf a->multicast = b->multicast; 891b60c5115SThomas Graf a->collisions = b->collisions; 892b60c5115SThomas Graf 893b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 894b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 895b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 896b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 897b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 898b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 899b60c5115SThomas Graf 900b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 901b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 902b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 903b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 904b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 905b60c5115SThomas Graf 906b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 907b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9086e7333d3SJarod Wilson 9096e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 91010708f37SJan Engelhardt } 91110708f37SJan Engelhardt 912c02db8c6SChris Wright /* All VF info */ 913115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 914115c9b81SGreg Rose u32 ext_filter_mask) 915ebc08a6fSWilliams, Mitch A { 9169af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 917c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9187e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 919045de01aSScott Feldman size += num_vfs * 9207e75f74aSSabrina Dubroca (nla_total_size(0) + 9217e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 92275345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9237e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9247e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 92579aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 92679aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 927ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9287e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 929945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 93001a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9313b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9327e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9333b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 934343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9353b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 936343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9373b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 938343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9393b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 940343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9413b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 942343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9433b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 944343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 945c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 946c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 947c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 948c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 949dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 950c02db8c6SChris Wright return size; 951c02db8c6SChris Wright } else 952ebc08a6fSWilliams, Mitch A return 0; 953ebc08a6fSWilliams, Mitch A } 954ebc08a6fSWilliams, Mitch A 955c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 956c53864fdSDavid Gibson u32 ext_filter_mask) 95757b61080SScott Feldman { 95857b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 95957b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 96057b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 96157b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 96257b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 96357b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 96457b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 96557b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 96657b61080SScott Feldman + port_size; 96757b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 96857b61080SScott Feldman + port_size; 96957b61080SScott Feldman 970c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 971c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 97257b61080SScott Feldman return 0; 97357b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 97457b61080SScott Feldman return port_self_size + vf_ports_size + 97557b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 97657b61080SScott Feldman else 97757b61080SScott Feldman return port_self_size; 97857b61080SScott Feldman } 97957b61080SScott Feldman 980b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 981d1fdd913SBrenden Blanco { 982b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 98358038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 984a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9854f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 986d1fdd913SBrenden Blanco 987d1fdd913SBrenden Blanco return xdp_size; 988d1fdd913SBrenden Blanco } 989d1fdd913SBrenden Blanco 99088f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 99188f4fb0cSJiri Pirko { 99288f4fb0cSJiri Pirko struct netdev_name_node *name_node; 99388f4fb0cSJiri Pirko size_t size; 99488f4fb0cSJiri Pirko 99588f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 99688f4fb0cSJiri Pirko return 0; 99788f4fb0cSJiri Pirko size = nla_total_size(0); 99888f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 99988f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 100088f4fb0cSJiri Pirko return size; 100188f4fb0cSJiri Pirko } 100288f4fb0cSJiri Pirko 1003*829eb208SRoopa Prabhu static size_t rtnl_proto_down_size(const struct net_device *dev) 1004*829eb208SRoopa Prabhu { 1005*829eb208SRoopa Prabhu size_t size = nla_total_size(1); 1006*829eb208SRoopa Prabhu 1007*829eb208SRoopa Prabhu if (dev->proto_down_reason) 1008*829eb208SRoopa Prabhu size += nla_total_size(0) + nla_total_size(4); 1009*829eb208SRoopa Prabhu 1010*829eb208SRoopa Prabhu return size; 1011*829eb208SRoopa Prabhu } 1012*829eb208SRoopa Prabhu 1013115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 1014115c9b81SGreg Rose u32 ext_filter_mask) 1015339bf98fSThomas Graf { 1016339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1017339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10180b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1019339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1020270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1021339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 102235c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1023339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1024339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1025339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1026339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1027339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1028339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1029339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10309a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1031edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 103276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 103376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10346919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10356919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1036339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 103738f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10382d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1039d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1040db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1041115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1042115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1043115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1044c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1045f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1046b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 104782f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 104888d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1049c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1050b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10513d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10526621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 105338e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1054*829eb208SRoopa Prabhu + rtnl_proto_down_size(dev) /* proto down */ 10557e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1056b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1057b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10583e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10593e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 106088f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1061f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 106279e1ad14SJiri Benc + 0; 1063339bf98fSThomas Graf } 1064339bf98fSThomas Graf 106557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 106657b61080SScott Feldman { 106757b61080SScott Feldman struct nlattr *vf_ports; 106857b61080SScott Feldman struct nlattr *vf_port; 106957b61080SScott Feldman int vf; 107057b61080SScott Feldman int err; 107157b61080SScott Feldman 1072ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 107357b61080SScott Feldman if (!vf_ports) 107457b61080SScott Feldman return -EMSGSIZE; 107557b61080SScott Feldman 107657b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1077ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 10788ca94183SScott Feldman if (!vf_port) 10798ca94183SScott Feldman goto nla_put_failure; 1080a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1081a6574349SDavid S. Miller goto nla_put_failure; 108257b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10838ca94183SScott Feldman if (err == -EMSGSIZE) 10848ca94183SScott Feldman goto nla_put_failure; 108557b61080SScott Feldman if (err) { 108657b61080SScott Feldman nla_nest_cancel(skb, vf_port); 108757b61080SScott Feldman continue; 108857b61080SScott Feldman } 108957b61080SScott Feldman nla_nest_end(skb, vf_port); 109057b61080SScott Feldman } 109157b61080SScott Feldman 109257b61080SScott Feldman nla_nest_end(skb, vf_ports); 109357b61080SScott Feldman 109457b61080SScott Feldman return 0; 10958ca94183SScott Feldman 10968ca94183SScott Feldman nla_put_failure: 10978ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10988ca94183SScott Feldman return -EMSGSIZE; 109957b61080SScott Feldman } 110057b61080SScott Feldman 110157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 110257b61080SScott Feldman { 110357b61080SScott Feldman struct nlattr *port_self; 110457b61080SScott Feldman int err; 110557b61080SScott Feldman 1106ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 110757b61080SScott Feldman if (!port_self) 110857b61080SScott Feldman return -EMSGSIZE; 110957b61080SScott Feldman 111057b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 111157b61080SScott Feldman if (err) { 111257b61080SScott Feldman nla_nest_cancel(skb, port_self); 11138ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 111457b61080SScott Feldman } 111557b61080SScott Feldman 111657b61080SScott Feldman nla_nest_end(skb, port_self); 111757b61080SScott Feldman 111857b61080SScott Feldman return 0; 111957b61080SScott Feldman } 112057b61080SScott Feldman 1121c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1122c53864fdSDavid Gibson u32 ext_filter_mask) 112357b61080SScott Feldman { 112457b61080SScott Feldman int err; 112557b61080SScott Feldman 1126c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1127c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 112857b61080SScott Feldman return 0; 112957b61080SScott Feldman 113057b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 113157b61080SScott Feldman if (err) 113257b61080SScott Feldman return err; 113357b61080SScott Feldman 113457b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 113557b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 113657b61080SScott Feldman if (err) 113757b61080SScott Feldman return err; 113857b61080SScott Feldman } 113957b61080SScott Feldman 114057b61080SScott Feldman return 0; 114157b61080SScott Feldman } 114257b61080SScott Feldman 114366cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 114466cae9edSJiri Pirko { 114566cae9edSJiri Pirko int err; 114602637fceSJiri Pirko struct netdev_phys_item_id ppid; 114766cae9edSJiri Pirko 114866cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 114966cae9edSJiri Pirko if (err) { 115066cae9edSJiri Pirko if (err == -EOPNOTSUPP) 115166cae9edSJiri Pirko return 0; 115266cae9edSJiri Pirko return err; 115366cae9edSJiri Pirko } 115466cae9edSJiri Pirko 115566cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 115666cae9edSJiri Pirko return -EMSGSIZE; 115766cae9edSJiri Pirko 115866cae9edSJiri Pirko return 0; 115966cae9edSJiri Pirko } 116066cae9edSJiri Pirko 1161db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1162db24a904SDavid Ahern { 1163db24a904SDavid Ahern char name[IFNAMSIZ]; 1164db24a904SDavid Ahern int err; 1165db24a904SDavid Ahern 1166db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1167db24a904SDavid Ahern if (err) { 1168db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1169db24a904SDavid Ahern return 0; 1170db24a904SDavid Ahern return err; 1171db24a904SDavid Ahern } 1172db24a904SDavid Ahern 117377ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1174db24a904SDavid Ahern return -EMSGSIZE; 1175db24a904SDavid Ahern 1176db24a904SDavid Ahern return 0; 1177db24a904SDavid Ahern } 1178db24a904SDavid Ahern 117982f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 118082f28412SJiri Pirko { 1181bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 118282f28412SJiri Pirko int err; 118382f28412SJiri Pirko 1184bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 118582f28412SJiri Pirko if (err) { 118682f28412SJiri Pirko if (err == -EOPNOTSUPP) 118782f28412SJiri Pirko return 0; 118882f28412SJiri Pirko return err; 118982f28412SJiri Pirko } 119082f28412SJiri Pirko 1191bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 119282f28412SJiri Pirko return -EMSGSIZE; 119382f28412SJiri Pirko 119482f28412SJiri Pirko return 0; 119582f28412SJiri Pirko } 119682f28412SJiri Pirko 1197b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1198b22b941bSHannes Frederic Sowa struct net_device *dev) 1199b22b941bSHannes Frederic Sowa { 1200550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1201b22b941bSHannes Frederic Sowa struct nlattr *attr; 1202b22b941bSHannes Frederic Sowa 120358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 120458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1205b22b941bSHannes Frederic Sowa if (!attr) 1206b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1207b22b941bSHannes Frederic Sowa 1208550bce59SRoopa Prabhu sp = nla_data(attr); 1209550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1210550bce59SRoopa Prabhu 1211550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1212550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1213550bce59SRoopa Prabhu if (!attr) 1214550bce59SRoopa Prabhu return -EMSGSIZE; 1215550bce59SRoopa Prabhu 1216550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1217b22b941bSHannes Frederic Sowa 1218b22b941bSHannes Frederic Sowa return 0; 1219b22b941bSHannes Frederic Sowa } 1220b22b941bSHannes Frederic Sowa 1221b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1222b22b941bSHannes Frederic Sowa struct net_device *dev, 1223b22b941bSHannes Frederic Sowa int vfs_num, 1224b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1225b22b941bSHannes Frederic Sowa { 1226b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 122779aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1228b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 122979aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1230b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1231b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1232b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1233b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1234b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1235b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1236b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 123775345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1238b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 123930aad417SDanit Goldberg struct ifla_vf_guid node_guid; 124030aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1241b22b941bSHannes Frederic Sowa 12420eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12430eed9cf5SMintz, Yuval 1244b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1245b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1246b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1247b22b941bSHannes Frederic Sowa * didn't report anything. 1248b22b941bSHannes Frederic Sowa */ 1249b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1250b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1251b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1252b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1253b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1254b22b941bSHannes Frederic Sowa */ 1255b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 125679aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 125779aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1258b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1259b22b941bSHannes Frederic Sowa return 0; 1260b22b941bSHannes Frederic Sowa 1261775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 12629fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 12639fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1264775f4f05SDan Carpenter 1265b22b941bSHannes Frederic Sowa vf_mac.vf = 1266b22b941bSHannes Frederic Sowa vf_vlan.vf = 126779aab093SMoshe Shemesh vf_vlan_info.vf = 1268b22b941bSHannes Frederic Sowa vf_rate.vf = 1269b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1270b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1271b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1272b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 12739aed6ae0SDanit Goldberg vf_trust.vf = 12749aed6ae0SDanit Goldberg node_guid.vf = 12759aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1276b22b941bSHannes Frederic Sowa 1277b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 127875345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1279b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1280b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 128179aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 128279aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 128379aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1284b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1285b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1286b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1287b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1288b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1289b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1290b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1291ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 129279aab093SMoshe Shemesh if (!vf) 129379aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1294b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 129575345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1296b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1297b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1298b22b941bSHannes Frederic Sowa &vf_rate) || 1299b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1300b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1301b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1302b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1303b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1304b22b941bSHannes Frederic Sowa &vf_linkstate) || 1305b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1306b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1307b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1308b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1309b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 131079aab093SMoshe Shemesh goto nla_put_vf_failure; 131130aad417SDanit Goldberg 131230aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 131330aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 131430aad417SDanit Goldberg &port_guid)) { 131530aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 131630aad417SDanit Goldberg &node_guid) || 131730aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 131830aad417SDanit Goldberg &port_guid)) 131930aad417SDanit Goldberg goto nla_put_vf_failure; 132030aad417SDanit Goldberg } 1321ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 132279aab093SMoshe Shemesh if (!vfvlanlist) 132379aab093SMoshe Shemesh goto nla_put_vf_failure; 132479aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 132579aab093SMoshe Shemesh &vf_vlan_info)) { 132679aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 132779aab093SMoshe Shemesh goto nla_put_vf_failure; 132879aab093SMoshe Shemesh } 132979aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1330b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1331b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1332b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1333b22b941bSHannes Frederic Sowa &vf_stats); 1334ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 133579aab093SMoshe Shemesh if (!vfstats) 133679aab093SMoshe Shemesh goto nla_put_vf_failure; 1337343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1338343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1339343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1340343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1341343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1342343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1343343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1344343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1345343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1346343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1347343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1348c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1349c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1350c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1351c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1352c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 135379aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 135479aab093SMoshe Shemesh goto nla_put_vf_failure; 135579aab093SMoshe Shemesh } 1356b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1357b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1358b22b941bSHannes Frederic Sowa return 0; 135979aab093SMoshe Shemesh 136079aab093SMoshe Shemesh nla_put_vf_failure: 136179aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 136279aab093SMoshe Shemesh nla_put_vfinfo_failure: 136379aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 136479aab093SMoshe Shemesh return -EMSGSIZE; 1365b22b941bSHannes Frederic Sowa } 1366b22b941bSHannes Frederic Sowa 1367250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1368250fc3dfSFlorian Westphal struct net_device *dev, 1369250fc3dfSFlorian Westphal u32 ext_filter_mask) 1370250fc3dfSFlorian Westphal { 1371250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1372250fc3dfSFlorian Westphal int i, num_vfs; 1373250fc3dfSFlorian Westphal 1374250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1375250fc3dfSFlorian Westphal return 0; 1376250fc3dfSFlorian Westphal 1377250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1378250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1379250fc3dfSFlorian Westphal return -EMSGSIZE; 1380250fc3dfSFlorian Westphal 1381250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1382250fc3dfSFlorian Westphal return 0; 1383250fc3dfSFlorian Westphal 1384ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1385250fc3dfSFlorian Westphal if (!vfinfo) 1386250fc3dfSFlorian Westphal return -EMSGSIZE; 1387250fc3dfSFlorian Westphal 1388250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1389250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1390250fc3dfSFlorian Westphal return -EMSGSIZE; 1391250fc3dfSFlorian Westphal } 1392250fc3dfSFlorian Westphal 1393250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1394250fc3dfSFlorian Westphal return 0; 1395250fc3dfSFlorian Westphal } 1396250fc3dfSFlorian Westphal 1397b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1398b22b941bSHannes Frederic Sowa { 13995f8e4474SKangjie Lu struct rtnl_link_ifmap map; 14005f8e4474SKangjie Lu 14015f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 14025f8e4474SKangjie Lu map.mem_start = dev->mem_start; 14035f8e4474SKangjie Lu map.mem_end = dev->mem_end; 14045f8e4474SKangjie Lu map.base_addr = dev->base_addr; 14055f8e4474SKangjie Lu map.irq = dev->irq; 14065f8e4474SKangjie Lu map.dma = dev->dma; 14075f8e4474SKangjie Lu map.port = dev->if_port; 14085f8e4474SKangjie Lu 1409270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1410b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1411b22b941bSHannes Frederic Sowa 1412b22b941bSHannes Frederic Sowa return 0; 1413b22b941bSHannes Frederic Sowa } 1414b22b941bSHannes Frederic Sowa 1415a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1416d67b9cd2SDaniel Borkmann { 141758038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1418d67b9cd2SDaniel Borkmann 1419d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1420d67b9cd2SDaniel Borkmann 142158038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1422a25717d2SJakub Kicinski if (!generic_xdp_prog) 1423a25717d2SJakub Kicinski return 0; 1424a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 142558038695SMartin KaFai Lau } 1426ce158e58SJakub Kicinski 1427a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1428a25717d2SJakub Kicinski { 1429a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1430a25717d2SJakub Kicinski } 1431118b4aa2SJakub Kicinski 1432a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1433a25717d2SJakub Kicinski { 1434a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1435a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1436a25717d2SJakub Kicinski } 1437a25717d2SJakub Kicinski 1438a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1439a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1440a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1441a25717d2SJakub Kicinski { 1442a25717d2SJakub Kicinski u32 curr_id; 1443a25717d2SJakub Kicinski int err; 1444a25717d2SJakub Kicinski 1445a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1446a25717d2SJakub Kicinski if (!curr_id) 1447a25717d2SJakub Kicinski return 0; 1448a25717d2SJakub Kicinski 1449a25717d2SJakub Kicinski *prog_id = curr_id; 1450a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1451a25717d2SJakub Kicinski if (err) 1452a25717d2SJakub Kicinski return err; 1453a25717d2SJakub Kicinski 1454a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1455a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1456a25717d2SJakub Kicinski else 1457a25717d2SJakub Kicinski *mode = tgt_mode; 1458a25717d2SJakub Kicinski 1459a25717d2SJakub Kicinski return 0; 1460d67b9cd2SDaniel Borkmann } 1461d67b9cd2SDaniel Borkmann 1462d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1463d1fdd913SBrenden Blanco { 1464d1fdd913SBrenden Blanco struct nlattr *xdp; 146558038695SMartin KaFai Lau u32 prog_id; 1466d1fdd913SBrenden Blanco int err; 14674f91da26SJakub Kicinski u8 mode; 1468d1fdd913SBrenden Blanco 1469ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1470d1fdd913SBrenden Blanco if (!xdp) 1471d1fdd913SBrenden Blanco return -EMSGSIZE; 1472b5cdae32SDavid S. Miller 1473a25717d2SJakub Kicinski prog_id = 0; 1474a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1475202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1476202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1477202aabe8SJakub Kicinski if (err) 1478a25717d2SJakub Kicinski goto err_cancel; 1479202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1480202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1481202aabe8SJakub Kicinski if (err) 1482a25717d2SJakub Kicinski goto err_cancel; 1483202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1484202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1485202aabe8SJakub Kicinski if (err) 1486a25717d2SJakub Kicinski goto err_cancel; 1487a25717d2SJakub Kicinski 14884f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1489d1fdd913SBrenden Blanco if (err) 1490d1fdd913SBrenden Blanco goto err_cancel; 1491d1fdd913SBrenden Blanco 1492a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 149358038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 149458038695SMartin KaFai Lau if (err) 149558038695SMartin KaFai Lau goto err_cancel; 149658038695SMartin KaFai Lau } 149758038695SMartin KaFai Lau 1498d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1499d1fdd913SBrenden Blanco return 0; 1500d1fdd913SBrenden Blanco 1501d1fdd913SBrenden Blanco err_cancel: 1502d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1503d1fdd913SBrenden Blanco return err; 1504d1fdd913SBrenden Blanco } 1505d1fdd913SBrenden Blanco 15063d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 15073d3ea5afSVlad Yasevich { 15083d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 15093d3ea5afSVlad Yasevich 15103d3ea5afSVlad Yasevich switch (event) { 15113d3ea5afSVlad Yasevich case NETDEV_REBOOT: 15123d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 15133d3ea5afSVlad Yasevich break; 15143d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 15153d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 15163d3ea5afSVlad Yasevich break; 15173d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15183d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15193d3ea5afSVlad Yasevich break; 15203d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15213d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15223d3ea5afSVlad Yasevich break; 15233d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15243d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15253d3ea5afSVlad Yasevich break; 15263d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15273d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15283d3ea5afSVlad Yasevich break; 15293d3ea5afSVlad Yasevich default: 15303d3ea5afSVlad Yasevich break; 15313d3ea5afSVlad Yasevich } 15323d3ea5afSVlad Yasevich 15333d3ea5afSVlad Yasevich return rtnl_event_type; 15343d3ea5afSVlad Yasevich } 15353d3ea5afSVlad Yasevich 153679110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 153779110a04SFlorian Westphal { 153879110a04SFlorian Westphal const struct net_device *upper_dev; 153979110a04SFlorian Westphal int ret = 0; 154079110a04SFlorian Westphal 154179110a04SFlorian Westphal rcu_read_lock(); 154279110a04SFlorian Westphal 154379110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 154479110a04SFlorian Westphal if (upper_dev) 154579110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 154679110a04SFlorian Westphal 154779110a04SFlorian Westphal rcu_read_unlock(); 154879110a04SFlorian Westphal return ret; 154979110a04SFlorian Westphal } 155079110a04SFlorian Westphal 1551feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1552feadc4b6SSabrina Dubroca bool force) 155379110a04SFlorian Westphal { 155479110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 155579110a04SFlorian Westphal 1556feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 155779110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1558feadc4b6SSabrina Dubroca 1559feadc4b6SSabrina Dubroca return 0; 156079110a04SFlorian Westphal } 156179110a04SFlorian Westphal 15626c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15636c557001SFlorian Westphal struct net_device *dev) 15646c557001SFlorian Westphal { 15656c557001SFlorian Westphal char buf[IFALIASZ]; 15666c557001SFlorian Westphal int ret; 15676c557001SFlorian Westphal 15686c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15696c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15706c557001SFlorian Westphal } 15716c557001SFlorian Westphal 1572b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 157379e1ad14SJiri Benc const struct net_device *dev, 1574d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1575b1e66b9aSFlorian Westphal { 1576feadc4b6SSabrina Dubroca bool put_iflink = false; 1577feadc4b6SSabrina Dubroca 1578b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1579b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1580b1e66b9aSFlorian Westphal 1581b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1582d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1583b1e66b9aSFlorian Westphal 1584b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1585b1e66b9aSFlorian Westphal return -EMSGSIZE; 1586feadc4b6SSabrina Dubroca 1587feadc4b6SSabrina Dubroca put_iflink = true; 1588b1e66b9aSFlorian Westphal } 1589b1e66b9aSFlorian Westphal } 1590b1e66b9aSFlorian Westphal 1591feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1592b1e66b9aSFlorian Westphal } 1593b1e66b9aSFlorian Westphal 1594070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1595070cbf5bSFlorian Westphal const struct net_device *dev, 1596070cbf5bSFlorian Westphal u32 ext_filter_mask) 1597070cbf5bSFlorian Westphal { 1598070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1599070cbf5bSFlorian Westphal struct nlattr *af_spec; 1600070cbf5bSFlorian Westphal 1601ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1602070cbf5bSFlorian Westphal if (!af_spec) 1603070cbf5bSFlorian Westphal return -EMSGSIZE; 1604070cbf5bSFlorian Westphal 16055fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1606070cbf5bSFlorian Westphal struct nlattr *af; 1607070cbf5bSFlorian Westphal int err; 1608070cbf5bSFlorian Westphal 1609070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1610070cbf5bSFlorian Westphal continue; 1611070cbf5bSFlorian Westphal 1612ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1613070cbf5bSFlorian Westphal if (!af) 1614070cbf5bSFlorian Westphal return -EMSGSIZE; 1615070cbf5bSFlorian Westphal 1616070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1617070cbf5bSFlorian Westphal /* 1618070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1619070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1620070cbf5bSFlorian Westphal * means we should trim the attribute header and 1621070cbf5bSFlorian Westphal * continue. 1622070cbf5bSFlorian Westphal */ 1623070cbf5bSFlorian Westphal if (err == -ENODATA) 1624070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1625070cbf5bSFlorian Westphal else if (err < 0) 1626070cbf5bSFlorian Westphal return -EMSGSIZE; 1627070cbf5bSFlorian Westphal 1628070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1629070cbf5bSFlorian Westphal } 1630070cbf5bSFlorian Westphal 1631070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1632070cbf5bSFlorian Westphal return 0; 1633070cbf5bSFlorian Westphal } 1634070cbf5bSFlorian Westphal 163588f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 163688f4fb0cSJiri Pirko const struct net_device *dev) 163788f4fb0cSJiri Pirko { 163888f4fb0cSJiri Pirko struct netdev_name_node *name_node; 163988f4fb0cSJiri Pirko int count = 0; 164088f4fb0cSJiri Pirko 164188f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 164288f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 164388f4fb0cSJiri Pirko return -EMSGSIZE; 164488f4fb0cSJiri Pirko count++; 164588f4fb0cSJiri Pirko } 164688f4fb0cSJiri Pirko return count; 164788f4fb0cSJiri Pirko } 164888f4fb0cSJiri Pirko 164988f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 165088f4fb0cSJiri Pirko const struct net_device *dev) 165188f4fb0cSJiri Pirko { 165288f4fb0cSJiri Pirko struct nlattr *prop_list; 165388f4fb0cSJiri Pirko int ret; 165488f4fb0cSJiri Pirko 165588f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 165688f4fb0cSJiri Pirko if (!prop_list) 165788f4fb0cSJiri Pirko return -EMSGSIZE; 165888f4fb0cSJiri Pirko 165988f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 166088f4fb0cSJiri Pirko if (ret <= 0) 166188f4fb0cSJiri Pirko goto nest_cancel; 166288f4fb0cSJiri Pirko 166388f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 166488f4fb0cSJiri Pirko return 0; 166588f4fb0cSJiri Pirko 166688f4fb0cSJiri Pirko nest_cancel: 166788f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 166888f4fb0cSJiri Pirko return ret; 166988f4fb0cSJiri Pirko } 167088f4fb0cSJiri Pirko 1671*829eb208SRoopa Prabhu static int rtnl_fill_proto_down(struct sk_buff *skb, 1672*829eb208SRoopa Prabhu const struct net_device *dev) 1673*829eb208SRoopa Prabhu { 1674*829eb208SRoopa Prabhu struct nlattr *pr; 1675*829eb208SRoopa Prabhu u32 preason; 1676*829eb208SRoopa Prabhu 1677*829eb208SRoopa Prabhu if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1678*829eb208SRoopa Prabhu goto nla_put_failure; 1679*829eb208SRoopa Prabhu 1680*829eb208SRoopa Prabhu preason = dev->proto_down_reason; 1681*829eb208SRoopa Prabhu if (!preason) 1682*829eb208SRoopa Prabhu return 0; 1683*829eb208SRoopa Prabhu 1684*829eb208SRoopa Prabhu pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1685*829eb208SRoopa Prabhu if (!pr) 1686*829eb208SRoopa Prabhu return -EMSGSIZE; 1687*829eb208SRoopa Prabhu 1688*829eb208SRoopa Prabhu if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1689*829eb208SRoopa Prabhu nla_nest_cancel(skb, pr); 1690*829eb208SRoopa Prabhu goto nla_put_failure; 1691*829eb208SRoopa Prabhu } 1692*829eb208SRoopa Prabhu 1693*829eb208SRoopa Prabhu nla_nest_end(skb, pr); 1694*829eb208SRoopa Prabhu return 0; 1695*829eb208SRoopa Prabhu 1696*829eb208SRoopa Prabhu nla_put_failure: 1697*829eb208SRoopa Prabhu return -EMSGSIZE; 1698*829eb208SRoopa Prabhu } 1699*829eb208SRoopa Prabhu 170079e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 170179e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1702575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 17033d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 170438e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1705d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1706b60c5115SThomas Graf { 1707b60c5115SThomas Graf struct ifinfomsg *ifm; 17081da177e4SLinus Torvalds struct nlmsghdr *nlh; 17091da177e4SLinus Torvalds 17102907c35fSEric Dumazet ASSERT_RTNL(); 1711b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1712b60c5115SThomas Graf if (nlh == NULL) 171326932566SPatrick McHardy return -EMSGSIZE; 17141da177e4SLinus Torvalds 1715b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1716b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1717b60c5115SThomas Graf ifm->__ifi_pad = 0; 1718b60c5115SThomas Graf ifm->ifi_type = dev->type; 1719b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1720b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1721b60c5115SThomas Graf ifm->ifi_change = change; 17221da177e4SLinus Torvalds 17237e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 172479e1ad14SJiri Benc goto nla_put_failure; 172579e1ad14SJiri Benc 1726a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1727a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1728a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1729a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1730a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1731a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 17323e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 17333e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1734a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1735edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 173676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1737c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1738c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 17391d69c2b3SMark A. Greer #ifdef CONFIG_RPS 174076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 17411d69c2b3SMark A. Greer #endif 174279110a04SFlorian Westphal put_master_ifindex(skb, dev) || 17439a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1744a6574349SDavid S. Miller (dev->qdisc && 1745a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 17466c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 17472d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1748b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1749b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1750b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1751b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1752b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1753b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1754a6574349SDavid S. Miller goto nla_put_failure; 17550b815a1aSStephen Hemminger 1756*829eb208SRoopa Prabhu if (rtnl_fill_proto_down(skb, dev)) 1757*829eb208SRoopa Prabhu goto nla_put_failure; 1758*829eb208SRoopa Prabhu 17593d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 17603d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 17613d3ea5afSVlad Yasevich goto nla_put_failure; 17623d3ea5afSVlad Yasevich } 17633d3ea5afSVlad Yasevich 1764b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1765a6574349SDavid S. Miller goto nla_put_failure; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds if (dev->addr_len) { 1768a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1769a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1770a6574349SDavid S. Miller goto nla_put_failure; 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 177366cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 177466cae9edSJiri Pirko goto nla_put_failure; 177566cae9edSJiri Pirko 1776db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1777db24a904SDavid Ahern goto nla_put_failure; 1778db24a904SDavid Ahern 177982f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 178082f28412SJiri Pirko goto nla_put_failure; 178182f28412SJiri Pirko 1782b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1783b60c5115SThomas Graf goto nla_put_failure; 1784b60c5115SThomas Graf 1785250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1786a6574349SDavid S. Miller goto nla_put_failure; 178757b61080SScott Feldman 1788c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 178957b61080SScott Feldman goto nla_put_failure; 179057b61080SScott Feldman 1791d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1792d1fdd913SBrenden Blanco goto nla_put_failure; 1793d1fdd913SBrenden Blanco 1794ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 179538f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 179638f7b870SPatrick McHardy goto nla_put_failure; 179738f7b870SPatrick McHardy } 179838f7b870SPatrick McHardy 1799d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1800d37512a2SNicolas Dichtel goto nla_put_failure; 1801d37512a2SNicolas Dichtel 18026621dd29SNicolas Dichtel if (new_nsid && 18036621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 18046621dd29SNicolas Dichtel goto nla_put_failure; 180538e01b30SNicolas Dichtel if (new_ifindex && 180638e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 180738e01b30SNicolas Dichtel goto nla_put_failure; 180838e01b30SNicolas Dichtel 1809f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1810f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1811f74877a5SMichal Kubecek goto nla_put_failure; 18126621dd29SNicolas Dichtel 18135fa85a09SFlorian Westphal rcu_read_lock(); 1814070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 18155fa85a09SFlorian Westphal goto nla_put_failure_rcu; 18165fa85a09SFlorian Westphal rcu_read_unlock(); 1817f8ff182cSThomas Graf 181888f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 181988f4fb0cSJiri Pirko goto nla_put_failure; 182088f4fb0cSJiri Pirko 1821053c095aSJohannes Berg nlmsg_end(skb, nlh); 1822053c095aSJohannes Berg return 0; 1823b60c5115SThomas Graf 18245fa85a09SFlorian Westphal nla_put_failure_rcu: 18255fa85a09SFlorian Westphal rcu_read_unlock(); 1826b60c5115SThomas Graf nla_put_failure: 182726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 182826932566SPatrick McHardy return -EMSGSIZE; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 1831f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1832f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1833f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1834f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1835f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1836f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1837f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1838f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1839f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1840f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1841f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1842f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1843f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1844f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1845f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1846f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 18472459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 18482459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 18492459b4c6SNicolas Dichtel */ 18502459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1851f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1852f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1853f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1854f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1855f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1856f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1857f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1858f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 185946e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 186046e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 186102637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 18622d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 186382f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1864317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 186588d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1866d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 18673d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1868db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 18697e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1870b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1871b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 18723e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 18733e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 187436fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 187536fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 187636fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1877f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1878*829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 1879f7b12606SJiri Pirko }; 1880f7b12606SJiri Pirko 1881f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1882f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1883f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1884f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1885f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1886f7b12606SJiri Pirko }; 1887f7b12606SJiri Pirko 1888f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1889364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 189075345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1891364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 189279aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1893364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1894364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1895364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1896364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 189701a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 18983b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1899dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1900cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1901cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 19023b766cd8SEran Ben Elisha }; 19033b766cd8SEran Ben Elisha 1904f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1905f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1906f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1907f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1908f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1909f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1910f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1911f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1912f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1913f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1914025331dfSDaniel Borkmann 1915025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1916025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1917025331dfSDaniel Borkmann * combination with structs. 1918025331dfSDaniel Borkmann */ 1919025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1920025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1921f7b12606SJiri Pirko }; 1922f7b12606SJiri Pirko 1923d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 192492234c8fSToke Høiland-Jørgensen [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 1925d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 192692234c8fSToke Høiland-Jørgensen [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 1927d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 192885de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 192958038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1930d1fdd913SBrenden Blanco }; 1931d1fdd913SBrenden Blanco 1932dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1933dc599f76SDavid Ahern { 1934dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1935dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1936dc599f76SDavid Ahern 19378cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 1938dc599f76SDavid Ahern return NULL; 1939dc599f76SDavid Ahern 1940dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1941dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1942dc599f76SDavid Ahern 1943dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1944dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1945dc599f76SDavid Ahern } 1946dc599f76SDavid Ahern 1947dc599f76SDavid Ahern return ops; 1948dc599f76SDavid Ahern } 1949dc599f76SDavid Ahern 1950dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1951dc599f76SDavid Ahern { 1952dc599f76SDavid Ahern struct net_device *master; 1953dc599f76SDavid Ahern 1954dc599f76SDavid Ahern if (!master_idx) 1955dc599f76SDavid Ahern return false; 1956dc599f76SDavid Ahern 1957dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1958dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1959dc599f76SDavid Ahern return true; 1960dc599f76SDavid Ahern 1961dc599f76SDavid Ahern return false; 1962dc599f76SDavid Ahern } 1963dc599f76SDavid Ahern 1964dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1965dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1966dc599f76SDavid Ahern { 1967dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1968dc599f76SDavid Ahern return true; 1969dc599f76SDavid Ahern 1970dc599f76SDavid Ahern return false; 1971dc599f76SDavid Ahern } 1972dc599f76SDavid Ahern 1973dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1974dc599f76SDavid Ahern int master_idx, 1975dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1976dc599f76SDavid Ahern { 1977dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1978dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1979dc599f76SDavid Ahern return true; 1980dc599f76SDavid Ahern 1981dc599f76SDavid Ahern return false; 1982dc599f76SDavid Ahern } 1983dc599f76SDavid Ahern 1984c383edc4SChristian Brauner /** 1985c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 1986c383edc4SChristian Brauner * @sk: netlink socket 1987c383edc4SChristian Brauner * @netnsid: network namespace identifier 1988c383edc4SChristian Brauner * 1989c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 1990c383edc4SChristian Brauner * pointer on failure. 1991c383edc4SChristian Brauner */ 1992c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 199379e1ad14SJiri Benc { 199479e1ad14SJiri Benc struct net *net; 199579e1ad14SJiri Benc 1996f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 199779e1ad14SJiri Benc if (!net) 199879e1ad14SJiri Benc return ERR_PTR(-EINVAL); 199979e1ad14SJiri Benc 200079e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 200179e1ad14SJiri Benc * the user namespace owning the target net ns. 200279e1ad14SJiri Benc */ 2003f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 200479e1ad14SJiri Benc put_net(net); 200579e1ad14SJiri Benc return ERR_PTR(-EACCES); 200679e1ad14SJiri Benc } 200779e1ad14SJiri Benc return net; 200879e1ad14SJiri Benc } 2009c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 201079e1ad14SJiri Benc 2011905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2012905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 2013905cf0abSDavid Ahern struct netlink_ext_ack *extack) 2014905cf0abSDavid Ahern { 2015905cf0abSDavid Ahern int hdrlen; 2016905cf0abSDavid Ahern 2017905cf0abSDavid Ahern if (strict_check) { 2018905cf0abSDavid Ahern struct ifinfomsg *ifm; 2019905cf0abSDavid Ahern 2020905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2021905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2022905cf0abSDavid Ahern return -EINVAL; 2023905cf0abSDavid Ahern } 2024905cf0abSDavid Ahern 2025905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 2026905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2027905cf0abSDavid Ahern ifm->ifi_change) { 2028905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2029905cf0abSDavid Ahern return -EINVAL; 2030905cf0abSDavid Ahern } 2031905cf0abSDavid Ahern if (ifm->ifi_index) { 2032905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2033905cf0abSDavid Ahern return -EINVAL; 2034905cf0abSDavid Ahern } 2035905cf0abSDavid Ahern 20368cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 20378cb08174SJohannes Berg IFLA_MAX, ifla_policy, 20388cb08174SJohannes Berg extack); 2039905cf0abSDavid Ahern } 2040905cf0abSDavid Ahern 2041905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 2042905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2043905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2044905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 2045905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2046905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 2047905cf0abSDavid Ahern */ 2048905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2049905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2050905cf0abSDavid Ahern 20518cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 20528cb08174SJohannes Berg extack); 2053905cf0abSDavid Ahern } 2054905cf0abSDavid Ahern 2055b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 20561da177e4SLinus Torvalds { 2057905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2058905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 20593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 206079e1ad14SJiri Benc struct net *tgt_net = net; 20617c28bd0bSEric Dumazet int h, s_h; 20627c28bd0bSEric Dumazet int idx = 0, s_idx; 20631da177e4SLinus Torvalds struct net_device *dev; 20647c28bd0bSEric Dumazet struct hlist_head *head; 2065115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2066115c9b81SGreg Rose u32 ext_filter_mask = 0; 2067dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2068dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2069dc599f76SDavid Ahern int master_idx = 0; 207079e1ad14SJiri Benc int netnsid = -1; 2071905cf0abSDavid Ahern int err, i; 20721da177e4SLinus Torvalds 20737c28bd0bSEric Dumazet s_h = cb->args[0]; 20747c28bd0bSEric Dumazet s_idx = cb->args[1]; 20757c28bd0bSEric Dumazet 2076905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2077905cf0abSDavid Ahern if (err < 0) { 2078905cf0abSDavid Ahern if (cb->strict_check) 2079905cf0abSDavid Ahern return err; 2080e5eca6d4SMichal Schmidt 2081905cf0abSDavid Ahern goto walk_entries; 208279e1ad14SJiri Benc } 208379e1ad14SJiri Benc 2084905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2085905cf0abSDavid Ahern if (!tb[i]) 2086905cf0abSDavid Ahern continue; 2087dc599f76SDavid Ahern 2088905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2089905cf0abSDavid Ahern switch (i) { 2090905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2091905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2092905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2093905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2094905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2095905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2096905cf0abSDavid Ahern } 2097905cf0abSDavid Ahern break; 2098905cf0abSDavid Ahern case IFLA_EXT_MASK: 2099905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2100905cf0abSDavid Ahern break; 2101905cf0abSDavid Ahern case IFLA_MASTER: 2102905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2103905cf0abSDavid Ahern break; 2104905cf0abSDavid Ahern case IFLA_LINKINFO: 2105905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2106905cf0abSDavid Ahern break; 2107905cf0abSDavid Ahern default: 2108905cf0abSDavid Ahern if (cb->strict_check) { 2109905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2110905cf0abSDavid Ahern return -EINVAL; 2111905cf0abSDavid Ahern } 2112905cf0abSDavid Ahern } 2113905cf0abSDavid Ahern } 2114dc599f76SDavid Ahern 2115dc599f76SDavid Ahern if (master_idx || kind_ops) 2116dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2117115c9b81SGreg Rose 2118905cf0abSDavid Ahern walk_entries: 21197c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 21207562f876SPavel Emelianov idx = 0; 212179e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2122cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2123dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 21243f0ae05dSZhang Shengju goto cont; 21251da177e4SLinus Torvalds if (idx < s_idx) 21267562f876SPavel Emelianov goto cont; 212779e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 212879e1ad14SJiri Benc RTM_NEWLINK, 212915e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2130905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 213138e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2132d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2133973462bbSDavid Gibson 2134f6c5775fSDavid Ahern if (err < 0) { 2135f6c5775fSDavid Ahern if (likely(skb->len)) 21367c28bd0bSEric Dumazet goto out; 21374e985adaSThomas Graf 2138f6c5775fSDavid Ahern goto out_err; 2139f6c5775fSDavid Ahern } 21407562f876SPavel Emelianov cont: 21417562f876SPavel Emelianov idx++; 21421da177e4SLinus Torvalds } 21437c28bd0bSEric Dumazet } 21447c28bd0bSEric Dumazet out: 2145f6c5775fSDavid Ahern err = skb->len; 2146f6c5775fSDavid Ahern out_err: 21477c28bd0bSEric Dumazet cb->args[1] = idx; 21487c28bd0bSEric Dumazet cb->args[0] = h; 2149d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 2150d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 215179e1ad14SJiri Benc if (netnsid >= 0) 215279e1ad14SJiri Benc put_net(tgt_net); 21531da177e4SLinus Torvalds 2154f6c5775fSDavid Ahern return err; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 2157fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2158fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2159f7b12606SJiri Pirko { 21608cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 21618cb08174SJohannes Berg exterr); 2162f7b12606SJiri Pirko } 2163f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 216457b61080SScott Feldman 216581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 216681adee47SEric W. Biederman { 216781adee47SEric W. Biederman struct net *net; 216881adee47SEric W. Biederman /* Examine the link attributes and figure out which 216981adee47SEric W. Biederman * network namespace we are talking about. 217081adee47SEric W. Biederman */ 217181adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 217281adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2173f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2174f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 217581adee47SEric W. Biederman else 217681adee47SEric W. Biederman net = get_net(src_net); 217781adee47SEric W. Biederman return net; 217881adee47SEric W. Biederman } 217981adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 218081adee47SEric W. Biederman 21817c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 21827c4f63baSChristian Brauner * examining the link attributes in the following order: 21837c4f63baSChristian Brauner * 21847c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 21857c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 21867e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 21877c4f63baSChristian Brauner */ 21887c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 21897c4f63baSChristian Brauner struct nlattr *tb[]) 21907c4f63baSChristian Brauner { 21917c4f63baSChristian Brauner struct net *net; 21927c4f63baSChristian Brauner 21937c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 21947c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 21957c4f63baSChristian Brauner 21967e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 21977c4f63baSChristian Brauner return get_net(src_net); 21987c4f63baSChristian Brauner 21997e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 22007c4f63baSChristian Brauner if (!net) 22017c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 22027c4f63baSChristian Brauner 22037c4f63baSChristian Brauner return net; 22047c4f63baSChristian Brauner } 22057c4f63baSChristian Brauner 22067c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 22077c4f63baSChristian Brauner struct net *src_net, 22087c4f63baSChristian Brauner struct nlattr *tb[], int cap) 22097c4f63baSChristian Brauner { 22107c4f63baSChristian Brauner struct net *net; 22117c4f63baSChristian Brauner 22127c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 22137c4f63baSChristian Brauner if (IS_ERR(net)) 22147c4f63baSChristian Brauner return net; 22157c4f63baSChristian Brauner 22167c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 22177c4f63baSChristian Brauner put_net(net); 22187c4f63baSChristian Brauner return ERR_PTR(-EPERM); 22197c4f63baSChristian Brauner } 22207c4f63baSChristian Brauner 22217c4f63baSChristian Brauner return net; 22227c4f63baSChristian Brauner } 22237c4f63baSChristian Brauner 22244ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 22254ff66caeSChristian Brauner * potentially referring to different network namespaces. 22264ff66caeSChristian Brauner */ 22274ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 22284ff66caeSChristian Brauner struct netlink_ext_ack *extack, 22294ff66caeSChristian Brauner bool netns_id_only) 22304ff66caeSChristian Brauner { 22314ff66caeSChristian Brauner 22324ff66caeSChristian Brauner if (netns_id_only) { 22334ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 22344ff66caeSChristian Brauner return 0; 22354ff66caeSChristian Brauner 22364ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 22374ff66caeSChristian Brauner return -EOPNOTSUPP; 22384ff66caeSChristian Brauner } 22394ff66caeSChristian Brauner 22407e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 22414ff66caeSChristian Brauner goto invalid_attr; 22424ff66caeSChristian Brauner 22437e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 22444ff66caeSChristian Brauner goto invalid_attr; 22454ff66caeSChristian Brauner 22467e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 22474ff66caeSChristian Brauner goto invalid_attr; 22484ff66caeSChristian Brauner 22494ff66caeSChristian Brauner return 0; 22504ff66caeSChristian Brauner 22514ff66caeSChristian Brauner invalid_attr: 22524ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 22534ff66caeSChristian Brauner return -EINVAL; 22544ff66caeSChristian Brauner } 22554ff66caeSChristian Brauner 22561840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 22571840bb13SThomas Graf { 22581840bb13SThomas Graf if (dev) { 22591840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 22601840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 22611840bb13SThomas Graf return -EINVAL; 22621840bb13SThomas Graf 22631840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 22641840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 22651840bb13SThomas Graf return -EINVAL; 22661840bb13SThomas Graf } 22671840bb13SThomas Graf 2268cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2269cf7afbfeSThomas Graf struct nlattr *af; 2270cf7afbfeSThomas Graf int rem, err; 2271cf7afbfeSThomas Graf 2272cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2273cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2274cf7afbfeSThomas Graf 22755fa85a09SFlorian Westphal rcu_read_lock(); 22765fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 22775fa85a09SFlorian Westphal if (!af_ops) { 22785fa85a09SFlorian Westphal rcu_read_unlock(); 2279cf7afbfeSThomas Graf return -EAFNOSUPPORT; 22805fa85a09SFlorian Westphal } 2281cf7afbfeSThomas Graf 22825fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 22835fa85a09SFlorian Westphal rcu_read_unlock(); 2284cf7afbfeSThomas Graf return -EOPNOTSUPP; 22855fa85a09SFlorian Westphal } 2286cf7afbfeSThomas Graf 2287cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 22886d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 22895fa85a09SFlorian Westphal if (err < 0) { 22905fa85a09SFlorian Westphal rcu_read_unlock(); 2291cf7afbfeSThomas Graf return err; 2292cf7afbfeSThomas Graf } 2293cf7afbfeSThomas Graf } 22945fa85a09SFlorian Westphal 22955fa85a09SFlorian Westphal rcu_read_unlock(); 22965fa85a09SFlorian Westphal } 2297cf7afbfeSThomas Graf } 2298cf7afbfeSThomas Graf 22991840bb13SThomas Graf return 0; 23001840bb13SThomas Graf } 23011840bb13SThomas Graf 2302cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2303cc8e27ccSEli Cohen int guid_type) 2304cc8e27ccSEli Cohen { 2305cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2306cc8e27ccSEli Cohen 2307cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2308cc8e27ccSEli Cohen } 2309cc8e27ccSEli Cohen 2310cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2311cc8e27ccSEli Cohen { 2312cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2313cc8e27ccSEli Cohen return -EOPNOTSUPP; 2314cc8e27ccSEli Cohen 2315cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2316cc8e27ccSEli Cohen } 2317cc8e27ccSEli Cohen 23184f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2319c02db8c6SChris Wright { 2320c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 23214f7d2cdfSDaniel Borkmann int err = -EINVAL; 2322c02db8c6SChris Wright 23234f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 23244f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 23254f7d2cdfSDaniel Borkmann 2326ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2327ff08ddbaSDan Carpenter return -EINVAL; 2328c02db8c6SChris Wright err = -EOPNOTSUPP; 2329c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2330c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2331c02db8c6SChris Wright ivm->mac); 23324f7d2cdfSDaniel Borkmann if (err < 0) 23334f7d2cdfSDaniel Borkmann return err; 2334c02db8c6SChris Wright } 23354f7d2cdfSDaniel Borkmann 23364f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 23374f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 23384f7d2cdfSDaniel Borkmann 2339ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2340ff08ddbaSDan Carpenter return -EINVAL; 2341c02db8c6SChris Wright err = -EOPNOTSUPP; 2342c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 23434f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 234479aab093SMoshe Shemesh ivv->qos, 234579aab093SMoshe Shemesh htons(ETH_P_8021Q)); 234679aab093SMoshe Shemesh if (err < 0) 234779aab093SMoshe Shemesh return err; 234879aab093SMoshe Shemesh } 234979aab093SMoshe Shemesh 235079aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 235179aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 235279aab093SMoshe Shemesh struct nlattr *attr; 235379aab093SMoshe Shemesh int rem, len = 0; 235479aab093SMoshe Shemesh 235579aab093SMoshe Shemesh err = -EOPNOTSUPP; 235679aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 235779aab093SMoshe Shemesh return err; 235879aab093SMoshe Shemesh 235979aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 236079aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 236179aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 236279aab093SMoshe Shemesh return -EINVAL; 236379aab093SMoshe Shemesh } 236479aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 236579aab093SMoshe Shemesh return -EOPNOTSUPP; 236679aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 236779aab093SMoshe Shemesh 236879aab093SMoshe Shemesh len++; 236979aab093SMoshe Shemesh } 2370fa34cd94SArnd Bergmann if (len == 0) 2371fa34cd94SArnd Bergmann return -EINVAL; 2372fa34cd94SArnd Bergmann 2373ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2374ff08ddbaSDan Carpenter return -EINVAL; 237579aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 237679aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 23774f7d2cdfSDaniel Borkmann if (err < 0) 23784f7d2cdfSDaniel Borkmann return err; 2379c02db8c6SChris Wright } 23804f7d2cdfSDaniel Borkmann 23814f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 23824f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2383ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 23844f7d2cdfSDaniel Borkmann 2385ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2386ff08ddbaSDan Carpenter return -EINVAL; 2387c02db8c6SChris Wright err = -EOPNOTSUPP; 2388ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 23894f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 23904f7d2cdfSDaniel Borkmann if (err < 0) 23914f7d2cdfSDaniel Borkmann return err; 23924f7d2cdfSDaniel Borkmann 2393ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2394ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2395ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2396ed616689SSucheta Chakraborty ivf.min_tx_rate, 2397c02db8c6SChris Wright ivt->rate); 23984f7d2cdfSDaniel Borkmann if (err < 0) 23994f7d2cdfSDaniel Borkmann return err; 2400c02db8c6SChris Wright } 24014f7d2cdfSDaniel Borkmann 24024f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 24034f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 24044f7d2cdfSDaniel Borkmann 2405ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2406ff08ddbaSDan Carpenter return -EINVAL; 2407ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2408ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2409ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2410ed616689SSucheta Chakraborty ivt->min_tx_rate, 2411ed616689SSucheta Chakraborty ivt->max_tx_rate); 24124f7d2cdfSDaniel Borkmann if (err < 0) 24134f7d2cdfSDaniel Borkmann return err; 2414ed616689SSucheta Chakraborty } 24154f7d2cdfSDaniel Borkmann 24164f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 24174f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 24184f7d2cdfSDaniel Borkmann 2419ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2420ff08ddbaSDan Carpenter return -EINVAL; 24215f8444a3SGreg Rose err = -EOPNOTSUPP; 24225f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 24235f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 24245f8444a3SGreg Rose ivs->setting); 24254f7d2cdfSDaniel Borkmann if (err < 0) 24264f7d2cdfSDaniel Borkmann return err; 24275f8444a3SGreg Rose } 24284f7d2cdfSDaniel Borkmann 24294f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 24304f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 24314f7d2cdfSDaniel Borkmann 2432ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2433ff08ddbaSDan Carpenter return -EINVAL; 24341d8faf48SRony Efraim err = -EOPNOTSUPP; 24351d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 24361d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 24371d8faf48SRony Efraim ivl->link_state); 24384f7d2cdfSDaniel Borkmann if (err < 0) 24394f7d2cdfSDaniel Borkmann return err; 24401d8faf48SRony Efraim } 24414f7d2cdfSDaniel Borkmann 24424f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 244301a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 244401a3d796SVlad Zolotarov 244501a3d796SVlad Zolotarov err = -EOPNOTSUPP; 24464f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2447ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2448ff08ddbaSDan Carpenter return -EINVAL; 244901a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 24504f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 245101a3d796SVlad Zolotarov ivrssq_en->setting); 24524f7d2cdfSDaniel Borkmann if (err < 0) 24534f7d2cdfSDaniel Borkmann return err; 245401a3d796SVlad Zolotarov } 24554f7d2cdfSDaniel Borkmann 2456dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2457dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2458dd461d6aSHiroshi Shimamoto 2459ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2460ff08ddbaSDan Carpenter return -EINVAL; 2461dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2462dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2463dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2464dd461d6aSHiroshi Shimamoto if (err < 0) 2465dd461d6aSHiroshi Shimamoto return err; 2466dd461d6aSHiroshi Shimamoto } 2467dd461d6aSHiroshi Shimamoto 2468cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2469cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2470cc8e27ccSEli Cohen 2471ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2472ff08ddbaSDan Carpenter return -EINVAL; 2473cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2474cc8e27ccSEli Cohen return -EOPNOTSUPP; 2475cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2476cc8e27ccSEli Cohen } 2477cc8e27ccSEli Cohen 2478cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2479cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2480cc8e27ccSEli Cohen 2481ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2482ff08ddbaSDan Carpenter return -EINVAL; 2483cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2484cc8e27ccSEli Cohen return -EOPNOTSUPP; 2485cc8e27ccSEli Cohen 2486cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2487cc8e27ccSEli Cohen } 2488cc8e27ccSEli Cohen 2489c02db8c6SChris Wright return err; 2490c02db8c6SChris Wright } 2491c02db8c6SChris Wright 249233eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 249333eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2494fbaec0eaSJiri Pirko { 2495898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2496fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2497fbaec0eaSJiri Pirko int err; 2498fbaec0eaSJiri Pirko 2499898e5061SJiri Pirko if (upper_dev) { 2500898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2501fbaec0eaSJiri Pirko return 0; 2502898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2503fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2504898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2505fbaec0eaSJiri Pirko if (err) 2506fbaec0eaSJiri Pirko return err; 2507fbaec0eaSJiri Pirko } else { 2508fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2509fbaec0eaSJiri Pirko } 2510fbaec0eaSJiri Pirko } 2511fbaec0eaSJiri Pirko 2512fbaec0eaSJiri Pirko if (ifindex) { 2513898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2514898e5061SJiri Pirko if (!upper_dev) 2515fbaec0eaSJiri Pirko return -EINVAL; 2516898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2517fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 251833eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2519fbaec0eaSJiri Pirko if (err) 2520fbaec0eaSJiri Pirko return err; 2521fbaec0eaSJiri Pirko } else { 2522fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2523fbaec0eaSJiri Pirko } 2524fbaec0eaSJiri Pirko } 2525fbaec0eaSJiri Pirko return 0; 2526fbaec0eaSJiri Pirko } 2527fbaec0eaSJiri Pirko 2528*829eb208SRoopa Prabhu static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2529*829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2530*829eb208SRoopa Prabhu [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2531*829eb208SRoopa Prabhu }; 2532*829eb208SRoopa Prabhu 2533*829eb208SRoopa Prabhu static int do_set_proto_down(struct net_device *dev, 2534*829eb208SRoopa Prabhu struct nlattr *nl_proto_down, 2535*829eb208SRoopa Prabhu struct nlattr *nl_proto_down_reason, 2536*829eb208SRoopa Prabhu struct netlink_ext_ack *extack) 2537*829eb208SRoopa Prabhu { 2538*829eb208SRoopa Prabhu struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2539*829eb208SRoopa Prabhu const struct net_device_ops *ops = dev->netdev_ops; 2540*829eb208SRoopa Prabhu unsigned long mask = 0; 2541*829eb208SRoopa Prabhu u32 value; 2542*829eb208SRoopa Prabhu bool proto_down; 2543*829eb208SRoopa Prabhu int err; 2544*829eb208SRoopa Prabhu 2545*829eb208SRoopa Prabhu if (!ops->ndo_change_proto_down) { 2546*829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2547*829eb208SRoopa Prabhu return -EOPNOTSUPP; 2548*829eb208SRoopa Prabhu } 2549*829eb208SRoopa Prabhu 2550*829eb208SRoopa Prabhu if (nl_proto_down_reason) { 2551*829eb208SRoopa Prabhu err = nla_parse_nested_deprecated(pdreason, 2552*829eb208SRoopa Prabhu IFLA_PROTO_DOWN_REASON_MAX, 2553*829eb208SRoopa Prabhu nl_proto_down_reason, 2554*829eb208SRoopa Prabhu ifla_proto_down_reason_policy, 2555*829eb208SRoopa Prabhu NULL); 2556*829eb208SRoopa Prabhu if (err < 0) 2557*829eb208SRoopa Prabhu return err; 2558*829eb208SRoopa Prabhu 2559*829eb208SRoopa Prabhu if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2560*829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2561*829eb208SRoopa Prabhu return -EINVAL; 2562*829eb208SRoopa Prabhu } 2563*829eb208SRoopa Prabhu 2564*829eb208SRoopa Prabhu value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2565*829eb208SRoopa Prabhu 2566*829eb208SRoopa Prabhu if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2567*829eb208SRoopa Prabhu mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2568*829eb208SRoopa Prabhu 2569*829eb208SRoopa Prabhu dev_change_proto_down_reason(dev, mask, value); 2570*829eb208SRoopa Prabhu } 2571*829eb208SRoopa Prabhu 2572*829eb208SRoopa Prabhu if (nl_proto_down) { 2573*829eb208SRoopa Prabhu proto_down = nla_get_u8(nl_proto_down); 2574*829eb208SRoopa Prabhu 2575*829eb208SRoopa Prabhu /* Dont turn off protodown if there are active reasons */ 2576*829eb208SRoopa Prabhu if (!proto_down && dev->proto_down_reason) { 2577*829eb208SRoopa Prabhu NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2578*829eb208SRoopa Prabhu return -EBUSY; 2579*829eb208SRoopa Prabhu } 2580*829eb208SRoopa Prabhu err = dev_change_proto_down(dev, 2581*829eb208SRoopa Prabhu proto_down); 2582*829eb208SRoopa Prabhu if (err) 2583*829eb208SRoopa Prabhu return err; 2584*829eb208SRoopa Prabhu } 2585*829eb208SRoopa Prabhu 2586*829eb208SRoopa Prabhu return 0; 2587*829eb208SRoopa Prabhu } 2588*829eb208SRoopa Prabhu 258990c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2590ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2591ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 259290f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 259390f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2594ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 259590c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 25960157f60cSPatrick McHardy { 2597d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 25980157f60cSPatrick McHardy int err; 25990157f60cSPatrick McHardy 2600644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2601644c7eebSEric Dumazet if (err < 0) 2602644c7eebSEric Dumazet return err; 2603644c7eebSEric Dumazet 26047e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 26057c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 26067c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2607d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2608d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2609d8a5ec67SEric W. Biederman goto errout; 2610d8a5ec67SEric W. Biederman } 26117c4f63baSChristian Brauner 2612d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2613d8a5ec67SEric W. Biederman put_net(net); 2614d8a5ec67SEric W. Biederman if (err) 2615d8a5ec67SEric W. Biederman goto errout; 261690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2617d8a5ec67SEric W. Biederman } 2618d8a5ec67SEric W. Biederman 26190157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 26200157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 26210157f60cSPatrick McHardy struct ifmap k_map; 26220157f60cSPatrick McHardy 2623d314774cSStephen Hemminger if (!ops->ndo_set_config) { 26240157f60cSPatrick McHardy err = -EOPNOTSUPP; 26250157f60cSPatrick McHardy goto errout; 26260157f60cSPatrick McHardy } 26270157f60cSPatrick McHardy 26280157f60cSPatrick McHardy if (!netif_device_present(dev)) { 26290157f60cSPatrick McHardy err = -ENODEV; 26300157f60cSPatrick McHardy goto errout; 26310157f60cSPatrick McHardy } 26320157f60cSPatrick McHardy 26330157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 26340157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 26350157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 26360157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 26370157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 26380157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 26390157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 26400157f60cSPatrick McHardy 2641d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 26420157f60cSPatrick McHardy if (err < 0) 26430157f60cSPatrick McHardy goto errout; 26440157f60cSPatrick McHardy 2645ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 26460157f60cSPatrick McHardy } 26470157f60cSPatrick McHardy 26480157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 26490157f60cSPatrick McHardy struct sockaddr *sa; 26500157f60cSPatrick McHardy int len; 26510157f60cSPatrick McHardy 2652153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2653153711f9SWANG Cong sizeof(*sa)); 26540157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 26550157f60cSPatrick McHardy if (!sa) { 26560157f60cSPatrick McHardy err = -ENOMEM; 26570157f60cSPatrick McHardy goto errout; 26580157f60cSPatrick McHardy } 26590157f60cSPatrick McHardy sa->sa_family = dev->type; 26600157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 26610157f60cSPatrick McHardy dev->addr_len); 26623a37a963SPetr Machata err = dev_set_mac_address(dev, sa, extack); 26630157f60cSPatrick McHardy kfree(sa); 26640157f60cSPatrick McHardy if (err) 26650157f60cSPatrick McHardy goto errout; 266690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26670157f60cSPatrick McHardy } 26680157f60cSPatrick McHardy 26690157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 26707a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 26710157f60cSPatrick McHardy if (err < 0) 26720157f60cSPatrick McHardy goto errout; 267390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26740157f60cSPatrick McHardy } 26750157f60cSPatrick McHardy 2676cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2677cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2678ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2679cbda10faSVlad Dogaru } 2680cbda10faSVlad Dogaru 26810157f60cSPatrick McHardy /* 26820157f60cSPatrick McHardy * Interface selected by interface index but interface 26830157f60cSPatrick McHardy * name provided implies that a name change has been 26840157f60cSPatrick McHardy * requested. 26850157f60cSPatrick McHardy */ 26860157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 26870157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 26880157f60cSPatrick McHardy if (err < 0) 26890157f60cSPatrick McHardy goto errout; 269090c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26910157f60cSPatrick McHardy } 26920157f60cSPatrick McHardy 26930b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 26940b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 26950b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 26960b815a1aSStephen Hemminger if (err < 0) 26970b815a1aSStephen Hemminger goto errout; 2698ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 26990b815a1aSStephen Hemminger } 27000b815a1aSStephen Hemminger 27010157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 27020157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2703e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 27040157f60cSPatrick McHardy } 27050157f60cSPatrick McHardy 27060157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2707567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2708567c5e13SPetr Machata extack); 27095f9021cfSJohannes Berg if (err < 0) 27105f9021cfSJohannes Berg goto errout; 27110157f60cSPatrick McHardy } 27120157f60cSPatrick McHardy 2713fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 271433eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2715fbaec0eaSJiri Pirko if (err) 2716fbaec0eaSJiri Pirko goto errout; 271790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2718fbaec0eaSJiri Pirko } 2719fbaec0eaSJiri Pirko 27209a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 27219a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 27229a57247fSJiri Pirko if (err) 27239a57247fSJiri Pirko goto errout; 272490c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 27259a57247fSJiri Pirko } 27269a57247fSJiri Pirko 27275d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 27280cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 27295d1180fcSNicolas Dichtel 27306a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 27316a643ddbSCong Wang if (err) 273208294a26SJason Wang goto errout; 27332d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 273408294a26SJason Wang } 27350157f60cSPatrick McHardy 273646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 273746e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 273846e6b992SStephen Hemminger 273946e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 274046e6b992SStephen Hemminger err = -EINVAL; 274146e6b992SStephen Hemminger goto errout; 274246e6b992SStephen Hemminger } 274346e6b992SStephen Hemminger 274446e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 274546e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 274646e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 274746e6b992SStephen Hemminger } 274846e6b992SStephen Hemminger } 274946e6b992SStephen Hemminger 275046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 275146e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 275246e6b992SStephen Hemminger 275346e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 275446e6b992SStephen Hemminger err = -EINVAL; 275546e6b992SStephen Hemminger goto errout; 275646e6b992SStephen Hemminger } 275746e6b992SStephen Hemminger 275846e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 275946e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 276046e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 276146e6b992SStephen Hemminger } 276246e6b992SStephen Hemminger } 276346e6b992SStephen Hemminger 27640157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 276593b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 27660157f60cSPatrick McHardy 27670157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 27681889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 27691889b0e7SNicolas Dichtel 27700157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 27711889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2772ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 27731889b0e7SNicolas Dichtel dev->link_mode = value; 277493b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 27750157f60cSPatrick McHardy } 27760157f60cSPatrick McHardy 2777c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 27784f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2779c02db8c6SChris Wright struct nlattr *attr; 2780c02db8c6SChris Wright int rem; 27814f7d2cdfSDaniel Borkmann 2782c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 27834f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 27844f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2785253683bbSDavid Howells err = -EINVAL; 2786c02db8c6SChris Wright goto errout; 2787253683bbSDavid Howells } 27888cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 27898cb08174SJohannes Berg attr, 27908cb08174SJohannes Berg ifla_vf_policy, 27918cb08174SJohannes Berg NULL); 27924f7d2cdfSDaniel Borkmann if (err < 0) 27934f7d2cdfSDaniel Borkmann goto errout; 27944f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2795ebc08a6fSWilliams, Mitch A if (err < 0) 2796ebc08a6fSWilliams, Mitch A goto errout; 2797ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2798ebc08a6fSWilliams, Mitch A } 2799ebc08a6fSWilliams, Mitch A } 28000157f60cSPatrick McHardy err = 0; 28010157f60cSPatrick McHardy 280257b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 280357b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 280457b61080SScott Feldman struct nlattr *attr; 280557b61080SScott Feldman int vf; 280657b61080SScott Feldman int rem; 280757b61080SScott Feldman 280857b61080SScott Feldman err = -EOPNOTSUPP; 280957b61080SScott Feldman if (!ops->ndo_set_vf_port) 281057b61080SScott Feldman goto errout; 281157b61080SScott Feldman 281257b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2813035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2814035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2815035d210fSDaniel Borkmann err = -EINVAL; 2816035d210fSDaniel Borkmann goto errout; 2817035d210fSDaniel Borkmann } 28188cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 28198cb08174SJohannes Berg attr, 28208cb08174SJohannes Berg ifla_port_policy, 28218cb08174SJohannes Berg NULL); 282257b61080SScott Feldman if (err < 0) 282357b61080SScott Feldman goto errout; 282457b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 282557b61080SScott Feldman err = -EOPNOTSUPP; 282657b61080SScott Feldman goto errout; 282757b61080SScott Feldman } 282857b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 282957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 283057b61080SScott Feldman if (err < 0) 283157b61080SScott Feldman goto errout; 2832ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 283357b61080SScott Feldman } 283457b61080SScott Feldman } 283557b61080SScott Feldman err = 0; 283657b61080SScott Feldman 283757b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 283857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 283957b61080SScott Feldman 28408cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 28418cb08174SJohannes Berg tb[IFLA_PORT_SELF], 28428cb08174SJohannes Berg ifla_port_policy, NULL); 284357b61080SScott Feldman if (err < 0) 284457b61080SScott Feldman goto errout; 284557b61080SScott Feldman 284657b61080SScott Feldman err = -EOPNOTSUPP; 284757b61080SScott Feldman if (ops->ndo_set_vf_port) 284857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 284957b61080SScott Feldman if (err < 0) 285057b61080SScott Feldman goto errout; 2851ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 285257b61080SScott Feldman } 2853f8ff182cSThomas Graf 2854f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2855f8ff182cSThomas Graf struct nlattr *af; 2856f8ff182cSThomas Graf int rem; 2857f8ff182cSThomas Graf 2858f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2859f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2860f8ff182cSThomas Graf 28615fa85a09SFlorian Westphal rcu_read_lock(); 28625fa85a09SFlorian Westphal 2863058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2864f8ff182cSThomas Graf 2865cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 28665fa85a09SFlorian Westphal if (err < 0) { 28675fa85a09SFlorian Westphal rcu_read_unlock(); 2868f8ff182cSThomas Graf goto errout; 28695fa85a09SFlorian Westphal } 2870f8ff182cSThomas Graf 28715fa85a09SFlorian Westphal rcu_read_unlock(); 2872ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2873f8ff182cSThomas Graf } 2874f8ff182cSThomas Graf } 287557b61080SScott Feldman err = 0; 287657b61080SScott Feldman 2877*829eb208SRoopa Prabhu if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 2878*829eb208SRoopa Prabhu err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 2879*829eb208SRoopa Prabhu tb[IFLA_PROTO_DOWN_REASON], extack); 288088d6378bSAnuradha Karuppiah if (err) 288188d6378bSAnuradha Karuppiah goto errout; 288288d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 288388d6378bSAnuradha Karuppiah } 288488d6378bSAnuradha Karuppiah 2885d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2886d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 288785de8576SDaniel Borkmann u32 xdp_flags = 0; 2888d1fdd913SBrenden Blanco 28898cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 28908cb08174SJohannes Berg tb[IFLA_XDP], 2891fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2892d1fdd913SBrenden Blanco if (err < 0) 2893d1fdd913SBrenden Blanco goto errout; 2894d1fdd913SBrenden Blanco 289558038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2896262d8625SBrenden Blanco err = -EINVAL; 2897262d8625SBrenden Blanco goto errout; 2898262d8625SBrenden Blanco } 289985de8576SDaniel Borkmann 290085de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 290185de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 290285de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 290385de8576SDaniel Borkmann err = -EINVAL; 290485de8576SDaniel Borkmann goto errout; 290585de8576SDaniel Borkmann } 2906ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 29070489df9aSDaniel Borkmann err = -EINVAL; 29080489df9aSDaniel Borkmann goto errout; 29090489df9aSDaniel Borkmann } 291085de8576SDaniel Borkmann } 291185de8576SDaniel Borkmann 2912d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 291392234c8fSToke Høiland-Jørgensen int expected_fd = -1; 291492234c8fSToke Høiland-Jørgensen 291592234c8fSToke Høiland-Jørgensen if (xdp_flags & XDP_FLAGS_REPLACE) { 291692234c8fSToke Høiland-Jørgensen if (!xdp[IFLA_XDP_EXPECTED_FD]) { 291792234c8fSToke Høiland-Jørgensen err = -EINVAL; 291892234c8fSToke Høiland-Jørgensen goto errout; 291992234c8fSToke Høiland-Jørgensen } 292092234c8fSToke Høiland-Jørgensen expected_fd = 292192234c8fSToke Høiland-Jørgensen nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 292292234c8fSToke Høiland-Jørgensen } 292392234c8fSToke Høiland-Jørgensen 2924ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 292585de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 292692234c8fSToke Høiland-Jørgensen expected_fd, 292785de8576SDaniel Borkmann xdp_flags); 2928d1fdd913SBrenden Blanco if (err) 2929d1fdd913SBrenden Blanco goto errout; 2930d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2931d1fdd913SBrenden Blanco } 2932d1fdd913SBrenden Blanco } 2933d1fdd913SBrenden Blanco 29340157f60cSPatrick McHardy errout: 2935ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 293664ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2937ba998906SNicolas Dichtel netdev_state_change(dev); 2938ba998906SNicolas Dichtel 2939ba998906SNicolas Dichtel if (err < 0) 2940e87cc472SJoe 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", 2941e87cc472SJoe Perches dev->name); 2942ba998906SNicolas Dichtel } 29430157f60cSPatrick McHardy 29440157f60cSPatrick McHardy return err; 29450157f60cSPatrick McHardy } 29460157f60cSPatrick McHardy 2947cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 2948cc6090e9SJiri Pirko struct nlattr *ifname_attr, 294976c9ac0eSJiri Pirko struct nlattr *altifname_attr, 2950cc6090e9SJiri Pirko char *ifname) 2951cc6090e9SJiri Pirko { 295276c9ac0eSJiri Pirko char buffer[ALTIFNAMSIZ]; 2953cc6090e9SJiri Pirko 2954cc6090e9SJiri Pirko if (!ifname) { 2955cc6090e9SJiri Pirko ifname = buffer; 2956cc6090e9SJiri Pirko if (ifname_attr) 2957cc6090e9SJiri Pirko nla_strlcpy(ifname, ifname_attr, IFNAMSIZ); 295876c9ac0eSJiri Pirko else if (altifname_attr) 295976c9ac0eSJiri Pirko nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ); 2960cc6090e9SJiri Pirko else 2961cc6090e9SJiri Pirko return NULL; 2962cc6090e9SJiri Pirko } 2963cc6090e9SJiri Pirko 2964cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 2965cc6090e9SJiri Pirko } 2966cc6090e9SJiri Pirko 2967c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2968c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2969da5e0494SThomas Graf { 29703b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2971da5e0494SThomas Graf struct ifinfomsg *ifm; 2972da5e0494SThomas Graf struct net_device *dev; 29730157f60cSPatrick McHardy int err; 2974da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 29751da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 29761da177e4SLinus Torvalds 29778cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 29788cb08174SJohannes Berg ifla_policy, extack); 2979da5e0494SThomas Graf if (err < 0) 2980da5e0494SThomas Graf goto errout; 29811da177e4SLinus Torvalds 29824ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 29834ff66caeSChristian Brauner if (err < 0) 29844ff66caeSChristian Brauner goto errout; 29854ff66caeSChristian Brauner 29865176f91eSThomas Graf if (tb[IFLA_IFNAME]) 29875176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 298878e5b891SPatrick McHardy else 298978e5b891SPatrick McHardy ifname[0] = '\0'; 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds err = -EINVAL; 2992da5e0494SThomas Graf ifm = nlmsg_data(nlh); 299351055be8SPatrick McHardy if (ifm->ifi_index > 0) 2994a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 299576c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 299676c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 2997da5e0494SThomas Graf else 2998da5e0494SThomas Graf goto errout; 29991da177e4SLinus Torvalds 3000da5e0494SThomas Graf if (dev == NULL) { 3001da5e0494SThomas Graf err = -ENODEV; 3002da5e0494SThomas Graf goto errout; 3003da5e0494SThomas Graf } 30041da177e4SLinus Torvalds 3005ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 3006da5e0494SThomas Graf errout: 30071da177e4SLinus Torvalds return err; 30081da177e4SLinus Torvalds } 30091da177e4SLinus Torvalds 301066400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 301166400d54SWANG Cong { 301266400d54SWANG Cong struct net_device *dev, *aux; 301366400d54SWANG Cong LIST_HEAD(list_kill); 301466400d54SWANG Cong bool found = false; 301566400d54SWANG Cong 301666400d54SWANG Cong if (!group) 301766400d54SWANG Cong return -EPERM; 301866400d54SWANG Cong 301966400d54SWANG Cong for_each_netdev(net, dev) { 302066400d54SWANG Cong if (dev->group == group) { 302166400d54SWANG Cong const struct rtnl_link_ops *ops; 302266400d54SWANG Cong 302366400d54SWANG Cong found = true; 302466400d54SWANG Cong ops = dev->rtnl_link_ops; 302566400d54SWANG Cong if (!ops || !ops->dellink) 302666400d54SWANG Cong return -EOPNOTSUPP; 302766400d54SWANG Cong } 302866400d54SWANG Cong } 302966400d54SWANG Cong 303066400d54SWANG Cong if (!found) 303166400d54SWANG Cong return -ENODEV; 303266400d54SWANG Cong 303366400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 303466400d54SWANG Cong if (dev->group == group) { 303566400d54SWANG Cong const struct rtnl_link_ops *ops; 303666400d54SWANG Cong 303766400d54SWANG Cong ops = dev->rtnl_link_ops; 303866400d54SWANG Cong ops->dellink(dev, &list_kill); 303966400d54SWANG Cong } 304066400d54SWANG Cong } 304166400d54SWANG Cong unregister_netdevice_many(&list_kill); 304266400d54SWANG Cong 304366400d54SWANG Cong return 0; 304466400d54SWANG Cong } 304566400d54SWANG Cong 3046614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 3047614732eaSThomas Graf { 3048614732eaSThomas Graf const struct rtnl_link_ops *ops; 3049614732eaSThomas Graf LIST_HEAD(list_kill); 3050614732eaSThomas Graf 3051614732eaSThomas Graf ops = dev->rtnl_link_ops; 3052614732eaSThomas Graf if (!ops || !ops->dellink) 3053614732eaSThomas Graf return -EOPNOTSUPP; 3054614732eaSThomas Graf 3055614732eaSThomas Graf ops->dellink(dev, &list_kill); 3056614732eaSThomas Graf unregister_netdevice_many(&list_kill); 3057614732eaSThomas Graf 3058614732eaSThomas Graf return 0; 3059614732eaSThomas Graf } 3060614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 3061614732eaSThomas Graf 3062c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3063c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 306438f7b870SPatrick McHardy { 30653b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3066b61ad68aSChristian Brauner struct net *tgt_net = net; 3067b61ad68aSChristian Brauner struct net_device *dev = NULL; 306838f7b870SPatrick McHardy struct ifinfomsg *ifm; 306938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 307038f7b870SPatrick McHardy int err; 3071b61ad68aSChristian Brauner int netnsid = -1; 307238f7b870SPatrick McHardy 30738cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 30748cb08174SJohannes Berg ifla_policy, extack); 307538f7b870SPatrick McHardy if (err < 0) 307638f7b870SPatrick McHardy return err; 307738f7b870SPatrick McHardy 30784ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 30794ff66caeSChristian Brauner if (err < 0) 30804ff66caeSChristian Brauner return err; 30814ff66caeSChristian Brauner 30827e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 30837e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3084c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3085b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 3086b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 3087b61ad68aSChristian Brauner } 3088b61ad68aSChristian Brauner 3089b61ad68aSChristian Brauner err = -EINVAL; 309038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 309138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3092b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 309376c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 309476c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 309576c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 309666400d54SWANG Cong else if (tb[IFLA_GROUP]) 3097b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 309838f7b870SPatrick McHardy else 3099b61ad68aSChristian Brauner goto out; 310038f7b870SPatrick McHardy 3101b61ad68aSChristian Brauner if (!dev) { 3102b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 3103b61ad68aSChristian Brauner err = -ENODEV; 310438f7b870SPatrick McHardy 3105b61ad68aSChristian Brauner goto out; 3106b61ad68aSChristian Brauner } 3107b61ad68aSChristian Brauner 3108b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 3109b61ad68aSChristian Brauner 3110b61ad68aSChristian Brauner out: 3111b61ad68aSChristian Brauner if (netnsid >= 0) 3112b61ad68aSChristian Brauner put_net(tgt_net); 3113b61ad68aSChristian Brauner 3114b61ad68aSChristian Brauner return err; 311538f7b870SPatrick McHardy } 311638f7b870SPatrick McHardy 31173729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 31183729d502SPatrick McHardy { 31193729d502SPatrick McHardy unsigned int old_flags; 31203729d502SPatrick McHardy int err; 31213729d502SPatrick McHardy 31223729d502SPatrick McHardy old_flags = dev->flags; 31233729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 31246d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 31256d040321SPetr Machata NULL); 31263729d502SPatrick McHardy if (err < 0) 31273729d502SPatrick McHardy return err; 31283729d502SPatrick McHardy } 31293729d502SPatrick McHardy 31308d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 313156a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 31328d356b89SRoopa Prabhu } else { 31333729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 3134a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 31358d356b89SRoopa Prabhu } 31363729d502SPatrick McHardy return 0; 31373729d502SPatrick McHardy } 31383729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 31393729d502SPatrick McHardy 3140d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3141d0522f1cSDavid Ahern unsigned char name_assign_type, 3142d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3143d0522f1cSDavid Ahern struct nlattr *tb[], 3144d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3145e7199288SPavel Emelianov { 3146e7199288SPavel Emelianov struct net_device *dev; 3147d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3148d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3149e7199288SPavel Emelianov 315076ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 315176ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 315276ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3153d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 315476ff5cc9SJiri Pirko 315576ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 315676ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 315776ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3158d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3159efacb309Sstephen hemminger 3160d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3161d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 31620e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3163d0522f1cSDavid Ahern } 31640e1d6ecaSEric Dumazet 3165d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3166d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 31670e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3168d0522f1cSDavid Ahern } 31690e1d6ecaSEric Dumazet 31705517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 3171c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 3172e7199288SPavel Emelianov if (!dev) 3173d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3174e7199288SPavel Emelianov 317581adee47SEric W. Biederman dev_net_set(dev, net); 317681adee47SEric W. Biederman dev->rtnl_link_ops = ops; 31773729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 317881adee47SEric W. Biederman 3179d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3180d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3181d836f5c6SEric Dumazet int err; 3182d836f5c6SEric Dumazet 3183d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3184d836f5c6SEric Dumazet if (err) { 3185d836f5c6SEric Dumazet free_netdev(dev); 3186d836f5c6SEric Dumazet return ERR_PTR(err); 3187d836f5c6SEric Dumazet } 3188d836f5c6SEric Dumazet dev->mtu = mtu; 3189d836f5c6SEric Dumazet } 31902afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3191e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 3192e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 31932afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 31942afb9b53SJiri Pirko } 3195e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3196e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3197e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3198e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3199e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3200e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 320193b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3202e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3203e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3204ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3205ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 320646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 320746e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 320846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 3209a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 3210e7199288SPavel Emelianov 3211e7199288SPavel Emelianov return dev; 3212e7199288SPavel Emelianov } 3213e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3214e7199288SPavel Emelianov 321590f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 321690f62cf3SEric W. Biederman struct net *net, int group, 3217e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3218ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3219e7ed828fSVlad Dogaru struct nlattr **tb) 3220e7ed828fSVlad Dogaru { 3221d079535dSWANG Cong struct net_device *dev, *aux; 3222e7ed828fSVlad Dogaru int err; 3223e7ed828fSVlad Dogaru 3224d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3225e7ed828fSVlad Dogaru if (dev->group == group) { 3226ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 3227e7ed828fSVlad Dogaru if (err < 0) 3228e7ed828fSVlad Dogaru return err; 3229e7ed828fSVlad Dogaru } 3230e7ed828fSVlad Dogaru } 3231e7ed828fSVlad Dogaru 3232e7ed828fSVlad Dogaru return 0; 3233e7ed828fSVlad Dogaru } 3234e7ed828fSVlad Dogaru 3235a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3236a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 323738f7b870SPatrick McHardy { 3238420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3239420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 3240420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3241420d0318SJakub Kicinski const struct rtnl_link_ops *m_ops = NULL; 3242420d0318SJakub Kicinski struct net_device *master_dev = NULL; 32433b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 324438f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 324538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 3246420d0318SJakub Kicinski struct net *dest_net, *link_net; 3247420d0318SJakub Kicinski struct nlattr **slave_data; 3248420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3249420d0318SJakub Kicinski struct net_device *dev; 3250420d0318SJakub Kicinski struct ifinfomsg *ifm; 3251420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 3252420d0318SJakub Kicinski struct nlattr **data; 325338f7b870SPatrick McHardy int err; 325438f7b870SPatrick McHardy 325595a5afcaSJohannes Berg #ifdef CONFIG_MODULES 325638f7b870SPatrick McHardy replay: 32578072f085SThomas Graf #endif 32588cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 32598cb08174SJohannes Berg ifla_policy, extack); 326038f7b870SPatrick McHardy if (err < 0) 326138f7b870SPatrick McHardy return err; 326238f7b870SPatrick McHardy 32634ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 32644ff66caeSChristian Brauner if (err < 0) 32654ff66caeSChristian Brauner return err; 32664ff66caeSChristian Brauner 326738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 326838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 326938f7b870SPatrick McHardy else 327038f7b870SPatrick McHardy ifname[0] = '\0'; 327138f7b870SPatrick McHardy 327238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 327338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3274881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 327576c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 327676c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 327738f7b870SPatrick McHardy else 327838f7b870SPatrick McHardy dev = NULL; 327938f7b870SPatrick McHardy 3280ba7d49b1SJiri Pirko if (dev) { 3281ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3282ba7d49b1SJiri Pirko if (master_dev) 3283ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3284ba7d49b1SJiri Pirko } 3285ba7d49b1SJiri Pirko 3286e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 3287e0d087afSEric Dumazet if (err < 0) 32881840bb13SThomas Graf return err; 32891840bb13SThomas Graf 329038f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 32918cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 32928cb08174SJohannes Berg tb[IFLA_LINKINFO], 32938cb08174SJohannes Berg ifla_info_policy, NULL); 329438f7b870SPatrick McHardy if (err < 0) 329538f7b870SPatrick McHardy return err; 329638f7b870SPatrick McHardy } else 329738f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 329838f7b870SPatrick McHardy 329938f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 330038f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 330138f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 330238f7b870SPatrick McHardy } else { 330338f7b870SPatrick McHardy kind[0] = '\0'; 330438f7b870SPatrick McHardy ops = NULL; 330538f7b870SPatrick McHardy } 330638f7b870SPatrick McHardy 3307420d0318SJakub Kicinski data = NULL; 330838f7b870SPatrick McHardy if (ops) { 3309ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3310ccf8dbcdSKees Cook return -EINVAL; 3311ccf8dbcdSKees Cook 331238f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 33138cb08174SJohannes Berg err = nla_parse_nested_deprecated(attr, ops->maxtype, 331438f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 331568d57f3bSDavid Ahern ops->policy, extack); 331638f7b870SPatrick McHardy if (err < 0) 331738f7b870SPatrick McHardy return err; 331838f7b870SPatrick McHardy data = attr; 331938f7b870SPatrick McHardy } 332038f7b870SPatrick McHardy if (ops->validate) { 3321a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 332238f7b870SPatrick McHardy if (err < 0) 332338f7b870SPatrick McHardy return err; 332438f7b870SPatrick McHardy } 332538f7b870SPatrick McHardy } 332638f7b870SPatrick McHardy 3327420d0318SJakub Kicinski slave_data = NULL; 3328ba7d49b1SJiri Pirko if (m_ops) { 3329af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3330ccf8dbcdSKees Cook return -EINVAL; 3331ccf8dbcdSKees Cook 3332ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3333ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 33348cb08174SJohannes Berg err = nla_parse_nested_deprecated(slave_attr, 33358cb08174SJohannes Berg m_ops->slave_maxtype, 3336ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 33378cb08174SJohannes Berg m_ops->slave_policy, 33388cb08174SJohannes Berg extack); 3339ba7d49b1SJiri Pirko if (err < 0) 3340ba7d49b1SJiri Pirko return err; 3341ba7d49b1SJiri Pirko slave_data = slave_attr; 3342ba7d49b1SJiri Pirko } 3343ba7d49b1SJiri Pirko } 3344ba7d49b1SJiri Pirko 334538f7b870SPatrick McHardy if (dev) { 334690c325e3SNicolas Dichtel int status = 0; 334738f7b870SPatrick McHardy 334838f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 334938f7b870SPatrick McHardy return -EEXIST; 335038f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 335138f7b870SPatrick McHardy return -EOPNOTSUPP; 335238f7b870SPatrick McHardy 335338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 335438f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 335538f7b870SPatrick McHardy !ops->changelink) 335638f7b870SPatrick McHardy return -EOPNOTSUPP; 335738f7b870SPatrick McHardy 3358ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 335938f7b870SPatrick McHardy if (err < 0) 336038f7b870SPatrick McHardy return err; 3361ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 336238f7b870SPatrick McHardy } 336338f7b870SPatrick McHardy 3364ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3365ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3366ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3367ba7d49b1SJiri Pirko 3368420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3369420d0318SJakub Kicinski slave_data, extack); 3370ba7d49b1SJiri Pirko if (err < 0) 3371ba7d49b1SJiri Pirko return err; 3372ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3373ba7d49b1SJiri Pirko } 3374ba7d49b1SJiri Pirko 3375420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 337638f7b870SPatrick McHardy } 337738f7b870SPatrick McHardy 3378ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3379ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 338090f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3381ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3382ddf9f970SJakub Kicinski ifm, extack, tb); 338338f7b870SPatrick McHardy return -ENODEV; 3384ffa934f1SPatrick McHardy } 338538f7b870SPatrick McHardy 3386160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 338738f7b870SPatrick McHardy return -EOPNOTSUPP; 338838f7b870SPatrick McHardy 338938f7b870SPatrick McHardy if (!ops) { 339095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 339138f7b870SPatrick McHardy if (kind[0]) { 339238f7b870SPatrick McHardy __rtnl_unlock(); 339338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 339438f7b870SPatrick McHardy rtnl_lock(); 339538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 339638f7b870SPatrick McHardy if (ops) 339738f7b870SPatrick McHardy goto replay; 339838f7b870SPatrick McHardy } 339938f7b870SPatrick McHardy #endif 340068d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 340138f7b870SPatrick McHardy return -EOPNOTSUPP; 340238f7b870SPatrick McHardy } 340338f7b870SPatrick McHardy 3404b0ab2fabSJiri Pirko if (!ops->setup) 3405b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3406b0ab2fabSJiri Pirko 34075517750fSTom Gundersen if (!ifname[0]) { 340838f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 34095517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 34105517750fSTom Gundersen } 341138f7b870SPatrick McHardy 34125bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 341313ad1774SEric W. Biederman if (IS_ERR(dest_net)) 341413ad1774SEric W. Biederman return PTR_ERR(dest_net); 341513ad1774SEric W. Biederman 3416317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3417317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3418317f4810SNicolas Dichtel 3419317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3420317f4810SNicolas Dichtel if (!link_net) { 342168d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3422317f4810SNicolas Dichtel err = -EINVAL; 3423317f4810SNicolas Dichtel goto out; 3424317f4810SNicolas Dichtel } 342506615bedSEric W. Biederman err = -EPERM; 342606615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 342706615bedSEric W. Biederman goto out; 3428420d0318SJakub Kicinski } else { 3429420d0318SJakub Kicinski link_net = NULL; 3430317f4810SNicolas Dichtel } 3431317f4810SNicolas Dichtel 3432317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3433d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 34349c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3435e7199288SPavel Emelianov err = PTR_ERR(dev); 34369c7dafbfSPavel Emelyanov goto out; 34379c7dafbfSPavel Emelyanov } 34389c7dafbfSPavel Emelyanov 34399c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 34409c7dafbfSPavel Emelyanov 34410e0eee24SCong Wang if (ops->newlink) { 3442420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 34430e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3444e51fb152SCong Wang * and unregister it on failure after registration 3445e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 34460e0eee24SCong Wang */ 3447e51fb152SCong Wang if (err < 0) { 3448e51fb152SCong Wang /* If device is not registered at all, free it now */ 3449cebb6975SWeilong Chen if (dev->reg_state == NETREG_UNINITIALIZED || 3450cebb6975SWeilong Chen dev->reg_state == NETREG_UNREGISTERED) 3451e51fb152SCong Wang free_netdev(dev); 34520e0eee24SCong Wang goto out; 3453e51fb152SCong Wang } 34540e0eee24SCong Wang } else { 34552d85cba2SPatrick McHardy err = register_netdevice(dev); 3456fce9b9beSDan Carpenter if (err < 0) { 345738f7b870SPatrick McHardy free_netdev(dev); 34583729d502SPatrick McHardy goto out; 3459fce9b9beSDan Carpenter } 34600e0eee24SCong Wang } 34613729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 346243638900SDavid S. Miller if (err < 0) 346343638900SDavid S. Miller goto out_unregister; 346443638900SDavid S. Miller if (link_net) { 346543638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 346643638900SDavid S. Miller if (err < 0) 346743638900SDavid S. Miller goto out_unregister; 346843638900SDavid S. Miller } 3469160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3470420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3471160ca014STheuns Verwoerd if (err) 3472160ca014STheuns Verwoerd goto out_unregister; 3473160ca014STheuns Verwoerd } 347443638900SDavid S. Miller out: 347543638900SDavid S. Miller if (link_net) 347643638900SDavid S. Miller put_net(link_net); 347743638900SDavid S. Miller put_net(dest_net); 347843638900SDavid S. Miller return err; 347943638900SDavid S. Miller out_unregister: 34807afb8886SWANG Cong if (ops->newlink) { 34817afb8886SWANG Cong LIST_HEAD(list_kill); 34827afb8886SWANG Cong 34837afb8886SWANG Cong ops->dellink(dev, &list_kill); 34847afb8886SWANG Cong unregister_netdevice_many(&list_kill); 34857afb8886SWANG Cong } else { 34863729d502SPatrick McHardy unregister_netdevice(dev); 34877afb8886SWANG Cong } 3488317f4810SNicolas Dichtel goto out; 3489317f4810SNicolas Dichtel } 349038f7b870SPatrick McHardy 3491a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3492a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3493a2939745SJakub Kicinski { 3494a2939745SJakub Kicinski struct nlattr **attr; 3495a2939745SJakub Kicinski int ret; 3496a2939745SJakub Kicinski 3497a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3498a2939745SJakub Kicinski if (!attr) 3499a2939745SJakub Kicinski return -ENOMEM; 3500a2939745SJakub Kicinski 3501a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3502a2939745SJakub Kicinski kfree(attr); 3503a2939745SJakub Kicinski return ret; 3504a2939745SJakub Kicinski } 3505a2939745SJakub Kicinski 35069b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 35079b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 35089b3757b0SJakub Kicinski struct nlattr **tb, 35099b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 35109b3757b0SJakub Kicinski { 35119b3757b0SJakub Kicinski struct ifinfomsg *ifm; 35129b3757b0SJakub Kicinski int i, err; 35139b3757b0SJakub Kicinski 35149b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 35159b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 35169b3757b0SJakub Kicinski return -EINVAL; 35179b3757b0SJakub Kicinski } 35189b3757b0SJakub Kicinski 35199b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 35208cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 35218cb08174SJohannes Berg ifla_policy, extack); 35229b3757b0SJakub Kicinski 35239b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 35249b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 35259b3757b0SJakub Kicinski ifm->ifi_change) { 35269b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 35279b3757b0SJakub Kicinski return -EINVAL; 35289b3757b0SJakub Kicinski } 35299b3757b0SJakub Kicinski 35308cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 35318cb08174SJohannes Berg ifla_policy, extack); 35329b3757b0SJakub Kicinski if (err) 35339b3757b0SJakub Kicinski return err; 35349b3757b0SJakub Kicinski 35359b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 35369b3757b0SJakub Kicinski if (!tb[i]) 35379b3757b0SJakub Kicinski continue; 35389b3757b0SJakub Kicinski 35399b3757b0SJakub Kicinski switch (i) { 35409b3757b0SJakub Kicinski case IFLA_IFNAME: 354176c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 35429b3757b0SJakub Kicinski case IFLA_EXT_MASK: 35439b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 35449b3757b0SJakub Kicinski break; 35459b3757b0SJakub Kicinski default: 35469b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 35479b3757b0SJakub Kicinski return -EINVAL; 35489b3757b0SJakub Kicinski } 35499b3757b0SJakub Kicinski } 35509b3757b0SJakub Kicinski 35519b3757b0SJakub Kicinski return 0; 35529b3757b0SJakub Kicinski } 35539b3757b0SJakub Kicinski 3554c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3555c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3556711e2c33SJean Tourrilhes { 35573b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 355879e1ad14SJiri Benc struct net *tgt_net = net; 3559b60c5115SThomas Graf struct ifinfomsg *ifm; 3560b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3561b60c5115SThomas Graf struct net_device *dev = NULL; 3562b60c5115SThomas Graf struct sk_buff *nskb; 356379e1ad14SJiri Benc int netnsid = -1; 3564339bf98fSThomas Graf int err; 3565115c9b81SGreg Rose u32 ext_filter_mask = 0; 3566711e2c33SJean Tourrilhes 35679b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3568b60c5115SThomas Graf if (err < 0) 35699918f230SEric Sesterhenn return err; 3570b60c5115SThomas Graf 35714ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 35724ff66caeSChristian Brauner if (err < 0) 35734ff66caeSChristian Brauner return err; 35744ff66caeSChristian Brauner 35757e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 35767e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3577c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 357879e1ad14SJiri Benc if (IS_ERR(tgt_net)) 357979e1ad14SJiri Benc return PTR_ERR(tgt_net); 358079e1ad14SJiri Benc } 358179e1ad14SJiri Benc 3582115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3583115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3584115c9b81SGreg Rose 358579e1ad14SJiri Benc err = -EINVAL; 3586b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3587a3d12891SEric Dumazet if (ifm->ifi_index > 0) 358879e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 358976c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 359076c9ac0eSJiri Pirko dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], 359176c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3592a3d12891SEric Dumazet else 359379e1ad14SJiri Benc goto out; 3594b60c5115SThomas Graf 359579e1ad14SJiri Benc err = -ENODEV; 3596a3d12891SEric Dumazet if (dev == NULL) 359779e1ad14SJiri Benc goto out; 3598a3d12891SEric Dumazet 359979e1ad14SJiri Benc err = -ENOBUFS; 3600115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3601a3d12891SEric Dumazet if (nskb == NULL) 360279e1ad14SJiri Benc goto out; 3603711e2c33SJean Tourrilhes 360479e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 360579e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 360679e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3607d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 360826932566SPatrick McHardy if (err < 0) { 360926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 361026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 361126932566SPatrick McHardy kfree_skb(nskb); 3612a3d12891SEric Dumazet } else 361315e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 361479e1ad14SJiri Benc out: 361579e1ad14SJiri Benc if (netnsid >= 0) 361679e1ad14SJiri Benc put_net(tgt_net); 3617b60c5115SThomas Graf 3618711e2c33SJean Tourrilhes return err; 3619711e2c33SJean Tourrilhes } 3620711e2c33SJean Tourrilhes 362136fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 362236fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 362336fbf1e5SJiri Pirko { 362436fbf1e5SJiri Pirko char *alt_ifname; 362536fbf1e5SJiri Pirko int err; 362636fbf1e5SJiri Pirko 362736fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 362836fbf1e5SJiri Pirko if (err) 362936fbf1e5SJiri Pirko return err; 363036fbf1e5SJiri Pirko 363144bfa9c5SEric Dumazet alt_ifname = nla_strdup(attr, GFP_KERNEL); 363236fbf1e5SJiri Pirko if (!alt_ifname) 363336fbf1e5SJiri Pirko return -ENOMEM; 363444bfa9c5SEric Dumazet 363544bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 363636fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 363744bfa9c5SEric Dumazet if (!err) 363844bfa9c5SEric Dumazet alt_ifname = NULL; 363936fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 364036fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 364136fbf1e5SJiri Pirko } else { 364244bfa9c5SEric Dumazet WARN_ON_ONCE(1); 364344bfa9c5SEric Dumazet err = -EINVAL; 364436fbf1e5SJiri Pirko } 364536fbf1e5SJiri Pirko 364644bfa9c5SEric Dumazet kfree(alt_ifname); 364744bfa9c5SEric Dumazet if (!err) 364836fbf1e5SJiri Pirko *changed = true; 364944bfa9c5SEric Dumazet return err; 365036fbf1e5SJiri Pirko } 365136fbf1e5SJiri Pirko 365236fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 365336fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 365436fbf1e5SJiri Pirko { 365536fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 365636fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 365736fbf1e5SJiri Pirko struct net_device *dev; 365836fbf1e5SJiri Pirko struct ifinfomsg *ifm; 365936fbf1e5SJiri Pirko bool changed = false; 366036fbf1e5SJiri Pirko struct nlattr *attr; 366136fbf1e5SJiri Pirko int err, rem; 366236fbf1e5SJiri Pirko 366336fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 366436fbf1e5SJiri Pirko if (err) 366536fbf1e5SJiri Pirko return err; 366636fbf1e5SJiri Pirko 366736fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 366836fbf1e5SJiri Pirko if (err) 366936fbf1e5SJiri Pirko return err; 367036fbf1e5SJiri Pirko 367136fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3672cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 367336fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 367476c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 367576c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 367676c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3677cc6090e9SJiri Pirko else 367836fbf1e5SJiri Pirko return -EINVAL; 367936fbf1e5SJiri Pirko 368036fbf1e5SJiri Pirko if (!dev) 368136fbf1e5SJiri Pirko return -ENODEV; 368236fbf1e5SJiri Pirko 368336fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 368436fbf1e5SJiri Pirko return 0; 368536fbf1e5SJiri Pirko 368636fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 368736fbf1e5SJiri Pirko switch (nla_type(attr)) { 368836fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 368936fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 369036fbf1e5SJiri Pirko if (err) 369136fbf1e5SJiri Pirko return err; 369236fbf1e5SJiri Pirko break; 369336fbf1e5SJiri Pirko } 369436fbf1e5SJiri Pirko } 369536fbf1e5SJiri Pirko 369636fbf1e5SJiri Pirko if (changed) 369736fbf1e5SJiri Pirko netdev_state_change(dev); 369836fbf1e5SJiri Pirko return 0; 369936fbf1e5SJiri Pirko } 370036fbf1e5SJiri Pirko 370136fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 370236fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 370336fbf1e5SJiri Pirko { 370436fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 370536fbf1e5SJiri Pirko } 370636fbf1e5SJiri Pirko 370736fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 370836fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 370936fbf1e5SJiri Pirko { 371036fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 371136fbf1e5SJiri Pirko } 371236fbf1e5SJiri Pirko 3713115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3714c7ac8679SGreg Rose { 3715115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3716115c9b81SGreg Rose struct net_device *dev; 3717115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3718115c9b81SGreg Rose u32 ext_filter_mask = 0; 3719115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3720e5eca6d4SMichal Schmidt int hdrlen; 3721115c9b81SGreg Rose 3722e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3723e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3724e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3725e5eca6d4SMichal Schmidt 37268cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3727115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3728115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3729a4b64fbeSEric Dumazet } 3730115c9b81SGreg Rose 3731115c9b81SGreg Rose if (!ext_filter_mask) 3732115c9b81SGreg Rose return NLMSG_GOODSIZE; 3733115c9b81SGreg Rose /* 3734115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3735115c9b81SGreg Rose * buffer size based upon the filter mask. 3736115c9b81SGreg Rose */ 37376853dd48SFlorian Westphal rcu_read_lock(); 37386853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3739115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3740115c9b81SGreg Rose if_nlmsg_size(dev, 3741115c9b81SGreg Rose ext_filter_mask)); 3742115c9b81SGreg Rose } 37436853dd48SFlorian Westphal rcu_read_unlock(); 3744115c9b81SGreg Rose 374593af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3746c7ac8679SGreg Rose } 3747c7ac8679SGreg Rose 374842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 37491da177e4SLinus Torvalds { 37501da177e4SLinus Torvalds int idx; 37511da177e4SLinus Torvalds int s_idx = cb->family; 375287ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3753c63586dcSDavid Ahern int ret = 0; 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds if (s_idx == 0) 37561da177e4SLinus Torvalds s_idx = 1; 37576853dd48SFlorian Westphal 375825239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3759addf9b90SFlorian Westphal struct rtnl_link **tab; 3760addf9b90SFlorian Westphal struct rtnl_link *link; 37616853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 37626853dd48SFlorian Westphal 37631da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 37641da177e4SLinus Torvalds continue; 37656853dd48SFlorian Westphal 3766addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 37671da177e4SLinus Torvalds continue; 37686853dd48SFlorian Westphal 3769addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3770addf9b90SFlorian Westphal if (!tab) 3771addf9b90SFlorian Westphal continue; 3772addf9b90SFlorian Westphal 3773addf9b90SFlorian Westphal link = tab[type]; 3774addf9b90SFlorian Westphal if (!link) 3775addf9b90SFlorian Westphal continue; 3776addf9b90SFlorian Westphal 3777addf9b90SFlorian Westphal dumpit = link->dumpit; 37786853dd48SFlorian Westphal if (!dumpit) 37796853dd48SFlorian Westphal continue; 37806853dd48SFlorian Westphal 37810465277fSNicolas Dichtel if (idx > s_idx) { 37821da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 37830465277fSNicolas Dichtel cb->prev_seq = 0; 37840465277fSNicolas Dichtel cb->seq = 0; 37850465277fSNicolas Dichtel } 3786c63586dcSDavid Ahern ret = dumpit(skb, cb); 37875e1acb4aSAlexey Kodanev if (ret) 37881da177e4SLinus Torvalds break; 37891da177e4SLinus Torvalds } 37901da177e4SLinus Torvalds cb->family = idx; 37911da177e4SLinus Torvalds 3792c63586dcSDavid Ahern return skb->len ? : ret; 37931da177e4SLinus Torvalds } 37941da177e4SLinus Torvalds 3795395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 37963d3ea5afSVlad Yasevich unsigned int change, 379738e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 379838e01b30SNicolas Dichtel int new_ifindex) 37991da177e4SLinus Torvalds { 3800c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 38011da177e4SLinus Torvalds struct sk_buff *skb; 38020ec6d3f4SThomas Graf int err = -ENOBUFS; 3803c7ac8679SGreg Rose size_t if_info_size; 38041da177e4SLinus Torvalds 38057f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 38060ec6d3f4SThomas Graf if (skb == NULL) 38070ec6d3f4SThomas Graf goto errout; 38081da177e4SLinus Torvalds 380979e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 381079e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 3811d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 381226932566SPatrick McHardy if (err < 0) { 381326932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 381426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 381526932566SPatrick McHardy kfree_skb(skb); 381626932566SPatrick McHardy goto errout; 381726932566SPatrick McHardy } 3818395eea6cSMahesh Bandewar return skb; 38190ec6d3f4SThomas Graf errout: 38200ec6d3f4SThomas Graf if (err < 0) 38214b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3822395eea6cSMahesh Bandewar return NULL; 3823395eea6cSMahesh Bandewar } 3824395eea6cSMahesh Bandewar 3825395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3826395eea6cSMahesh Bandewar { 3827395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3828395eea6cSMahesh Bandewar 3829395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3830395eea6cSMahesh Bandewar } 3831395eea6cSMahesh Bandewar 38323d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 38333d3ea5afSVlad Yasevich unsigned int change, u32 event, 383438e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3835395eea6cSMahesh Bandewar { 3836395eea6cSMahesh Bandewar struct sk_buff *skb; 3837395eea6cSMahesh Bandewar 3838ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3839ed2a80abSNicolas Dichtel return; 3840ed2a80abSNicolas Dichtel 384138e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 384238e01b30SNicolas Dichtel new_ifindex); 3843395eea6cSMahesh Bandewar if (skb) 3844395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 38451da177e4SLinus Torvalds } 38463d3ea5afSVlad Yasevich 38473d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 38483d3ea5afSVlad Yasevich gfp_t flags) 38493d3ea5afSVlad Yasevich { 385038e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 385138e01b30SNicolas Dichtel NULL, 0); 38523d3ea5afSVlad Yasevich } 38531da177e4SLinus Torvalds 38546621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 385538e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 38566621dd29SNicolas Dichtel { 38576621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 385838e01b30SNicolas Dichtel new_nsid, new_ifindex); 38596621dd29SNicolas Dichtel } 38606621dd29SNicolas Dichtel 3861d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3862d83b0603SJohn Fastabend struct net_device *dev, 38631e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 38641c104a6bSNicolas Dichtel int type, unsigned int flags, 3865b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3866d83b0603SJohn Fastabend { 3867d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3868d83b0603SJohn Fastabend struct ndmsg *ndm; 3869d83b0603SJohn Fastabend 38701c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3871d83b0603SJohn Fastabend if (!nlh) 3872d83b0603SJohn Fastabend return -EMSGSIZE; 3873d83b0603SJohn Fastabend 3874d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3875d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3876d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3877d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3878d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3879d83b0603SJohn Fastabend ndm->ndm_type = 0; 3880d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3881b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3882d83b0603SJohn Fastabend 3883d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3884d83b0603SJohn Fastabend goto nla_put_failure; 38851e53d5bbSHubert Sokolowski if (vid) 38861e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 38871e53d5bbSHubert Sokolowski goto nla_put_failure; 3888d83b0603SJohn Fastabend 3889053c095aSJohannes Berg nlmsg_end(skb, nlh); 3890053c095aSJohannes Berg return 0; 3891d83b0603SJohn Fastabend 3892d83b0603SJohn Fastabend nla_put_failure: 3893d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3894d83b0603SJohn Fastabend return -EMSGSIZE; 3895d83b0603SJohn Fastabend } 3896d83b0603SJohn Fastabend 38973ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 38983ff661c3SJohn Fastabend { 3899f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3900f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3901f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3902f82ef3e1SSabrina Dubroca 0; 39033ff661c3SJohn Fastabend } 39043ff661c3SJohn Fastabend 3905b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3906b3379041SHubert Sokolowski u16 ndm_state) 39073ff661c3SJohn Fastabend { 39083ff661c3SJohn Fastabend struct net *net = dev_net(dev); 39093ff661c3SJohn Fastabend struct sk_buff *skb; 39103ff661c3SJohn Fastabend int err = -ENOBUFS; 39113ff661c3SJohn Fastabend 39123ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 39133ff661c3SJohn Fastabend if (!skb) 39143ff661c3SJohn Fastabend goto errout; 39153ff661c3SJohn Fastabend 39161e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3917b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 39183ff661c3SJohn Fastabend if (err < 0) { 39193ff661c3SJohn Fastabend kfree_skb(skb); 39203ff661c3SJohn Fastabend goto errout; 39213ff661c3SJohn Fastabend } 39223ff661c3SJohn Fastabend 39233ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 39243ff661c3SJohn Fastabend return; 39253ff661c3SJohn Fastabend errout: 39263ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 39273ff661c3SJohn Fastabend } 39283ff661c3SJohn Fastabend 3929a986967eSBart Van Assche /* 3930090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3931090096bfSVlad Yasevich */ 3932090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3933090096bfSVlad Yasevich struct nlattr *tb[], 3934090096bfSVlad Yasevich struct net_device *dev, 3935f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3936090096bfSVlad Yasevich u16 flags) 3937090096bfSVlad Yasevich { 3938090096bfSVlad Yasevich int err = -EINVAL; 3939090096bfSVlad Yasevich 3940090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3941090096bfSVlad Yasevich * implement its own handler for this. 3942090096bfSVlad Yasevich */ 3943090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3944090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3945090096bfSVlad Yasevich return err; 3946090096bfSVlad Yasevich } 3947090096bfSVlad Yasevich 394865891feaSOr Gerlitz if (vid) { 394965891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 395065891feaSOr Gerlitz return err; 395165891feaSOr Gerlitz } 395265891feaSOr Gerlitz 3953090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3954090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3955090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3956090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3957090096bfSVlad Yasevich 3958090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3959090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3960090096bfSVlad Yasevich err = 0; 3961090096bfSVlad Yasevich 3962090096bfSVlad Yasevich return err; 3963090096bfSVlad Yasevich } 3964090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3965090096bfSVlad Yasevich 3966b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3967b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3968f6f6424bSJiri Pirko { 3969f6f6424bSJiri Pirko u16 vid = 0; 3970f6f6424bSJiri Pirko 3971f6f6424bSJiri Pirko if (vlan_attr) { 3972f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3973b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3974f6f6424bSJiri Pirko return -EINVAL; 3975f6f6424bSJiri Pirko } 3976f6f6424bSJiri Pirko 3977f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3978f6f6424bSJiri Pirko 3979f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3980b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3981f6f6424bSJiri Pirko return -EINVAL; 3982f6f6424bSJiri Pirko } 3983f6f6424bSJiri Pirko } 3984f6f6424bSJiri Pirko *p_vid = vid; 3985f6f6424bSJiri Pirko return 0; 3986f6f6424bSJiri Pirko } 3987f6f6424bSJiri Pirko 3988c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3989c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 399077162022SJohn Fastabend { 399177162022SJohn Fastabend struct net *net = sock_net(skb->sk); 399277162022SJohn Fastabend struct ndmsg *ndm; 399377162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 399477162022SJohn Fastabend struct net_device *dev; 399577162022SJohn Fastabend u8 *addr; 3996f6f6424bSJiri Pirko u16 vid; 399777162022SJohn Fastabend int err; 399877162022SJohn Fastabend 39998cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 40008cb08174SJohannes Berg extack); 400177162022SJohn Fastabend if (err < 0) 400277162022SJohn Fastabend return err; 400377162022SJohn Fastabend 400477162022SJohn Fastabend ndm = nlmsg_data(nlh); 400577162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4006b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 400777162022SJohn Fastabend return -EINVAL; 400877162022SJohn Fastabend } 400977162022SJohn Fastabend 401077162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 401177162022SJohn Fastabend if (dev == NULL) { 4012b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 401377162022SJohn Fastabend return -ENODEV; 401477162022SJohn Fastabend } 401577162022SJohn Fastabend 401677162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4017b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 401877162022SJohn Fastabend return -EINVAL; 401977162022SJohn Fastabend } 402077162022SJohn Fastabend 4021da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4022da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4023da715775SIdo Schimmel return -EINVAL; 4024da715775SIdo Schimmel } 4025da715775SIdo Schimmel 402677162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 402777162022SJohn Fastabend 4028b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4029f6f6424bSJiri Pirko if (err) 4030f6f6424bSJiri Pirko return err; 4031f6f6424bSJiri Pirko 403277162022SJohn Fastabend err = -EOPNOTSUPP; 403377162022SJohn Fastabend 403477162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 403577162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 40362e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4037898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4038898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 4039898e5061SJiri Pirko 4040f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 404187b0984eSPetr Machata nlh->nlmsg_flags, extack); 404277162022SJohn Fastabend if (err) 404377162022SJohn Fastabend goto out; 404477162022SJohn Fastabend else 404577162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 404677162022SJohn Fastabend } 404777162022SJohn Fastabend 404877162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4049090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 4050090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 4051090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4052f6f6424bSJiri Pirko vid, 405387b0984eSPetr Machata nlh->nlmsg_flags, 405487b0984eSPetr Machata extack); 4055090096bfSVlad Yasevich else 4056f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 405777162022SJohn Fastabend nlh->nlmsg_flags); 405877162022SJohn Fastabend 40593ff661c3SJohn Fastabend if (!err) { 4060b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4061b3379041SHubert Sokolowski ndm->ndm_state); 406277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 406377162022SJohn Fastabend } 40643ff661c3SJohn Fastabend } 406577162022SJohn Fastabend out: 406677162022SJohn Fastabend return err; 406777162022SJohn Fastabend } 406877162022SJohn Fastabend 4069a986967eSBart Van Assche /* 4070090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4071090096bfSVlad Yasevich */ 4072090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 4073090096bfSVlad Yasevich struct nlattr *tb[], 4074090096bfSVlad Yasevich struct net_device *dev, 4075f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 4076090096bfSVlad Yasevich { 4077c8a89c4aSAlexander Duyck int err = -EINVAL; 4078090096bfSVlad Yasevich 4079090096bfSVlad Yasevich /* If aging addresses are supported device will need to 4080090096bfSVlad Yasevich * implement its own handler for this. 4081090096bfSVlad Yasevich */ 408264535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 4083090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 4084c8a89c4aSAlexander Duyck return err; 4085090096bfSVlad Yasevich } 4086090096bfSVlad Yasevich 4087090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4088090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 4089090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 4090090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 4091090096bfSVlad Yasevich 4092090096bfSVlad Yasevich return err; 4093090096bfSVlad Yasevich } 4094090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 4095090096bfSVlad Yasevich 4096c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4097c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 409877162022SJohn Fastabend { 409977162022SJohn Fastabend struct net *net = sock_net(skb->sk); 410077162022SJohn Fastabend struct ndmsg *ndm; 41011690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 410277162022SJohn Fastabend struct net_device *dev; 410377162022SJohn Fastabend __u8 *addr; 41047d311801SColin Ian King int err; 4105f6f6424bSJiri Pirko u16 vid; 410677162022SJohn Fastabend 410790f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 41081690be63SVlad Yasevich return -EPERM; 41091690be63SVlad Yasevich 41108cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 41118cb08174SJohannes Berg extack); 41121690be63SVlad Yasevich if (err < 0) 41131690be63SVlad Yasevich return err; 411477162022SJohn Fastabend 411577162022SJohn Fastabend ndm = nlmsg_data(nlh); 411677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 4117b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 411877162022SJohn Fastabend return -EINVAL; 411977162022SJohn Fastabend } 412077162022SJohn Fastabend 412177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 412277162022SJohn Fastabend if (dev == NULL) { 4123b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 412477162022SJohn Fastabend return -ENODEV; 412577162022SJohn Fastabend } 412677162022SJohn Fastabend 41271690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4128b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 412977162022SJohn Fastabend return -EINVAL; 413077162022SJohn Fastabend } 413177162022SJohn Fastabend 4132da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4133da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4134da715775SIdo Schimmel return -EINVAL; 4135da715775SIdo Schimmel } 4136da715775SIdo Schimmel 41371690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 41381690be63SVlad Yasevich 4139b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4140f6f6424bSJiri Pirko if (err) 4141f6f6424bSJiri Pirko return err; 4142f6f6424bSJiri Pirko 414377162022SJohn Fastabend err = -EOPNOTSUPP; 414477162022SJohn Fastabend 414577162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 414677162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 41472e92a2d0SJulian Wiedmann netif_is_bridge_port(dev)) { 4148898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4149898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 415077162022SJohn Fastabend 4151898e5061SJiri Pirko if (ops->ndo_fdb_del) 4152f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 415377162022SJohn Fastabend 415477162022SJohn Fastabend if (err) 415577162022SJohn Fastabend goto out; 415677162022SJohn Fastabend else 415777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 415877162022SJohn Fastabend } 415977162022SJohn Fastabend 416077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4161090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 4162090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 4163f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 4164f6f6424bSJiri Pirko vid); 4165090096bfSVlad Yasevich else 4166f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 416777162022SJohn Fastabend 41683ff661c3SJohn Fastabend if (!err) { 4169b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4170b3379041SHubert Sokolowski ndm->ndm_state); 417177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 417277162022SJohn Fastabend } 41733ff661c3SJohn Fastabend } 417477162022SJohn Fastabend out: 417577162022SJohn Fastabend return err; 417677162022SJohn Fastabend } 417777162022SJohn Fastabend 4178d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4179d83b0603SJohn Fastabend struct netlink_callback *cb, 4180d83b0603SJohn Fastabend struct net_device *dev, 4181d83b0603SJohn Fastabend int *idx, 4182d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4183d83b0603SJohn Fastabend { 4184d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4185d83b0603SJohn Fastabend int err; 418615e47304SEric W. Biederman u32 portid, seq; 4187d83b0603SJohn Fastabend 418815e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4189d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4190d83b0603SJohn Fastabend 4191d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4192d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4193d83b0603SJohn Fastabend goto skip; 4194d83b0603SJohn Fastabend 41951e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4196a7a558feSJohn Fastabend portid, seq, 41971c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4198b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4199d83b0603SJohn Fastabend if (err < 0) 4200d83b0603SJohn Fastabend return err; 4201d83b0603SJohn Fastabend skip: 4202d83b0603SJohn Fastabend *idx += 1; 4203d83b0603SJohn Fastabend } 4204d83b0603SJohn Fastabend return 0; 4205d83b0603SJohn Fastabend } 4206d83b0603SJohn Fastabend 4207d83b0603SJohn Fastabend /** 42082c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4209a986967eSBart Van Assche * @skb: socket buffer to store message in 4210a986967eSBart Van Assche * @cb: netlink callback 4211d83b0603SJohn Fastabend * @dev: netdevice 4212a986967eSBart Van Assche * @filter_dev: ignored 4213a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4214d83b0603SJohn Fastabend * 4215d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 421691f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4217d83b0603SJohn Fastabend */ 4218d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4219d83b0603SJohn Fastabend struct netlink_callback *cb, 4220d83b0603SJohn Fastabend struct net_device *dev, 42215d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4222d297653dSRoopa Prabhu int *idx) 4223d83b0603SJohn Fastabend { 4224d83b0603SJohn Fastabend int err; 4225d83b0603SJohn Fastabend 422668883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 422768883893SEric Dumazet return -EINVAL; 422868883893SEric Dumazet 4229d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4230d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4231d83b0603SJohn Fastabend if (err) 4232d83b0603SJohn Fastabend goto out; 42332934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4234d83b0603SJohn Fastabend out: 4235d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4236d297653dSRoopa Prabhu return err; 4237d83b0603SJohn Fastabend } 4238d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4239d83b0603SJohn Fastabend 42408c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 42418c6e137fSDavid Ahern int *br_idx, int *brport_idx, 42428c6e137fSDavid Ahern struct netlink_ext_ack *extack) 42438c6e137fSDavid Ahern { 42448c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 42458c6e137fSDavid Ahern struct ndmsg *ndm; 42468c6e137fSDavid Ahern int err, i; 42478c6e137fSDavid Ahern 42488c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 42498c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 42508c6e137fSDavid Ahern return -EINVAL; 42518c6e137fSDavid Ahern } 42528c6e137fSDavid Ahern 42538c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 42548c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 42558c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 42568b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 42578c6e137fSDavid Ahern return -EINVAL; 42588c6e137fSDavid Ahern } 42598c6e137fSDavid Ahern 42608cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 42618cb08174SJohannes Berg NDA_MAX, NULL, extack); 42628c6e137fSDavid Ahern if (err < 0) 42638c6e137fSDavid Ahern return err; 42648c6e137fSDavid Ahern 42658c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 42668c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 42678c6e137fSDavid Ahern if (!tb[i]) 42688c6e137fSDavid Ahern continue; 42698c6e137fSDavid Ahern 42708c6e137fSDavid Ahern switch (i) { 42718c6e137fSDavid Ahern case NDA_IFINDEX: 42728c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 42738c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 42748c6e137fSDavid Ahern return -EINVAL; 42758c6e137fSDavid Ahern } 42768c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 42778c6e137fSDavid Ahern break; 42788c6e137fSDavid Ahern case NDA_MASTER: 42798c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 42808c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 42818c6e137fSDavid Ahern return -EINVAL; 42828c6e137fSDavid Ahern } 42838c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 42848c6e137fSDavid Ahern break; 42858c6e137fSDavid Ahern default: 42868c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 42878c6e137fSDavid Ahern return -EINVAL; 42888c6e137fSDavid Ahern } 42898c6e137fSDavid Ahern } 42908c6e137fSDavid Ahern 42918c6e137fSDavid Ahern return 0; 42928c6e137fSDavid Ahern } 42938c6e137fSDavid Ahern 42948dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 42958dfbda19SDavid Ahern int *br_idx, int *brport_idx, 42968dfbda19SDavid Ahern struct netlink_ext_ack *extack) 42978dfbda19SDavid Ahern { 42988dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 42998dfbda19SDavid Ahern int err; 43008dfbda19SDavid Ahern 43018dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 43028dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 43038dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 43048dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 43058dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 43068dfbda19SDavid Ahern * with an optional attribute. 43078dfbda19SDavid Ahern */ 43088dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 43098dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 43108dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 43114565d7e5SDavid Ahern struct ifinfomsg *ifm; 43124565d7e5SDavid Ahern 43138cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 43148cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 43158cb08174SJohannes Berg extack); 43168dfbda19SDavid Ahern if (err < 0) { 43178dfbda19SDavid Ahern return -EINVAL; 43188dfbda19SDavid Ahern } else if (err == 0) { 43198dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 43208dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 43218dfbda19SDavid Ahern } 43228dfbda19SDavid Ahern 43234565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 43248dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 43258dfbda19SDavid Ahern } 43268dfbda19SDavid Ahern return 0; 43278dfbda19SDavid Ahern } 43288dfbda19SDavid Ahern 432977162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 433077162022SJohn Fastabend { 433177162022SJohn Fastabend struct net_device *dev; 43325e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 43335e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 43345e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 43355e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4336d297653dSRoopa Prabhu struct hlist_head *head; 43375e6d2435SJamal Hadi Salim int brport_idx = 0; 43385e6d2435SJamal Hadi Salim int br_idx = 0; 4339d297653dSRoopa Prabhu int h, s_h; 4340d297653dSRoopa Prabhu int idx = 0, s_idx; 4341d297653dSRoopa Prabhu int err = 0; 4342d297653dSRoopa Prabhu int fidx = 0; 434377162022SJohn Fastabend 43448c6e137fSDavid Ahern if (cb->strict_check) 43458c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 43468c6e137fSDavid Ahern cb->extack); 43478c6e137fSDavid Ahern else 43488dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 43498dfbda19SDavid Ahern cb->extack); 43508dfbda19SDavid Ahern if (err < 0) 43518dfbda19SDavid Ahern return err; 43525e6d2435SJamal Hadi Salim 43535e6d2435SJamal Hadi Salim if (br_idx) { 43545e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 43555e6d2435SJamal Hadi Salim if (!br_dev) 43565e6d2435SJamal Hadi Salim return -ENODEV; 43575e6d2435SJamal Hadi Salim 4358898e5061SJiri Pirko ops = br_dev->netdev_ops; 43595e6d2435SJamal Hadi Salim } 43605e6d2435SJamal Hadi Salim 4361d297653dSRoopa Prabhu s_h = cb->args[0]; 4362d297653dSRoopa Prabhu s_idx = cb->args[1]; 4363d297653dSRoopa Prabhu 4364d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4365d297653dSRoopa Prabhu idx = 0; 4366d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4367d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4368d297653dSRoopa Prabhu 43695e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 43705e6d2435SJamal Hadi Salim continue; 43715e6d2435SJamal Hadi Salim 43725e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 43732e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 43745e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 43755e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 43765e6d2435SJamal Hadi Salim } 43775e6d2435SJamal Hadi Salim } else { 43785e6d2435SJamal Hadi Salim if (dev != br_dev && 43792e92a2d0SJulian Wiedmann !netif_is_bridge_port(dev)) 43805e6d2435SJamal Hadi Salim continue; 43815e6d2435SJamal Hadi Salim 43825e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 43835e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 43845e6d2435SJamal Hadi Salim continue; 43855e6d2435SJamal Hadi Salim cops = ops; 43865e6d2435SJamal Hadi Salim } 43875e6d2435SJamal Hadi Salim 4388d297653dSRoopa Prabhu if (idx < s_idx) 4389d297653dSRoopa Prabhu goto cont; 4390d297653dSRoopa Prabhu 43912e92a2d0SJulian Wiedmann if (netif_is_bridge_port(dev)) { 4392d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4393d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4394d297653dSRoopa Prabhu br_dev, dev, 4395d297653dSRoopa Prabhu &fidx); 4396d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4397d297653dSRoopa Prabhu goto out; 439877162022SJohn Fastabend } 4399d297653dSRoopa Prabhu } 440077162022SJohn Fastabend 44015e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4402d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4403d297653dSRoopa Prabhu dev, NULL, 4404d297653dSRoopa Prabhu &fidx); 44056cb69742SHubert Sokolowski else 4406d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4407d297653dSRoopa Prabhu &fidx); 4408d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4409d297653dSRoopa Prabhu goto out; 44105e6d2435SJamal Hadi Salim 44115e6d2435SJamal Hadi Salim cops = NULL; 4412d297653dSRoopa Prabhu 4413d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4414d297653dSRoopa Prabhu cb->args[2] = 0; 4415d297653dSRoopa Prabhu fidx = 0; 4416d297653dSRoopa Prabhu cont: 4417d297653dSRoopa Prabhu idx++; 4418d297653dSRoopa Prabhu } 441977162022SJohn Fastabend } 442077162022SJohn Fastabend 4421d297653dSRoopa Prabhu out: 4422d297653dSRoopa Prabhu cb->args[0] = h; 4423d297653dSRoopa Prabhu cb->args[1] = idx; 4424d297653dSRoopa Prabhu cb->args[2] = fidx; 4425d297653dSRoopa Prabhu 442677162022SJohn Fastabend return skb->len; 442777162022SJohn Fastabend } 442877162022SJohn Fastabend 44295b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 44305b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 44315b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 44325b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 44335b2f94b2SRoopa Prabhu { 44345b2f94b2SRoopa Prabhu struct ndmsg *ndm; 44355b2f94b2SRoopa Prabhu int err, i; 44365b2f94b2SRoopa Prabhu 44375b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 44385b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 44395b2f94b2SRoopa Prabhu return -EINVAL; 44405b2f94b2SRoopa Prabhu } 44415b2f94b2SRoopa Prabhu 44425b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 44435b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 44445b2f94b2SRoopa Prabhu ndm->ndm_type) { 44455b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 44465b2f94b2SRoopa Prabhu return -EINVAL; 44475b2f94b2SRoopa Prabhu } 44485b2f94b2SRoopa Prabhu 44495b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 44505b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 44515b2f94b2SRoopa Prabhu return -EINVAL; 44525b2f94b2SRoopa Prabhu } 44535b2f94b2SRoopa Prabhu 44548cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 44558cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 44565b2f94b2SRoopa Prabhu if (err < 0) 44575b2f94b2SRoopa Prabhu return err; 44585b2f94b2SRoopa Prabhu 44595b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 44605b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 44615b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 44625b2f94b2SRoopa Prabhu if (!tb[i]) 44635b2f94b2SRoopa Prabhu continue; 44645b2f94b2SRoopa Prabhu 44655b2f94b2SRoopa Prabhu switch (i) { 44665b2f94b2SRoopa Prabhu case NDA_MASTER: 44675b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 44685b2f94b2SRoopa Prabhu break; 44695b2f94b2SRoopa Prabhu case NDA_LLADDR: 44705b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 44715b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 44725b2f94b2SRoopa Prabhu return -EINVAL; 44735b2f94b2SRoopa Prabhu } 44745b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 44755b2f94b2SRoopa Prabhu break; 44765b2f94b2SRoopa Prabhu case NDA_VLAN: 44775b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 44785b2f94b2SRoopa Prabhu if (err) 44795b2f94b2SRoopa Prabhu return err; 44805b2f94b2SRoopa Prabhu break; 44815b2f94b2SRoopa Prabhu case NDA_VNI: 44825b2f94b2SRoopa Prabhu break; 44835b2f94b2SRoopa Prabhu default: 44845b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 44855b2f94b2SRoopa Prabhu return -EINVAL; 44865b2f94b2SRoopa Prabhu } 44875b2f94b2SRoopa Prabhu } 44885b2f94b2SRoopa Prabhu 44895b2f94b2SRoopa Prabhu return 0; 44905b2f94b2SRoopa Prabhu } 44915b2f94b2SRoopa Prabhu 44925b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 44935b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 44945b2f94b2SRoopa Prabhu { 44955b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 44965b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 44975b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 44985b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 44995b2f94b2SRoopa Prabhu struct sk_buff *skb; 45005b2f94b2SRoopa Prabhu int brport_idx = 0; 45015b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 45025b2f94b2SRoopa Prabhu int br_idx = 0; 45035b2f94b2SRoopa Prabhu u8 *addr = NULL; 45045b2f94b2SRoopa Prabhu u16 vid = 0; 45055b2f94b2SRoopa Prabhu int err; 45065b2f94b2SRoopa Prabhu 45075b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 45085b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 45095b2f94b2SRoopa Prabhu if (err < 0) 45105b2f94b2SRoopa Prabhu return err; 45115b2f94b2SRoopa Prabhu 4512f989d03eSNikolay Aleksandrov if (!addr) { 4513f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4514f989d03eSNikolay Aleksandrov return -EINVAL; 4515f989d03eSNikolay Aleksandrov } 4516f989d03eSNikolay Aleksandrov 45175b2f94b2SRoopa Prabhu if (brport_idx) { 45185b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 45195b2f94b2SRoopa Prabhu if (!dev) { 45205b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 45215b2f94b2SRoopa Prabhu return -ENODEV; 45225b2f94b2SRoopa Prabhu } 45235b2f94b2SRoopa Prabhu } 45245b2f94b2SRoopa Prabhu 45255b2f94b2SRoopa Prabhu if (br_idx) { 45265b2f94b2SRoopa Prabhu if (dev) { 45275b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 45285b2f94b2SRoopa Prabhu return -EINVAL; 45295b2f94b2SRoopa Prabhu } 45305b2f94b2SRoopa Prabhu 45315b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 45325b2f94b2SRoopa Prabhu if (!br_dev) { 45335b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 45345b2f94b2SRoopa Prabhu return -EINVAL; 45355b2f94b2SRoopa Prabhu } 45365b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 45375b2f94b2SRoopa Prabhu } 45385b2f94b2SRoopa Prabhu 45395b2f94b2SRoopa Prabhu if (dev) { 45405b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 45412e92a2d0SJulian Wiedmann if (!netif_is_bridge_port(dev)) { 45425b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 45435b2f94b2SRoopa Prabhu return -EINVAL; 45445b2f94b2SRoopa Prabhu } 45455b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 45465b2f94b2SRoopa Prabhu if (!br_dev) { 45475b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 45485b2f94b2SRoopa Prabhu return -EINVAL; 45495b2f94b2SRoopa Prabhu } 45505b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 45515b2f94b2SRoopa Prabhu } else { 45525b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 45535b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 45545b2f94b2SRoopa Prabhu return -EINVAL; 45555b2f94b2SRoopa Prabhu } 45565b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 45575b2f94b2SRoopa Prabhu } 45585b2f94b2SRoopa Prabhu } 45595b2f94b2SRoopa Prabhu 45605b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 45615b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 45625b2f94b2SRoopa Prabhu return -ENODEV; 45635b2f94b2SRoopa Prabhu } 45645b2f94b2SRoopa Prabhu 45655b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 45665b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 45675b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 45685b2f94b2SRoopa Prabhu } 45695b2f94b2SRoopa Prabhu 45705b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 45715b2f94b2SRoopa Prabhu if (!skb) 45725b2f94b2SRoopa Prabhu return -ENOBUFS; 45735b2f94b2SRoopa Prabhu 45745b2f94b2SRoopa Prabhu if (br_dev) 45755b2f94b2SRoopa Prabhu dev = br_dev; 45765b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 45775b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 45785b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 45795b2f94b2SRoopa Prabhu if (err) 45805b2f94b2SRoopa Prabhu goto out; 45815b2f94b2SRoopa Prabhu 45825b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 45835b2f94b2SRoopa Prabhu out: 45845b2f94b2SRoopa Prabhu kfree_skb(skb); 45855b2f94b2SRoopa Prabhu return err; 45865b2f94b2SRoopa Prabhu } 45875b2f94b2SRoopa Prabhu 45882c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 45892c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 45902c3c031cSScott Feldman { 45912c3c031cSScott Feldman if (mask & flag) 45922c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 45932c3c031cSScott Feldman return 0; 45942c3c031cSScott Feldman } 45952c3c031cSScott Feldman 4596815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 45972c3c031cSScott Feldman struct net_device *dev, u16 mode, 45987d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 45997d4f8d87SScott Feldman u32 filter_mask, 46007d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 46017d4f8d87SScott Feldman struct net_device *dev, 46027d4f8d87SScott Feldman u32 filter_mask)) 4603815cccbfSJohn Fastabend { 4604815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4605815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4606815cccbfSJohn Fastabend struct nlattr *br_afspec; 46072c3c031cSScott Feldman struct nlattr *protinfo; 4608815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4609898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 46107d4f8d87SScott Feldman int err = 0; 4611815cccbfSJohn Fastabend 461246c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4613815cccbfSJohn Fastabend if (nlh == NULL) 4614815cccbfSJohn Fastabend return -EMSGSIZE; 4615815cccbfSJohn Fastabend 4616815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4617815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4618815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4619815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4620815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4621815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4622815cccbfSJohn Fastabend ifm->ifi_change = 0; 4623815cccbfSJohn Fastabend 4624815cccbfSJohn Fastabend 4625815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4626815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4627815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4628898e5061SJiri Pirko (br_dev && 4629898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4630815cccbfSJohn Fastabend (dev->addr_len && 4631815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4632a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4633a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4634815cccbfSJohn Fastabend goto nla_put_failure; 4635815cccbfSJohn Fastabend 4636ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4637815cccbfSJohn Fastabend if (!br_afspec) 4638815cccbfSJohn Fastabend goto nla_put_failure; 4639815cccbfSJohn Fastabend 46401d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4641815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4642815cccbfSJohn Fastabend goto nla_put_failure; 4643815cccbfSJohn Fastabend } 46441d460b98SRoopa Prabhu 46451d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 46461d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 46471d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 46481d460b98SRoopa Prabhu goto nla_put_failure; 46491d460b98SRoopa Prabhu } 46501d460b98SRoopa Prabhu } 46517d4f8d87SScott Feldman if (vlan_fill) { 46527d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 46537d4f8d87SScott Feldman if (err) { 46547d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 46557d4f8d87SScott Feldman goto nla_put_failure; 46567d4f8d87SScott Feldman } 46577d4f8d87SScott Feldman } 4658815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4659815cccbfSJohn Fastabend 4660ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 46612c3c031cSScott Feldman if (!protinfo) 46622c3c031cSScott Feldman goto nla_put_failure; 46632c3c031cSScott Feldman 46642c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 46652c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 46662c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46672c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 46682c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46692c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 46702c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 46712c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46722c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 46732c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46742c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 46752c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46762c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 46772c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 46782c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 46792c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 4680583cb0b4SJulian Wiedmann IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4681583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4682583cb0b4SJulian Wiedmann IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4683583cb0b4SJulian Wiedmann brport_nla_put_flag(skb, flags, mask, 4684583cb0b4SJulian Wiedmann IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 46852c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 46862c3c031cSScott Feldman goto nla_put_failure; 46872c3c031cSScott Feldman } 46882c3c031cSScott Feldman 46892c3c031cSScott Feldman nla_nest_end(skb, protinfo); 46902c3c031cSScott Feldman 4691053c095aSJohannes Berg nlmsg_end(skb, nlh); 4692053c095aSJohannes Berg return 0; 4693815cccbfSJohn Fastabend nla_put_failure: 4694815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 46957d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4696815cccbfSJohn Fastabend } 46977d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4698815cccbfSJohn Fastabend 46992d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 47002d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 47012d011be8SDavid Ahern struct netlink_ext_ack *extack) 47022d011be8SDavid Ahern { 47032d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 47042d011be8SDavid Ahern int err, i; 47052d011be8SDavid Ahern 47062d011be8SDavid Ahern if (strict_check) { 47072d011be8SDavid Ahern struct ifinfomsg *ifm; 47082d011be8SDavid Ahern 47092d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 47102d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 47112d011be8SDavid Ahern return -EINVAL; 47122d011be8SDavid Ahern } 47132d011be8SDavid Ahern 47142d011be8SDavid Ahern ifm = nlmsg_data(nlh); 47152d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 47162d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 47172d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 47182d011be8SDavid Ahern return -EINVAL; 47192d011be8SDavid Ahern } 47202d011be8SDavid Ahern 47218cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 47228cb08174SJohannes Berg sizeof(struct ifinfomsg), 47238cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 47248cb08174SJohannes Berg extack); 47252d011be8SDavid Ahern } else { 47268cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 47278cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 47288cb08174SJohannes Berg extack); 47292d011be8SDavid Ahern } 47302d011be8SDavid Ahern if (err < 0) 47312d011be8SDavid Ahern return err; 47322d011be8SDavid Ahern 47332d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 47342d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 47352d011be8SDavid Ahern if (!tb[i]) 47362d011be8SDavid Ahern continue; 47372d011be8SDavid Ahern 47382d011be8SDavid Ahern switch (i) { 47392d011be8SDavid Ahern case IFLA_EXT_MASK: 47402d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 47412d011be8SDavid Ahern break; 47422d011be8SDavid Ahern default: 47432d011be8SDavid Ahern if (strict_check) { 47442d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 47452d011be8SDavid Ahern return -EINVAL; 47462d011be8SDavid Ahern } 47472d011be8SDavid Ahern } 47482d011be8SDavid Ahern } 47492d011be8SDavid Ahern 47502d011be8SDavid Ahern return 0; 47512d011be8SDavid Ahern } 47522d011be8SDavid Ahern 4753e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4754e5a55a89SJohn Fastabend { 47552d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4756e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4757e5a55a89SJohn Fastabend struct net_device *dev; 4758e5a55a89SJohn Fastabend int idx = 0; 4759e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 47602d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 47616cbdceebSVlad Yasevich u32 filter_mask = 0; 4762d64f69b0SRoopa Prabhu int err; 47636cbdceebSVlad Yasevich 47642d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 47652d011be8SDavid Ahern cb->extack); 47662d011be8SDavid Ahern if (err < 0 && cb->strict_check) 47672d011be8SDavid Ahern return err; 4768e5a55a89SJohn Fastabend 4769e5a55a89SJohn Fastabend rcu_read_lock(); 4770e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4771e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4772898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4773e5a55a89SJohn Fastabend 4774898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4775d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4776d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4777d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4778d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4779f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4780f6c5775fSDavid Ahern if (likely(skb->len)) 4781e5a55a89SJohn Fastabend break; 4782f6c5775fSDavid Ahern 4783f6c5775fSDavid Ahern goto out_err; 4784f6c5775fSDavid Ahern } 4785d64f69b0SRoopa Prabhu } 4786e5a55a89SJohn Fastabend idx++; 4787e5a55a89SJohn Fastabend } 4788e5a55a89SJohn Fastabend 4789e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4790d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4791d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4792d64f69b0SRoopa Prabhu seq, dev, 479346c264daSNicolas Dichtel filter_mask, 4794d64f69b0SRoopa Prabhu NLM_F_MULTI); 4795f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4796f6c5775fSDavid Ahern if (likely(skb->len)) 4797e5a55a89SJohn Fastabend break; 4798f6c5775fSDavid Ahern 4799f6c5775fSDavid Ahern goto out_err; 4800f6c5775fSDavid Ahern } 4801d64f69b0SRoopa Prabhu } 4802e5a55a89SJohn Fastabend idx++; 4803e5a55a89SJohn Fastabend } 4804e5a55a89SJohn Fastabend } 4805f6c5775fSDavid Ahern err = skb->len; 4806f6c5775fSDavid Ahern out_err: 4807e5a55a89SJohn Fastabend rcu_read_unlock(); 4808e5a55a89SJohn Fastabend cb->args[0] = idx; 4809e5a55a89SJohn Fastabend 4810f6c5775fSDavid Ahern return err; 4811e5a55a89SJohn Fastabend } 4812e5a55a89SJohn Fastabend 48132469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 48142469ffd7SJohn Fastabend { 48152469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 48162469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 48172469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 48182469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 48192469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 48202469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 48212469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 48222469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 48232469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 48242469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 48252469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 48262469ffd7SJohn Fastabend } 48272469ffd7SJohn Fastabend 482802dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 48292469ffd7SJohn Fastabend { 48302469ffd7SJohn Fastabend struct net *net = dev_net(dev); 48312469ffd7SJohn Fastabend struct sk_buff *skb; 48322469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 48332469ffd7SJohn Fastabend 483402dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 483502dba438SRoopa Prabhu return 0; 483602dba438SRoopa Prabhu 48372469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 48382469ffd7SJohn Fastabend if (!skb) { 48392469ffd7SJohn Fastabend err = -ENOMEM; 48402469ffd7SJohn Fastabend goto errout; 48412469ffd7SJohn Fastabend } 48422469ffd7SJohn Fastabend 484346c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4844c38e01b8SJohn Fastabend if (err < 0) 4845c38e01b8SJohn Fastabend goto errout; 48462469ffd7SJohn Fastabend 484759ccaaaaSRoopa Prabhu if (!skb->len) 484859ccaaaaSRoopa Prabhu goto errout; 484959ccaaaaSRoopa Prabhu 48502469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 48512469ffd7SJohn Fastabend return 0; 48522469ffd7SJohn Fastabend errout: 48532469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 48542469ffd7SJohn Fastabend kfree_skb(skb); 485559ccaaaaSRoopa Prabhu if (err) 48562469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 48572469ffd7SJohn Fastabend return err; 48582469ffd7SJohn Fastabend } 48592469ffd7SJohn Fastabend 4860c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4861c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4862e5a55a89SJohn Fastabend { 4863e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4864e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4865e5a55a89SJohn Fastabend struct net_device *dev; 48662469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 48672469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 48684de8b413SRosen, Rami u16 flags = 0; 4869c38e01b8SJohn Fastabend bool have_flags = false; 4870e5a55a89SJohn Fastabend 4871e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4872e5a55a89SJohn Fastabend return -EINVAL; 4873e5a55a89SJohn Fastabend 4874e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4875e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4876e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4877e5a55a89SJohn Fastabend 4878e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4879e5a55a89SJohn Fastabend if (!dev) { 4880b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4881e5a55a89SJohn Fastabend return -ENODEV; 4882e5a55a89SJohn Fastabend } 4883e5a55a89SJohn Fastabend 48842469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 48852469ffd7SJohn Fastabend if (br_spec) { 48862469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 48872469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 48886e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 48896e8d1c55SThomas Graf return -EINVAL; 48906e8d1c55SThomas Graf 4891c38e01b8SJohn Fastabend have_flags = true; 48922469ffd7SJohn Fastabend flags = nla_get_u16(attr); 48932469ffd7SJohn Fastabend break; 48942469ffd7SJohn Fastabend } 48952469ffd7SJohn Fastabend } 48962469ffd7SJohn Fastabend } 48972469ffd7SJohn Fastabend 48982469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4899898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4900898e5061SJiri Pirko 4901898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 49022469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4903e5a55a89SJohn Fastabend goto out; 4904e5a55a89SJohn Fastabend } 4905e5a55a89SJohn Fastabend 49062fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 49072fd527b7SPetr Machata extack); 49082469ffd7SJohn Fastabend if (err) 49092469ffd7SJohn Fastabend goto out; 49102469ffd7SJohn Fastabend 49112469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 49122469ffd7SJohn Fastabend } 49132469ffd7SJohn Fastabend 49142469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 49152469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 49162469ffd7SJohn Fastabend err = -EOPNOTSUPP; 49172469ffd7SJohn Fastabend else 4918add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 49192fd527b7SPetr Machata flags, 49202fd527b7SPetr Machata extack); 492102dba438SRoopa Prabhu if (!err) { 49222469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 492302dba438SRoopa Prabhu 492402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 492502dba438SRoopa Prabhu * change 492602dba438SRoopa Prabhu */ 492702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 492802dba438SRoopa Prabhu } 49292469ffd7SJohn Fastabend } 49302469ffd7SJohn Fastabend 4931c38e01b8SJohn Fastabend if (have_flags) 49322469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4933e5a55a89SJohn Fastabend out: 4934e5a55a89SJohn Fastabend return err; 4935e5a55a89SJohn Fastabend } 4936e5a55a89SJohn Fastabend 4937c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4938c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4939407af329SVlad Yasevich { 4940407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4941407af329SVlad Yasevich struct ifinfomsg *ifm; 4942407af329SVlad Yasevich struct net_device *dev; 4943407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4944407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 49454de8b413SRosen, Rami u16 flags = 0; 4946407af329SVlad Yasevich bool have_flags = false; 4947407af329SVlad Yasevich 4948407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4949407af329SVlad Yasevich return -EINVAL; 4950407af329SVlad Yasevich 4951407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4952407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4953407af329SVlad Yasevich return -EPFNOSUPPORT; 4954407af329SVlad Yasevich 4955407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4956407af329SVlad Yasevich if (!dev) { 4957b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4958407af329SVlad Yasevich return -ENODEV; 4959407af329SVlad Yasevich } 4960407af329SVlad Yasevich 4961407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4962407af329SVlad Yasevich if (br_spec) { 4963407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4964407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 49656e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 49666e8d1c55SThomas Graf return -EINVAL; 49676e8d1c55SThomas Graf 4968407af329SVlad Yasevich have_flags = true; 4969407af329SVlad Yasevich flags = nla_get_u16(attr); 4970407af329SVlad Yasevich break; 4971407af329SVlad Yasevich } 4972407af329SVlad Yasevich } 4973407af329SVlad Yasevich } 4974407af329SVlad Yasevich 4975407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4976407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4977407af329SVlad Yasevich 4978407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4979407af329SVlad Yasevich err = -EOPNOTSUPP; 4980407af329SVlad Yasevich goto out; 4981407af329SVlad Yasevich } 4982407af329SVlad Yasevich 4983add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4984407af329SVlad Yasevich if (err) 4985407af329SVlad Yasevich goto out; 4986407af329SVlad Yasevich 4987407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4988407af329SVlad Yasevich } 4989407af329SVlad Yasevich 4990407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4991407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4992407af329SVlad Yasevich err = -EOPNOTSUPP; 4993407af329SVlad Yasevich else 4994add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4995add511b3SRoopa Prabhu flags); 4996407af329SVlad Yasevich 499702dba438SRoopa Prabhu if (!err) { 4998407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 499902dba438SRoopa Prabhu 500002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 500102dba438SRoopa Prabhu * change 500202dba438SRoopa Prabhu */ 500302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 500402dba438SRoopa Prabhu } 5005407af329SVlad Yasevich } 5006407af329SVlad Yasevich 5007407af329SVlad Yasevich if (have_flags) 5008407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 5009407af329SVlad Yasevich out: 5010407af329SVlad Yasevich return err; 5011407af329SVlad Yasevich } 5012407af329SVlad Yasevich 5013e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5014e8872a25SNikolay Aleksandrov { 5015e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5016e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 5017e8872a25SNikolay Aleksandrov } 5018e8872a25SNikolay Aleksandrov 501969ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 502069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 502169ae6ad2SNogah Frankel { 502269ae6ad2SNogah Frankel switch (attr_id) { 502369ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 502469ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 502569ae6ad2SNogah Frankel } 502669ae6ad2SNogah Frankel 502769ae6ad2SNogah Frankel return 0; 502869ae6ad2SNogah Frankel } 502969ae6ad2SNogah Frankel 503069ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 503169ae6ad2SNogah Frankel int *prividx) 503269ae6ad2SNogah Frankel { 503369ae6ad2SNogah Frankel struct nlattr *attr = NULL; 503469ae6ad2SNogah Frankel int attr_id, size; 503569ae6ad2SNogah Frankel void *attr_data; 503669ae6ad2SNogah Frankel int err; 503769ae6ad2SNogah Frankel 503869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 503969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 504069ae6ad2SNogah Frankel return -ENODATA; 504169ae6ad2SNogah Frankel 504269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 504369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 504469ae6ad2SNogah Frankel if (attr_id < *prividx) 504569ae6ad2SNogah Frankel continue; 504669ae6ad2SNogah Frankel 504769ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 504869ae6ad2SNogah Frankel if (!size) 504969ae6ad2SNogah Frankel continue; 505069ae6ad2SNogah Frankel 50513df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 505269ae6ad2SNogah Frankel continue; 505369ae6ad2SNogah Frankel 505469ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 505569ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 505669ae6ad2SNogah Frankel if (!attr) 505769ae6ad2SNogah Frankel goto nla_put_failure; 505869ae6ad2SNogah Frankel 505969ae6ad2SNogah Frankel attr_data = nla_data(attr); 506069ae6ad2SNogah Frankel memset(attr_data, 0, size); 506169ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 506269ae6ad2SNogah Frankel attr_data); 506369ae6ad2SNogah Frankel if (err) 506469ae6ad2SNogah Frankel goto get_offload_stats_failure; 506569ae6ad2SNogah Frankel } 506669ae6ad2SNogah Frankel 506769ae6ad2SNogah Frankel if (!attr) 506869ae6ad2SNogah Frankel return -ENODATA; 506969ae6ad2SNogah Frankel 507069ae6ad2SNogah Frankel *prividx = 0; 507169ae6ad2SNogah Frankel return 0; 507269ae6ad2SNogah Frankel 507369ae6ad2SNogah Frankel nla_put_failure: 507469ae6ad2SNogah Frankel err = -EMSGSIZE; 507569ae6ad2SNogah Frankel get_offload_stats_failure: 507669ae6ad2SNogah Frankel *prividx = attr_id; 507769ae6ad2SNogah Frankel return err; 507869ae6ad2SNogah Frankel } 507969ae6ad2SNogah Frankel 508069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 508169ae6ad2SNogah Frankel { 508269ae6ad2SNogah Frankel int nla_size = 0; 508369ae6ad2SNogah Frankel int attr_id; 508469ae6ad2SNogah Frankel int size; 508569ae6ad2SNogah Frankel 508669ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 508769ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 508869ae6ad2SNogah Frankel return 0; 508969ae6ad2SNogah Frankel 509069ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 509169ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 50923df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 509369ae6ad2SNogah Frankel continue; 509469ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 509569ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 509669ae6ad2SNogah Frankel } 509769ae6ad2SNogah Frankel 509869ae6ad2SNogah Frankel if (nla_size != 0) 509969ae6ad2SNogah Frankel nla_size += nla_total_size(0); 510069ae6ad2SNogah Frankel 510169ae6ad2SNogah Frankel return nla_size; 510269ae6ad2SNogah Frankel } 510369ae6ad2SNogah Frankel 510410c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 510510c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 5106e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 5107e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 510810c9ead9SRoopa Prabhu { 510910c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 511010c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 511110c9ead9SRoopa Prabhu struct nlattr *attr; 5112e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 511369ae6ad2SNogah Frankel int err; 511410c9ead9SRoopa Prabhu 511510c9ead9SRoopa Prabhu ASSERT_RTNL(); 511610c9ead9SRoopa Prabhu 511710c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 511810c9ead9SRoopa Prabhu if (!nlh) 511910c9ead9SRoopa Prabhu return -EMSGSIZE; 512010c9ead9SRoopa Prabhu 512110c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 5122ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 5123ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 5124ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 512510c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 512610c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 512710c9ead9SRoopa Prabhu 5128e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 512910c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 513010c9ead9SRoopa Prabhu 513158414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 513258414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 513358414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 513410c9ead9SRoopa Prabhu if (!attr) 513510c9ead9SRoopa Prabhu goto nla_put_failure; 513610c9ead9SRoopa Prabhu 513710c9ead9SRoopa Prabhu sp = nla_data(attr); 513810c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 513910c9ead9SRoopa Prabhu } 514010c9ead9SRoopa Prabhu 514197a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 514297a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 514397a47facSNikolay Aleksandrov 514497a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 514597a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5146ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 514797a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 514897a47facSNikolay Aleksandrov if (!attr) 514997a47facSNikolay Aleksandrov goto nla_put_failure; 515097a47facSNikolay Aleksandrov 515180e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 515280e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 515380e73cc5SNikolay Aleksandrov if (err) 515480e73cc5SNikolay Aleksandrov goto nla_put_failure; 515580e73cc5SNikolay Aleksandrov *idxattr = 0; 515680e73cc5SNikolay Aleksandrov } 515780e73cc5SNikolay Aleksandrov } 515880e73cc5SNikolay Aleksandrov 515980e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 516080e73cc5SNikolay Aleksandrov *idxattr)) { 516180e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 516280e73cc5SNikolay Aleksandrov const struct net_device *master; 516380e73cc5SNikolay Aleksandrov 516480e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 516580e73cc5SNikolay Aleksandrov if (master) 516680e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 516780e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 516880e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5169ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 517080e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 517180e73cc5SNikolay Aleksandrov if (!attr) 517280e73cc5SNikolay Aleksandrov goto nla_put_failure; 517380e73cc5SNikolay Aleksandrov 517480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 517597a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 517697a47facSNikolay Aleksandrov if (err) 517797a47facSNikolay Aleksandrov goto nla_put_failure; 517897a47facSNikolay Aleksandrov *idxattr = 0; 517997a47facSNikolay Aleksandrov } 518097a47facSNikolay Aleksandrov } 518197a47facSNikolay Aleksandrov 518269ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 518369ae6ad2SNogah Frankel *idxattr)) { 518469ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5185ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5186ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 518769ae6ad2SNogah Frankel if (!attr) 518869ae6ad2SNogah Frankel goto nla_put_failure; 518969ae6ad2SNogah Frankel 519069ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 519169ae6ad2SNogah Frankel if (err == -ENODATA) 519269ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 519369ae6ad2SNogah Frankel else 519469ae6ad2SNogah Frankel nla_nest_end(skb, attr); 519569ae6ad2SNogah Frankel 519669ae6ad2SNogah Frankel if (err && err != -ENODATA) 519769ae6ad2SNogah Frankel goto nla_put_failure; 519869ae6ad2SNogah Frankel *idxattr = 0; 519969ae6ad2SNogah Frankel } 520069ae6ad2SNogah Frankel 5201aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5202aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5203aefb4d4aSRobert Shearman 5204aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5205ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5206aefb4d4aSRobert Shearman if (!attr) 5207aefb4d4aSRobert Shearman goto nla_put_failure; 5208aefb4d4aSRobert Shearman 52095fa85a09SFlorian Westphal rcu_read_lock(); 52105fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5211aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5212aefb4d4aSRobert Shearman struct nlattr *af; 5213aefb4d4aSRobert Shearman int err; 5214aefb4d4aSRobert Shearman 5215ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5216ae0be8deSMichal Kubecek af_ops->family); 52175fa85a09SFlorian Westphal if (!af) { 52185fa85a09SFlorian Westphal rcu_read_unlock(); 5219aefb4d4aSRobert Shearman goto nla_put_failure; 52205fa85a09SFlorian Westphal } 5221aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5222aefb4d4aSRobert Shearman 52235fa85a09SFlorian Westphal if (err == -ENODATA) { 5224aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 52255fa85a09SFlorian Westphal } else if (err < 0) { 52265fa85a09SFlorian Westphal rcu_read_unlock(); 5227aefb4d4aSRobert Shearman goto nla_put_failure; 52285fa85a09SFlorian Westphal } 5229aefb4d4aSRobert Shearman 5230aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5231aefb4d4aSRobert Shearman } 5232aefb4d4aSRobert Shearman } 52335fa85a09SFlorian Westphal rcu_read_unlock(); 5234aefb4d4aSRobert Shearman 5235aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5236aefb4d4aSRobert Shearman 5237aefb4d4aSRobert Shearman *idxattr = 0; 5238aefb4d4aSRobert Shearman } 5239aefb4d4aSRobert Shearman 524010c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 524110c9ead9SRoopa Prabhu 524210c9ead9SRoopa Prabhu return 0; 524310c9ead9SRoopa Prabhu 524410c9ead9SRoopa Prabhu nla_put_failure: 5245e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5246e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 524710c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5248e8872a25SNikolay Aleksandrov else 5249e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 525010c9ead9SRoopa Prabhu 525110c9ead9SRoopa Prabhu return -EMSGSIZE; 525210c9ead9SRoopa Prabhu } 525310c9ead9SRoopa Prabhu 525410c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 525510c9ead9SRoopa Prabhu u32 filter_mask) 525610c9ead9SRoopa Prabhu { 525710c9ead9SRoopa Prabhu size_t size = 0; 525810c9ead9SRoopa Prabhu 5259e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 526010c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 526110c9ead9SRoopa Prabhu 526297a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 526397a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 526480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 526597a47facSNikolay Aleksandrov 526697a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 526780e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 526880e73cc5SNikolay Aleksandrov attr)); 526997a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 527097a47facSNikolay Aleksandrov size += nla_total_size(0); 527197a47facSNikolay Aleksandrov } 527297a47facSNikolay Aleksandrov } 527397a47facSNikolay Aleksandrov 527480e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 527580e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 527680e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 527780e73cc5SNikolay Aleksandrov const struct net_device *master; 527880e73cc5SNikolay Aleksandrov 527980e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 528080e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 528180e73cc5SNikolay Aleksandrov if (master) 528280e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 528380e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 528480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 528580e73cc5SNikolay Aleksandrov 528680e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 528780e73cc5SNikolay Aleksandrov attr)); 528880e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 528980e73cc5SNikolay Aleksandrov size += nla_total_size(0); 529080e73cc5SNikolay Aleksandrov } 529180e73cc5SNikolay Aleksandrov } 529280e73cc5SNikolay Aleksandrov 529369ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 529469ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 529569ae6ad2SNogah Frankel 5296aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5297aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5298aefb4d4aSRobert Shearman 5299aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5300aefb4d4aSRobert Shearman size += nla_total_size(0); 5301aefb4d4aSRobert Shearman 53025fa85a09SFlorian Westphal rcu_read_lock(); 53035fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5304aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5305aefb4d4aSRobert Shearman size += nla_total_size( 5306aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5307aefb4d4aSRobert Shearman 5308aefb4d4aSRobert Shearman /* for AF_* */ 5309aefb4d4aSRobert Shearman size += nla_total_size(0); 5310aefb4d4aSRobert Shearman } 5311aefb4d4aSRobert Shearman } 53125fa85a09SFlorian Westphal rcu_read_unlock(); 5313aefb4d4aSRobert Shearman } 5314aefb4d4aSRobert Shearman 531510c9ead9SRoopa Prabhu return size; 531610c9ead9SRoopa Prabhu } 531710c9ead9SRoopa Prabhu 531851bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 531951bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 532051bc860dSJakub Kicinski { 532151bc860dSJakub Kicinski struct if_stats_msg *ifsm; 532251bc860dSJakub Kicinski 532369f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 532451bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 532551bc860dSJakub Kicinski return -EINVAL; 532651bc860dSJakub Kicinski } 532751bc860dSJakub Kicinski 532851bc860dSJakub Kicinski if (!strict_check) 532951bc860dSJakub Kicinski return 0; 533051bc860dSJakub Kicinski 533151bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 533251bc860dSJakub Kicinski 533351bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 533451bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 533551bc860dSJakub Kicinski */ 533651bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 533751bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 533851bc860dSJakub Kicinski return -EINVAL; 533951bc860dSJakub Kicinski } 534051bc860dSJakub Kicinski if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 534151bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 534251bc860dSJakub Kicinski return -EINVAL; 534351bc860dSJakub Kicinski } 53446300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 53456300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 53466300acb2SJakub Kicinski return -EINVAL; 53476300acb2SJakub Kicinski } 534851bc860dSJakub Kicinski 534951bc860dSJakub Kicinski return 0; 535051bc860dSJakub Kicinski } 535151bc860dSJakub Kicinski 5352c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5353c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 535410c9ead9SRoopa Prabhu { 535510c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 535610c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5357e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5358e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 535910c9ead9SRoopa Prabhu struct sk_buff *nskb; 536010c9ead9SRoopa Prabhu u32 filter_mask; 536110c9ead9SRoopa Prabhu int err; 536210c9ead9SRoopa Prabhu 536351bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 536451bc860dSJakub Kicinski false, extack); 536551bc860dSJakub Kicinski if (err) 536651bc860dSJakub Kicinski return err; 53674775cc1fSMathias Krause 536810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 536910c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 537010c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 537110c9ead9SRoopa Prabhu else 537210c9ead9SRoopa Prabhu return -EINVAL; 537310c9ead9SRoopa Prabhu 537410c9ead9SRoopa Prabhu if (!dev) 537510c9ead9SRoopa Prabhu return -ENODEV; 537610c9ead9SRoopa Prabhu 537710c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 537810c9ead9SRoopa Prabhu if (!filter_mask) 537910c9ead9SRoopa Prabhu return -EINVAL; 538010c9ead9SRoopa Prabhu 538110c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 538210c9ead9SRoopa Prabhu if (!nskb) 538310c9ead9SRoopa Prabhu return -ENOBUFS; 538410c9ead9SRoopa Prabhu 538510c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 538610c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5387e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 538810c9ead9SRoopa Prabhu if (err < 0) { 538910c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 539010c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 539110c9ead9SRoopa Prabhu kfree_skb(nskb); 539210c9ead9SRoopa Prabhu } else { 539310c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 539410c9ead9SRoopa Prabhu } 539510c9ead9SRoopa Prabhu 539610c9ead9SRoopa Prabhu return err; 539710c9ead9SRoopa Prabhu } 539810c9ead9SRoopa Prabhu 539910c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 540010c9ead9SRoopa Prabhu { 5401841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5402e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 540310c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 540410c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5405e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5406e8872a25SNikolay Aleksandrov struct hlist_head *head; 5407e8872a25SNikolay Aleksandrov struct net_device *dev; 540810c9ead9SRoopa Prabhu u32 filter_mask = 0; 5409e8872a25SNikolay Aleksandrov int idx = 0; 541010c9ead9SRoopa Prabhu 541110c9ead9SRoopa Prabhu s_h = cb->args[0]; 541210c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5413e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5414e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 541510c9ead9SRoopa Prabhu 541610c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 541710c9ead9SRoopa Prabhu 541851bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 541951bc860dSJakub Kicinski if (err) 542051bc860dSJakub Kicinski return err; 54214775cc1fSMathias Krause 542210c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 5423841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 5424841891ecSDavid Ahern if (!filter_mask) { 5425841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5426841891ecSDavid Ahern return -EINVAL; 5427841891ecSDavid Ahern } 542810c9ead9SRoopa Prabhu 542910c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 543010c9ead9SRoopa Prabhu idx = 0; 543110c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 543210c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 543310c9ead9SRoopa Prabhu if (idx < s_idx) 543410c9ead9SRoopa Prabhu goto cont; 543510c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 543610c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 543710c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 5438e8872a25SNikolay Aleksandrov flags, filter_mask, 5439e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 544010c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 544110c9ead9SRoopa Prabhu * we're in trouble 544210c9ead9SRoopa Prabhu */ 544310c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 544410c9ead9SRoopa Prabhu 544510c9ead9SRoopa Prabhu if (err < 0) 544610c9ead9SRoopa Prabhu goto out; 5447e8872a25SNikolay Aleksandrov s_prividx = 0; 5448e8872a25SNikolay Aleksandrov s_idxattr = 0; 544910c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 545010c9ead9SRoopa Prabhu cont: 545110c9ead9SRoopa Prabhu idx++; 545210c9ead9SRoopa Prabhu } 545310c9ead9SRoopa Prabhu } 545410c9ead9SRoopa Prabhu out: 5455e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5456e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 545710c9ead9SRoopa Prabhu cb->args[1] = idx; 545810c9ead9SRoopa Prabhu cb->args[0] = h; 545910c9ead9SRoopa Prabhu 546010c9ead9SRoopa Prabhu return skb->len; 546110c9ead9SRoopa Prabhu } 546210c9ead9SRoopa Prabhu 54631da177e4SLinus Torvalds /* Process one rtnetlink message. */ 54641da177e4SLinus Torvalds 54652d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 54662d4bc933SJohannes Berg struct netlink_ext_ack *extack) 54671da177e4SLinus Torvalds { 54683b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5469addf9b90SFlorian Westphal struct rtnl_link *link; 5470e4202511SFlorian Westphal struct module *owner; 54716853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5472e2849863SThomas Graf rtnl_doit_func doit; 547362256f98SFlorian Westphal unsigned int flags; 5474617cfc75SAlexander Kuleshov int kind; 54751da177e4SLinus Torvalds int family; 54761da177e4SLinus Torvalds int type; 54771da177e4SLinus Torvalds 54781da177e4SLinus Torvalds type = nlh->nlmsg_type; 54791da177e4SLinus Torvalds if (type > RTM_MAX) 5480038890feSThomas Graf return -EOPNOTSUPP; 54811da177e4SLinus Torvalds 54821da177e4SLinus Torvalds type -= RTM_BASE; 54831da177e4SLinus Torvalds 54841da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5485573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 54861da177e4SLinus Torvalds return 0; 54871da177e4SLinus Torvalds 5488573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 54891da177e4SLinus Torvalds kind = type&3; 54901da177e4SLinus Torvalds 549190f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 54921d00a4ebSThomas Graf return -EPERM; 54931da177e4SLinus Torvalds 54946853dd48SFlorian Westphal rcu_read_lock(); 5495b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 549697c53cacSDenis V. Lunev struct sock *rtnl; 5497e2849863SThomas Graf rtnl_dumpit_func dumpit; 5498c7ac8679SGreg Rose u16 min_dump_alloc = 0; 54991da177e4SLinus Torvalds 5500addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5501addf9b90SFlorian Westphal if (!link || !link->dumpit) { 55026853dd48SFlorian Westphal family = PF_UNSPEC; 5503addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5504addf9b90SFlorian Westphal if (!link || !link->dumpit) 55056853dd48SFlorian Westphal goto err_unlock; 55066853dd48SFlorian Westphal } 5507e4202511SFlorian Westphal owner = link->owner; 5508addf9b90SFlorian Westphal dumpit = link->dumpit; 55096853dd48SFlorian Westphal 55105c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5511e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 55121da177e4SLinus Torvalds 5513e4202511SFlorian Westphal err = 0; 5514e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5515e4202511SFlorian Westphal if (!try_module_get(owner)) 5516e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5517e4202511SFlorian Westphal 55186853dd48SFlorian Westphal rcu_read_unlock(); 55196853dd48SFlorian Westphal 552097c53cacSDenis V. Lunev rtnl = net->rtnl; 5521e4202511SFlorian Westphal if (err == 0) { 552280d326faSPablo Neira Ayuso struct netlink_dump_control c = { 552380d326faSPablo Neira Ayuso .dump = dumpit, 552480d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5525e4202511SFlorian Westphal .module = owner, 552680d326faSPablo Neira Ayuso }; 552780d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5528e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5529e4202511SFlorian Westphal * module if dump is still in progress. 5530e4202511SFlorian Westphal */ 5531e4202511SFlorian Westphal module_put(owner); 553280d326faSPablo Neira Ayuso } 55332907c35fSEric Dumazet return err; 55341da177e4SLinus Torvalds } 55351da177e4SLinus Torvalds 5536addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5537addf9b90SFlorian Westphal if (!link || !link->doit) { 55388caa38b5SFlorian Westphal family = PF_UNSPEC; 5539addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 5540addf9b90SFlorian Westphal if (!link || !link->doit) 5541addf9b90SFlorian Westphal goto out_unlock; 55428caa38b5SFlorian Westphal } 55438caa38b5SFlorian Westphal 5544e4202511SFlorian Westphal owner = link->owner; 5545e4202511SFlorian Westphal if (!try_module_get(owner)) { 5546e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5547e4202511SFlorian Westphal goto out_unlock; 5548e4202511SFlorian Westphal } 5549e4202511SFlorian Westphal 5550addf9b90SFlorian Westphal flags = link->flags; 555162256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5552addf9b90SFlorian Westphal doit = link->doit; 555362256f98SFlorian Westphal rcu_read_unlock(); 555462256f98SFlorian Westphal if (doit) 555562256f98SFlorian Westphal err = doit(skb, nlh, extack); 5556e4202511SFlorian Westphal module_put(owner); 555762256f98SFlorian Westphal return err; 555862256f98SFlorian Westphal } 55596853dd48SFlorian Westphal rcu_read_unlock(); 55606853dd48SFlorian Westphal 55610cc09020SFlorian Westphal rtnl_lock(); 5562addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5563addf9b90SFlorian Westphal if (link && link->doit) 5564addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 55650cc09020SFlorian Westphal rtnl_unlock(); 5566addf9b90SFlorian Westphal 5567e4202511SFlorian Westphal module_put(owner); 5568e4202511SFlorian Westphal 5569addf9b90SFlorian Westphal return err; 5570addf9b90SFlorian Westphal 5571addf9b90SFlorian Westphal out_unlock: 5572addf9b90SFlorian Westphal rcu_read_unlock(); 55730cc09020SFlorian Westphal return err; 55740cc09020SFlorian Westphal 55750cc09020SFlorian Westphal err_unlock: 55766853dd48SFlorian Westphal rcu_read_unlock(); 55770cc09020SFlorian Westphal return -EOPNOTSUPP; 55781da177e4SLinus Torvalds } 55791da177e4SLinus Torvalds 5580cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 55811da177e4SLinus Torvalds { 5582cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 55831da177e4SLinus Torvalds } 55841da177e4SLinus Torvalds 55855f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 55865f729eaaSJulien Gomes { 55875f729eaaSJulien Gomes switch (group) { 55885f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 55895f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 55905f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 55915f729eaaSJulien Gomes return -EPERM; 55925f729eaaSJulien Gomes break; 55935f729eaaSJulien Gomes } 55945f729eaaSJulien Gomes return 0; 55955f729eaaSJulien Gomes } 55965f729eaaSJulien Gomes 55971da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 55981da177e4SLinus Torvalds { 5599351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5600e9dc8653SEric W. Biederman 56011da177e4SLinus Torvalds switch (event) { 56025138e86fSVlad Yasevich case NETDEV_REBOOT: 56038a212589SXin Long case NETDEV_CHANGEMTU: 56043753654eSDavid Ahern case NETDEV_CHANGEADDR: 56055138e86fSVlad Yasevich case NETDEV_CHANGENAME: 56065138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 56075138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 5608e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 56095138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 5610dc709f37SXin Long case NETDEV_CHANGEUPPER: 56115138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 56125138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 5613eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 5614ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 56153d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 561638e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 56171da177e4SLinus Torvalds break; 56181da177e4SLinus Torvalds default: 56191da177e4SLinus Torvalds break; 56201da177e4SLinus Torvalds } 56211da177e4SLinus Torvalds return NOTIFY_DONE; 56221da177e4SLinus Torvalds } 56231da177e4SLinus Torvalds 56241da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 56251da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 56261da177e4SLinus Torvalds }; 56271da177e4SLinus Torvalds 562897c53cacSDenis V. Lunev 56292c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 563097c53cacSDenis V. Lunev { 563197c53cacSDenis V. Lunev struct sock *sk; 5632a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 5633a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 5634a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 5635a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 56369785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 56375f729eaaSJulien Gomes .bind = rtnetlink_bind, 5638a31f2d17SPablo Neira Ayuso }; 5639a31f2d17SPablo Neira Ayuso 56409f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 564197c53cacSDenis V. Lunev if (!sk) 564297c53cacSDenis V. Lunev return -ENOMEM; 564397c53cacSDenis V. Lunev net->rtnl = sk; 564497c53cacSDenis V. Lunev return 0; 564597c53cacSDenis V. Lunev } 564697c53cacSDenis V. Lunev 56472c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 564897c53cacSDenis V. Lunev { 5649b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 565097c53cacSDenis V. Lunev net->rtnl = NULL; 565197c53cacSDenis V. Lunev } 565297c53cacSDenis V. Lunev 565397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 565497c53cacSDenis V. Lunev .init = rtnetlink_net_init, 565597c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 565697c53cacSDenis V. Lunev }; 565797c53cacSDenis V. Lunev 56581da177e4SLinus Torvalds void __init rtnetlink_init(void) 56591da177e4SLinus Torvalds { 566097c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 56611da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 566297c53cacSDenis V. Lunev 56631da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 5664340d17fcSThomas Graf 5665c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5666b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 5667b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5668b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5669b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5670687ad8ccSThomas Graf 5671b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5672b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5673b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 567477162022SJohn Fastabend 567536fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 567636fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 567736fbf1e5SJiri Pirko 5678b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5679b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 56805b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5681e5a55a89SJohn Fastabend 5682b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5683b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5684b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 568510c9ead9SRoopa Prabhu 568610c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5687b97bac64SFlorian Westphal 0); 56881da177e4SLinus Torvalds } 5689