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 || 832b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 833b00055aaSStefan Rompf !netif_dormant(dev)) 834b00055aaSStefan Rompf operstate = IF_OPER_UP; 835b00055aaSStefan Rompf break; 836b00055aaSStefan Rompf 837b00055aaSStefan Rompf case IF_OPER_DORMANT: 838b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 839b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 840b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 841b00055aaSStefan Rompf break; 8423ff50b79SStephen Hemminger } 843b00055aaSStefan Rompf 844b00055aaSStefan Rompf if (dev->operstate != operstate) { 845b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 846b00055aaSStefan Rompf dev->operstate = operstate; 847b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 848b00055aaSStefan Rompf netdev_state_change(dev); 84993b2d4a2SDavid S. Miller } 850b00055aaSStefan Rompf } 851b00055aaSStefan Rompf 852b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 853b1beb681SJiri Benc { 854b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 855b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 856b1beb681SJiri Benc } 857b1beb681SJiri Benc 8583729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8593729d502SPatrick McHardy const struct ifinfomsg *ifm) 8603729d502SPatrick McHardy { 8613729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8623729d502SPatrick McHardy 8633729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8643729d502SPatrick McHardy if (ifm->ifi_change) 8653729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 866b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8673729d502SPatrick McHardy 8683729d502SPatrick McHardy return flags; 8693729d502SPatrick McHardy } 8703729d502SPatrick McHardy 871b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 872be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8731da177e4SLinus Torvalds { 874b60c5115SThomas Graf a->rx_packets = b->rx_packets; 875b60c5115SThomas Graf a->tx_packets = b->tx_packets; 876b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 877b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 878b60c5115SThomas Graf a->rx_errors = b->rx_errors; 879b60c5115SThomas Graf a->tx_errors = b->tx_errors; 880b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 881b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 882b60c5115SThomas Graf 883b60c5115SThomas Graf a->multicast = b->multicast; 884b60c5115SThomas Graf a->collisions = b->collisions; 885b60c5115SThomas Graf 886b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 887b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 888b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 889b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 890b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 891b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 892b60c5115SThomas Graf 893b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 894b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 895b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 896b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 897b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 898b60c5115SThomas Graf 899b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 900b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9016e7333d3SJarod Wilson 9026e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 90310708f37SJan Engelhardt } 90410708f37SJan Engelhardt 905c02db8c6SChris Wright /* All VF info */ 906115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 907115c9b81SGreg Rose u32 ext_filter_mask) 908ebc08a6fSWilliams, Mitch A { 9099af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 910c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9117e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 912045de01aSScott Feldman size += num_vfs * 9137e75f74aSSabrina Dubroca (nla_total_size(0) + 9147e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 91575345f88SDenis Kirjanov nla_total_size(sizeof(struct ifla_vf_broadcast)) + 9167e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9177e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 91879aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 91979aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 920ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9217e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 922945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 92301a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9243b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9257e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9263b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 927343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9283b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 929343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9303b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 931343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9323b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 933343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9343b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 935343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9363b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 937343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 938c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 939c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 940c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 941c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 942dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 943c02db8c6SChris Wright return size; 944c02db8c6SChris Wright } else 945ebc08a6fSWilliams, Mitch A return 0; 946ebc08a6fSWilliams, Mitch A } 947ebc08a6fSWilliams, Mitch A 948c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 949c53864fdSDavid Gibson u32 ext_filter_mask) 95057b61080SScott Feldman { 95157b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 95257b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 95357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 95457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 95557b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 95657b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 95757b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 95857b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 95957b61080SScott Feldman + port_size; 96057b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 96157b61080SScott Feldman + port_size; 96257b61080SScott Feldman 963c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 964c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 96557b61080SScott Feldman return 0; 96657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 96757b61080SScott Feldman return port_self_size + vf_ports_size + 96857b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 96957b61080SScott Feldman else 97057b61080SScott Feldman return port_self_size; 97157b61080SScott Feldman } 97257b61080SScott Feldman 973b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 974d1fdd913SBrenden Blanco { 975b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 97658038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 977a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9784f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 979d1fdd913SBrenden Blanco 980d1fdd913SBrenden Blanco return xdp_size; 981d1fdd913SBrenden Blanco } 982d1fdd913SBrenden Blanco 98388f4fb0cSJiri Pirko static size_t rtnl_prop_list_size(const struct net_device *dev) 98488f4fb0cSJiri Pirko { 98588f4fb0cSJiri Pirko struct netdev_name_node *name_node; 98688f4fb0cSJiri Pirko size_t size; 98788f4fb0cSJiri Pirko 98888f4fb0cSJiri Pirko if (list_empty(&dev->name_node->list)) 98988f4fb0cSJiri Pirko return 0; 99088f4fb0cSJiri Pirko size = nla_total_size(0); 99188f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) 99288f4fb0cSJiri Pirko size += nla_total_size(ALTIFNAMSIZ); 99388f4fb0cSJiri Pirko return size; 99488f4fb0cSJiri Pirko } 99588f4fb0cSJiri Pirko 996115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 997115c9b81SGreg Rose u32 ext_filter_mask) 998339bf98fSThomas Graf { 999339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1000339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 10010b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1002339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1003270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1004339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 100535c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1006339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1007339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1008339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 1009339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 1010339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 1011339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 1012339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10139a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1014edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 101576ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 101676ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10176919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10186919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1019339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 102038f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10212d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1022d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1023db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1024115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1025115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1026115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1027c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1028f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1029b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 103082f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 103188d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1032c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1033b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10343d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10356621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 103638e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 103703ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 10387e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1039b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1040b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10413e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10423e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 104388f4fb0cSJiri Pirko + rtnl_prop_list_size(dev) 1044f74877a5SMichal Kubecek + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 104579e1ad14SJiri Benc + 0; 1046339bf98fSThomas Graf } 1047339bf98fSThomas Graf 104857b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 104957b61080SScott Feldman { 105057b61080SScott Feldman struct nlattr *vf_ports; 105157b61080SScott Feldman struct nlattr *vf_port; 105257b61080SScott Feldman int vf; 105357b61080SScott Feldman int err; 105457b61080SScott Feldman 1055ae0be8deSMichal Kubecek vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 105657b61080SScott Feldman if (!vf_ports) 105757b61080SScott Feldman return -EMSGSIZE; 105857b61080SScott Feldman 105957b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1060ae0be8deSMichal Kubecek vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 10618ca94183SScott Feldman if (!vf_port) 10628ca94183SScott Feldman goto nla_put_failure; 1063a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1064a6574349SDavid S. Miller goto nla_put_failure; 106557b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10668ca94183SScott Feldman if (err == -EMSGSIZE) 10678ca94183SScott Feldman goto nla_put_failure; 106857b61080SScott Feldman if (err) { 106957b61080SScott Feldman nla_nest_cancel(skb, vf_port); 107057b61080SScott Feldman continue; 107157b61080SScott Feldman } 107257b61080SScott Feldman nla_nest_end(skb, vf_port); 107357b61080SScott Feldman } 107457b61080SScott Feldman 107557b61080SScott Feldman nla_nest_end(skb, vf_ports); 107657b61080SScott Feldman 107757b61080SScott Feldman return 0; 10788ca94183SScott Feldman 10798ca94183SScott Feldman nla_put_failure: 10808ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10818ca94183SScott Feldman return -EMSGSIZE; 108257b61080SScott Feldman } 108357b61080SScott Feldman 108457b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 108557b61080SScott Feldman { 108657b61080SScott Feldman struct nlattr *port_self; 108757b61080SScott Feldman int err; 108857b61080SScott Feldman 1089ae0be8deSMichal Kubecek port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 109057b61080SScott Feldman if (!port_self) 109157b61080SScott Feldman return -EMSGSIZE; 109257b61080SScott Feldman 109357b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 109457b61080SScott Feldman if (err) { 109557b61080SScott Feldman nla_nest_cancel(skb, port_self); 10968ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 109757b61080SScott Feldman } 109857b61080SScott Feldman 109957b61080SScott Feldman nla_nest_end(skb, port_self); 110057b61080SScott Feldman 110157b61080SScott Feldman return 0; 110257b61080SScott Feldman } 110357b61080SScott Feldman 1104c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1105c53864fdSDavid Gibson u32 ext_filter_mask) 110657b61080SScott Feldman { 110757b61080SScott Feldman int err; 110857b61080SScott Feldman 1109c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1110c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 111157b61080SScott Feldman return 0; 111257b61080SScott Feldman 111357b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 111457b61080SScott Feldman if (err) 111557b61080SScott Feldman return err; 111657b61080SScott Feldman 111757b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 111857b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 111957b61080SScott Feldman if (err) 112057b61080SScott Feldman return err; 112157b61080SScott Feldman } 112257b61080SScott Feldman 112357b61080SScott Feldman return 0; 112457b61080SScott Feldman } 112557b61080SScott Feldman 112666cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 112766cae9edSJiri Pirko { 112866cae9edSJiri Pirko int err; 112902637fceSJiri Pirko struct netdev_phys_item_id ppid; 113066cae9edSJiri Pirko 113166cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 113266cae9edSJiri Pirko if (err) { 113366cae9edSJiri Pirko if (err == -EOPNOTSUPP) 113466cae9edSJiri Pirko return 0; 113566cae9edSJiri Pirko return err; 113666cae9edSJiri Pirko } 113766cae9edSJiri Pirko 113866cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 113966cae9edSJiri Pirko return -EMSGSIZE; 114066cae9edSJiri Pirko 114166cae9edSJiri Pirko return 0; 114266cae9edSJiri Pirko } 114366cae9edSJiri Pirko 1144db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1145db24a904SDavid Ahern { 1146db24a904SDavid Ahern char name[IFNAMSIZ]; 1147db24a904SDavid Ahern int err; 1148db24a904SDavid Ahern 1149db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1150db24a904SDavid Ahern if (err) { 1151db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1152db24a904SDavid Ahern return 0; 1153db24a904SDavid Ahern return err; 1154db24a904SDavid Ahern } 1155db24a904SDavid Ahern 115677ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1157db24a904SDavid Ahern return -EMSGSIZE; 1158db24a904SDavid Ahern 1159db24a904SDavid Ahern return 0; 1160db24a904SDavid Ahern } 1161db24a904SDavid Ahern 116282f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 116382f28412SJiri Pirko { 1164bccb3025SFlorian Fainelli struct netdev_phys_item_id ppid = { }; 116582f28412SJiri Pirko int err; 116682f28412SJiri Pirko 1167bccb3025SFlorian Fainelli err = dev_get_port_parent_id(dev, &ppid, false); 116882f28412SJiri Pirko if (err) { 116982f28412SJiri Pirko if (err == -EOPNOTSUPP) 117082f28412SJiri Pirko return 0; 117182f28412SJiri Pirko return err; 117282f28412SJiri Pirko } 117382f28412SJiri Pirko 1174bccb3025SFlorian Fainelli if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 117582f28412SJiri Pirko return -EMSGSIZE; 117682f28412SJiri Pirko 117782f28412SJiri Pirko return 0; 117882f28412SJiri Pirko } 117982f28412SJiri Pirko 1180b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1181b22b941bSHannes Frederic Sowa struct net_device *dev) 1182b22b941bSHannes Frederic Sowa { 1183550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1184b22b941bSHannes Frederic Sowa struct nlattr *attr; 1185b22b941bSHannes Frederic Sowa 118658414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 118758414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1188b22b941bSHannes Frederic Sowa if (!attr) 1189b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1190b22b941bSHannes Frederic Sowa 1191550bce59SRoopa Prabhu sp = nla_data(attr); 1192550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1193550bce59SRoopa Prabhu 1194550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1195550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1196550bce59SRoopa Prabhu if (!attr) 1197550bce59SRoopa Prabhu return -EMSGSIZE; 1198550bce59SRoopa Prabhu 1199550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1200b22b941bSHannes Frederic Sowa 1201b22b941bSHannes Frederic Sowa return 0; 1202b22b941bSHannes Frederic Sowa } 1203b22b941bSHannes Frederic Sowa 1204b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1205b22b941bSHannes Frederic Sowa struct net_device *dev, 1206b22b941bSHannes Frederic Sowa int vfs_num, 1207b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1208b22b941bSHannes Frederic Sowa { 1209b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 121079aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1211b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 121279aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1213b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1214b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1215b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1216b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1217b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1218b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1219b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 122075345f88SDenis Kirjanov struct ifla_vf_broadcast vf_broadcast; 1221b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 122230aad417SDanit Goldberg struct ifla_vf_guid node_guid; 122330aad417SDanit Goldberg struct ifla_vf_guid port_guid; 1224b22b941bSHannes Frederic Sowa 12250eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12260eed9cf5SMintz, Yuval 1227b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1228b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1229b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1230b22b941bSHannes Frederic Sowa * didn't report anything. 1231b22b941bSHannes Frederic Sowa */ 1232b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1233b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1234b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1235b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1236b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1237b22b941bSHannes Frederic Sowa */ 1238b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 123979aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 124079aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1241b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1242b22b941bSHannes Frederic Sowa return 0; 1243b22b941bSHannes Frederic Sowa 1244775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 12459fbf082fSLeon Romanovsky memset(&node_guid, 0, sizeof(node_guid)); 12469fbf082fSLeon Romanovsky memset(&port_guid, 0, sizeof(port_guid)); 1247775f4f05SDan Carpenter 1248b22b941bSHannes Frederic Sowa vf_mac.vf = 1249b22b941bSHannes Frederic Sowa vf_vlan.vf = 125079aab093SMoshe Shemesh vf_vlan_info.vf = 1251b22b941bSHannes Frederic Sowa vf_rate.vf = 1252b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1253b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1254b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1255b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 12569aed6ae0SDanit Goldberg vf_trust.vf = 12579aed6ae0SDanit Goldberg node_guid.vf = 12589aed6ae0SDanit Goldberg port_guid.vf = ivi.vf; 1259b22b941bSHannes Frederic Sowa 1260b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 126175345f88SDenis Kirjanov memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1262b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1263b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 126479aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 126579aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 126679aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1267b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1268b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1269b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1270b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1271b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1272b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1273b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1274ae0be8deSMichal Kubecek vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 127579aab093SMoshe Shemesh if (!vf) 127679aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1277b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 127875345f88SDenis Kirjanov nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1279b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1280b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1281b22b941bSHannes Frederic Sowa &vf_rate) || 1282b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1283b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1284b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1285b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1286b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1287b22b941bSHannes Frederic Sowa &vf_linkstate) || 1288b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1289b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1290b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1291b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1292b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 129379aab093SMoshe Shemesh goto nla_put_vf_failure; 129430aad417SDanit Goldberg 129530aad417SDanit Goldberg if (dev->netdev_ops->ndo_get_vf_guid && 129630aad417SDanit Goldberg !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 129730aad417SDanit Goldberg &port_guid)) { 129830aad417SDanit Goldberg if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 129930aad417SDanit Goldberg &node_guid) || 130030aad417SDanit Goldberg nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 130130aad417SDanit Goldberg &port_guid)) 130230aad417SDanit Goldberg goto nla_put_vf_failure; 130330aad417SDanit Goldberg } 1304ae0be8deSMichal Kubecek vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 130579aab093SMoshe Shemesh if (!vfvlanlist) 130679aab093SMoshe Shemesh goto nla_put_vf_failure; 130779aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 130879aab093SMoshe Shemesh &vf_vlan_info)) { 130979aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 131079aab093SMoshe Shemesh goto nla_put_vf_failure; 131179aab093SMoshe Shemesh } 131279aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1313b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1314b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1315b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1316b22b941bSHannes Frederic Sowa &vf_stats); 1317ae0be8deSMichal Kubecek vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 131879aab093SMoshe Shemesh if (!vfstats) 131979aab093SMoshe Shemesh goto nla_put_vf_failure; 1320343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1321343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1322343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1323343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1324343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1325343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1326343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1327343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1328343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1329343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1330343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1331c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1332c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1333c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1334c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1335c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 133679aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 133779aab093SMoshe Shemesh goto nla_put_vf_failure; 133879aab093SMoshe Shemesh } 1339b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1340b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1341b22b941bSHannes Frederic Sowa return 0; 134279aab093SMoshe Shemesh 134379aab093SMoshe Shemesh nla_put_vf_failure: 134479aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 134579aab093SMoshe Shemesh nla_put_vfinfo_failure: 134679aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 134779aab093SMoshe Shemesh return -EMSGSIZE; 1348b22b941bSHannes Frederic Sowa } 1349b22b941bSHannes Frederic Sowa 1350250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1351250fc3dfSFlorian Westphal struct net_device *dev, 1352250fc3dfSFlorian Westphal u32 ext_filter_mask) 1353250fc3dfSFlorian Westphal { 1354250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1355250fc3dfSFlorian Westphal int i, num_vfs; 1356250fc3dfSFlorian Westphal 1357250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1358250fc3dfSFlorian Westphal return 0; 1359250fc3dfSFlorian Westphal 1360250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1361250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1362250fc3dfSFlorian Westphal return -EMSGSIZE; 1363250fc3dfSFlorian Westphal 1364250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1365250fc3dfSFlorian Westphal return 0; 1366250fc3dfSFlorian Westphal 1367ae0be8deSMichal Kubecek vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1368250fc3dfSFlorian Westphal if (!vfinfo) 1369250fc3dfSFlorian Westphal return -EMSGSIZE; 1370250fc3dfSFlorian Westphal 1371250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1372250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1373250fc3dfSFlorian Westphal return -EMSGSIZE; 1374250fc3dfSFlorian Westphal } 1375250fc3dfSFlorian Westphal 1376250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1377250fc3dfSFlorian Westphal return 0; 1378250fc3dfSFlorian Westphal } 1379250fc3dfSFlorian Westphal 1380b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1381b22b941bSHannes Frederic Sowa { 13825f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13835f8e4474SKangjie Lu 13845f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13855f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13865f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13875f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13885f8e4474SKangjie Lu map.irq = dev->irq; 13895f8e4474SKangjie Lu map.dma = dev->dma; 13905f8e4474SKangjie Lu map.port = dev->if_port; 13915f8e4474SKangjie Lu 1392270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1393b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1394b22b941bSHannes Frederic Sowa 1395b22b941bSHannes Frederic Sowa return 0; 1396b22b941bSHannes Frederic Sowa } 1397b22b941bSHannes Frederic Sowa 1398a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1399d67b9cd2SDaniel Borkmann { 140058038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1401d67b9cd2SDaniel Borkmann 1402d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1403d67b9cd2SDaniel Borkmann 140458038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1405a25717d2SJakub Kicinski if (!generic_xdp_prog) 1406a25717d2SJakub Kicinski return 0; 1407a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 140858038695SMartin KaFai Lau } 1409ce158e58SJakub Kicinski 1410a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1411a25717d2SJakub Kicinski { 1412a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1413a25717d2SJakub Kicinski } 1414118b4aa2SJakub Kicinski 1415a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1416a25717d2SJakub Kicinski { 1417a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1418a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1419a25717d2SJakub Kicinski } 1420a25717d2SJakub Kicinski 1421a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1422a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1423a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1424a25717d2SJakub Kicinski { 1425a25717d2SJakub Kicinski u32 curr_id; 1426a25717d2SJakub Kicinski int err; 1427a25717d2SJakub Kicinski 1428a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1429a25717d2SJakub Kicinski if (!curr_id) 1430a25717d2SJakub Kicinski return 0; 1431a25717d2SJakub Kicinski 1432a25717d2SJakub Kicinski *prog_id = curr_id; 1433a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1434a25717d2SJakub Kicinski if (err) 1435a25717d2SJakub Kicinski return err; 1436a25717d2SJakub Kicinski 1437a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1438a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1439a25717d2SJakub Kicinski else 1440a25717d2SJakub Kicinski *mode = tgt_mode; 1441a25717d2SJakub Kicinski 1442a25717d2SJakub Kicinski return 0; 1443d67b9cd2SDaniel Borkmann } 1444d67b9cd2SDaniel Borkmann 1445d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1446d1fdd913SBrenden Blanco { 1447d1fdd913SBrenden Blanco struct nlattr *xdp; 144858038695SMartin KaFai Lau u32 prog_id; 1449d1fdd913SBrenden Blanco int err; 14504f91da26SJakub Kicinski u8 mode; 1451d1fdd913SBrenden Blanco 1452ae0be8deSMichal Kubecek xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1453d1fdd913SBrenden Blanco if (!xdp) 1454d1fdd913SBrenden Blanco return -EMSGSIZE; 1455b5cdae32SDavid S. Miller 1456a25717d2SJakub Kicinski prog_id = 0; 1457a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1458202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1459202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1460202aabe8SJakub Kicinski if (err) 1461a25717d2SJakub Kicinski goto err_cancel; 1462202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1463202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1464202aabe8SJakub Kicinski if (err) 1465a25717d2SJakub Kicinski goto err_cancel; 1466202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1467202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1468202aabe8SJakub Kicinski if (err) 1469a25717d2SJakub Kicinski goto err_cancel; 1470a25717d2SJakub Kicinski 14714f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1472d1fdd913SBrenden Blanco if (err) 1473d1fdd913SBrenden Blanco goto err_cancel; 1474d1fdd913SBrenden Blanco 1475a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 147658038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 147758038695SMartin KaFai Lau if (err) 147858038695SMartin KaFai Lau goto err_cancel; 147958038695SMartin KaFai Lau } 148058038695SMartin KaFai Lau 1481d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1482d1fdd913SBrenden Blanco return 0; 1483d1fdd913SBrenden Blanco 1484d1fdd913SBrenden Blanco err_cancel: 1485d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1486d1fdd913SBrenden Blanco return err; 1487d1fdd913SBrenden Blanco } 1488d1fdd913SBrenden Blanco 14893d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14903d3ea5afSVlad Yasevich { 14913d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14923d3ea5afSVlad Yasevich 14933d3ea5afSVlad Yasevich switch (event) { 14943d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14953d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14963d3ea5afSVlad Yasevich break; 14973d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14983d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14993d3ea5afSVlad Yasevich break; 15003d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 15013d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 15023d3ea5afSVlad Yasevich break; 15033d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 15043d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 15053d3ea5afSVlad Yasevich break; 15063d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 15073d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 15083d3ea5afSVlad Yasevich break; 15093d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 15103d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 15113d3ea5afSVlad Yasevich break; 15123d3ea5afSVlad Yasevich default: 15133d3ea5afSVlad Yasevich break; 15143d3ea5afSVlad Yasevich } 15153d3ea5afSVlad Yasevich 15163d3ea5afSVlad Yasevich return rtnl_event_type; 15173d3ea5afSVlad Yasevich } 15183d3ea5afSVlad Yasevich 151979110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 152079110a04SFlorian Westphal { 152179110a04SFlorian Westphal const struct net_device *upper_dev; 152279110a04SFlorian Westphal int ret = 0; 152379110a04SFlorian Westphal 152479110a04SFlorian Westphal rcu_read_lock(); 152579110a04SFlorian Westphal 152679110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 152779110a04SFlorian Westphal if (upper_dev) 152879110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 152979110a04SFlorian Westphal 153079110a04SFlorian Westphal rcu_read_unlock(); 153179110a04SFlorian Westphal return ret; 153279110a04SFlorian Westphal } 153379110a04SFlorian Westphal 1534feadc4b6SSabrina Dubroca static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1535feadc4b6SSabrina Dubroca bool force) 153679110a04SFlorian Westphal { 153779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 153879110a04SFlorian Westphal 1539feadc4b6SSabrina Dubroca if (force || dev->ifindex != ifindex) 154079110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 1541feadc4b6SSabrina Dubroca 1542feadc4b6SSabrina Dubroca return 0; 154379110a04SFlorian Westphal } 154479110a04SFlorian Westphal 15456c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15466c557001SFlorian Westphal struct net_device *dev) 15476c557001SFlorian Westphal { 15486c557001SFlorian Westphal char buf[IFALIASZ]; 15496c557001SFlorian Westphal int ret; 15506c557001SFlorian Westphal 15516c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15526c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15536c557001SFlorian Westphal } 15546c557001SFlorian Westphal 1555b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 155679e1ad14SJiri Benc const struct net_device *dev, 1557d4e4fdf9SGuillaume Nault struct net *src_net, gfp_t gfp) 1558b1e66b9aSFlorian Westphal { 1559feadc4b6SSabrina Dubroca bool put_iflink = false; 1560feadc4b6SSabrina Dubroca 1561b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1562b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1563b1e66b9aSFlorian Westphal 1564b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 1565d4e4fdf9SGuillaume Nault int id = peernet2id_alloc(src_net, link_net, gfp); 1566b1e66b9aSFlorian Westphal 1567b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1568b1e66b9aSFlorian Westphal return -EMSGSIZE; 1569feadc4b6SSabrina Dubroca 1570feadc4b6SSabrina Dubroca put_iflink = true; 1571b1e66b9aSFlorian Westphal } 1572b1e66b9aSFlorian Westphal } 1573b1e66b9aSFlorian Westphal 1574feadc4b6SSabrina Dubroca return nla_put_iflink(skb, dev, put_iflink); 1575b1e66b9aSFlorian Westphal } 1576b1e66b9aSFlorian Westphal 1577070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1578070cbf5bSFlorian Westphal const struct net_device *dev, 1579070cbf5bSFlorian Westphal u32 ext_filter_mask) 1580070cbf5bSFlorian Westphal { 1581070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1582070cbf5bSFlorian Westphal struct nlattr *af_spec; 1583070cbf5bSFlorian Westphal 1584ae0be8deSMichal Kubecek af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1585070cbf5bSFlorian Westphal if (!af_spec) 1586070cbf5bSFlorian Westphal return -EMSGSIZE; 1587070cbf5bSFlorian Westphal 15885fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1589070cbf5bSFlorian Westphal struct nlattr *af; 1590070cbf5bSFlorian Westphal int err; 1591070cbf5bSFlorian Westphal 1592070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1593070cbf5bSFlorian Westphal continue; 1594070cbf5bSFlorian Westphal 1595ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, af_ops->family); 1596070cbf5bSFlorian Westphal if (!af) 1597070cbf5bSFlorian Westphal return -EMSGSIZE; 1598070cbf5bSFlorian Westphal 1599070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1600070cbf5bSFlorian Westphal /* 1601070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1602070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1603070cbf5bSFlorian Westphal * means we should trim the attribute header and 1604070cbf5bSFlorian Westphal * continue. 1605070cbf5bSFlorian Westphal */ 1606070cbf5bSFlorian Westphal if (err == -ENODATA) 1607070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1608070cbf5bSFlorian Westphal else if (err < 0) 1609070cbf5bSFlorian Westphal return -EMSGSIZE; 1610070cbf5bSFlorian Westphal 1611070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1612070cbf5bSFlorian Westphal } 1613070cbf5bSFlorian Westphal 1614070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1615070cbf5bSFlorian Westphal return 0; 1616070cbf5bSFlorian Westphal } 1617070cbf5bSFlorian Westphal 161888f4fb0cSJiri Pirko static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 161988f4fb0cSJiri Pirko const struct net_device *dev) 162088f4fb0cSJiri Pirko { 162188f4fb0cSJiri Pirko struct netdev_name_node *name_node; 162288f4fb0cSJiri Pirko int count = 0; 162388f4fb0cSJiri Pirko 162488f4fb0cSJiri Pirko list_for_each_entry(name_node, &dev->name_node->list, list) { 162588f4fb0cSJiri Pirko if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 162688f4fb0cSJiri Pirko return -EMSGSIZE; 162788f4fb0cSJiri Pirko count++; 162888f4fb0cSJiri Pirko } 162988f4fb0cSJiri Pirko return count; 163088f4fb0cSJiri Pirko } 163188f4fb0cSJiri Pirko 163288f4fb0cSJiri Pirko static int rtnl_fill_prop_list(struct sk_buff *skb, 163388f4fb0cSJiri Pirko const struct net_device *dev) 163488f4fb0cSJiri Pirko { 163588f4fb0cSJiri Pirko struct nlattr *prop_list; 163688f4fb0cSJiri Pirko int ret; 163788f4fb0cSJiri Pirko 163888f4fb0cSJiri Pirko prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 163988f4fb0cSJiri Pirko if (!prop_list) 164088f4fb0cSJiri Pirko return -EMSGSIZE; 164188f4fb0cSJiri Pirko 164288f4fb0cSJiri Pirko ret = rtnl_fill_alt_ifnames(skb, dev); 164388f4fb0cSJiri Pirko if (ret <= 0) 164488f4fb0cSJiri Pirko goto nest_cancel; 164588f4fb0cSJiri Pirko 164688f4fb0cSJiri Pirko nla_nest_end(skb, prop_list); 164788f4fb0cSJiri Pirko return 0; 164888f4fb0cSJiri Pirko 164988f4fb0cSJiri Pirko nest_cancel: 165088f4fb0cSJiri Pirko nla_nest_cancel(skb, prop_list); 165188f4fb0cSJiri Pirko return ret; 165288f4fb0cSJiri Pirko } 165388f4fb0cSJiri Pirko 165479e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 165579e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1656575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 16573d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 165838e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 1659d4e4fdf9SGuillaume Nault int tgt_netnsid, gfp_t gfp) 1660b60c5115SThomas Graf { 1661b60c5115SThomas Graf struct ifinfomsg *ifm; 16621da177e4SLinus Torvalds struct nlmsghdr *nlh; 16631da177e4SLinus Torvalds 16642907c35fSEric Dumazet ASSERT_RTNL(); 1665b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1666b60c5115SThomas Graf if (nlh == NULL) 166726932566SPatrick McHardy return -EMSGSIZE; 16681da177e4SLinus Torvalds 1669b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1670b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1671b60c5115SThomas Graf ifm->__ifi_pad = 0; 1672b60c5115SThomas Graf ifm->ifi_type = dev->type; 1673b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1674b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1675b60c5115SThomas Graf ifm->ifi_change = change; 16761da177e4SLinus Torvalds 16777e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 167879e1ad14SJiri Benc goto nla_put_failure; 167979e1ad14SJiri Benc 1680a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1681a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1682a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1683a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1684a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1685a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 16863e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 16873e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1688a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1689edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 169076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1691c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1692c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 16931d69c2b3SMark A. Greer #ifdef CONFIG_RPS 169476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 16951d69c2b3SMark A. Greer #endif 169679110a04SFlorian Westphal put_master_ifindex(skb, dev) || 16979a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1698a6574349SDavid S. Miller (dev->qdisc && 1699a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 17006c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 17012d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1702b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1703b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1704b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1705b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1706b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1707b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1708b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1709a6574349SDavid S. Miller goto nla_put_failure; 17100b815a1aSStephen Hemminger 17113d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 17123d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 17133d3ea5afSVlad Yasevich goto nla_put_failure; 17143d3ea5afSVlad Yasevich } 17153d3ea5afSVlad Yasevich 1716b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1717a6574349SDavid S. Miller goto nla_put_failure; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds if (dev->addr_len) { 1720a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1721a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1722a6574349SDavid S. Miller goto nla_put_failure; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 172566cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 172666cae9edSJiri Pirko goto nla_put_failure; 172766cae9edSJiri Pirko 1728db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1729db24a904SDavid Ahern goto nla_put_failure; 1730db24a904SDavid Ahern 173182f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 173282f28412SJiri Pirko goto nla_put_failure; 173382f28412SJiri Pirko 1734b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1735b60c5115SThomas Graf goto nla_put_failure; 1736b60c5115SThomas Graf 1737250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1738a6574349SDavid S. Miller goto nla_put_failure; 173957b61080SScott Feldman 1740c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 174157b61080SScott Feldman goto nla_put_failure; 174257b61080SScott Feldman 1743d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1744d1fdd913SBrenden Blanco goto nla_put_failure; 1745d1fdd913SBrenden Blanco 1746ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 174738f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 174838f7b870SPatrick McHardy goto nla_put_failure; 174938f7b870SPatrick McHardy } 175038f7b870SPatrick McHardy 1751d4e4fdf9SGuillaume Nault if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp)) 1752d37512a2SNicolas Dichtel goto nla_put_failure; 1753d37512a2SNicolas Dichtel 17546621dd29SNicolas Dichtel if (new_nsid && 17556621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 17566621dd29SNicolas Dichtel goto nla_put_failure; 175738e01b30SNicolas Dichtel if (new_ifindex && 175838e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 175938e01b30SNicolas Dichtel goto nla_put_failure; 176038e01b30SNicolas Dichtel 1761f74877a5SMichal Kubecek if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 1762f74877a5SMichal Kubecek nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 1763f74877a5SMichal Kubecek goto nla_put_failure; 17646621dd29SNicolas Dichtel 17655fa85a09SFlorian Westphal rcu_read_lock(); 1766070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 17675fa85a09SFlorian Westphal goto nla_put_failure_rcu; 17685fa85a09SFlorian Westphal rcu_read_unlock(); 1769f8ff182cSThomas Graf 177088f4fb0cSJiri Pirko if (rtnl_fill_prop_list(skb, dev)) 177188f4fb0cSJiri Pirko goto nla_put_failure; 177288f4fb0cSJiri Pirko 1773053c095aSJohannes Berg nlmsg_end(skb, nlh); 1774053c095aSJohannes Berg return 0; 1775b60c5115SThomas Graf 17765fa85a09SFlorian Westphal nla_put_failure_rcu: 17775fa85a09SFlorian Westphal rcu_read_unlock(); 1778b60c5115SThomas Graf nla_put_failure: 177926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 178026932566SPatrick McHardy return -EMSGSIZE; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 1783f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1784f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1785f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1786f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1787f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1788f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1789f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1790f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1791f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1792f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1793f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1794f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1795f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1796f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1797f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1798f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 17992459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 18002459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 18012459b4c6SNicolas Dichtel */ 18022459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1803f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1804f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1805f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1806f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1807f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1808f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1809f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1810f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 181146e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 181246e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 181302637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 18142d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 181582f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1816317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 181788d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1818d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 18193d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1820db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 18217e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1822b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1823b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 18243e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 18253e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 182636fbf1e5SJiri Pirko [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 182736fbf1e5SJiri Pirko [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 182836fbf1e5SJiri Pirko .len = ALTIFNAMSIZ - 1 }, 1829f74877a5SMichal Kubecek [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 1830f7b12606SJiri Pirko }; 1831f7b12606SJiri Pirko 1832f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1833f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1834f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1835f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1836f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1837f7b12606SJiri Pirko }; 1838f7b12606SJiri Pirko 1839f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1840364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 184175345f88SDenis Kirjanov [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 1842364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 184379aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1844364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1845364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1846364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1847364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 184801a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 18493b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1850dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1851cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1852cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 18533b766cd8SEran Ben Elisha }; 18543b766cd8SEran Ben Elisha 1855f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1856f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1857f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1858f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1859f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1860f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1861f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1862f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1863f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1864f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1865025331dfSDaniel Borkmann 1866025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1867025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1868025331dfSDaniel Borkmann * combination with structs. 1869025331dfSDaniel Borkmann */ 1870025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1871025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1872f7b12606SJiri Pirko }; 1873f7b12606SJiri Pirko 1874d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1875d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1876d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 187785de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 187858038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1879d1fdd913SBrenden Blanco }; 1880d1fdd913SBrenden Blanco 1881dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1882dc599f76SDavid Ahern { 1883dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1884dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1885dc599f76SDavid Ahern 18868cb08174SJohannes Berg if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 1887dc599f76SDavid Ahern return NULL; 1888dc599f76SDavid Ahern 1889dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1890dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1891dc599f76SDavid Ahern 1892dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1893dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1894dc599f76SDavid Ahern } 1895dc599f76SDavid Ahern 1896dc599f76SDavid Ahern return ops; 1897dc599f76SDavid Ahern } 1898dc599f76SDavid Ahern 1899dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1900dc599f76SDavid Ahern { 1901dc599f76SDavid Ahern struct net_device *master; 1902dc599f76SDavid Ahern 1903dc599f76SDavid Ahern if (!master_idx) 1904dc599f76SDavid Ahern return false; 1905dc599f76SDavid Ahern 1906dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1907dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1908dc599f76SDavid Ahern return true; 1909dc599f76SDavid Ahern 1910dc599f76SDavid Ahern return false; 1911dc599f76SDavid Ahern } 1912dc599f76SDavid Ahern 1913dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1914dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1915dc599f76SDavid Ahern { 1916dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1917dc599f76SDavid Ahern return true; 1918dc599f76SDavid Ahern 1919dc599f76SDavid Ahern return false; 1920dc599f76SDavid Ahern } 1921dc599f76SDavid Ahern 1922dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1923dc599f76SDavid Ahern int master_idx, 1924dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1925dc599f76SDavid Ahern { 1926dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1927dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1928dc599f76SDavid Ahern return true; 1929dc599f76SDavid Ahern 1930dc599f76SDavid Ahern return false; 1931dc599f76SDavid Ahern } 1932dc599f76SDavid Ahern 1933c383edc4SChristian Brauner /** 1934c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 1935c383edc4SChristian Brauner * @sk: netlink socket 1936c383edc4SChristian Brauner * @netnsid: network namespace identifier 1937c383edc4SChristian Brauner * 1938c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 1939c383edc4SChristian Brauner * pointer on failure. 1940c383edc4SChristian Brauner */ 1941c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 194279e1ad14SJiri Benc { 194379e1ad14SJiri Benc struct net *net; 194479e1ad14SJiri Benc 1945f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 194679e1ad14SJiri Benc if (!net) 194779e1ad14SJiri Benc return ERR_PTR(-EINVAL); 194879e1ad14SJiri Benc 194979e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 195079e1ad14SJiri Benc * the user namespace owning the target net ns. 195179e1ad14SJiri Benc */ 1952f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 195379e1ad14SJiri Benc put_net(net); 195479e1ad14SJiri Benc return ERR_PTR(-EACCES); 195579e1ad14SJiri Benc } 195679e1ad14SJiri Benc return net; 195779e1ad14SJiri Benc } 1958c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 195979e1ad14SJiri Benc 1960905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 1961905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 1962905cf0abSDavid Ahern struct netlink_ext_ack *extack) 1963905cf0abSDavid Ahern { 1964905cf0abSDavid Ahern int hdrlen; 1965905cf0abSDavid Ahern 1966905cf0abSDavid Ahern if (strict_check) { 1967905cf0abSDavid Ahern struct ifinfomsg *ifm; 1968905cf0abSDavid Ahern 1969905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 1970905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 1971905cf0abSDavid Ahern return -EINVAL; 1972905cf0abSDavid Ahern } 1973905cf0abSDavid Ahern 1974905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 1975905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 1976905cf0abSDavid Ahern ifm->ifi_change) { 1977905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 1978905cf0abSDavid Ahern return -EINVAL; 1979905cf0abSDavid Ahern } 1980905cf0abSDavid Ahern if (ifm->ifi_index) { 1981905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 1982905cf0abSDavid Ahern return -EINVAL; 1983905cf0abSDavid Ahern } 1984905cf0abSDavid Ahern 19858cb08174SJohannes Berg return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 19868cb08174SJohannes Berg IFLA_MAX, ifla_policy, 19878cb08174SJohannes Berg extack); 1988905cf0abSDavid Ahern } 1989905cf0abSDavid Ahern 1990905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 1991905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1992905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1993905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 1994905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1995905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 1996905cf0abSDavid Ahern */ 1997905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 1998905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1999905cf0abSDavid Ahern 20008cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 20018cb08174SJohannes Berg extack); 2002905cf0abSDavid Ahern } 2003905cf0abSDavid Ahern 2004b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 20051da177e4SLinus Torvalds { 2006905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 2007905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 20083b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 200979e1ad14SJiri Benc struct net *tgt_net = net; 20107c28bd0bSEric Dumazet int h, s_h; 20117c28bd0bSEric Dumazet int idx = 0, s_idx; 20121da177e4SLinus Torvalds struct net_device *dev; 20137c28bd0bSEric Dumazet struct hlist_head *head; 2014115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2015115c9b81SGreg Rose u32 ext_filter_mask = 0; 2016dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 2017dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 2018dc599f76SDavid Ahern int master_idx = 0; 201979e1ad14SJiri Benc int netnsid = -1; 2020905cf0abSDavid Ahern int err, i; 20211da177e4SLinus Torvalds 20227c28bd0bSEric Dumazet s_h = cb->args[0]; 20237c28bd0bSEric Dumazet s_idx = cb->args[1]; 20247c28bd0bSEric Dumazet 2025905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2026905cf0abSDavid Ahern if (err < 0) { 2027905cf0abSDavid Ahern if (cb->strict_check) 2028905cf0abSDavid Ahern return err; 2029e5eca6d4SMichal Schmidt 2030905cf0abSDavid Ahern goto walk_entries; 203179e1ad14SJiri Benc } 203279e1ad14SJiri Benc 2033905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 2034905cf0abSDavid Ahern if (!tb[i]) 2035905cf0abSDavid Ahern continue; 2036dc599f76SDavid Ahern 2037905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 2038905cf0abSDavid Ahern switch (i) { 2039905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 2040905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 2041905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2042905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 2043905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2044905cf0abSDavid Ahern return PTR_ERR(tgt_net); 2045905cf0abSDavid Ahern } 2046905cf0abSDavid Ahern break; 2047905cf0abSDavid Ahern case IFLA_EXT_MASK: 2048905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 2049905cf0abSDavid Ahern break; 2050905cf0abSDavid Ahern case IFLA_MASTER: 2051905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 2052905cf0abSDavid Ahern break; 2053905cf0abSDavid Ahern case IFLA_LINKINFO: 2054905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 2055905cf0abSDavid Ahern break; 2056905cf0abSDavid Ahern default: 2057905cf0abSDavid Ahern if (cb->strict_check) { 2058905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2059905cf0abSDavid Ahern return -EINVAL; 2060905cf0abSDavid Ahern } 2061905cf0abSDavid Ahern } 2062905cf0abSDavid Ahern } 2063dc599f76SDavid Ahern 2064dc599f76SDavid Ahern if (master_idx || kind_ops) 2065dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 2066115c9b81SGreg Rose 2067905cf0abSDavid Ahern walk_entries: 20687c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 20697562f876SPavel Emelianov idx = 0; 207079e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 2071cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 2072dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 20733f0ae05dSZhang Shengju goto cont; 20741da177e4SLinus Torvalds if (idx < s_idx) 20757562f876SPavel Emelianov goto cont; 207679e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 207779e1ad14SJiri Benc RTM_NEWLINK, 207815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 2079905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 208038e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 2081d4e4fdf9SGuillaume Nault netnsid, GFP_KERNEL); 2082973462bbSDavid Gibson 2083f6c5775fSDavid Ahern if (err < 0) { 2084f6c5775fSDavid Ahern if (likely(skb->len)) 20857c28bd0bSEric Dumazet goto out; 20864e985adaSThomas Graf 2087f6c5775fSDavid Ahern goto out_err; 2088f6c5775fSDavid Ahern } 20897562f876SPavel Emelianov cont: 20907562f876SPavel Emelianov idx++; 20911da177e4SLinus Torvalds } 20927c28bd0bSEric Dumazet } 20937c28bd0bSEric Dumazet out: 2094f6c5775fSDavid Ahern err = skb->len; 2095f6c5775fSDavid Ahern out_err: 20967c28bd0bSEric Dumazet cb->args[1] = idx; 20977c28bd0bSEric Dumazet cb->args[0] = h; 2098d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 2099d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 210079e1ad14SJiri Benc if (netnsid >= 0) 210179e1ad14SJiri Benc put_net(tgt_net); 21021da177e4SLinus Torvalds 2103f6c5775fSDavid Ahern return err; 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 2106fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2107fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2108f7b12606SJiri Pirko { 21098cb08174SJohannes Berg return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 21108cb08174SJohannes Berg exterr); 2111f7b12606SJiri Pirko } 2112f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 211357b61080SScott Feldman 211481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 211581adee47SEric W. Biederman { 211681adee47SEric W. Biederman struct net *net; 211781adee47SEric W. Biederman /* Examine the link attributes and figure out which 211881adee47SEric W. Biederman * network namespace we are talking about. 211981adee47SEric W. Biederman */ 212081adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 212181adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2122f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2123f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 212481adee47SEric W. Biederman else 212581adee47SEric W. Biederman net = get_net(src_net); 212681adee47SEric W. Biederman return net; 212781adee47SEric W. Biederman } 212881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 212981adee47SEric W. Biederman 21307c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 21317c4f63baSChristian Brauner * examining the link attributes in the following order: 21327c4f63baSChristian Brauner * 21337c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 21347c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 21357e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 21367c4f63baSChristian Brauner */ 21377c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 21387c4f63baSChristian Brauner struct nlattr *tb[]) 21397c4f63baSChristian Brauner { 21407c4f63baSChristian Brauner struct net *net; 21417c4f63baSChristian Brauner 21427c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 21437c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 21447c4f63baSChristian Brauner 21457e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 21467c4f63baSChristian Brauner return get_net(src_net); 21477c4f63baSChristian Brauner 21487e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 21497c4f63baSChristian Brauner if (!net) 21507c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 21517c4f63baSChristian Brauner 21527c4f63baSChristian Brauner return net; 21537c4f63baSChristian Brauner } 21547c4f63baSChristian Brauner 21557c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 21567c4f63baSChristian Brauner struct net *src_net, 21577c4f63baSChristian Brauner struct nlattr *tb[], int cap) 21587c4f63baSChristian Brauner { 21597c4f63baSChristian Brauner struct net *net; 21607c4f63baSChristian Brauner 21617c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 21627c4f63baSChristian Brauner if (IS_ERR(net)) 21637c4f63baSChristian Brauner return net; 21647c4f63baSChristian Brauner 21657c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 21667c4f63baSChristian Brauner put_net(net); 21677c4f63baSChristian Brauner return ERR_PTR(-EPERM); 21687c4f63baSChristian Brauner } 21697c4f63baSChristian Brauner 21707c4f63baSChristian Brauner return net; 21717c4f63baSChristian Brauner } 21727c4f63baSChristian Brauner 21734ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 21744ff66caeSChristian Brauner * potentially referring to different network namespaces. 21754ff66caeSChristian Brauner */ 21764ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 21774ff66caeSChristian Brauner struct netlink_ext_ack *extack, 21784ff66caeSChristian Brauner bool netns_id_only) 21794ff66caeSChristian Brauner { 21804ff66caeSChristian Brauner 21814ff66caeSChristian Brauner if (netns_id_only) { 21824ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 21834ff66caeSChristian Brauner return 0; 21844ff66caeSChristian Brauner 21854ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 21864ff66caeSChristian Brauner return -EOPNOTSUPP; 21874ff66caeSChristian Brauner } 21884ff66caeSChristian Brauner 21897e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 21904ff66caeSChristian Brauner goto invalid_attr; 21914ff66caeSChristian Brauner 21927e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 21934ff66caeSChristian Brauner goto invalid_attr; 21944ff66caeSChristian Brauner 21957e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 21964ff66caeSChristian Brauner goto invalid_attr; 21974ff66caeSChristian Brauner 21984ff66caeSChristian Brauner return 0; 21994ff66caeSChristian Brauner 22004ff66caeSChristian Brauner invalid_attr: 22014ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 22024ff66caeSChristian Brauner return -EINVAL; 22034ff66caeSChristian Brauner } 22044ff66caeSChristian Brauner 22051840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 22061840bb13SThomas Graf { 22071840bb13SThomas Graf if (dev) { 22081840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 22091840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 22101840bb13SThomas Graf return -EINVAL; 22111840bb13SThomas Graf 22121840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 22131840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 22141840bb13SThomas Graf return -EINVAL; 22151840bb13SThomas Graf } 22161840bb13SThomas Graf 2217cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2218cf7afbfeSThomas Graf struct nlattr *af; 2219cf7afbfeSThomas Graf int rem, err; 2220cf7afbfeSThomas Graf 2221cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2222cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2223cf7afbfeSThomas Graf 22245fa85a09SFlorian Westphal rcu_read_lock(); 22255fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 22265fa85a09SFlorian Westphal if (!af_ops) { 22275fa85a09SFlorian Westphal rcu_read_unlock(); 2228cf7afbfeSThomas Graf return -EAFNOSUPPORT; 22295fa85a09SFlorian Westphal } 2230cf7afbfeSThomas Graf 22315fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 22325fa85a09SFlorian Westphal rcu_read_unlock(); 2233cf7afbfeSThomas Graf return -EOPNOTSUPP; 22345fa85a09SFlorian Westphal } 2235cf7afbfeSThomas Graf 2236cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 22376d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 22385fa85a09SFlorian Westphal if (err < 0) { 22395fa85a09SFlorian Westphal rcu_read_unlock(); 2240cf7afbfeSThomas Graf return err; 2241cf7afbfeSThomas Graf } 2242cf7afbfeSThomas Graf } 22435fa85a09SFlorian Westphal 22445fa85a09SFlorian Westphal rcu_read_unlock(); 22455fa85a09SFlorian Westphal } 2246cf7afbfeSThomas Graf } 2247cf7afbfeSThomas Graf 22481840bb13SThomas Graf return 0; 22491840bb13SThomas Graf } 22501840bb13SThomas Graf 2251cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2252cc8e27ccSEli Cohen int guid_type) 2253cc8e27ccSEli Cohen { 2254cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2255cc8e27ccSEli Cohen 2256cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2257cc8e27ccSEli Cohen } 2258cc8e27ccSEli Cohen 2259cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2260cc8e27ccSEli Cohen { 2261cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2262cc8e27ccSEli Cohen return -EOPNOTSUPP; 2263cc8e27ccSEli Cohen 2264cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2265cc8e27ccSEli Cohen } 2266cc8e27ccSEli Cohen 22674f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2268c02db8c6SChris Wright { 2269c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 22704f7d2cdfSDaniel Borkmann int err = -EINVAL; 2271c02db8c6SChris Wright 22724f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 22734f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 22744f7d2cdfSDaniel Borkmann 2275ff08ddbaSDan Carpenter if (ivm->vf >= INT_MAX) 2276ff08ddbaSDan Carpenter return -EINVAL; 2277c02db8c6SChris Wright err = -EOPNOTSUPP; 2278c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2279c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2280c02db8c6SChris Wright ivm->mac); 22814f7d2cdfSDaniel Borkmann if (err < 0) 22824f7d2cdfSDaniel Borkmann return err; 2283c02db8c6SChris Wright } 22844f7d2cdfSDaniel Borkmann 22854f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 22864f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 22874f7d2cdfSDaniel Borkmann 2288ff08ddbaSDan Carpenter if (ivv->vf >= INT_MAX) 2289ff08ddbaSDan Carpenter return -EINVAL; 2290c02db8c6SChris Wright err = -EOPNOTSUPP; 2291c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 22924f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 229379aab093SMoshe Shemesh ivv->qos, 229479aab093SMoshe Shemesh htons(ETH_P_8021Q)); 229579aab093SMoshe Shemesh if (err < 0) 229679aab093SMoshe Shemesh return err; 229779aab093SMoshe Shemesh } 229879aab093SMoshe Shemesh 229979aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 230079aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 230179aab093SMoshe Shemesh struct nlattr *attr; 230279aab093SMoshe Shemesh int rem, len = 0; 230379aab093SMoshe Shemesh 230479aab093SMoshe Shemesh err = -EOPNOTSUPP; 230579aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 230679aab093SMoshe Shemesh return err; 230779aab093SMoshe Shemesh 230879aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 230979aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 231079aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 231179aab093SMoshe Shemesh return -EINVAL; 231279aab093SMoshe Shemesh } 231379aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 231479aab093SMoshe Shemesh return -EOPNOTSUPP; 231579aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 231679aab093SMoshe Shemesh 231779aab093SMoshe Shemesh len++; 231879aab093SMoshe Shemesh } 2319fa34cd94SArnd Bergmann if (len == 0) 2320fa34cd94SArnd Bergmann return -EINVAL; 2321fa34cd94SArnd Bergmann 2322ff08ddbaSDan Carpenter if (ivvl[0]->vf >= INT_MAX) 2323ff08ddbaSDan Carpenter return -EINVAL; 232479aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 232579aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 23264f7d2cdfSDaniel Borkmann if (err < 0) 23274f7d2cdfSDaniel Borkmann return err; 2328c02db8c6SChris Wright } 23294f7d2cdfSDaniel Borkmann 23304f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 23314f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2332ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 23334f7d2cdfSDaniel Borkmann 2334ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2335ff08ddbaSDan Carpenter return -EINVAL; 2336c02db8c6SChris Wright err = -EOPNOTSUPP; 2337ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 23384f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 23394f7d2cdfSDaniel Borkmann if (err < 0) 23404f7d2cdfSDaniel Borkmann return err; 23414f7d2cdfSDaniel Borkmann 2342ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2343ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2344ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2345ed616689SSucheta Chakraborty ivf.min_tx_rate, 2346c02db8c6SChris Wright ivt->rate); 23474f7d2cdfSDaniel Borkmann if (err < 0) 23484f7d2cdfSDaniel Borkmann return err; 2349c02db8c6SChris Wright } 23504f7d2cdfSDaniel Borkmann 23514f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 23524f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 23534f7d2cdfSDaniel Borkmann 2354ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2355ff08ddbaSDan Carpenter return -EINVAL; 2356ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2357ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2358ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2359ed616689SSucheta Chakraborty ivt->min_tx_rate, 2360ed616689SSucheta Chakraborty ivt->max_tx_rate); 23614f7d2cdfSDaniel Borkmann if (err < 0) 23624f7d2cdfSDaniel Borkmann return err; 2363ed616689SSucheta Chakraborty } 23644f7d2cdfSDaniel Borkmann 23654f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 23664f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 23674f7d2cdfSDaniel Borkmann 2368ff08ddbaSDan Carpenter if (ivs->vf >= INT_MAX) 2369ff08ddbaSDan Carpenter return -EINVAL; 23705f8444a3SGreg Rose err = -EOPNOTSUPP; 23715f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 23725f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 23735f8444a3SGreg Rose ivs->setting); 23744f7d2cdfSDaniel Borkmann if (err < 0) 23754f7d2cdfSDaniel Borkmann return err; 23765f8444a3SGreg Rose } 23774f7d2cdfSDaniel Borkmann 23784f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 23794f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 23804f7d2cdfSDaniel Borkmann 2381ff08ddbaSDan Carpenter if (ivl->vf >= INT_MAX) 2382ff08ddbaSDan Carpenter return -EINVAL; 23831d8faf48SRony Efraim err = -EOPNOTSUPP; 23841d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 23851d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 23861d8faf48SRony Efraim ivl->link_state); 23874f7d2cdfSDaniel Borkmann if (err < 0) 23884f7d2cdfSDaniel Borkmann return err; 23891d8faf48SRony Efraim } 23904f7d2cdfSDaniel Borkmann 23914f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 239201a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 239301a3d796SVlad Zolotarov 239401a3d796SVlad Zolotarov err = -EOPNOTSUPP; 23954f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2396ff08ddbaSDan Carpenter if (ivrssq_en->vf >= INT_MAX) 2397ff08ddbaSDan Carpenter return -EINVAL; 239801a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 23994f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 240001a3d796SVlad Zolotarov ivrssq_en->setting); 24014f7d2cdfSDaniel Borkmann if (err < 0) 24024f7d2cdfSDaniel Borkmann return err; 240301a3d796SVlad Zolotarov } 24044f7d2cdfSDaniel Borkmann 2405dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2406dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2407dd461d6aSHiroshi Shimamoto 2408ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2409ff08ddbaSDan Carpenter return -EINVAL; 2410dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2411dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2412dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2413dd461d6aSHiroshi Shimamoto if (err < 0) 2414dd461d6aSHiroshi Shimamoto return err; 2415dd461d6aSHiroshi Shimamoto } 2416dd461d6aSHiroshi Shimamoto 2417cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2418cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2419cc8e27ccSEli Cohen 2420ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2421ff08ddbaSDan Carpenter return -EINVAL; 2422cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2423cc8e27ccSEli Cohen return -EOPNOTSUPP; 2424cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2425cc8e27ccSEli Cohen } 2426cc8e27ccSEli Cohen 2427cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2428cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2429cc8e27ccSEli Cohen 2430ff08ddbaSDan Carpenter if (ivt->vf >= INT_MAX) 2431ff08ddbaSDan Carpenter return -EINVAL; 2432cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2433cc8e27ccSEli Cohen return -EOPNOTSUPP; 2434cc8e27ccSEli Cohen 2435cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2436cc8e27ccSEli Cohen } 2437cc8e27ccSEli Cohen 2438c02db8c6SChris Wright return err; 2439c02db8c6SChris Wright } 2440c02db8c6SChris Wright 244133eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 244233eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2443fbaec0eaSJiri Pirko { 2444898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2445fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2446fbaec0eaSJiri Pirko int err; 2447fbaec0eaSJiri Pirko 2448898e5061SJiri Pirko if (upper_dev) { 2449898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2450fbaec0eaSJiri Pirko return 0; 2451898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2452fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2453898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2454fbaec0eaSJiri Pirko if (err) 2455fbaec0eaSJiri Pirko return err; 2456ab92d68fSTaehee Yoo netdev_update_lockdep_key(dev); 2457fbaec0eaSJiri Pirko } else { 2458fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2459fbaec0eaSJiri Pirko } 2460fbaec0eaSJiri Pirko } 2461fbaec0eaSJiri Pirko 2462fbaec0eaSJiri Pirko if (ifindex) { 2463898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2464898e5061SJiri Pirko if (!upper_dev) 2465fbaec0eaSJiri Pirko return -EINVAL; 2466898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2467fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 246833eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2469fbaec0eaSJiri Pirko if (err) 2470fbaec0eaSJiri Pirko return err; 2471fbaec0eaSJiri Pirko } else { 2472fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2473fbaec0eaSJiri Pirko } 2474fbaec0eaSJiri Pirko } 2475fbaec0eaSJiri Pirko return 0; 2476fbaec0eaSJiri Pirko } 2477fbaec0eaSJiri Pirko 247890c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2479ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2480ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 248190f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 248290f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2483ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 248490c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 24850157f60cSPatrick McHardy { 2486d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 24870157f60cSPatrick McHardy int err; 24880157f60cSPatrick McHardy 2489644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2490644c7eebSEric Dumazet if (err < 0) 2491644c7eebSEric Dumazet return err; 2492644c7eebSEric Dumazet 24937e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 24947c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 24957c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2496d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2497d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2498d8a5ec67SEric W. Biederman goto errout; 2499d8a5ec67SEric W. Biederman } 25007c4f63baSChristian Brauner 2501d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2502d8a5ec67SEric W. Biederman put_net(net); 2503d8a5ec67SEric W. Biederman if (err) 2504d8a5ec67SEric W. Biederman goto errout; 250590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2506d8a5ec67SEric W. Biederman } 2507d8a5ec67SEric W. Biederman 25080157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 25090157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 25100157f60cSPatrick McHardy struct ifmap k_map; 25110157f60cSPatrick McHardy 2512d314774cSStephen Hemminger if (!ops->ndo_set_config) { 25130157f60cSPatrick McHardy err = -EOPNOTSUPP; 25140157f60cSPatrick McHardy goto errout; 25150157f60cSPatrick McHardy } 25160157f60cSPatrick McHardy 25170157f60cSPatrick McHardy if (!netif_device_present(dev)) { 25180157f60cSPatrick McHardy err = -ENODEV; 25190157f60cSPatrick McHardy goto errout; 25200157f60cSPatrick McHardy } 25210157f60cSPatrick McHardy 25220157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 25230157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 25240157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 25250157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 25260157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 25270157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 25280157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 25290157f60cSPatrick McHardy 2530d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 25310157f60cSPatrick McHardy if (err < 0) 25320157f60cSPatrick McHardy goto errout; 25330157f60cSPatrick McHardy 2534ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 25350157f60cSPatrick McHardy } 25360157f60cSPatrick McHardy 25370157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 25380157f60cSPatrick McHardy struct sockaddr *sa; 25390157f60cSPatrick McHardy int len; 25400157f60cSPatrick McHardy 2541153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2542153711f9SWANG Cong sizeof(*sa)); 25430157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 25440157f60cSPatrick McHardy if (!sa) { 25450157f60cSPatrick McHardy err = -ENOMEM; 25460157f60cSPatrick McHardy goto errout; 25470157f60cSPatrick McHardy } 25480157f60cSPatrick McHardy sa->sa_family = dev->type; 25490157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 25500157f60cSPatrick McHardy dev->addr_len); 25513a37a963SPetr Machata err = dev_set_mac_address(dev, sa, extack); 25520157f60cSPatrick McHardy kfree(sa); 25530157f60cSPatrick McHardy if (err) 25540157f60cSPatrick McHardy goto errout; 255590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25560157f60cSPatrick McHardy } 25570157f60cSPatrick McHardy 25580157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 25597a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 25600157f60cSPatrick McHardy if (err < 0) 25610157f60cSPatrick McHardy goto errout; 256290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25630157f60cSPatrick McHardy } 25640157f60cSPatrick McHardy 2565cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2566cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2567ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2568cbda10faSVlad Dogaru } 2569cbda10faSVlad Dogaru 25700157f60cSPatrick McHardy /* 25710157f60cSPatrick McHardy * Interface selected by interface index but interface 25720157f60cSPatrick McHardy * name provided implies that a name change has been 25730157f60cSPatrick McHardy * requested. 25740157f60cSPatrick McHardy */ 25750157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 25760157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 25770157f60cSPatrick McHardy if (err < 0) 25780157f60cSPatrick McHardy goto errout; 257990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25800157f60cSPatrick McHardy } 25810157f60cSPatrick McHardy 25820b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 25830b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 25840b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 25850b815a1aSStephen Hemminger if (err < 0) 25860b815a1aSStephen Hemminger goto errout; 2587ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 25880b815a1aSStephen Hemminger } 25890b815a1aSStephen Hemminger 25900157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 25910157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2592e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 25930157f60cSPatrick McHardy } 25940157f60cSPatrick McHardy 25950157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2596567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2597567c5e13SPetr Machata extack); 25985f9021cfSJohannes Berg if (err < 0) 25995f9021cfSJohannes Berg goto errout; 26000157f60cSPatrick McHardy } 26010157f60cSPatrick McHardy 2602fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 260333eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2604fbaec0eaSJiri Pirko if (err) 2605fbaec0eaSJiri Pirko goto errout; 260690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2607fbaec0eaSJiri Pirko } 2608fbaec0eaSJiri Pirko 26099a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 26109a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 26119a57247fSJiri Pirko if (err) 26129a57247fSJiri Pirko goto errout; 261390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 26149a57247fSJiri Pirko } 26159a57247fSJiri Pirko 26165d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 26170cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 26185d1180fcSNicolas Dichtel 26196a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 26206a643ddbSCong Wang if (err) 262108294a26SJason Wang goto errout; 26222d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 262308294a26SJason Wang } 26240157f60cSPatrick McHardy 262546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 262646e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 262746e6b992SStephen Hemminger 262846e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 262946e6b992SStephen Hemminger err = -EINVAL; 263046e6b992SStephen Hemminger goto errout; 263146e6b992SStephen Hemminger } 263246e6b992SStephen Hemminger 263346e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 263446e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 263546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 263646e6b992SStephen Hemminger } 263746e6b992SStephen Hemminger } 263846e6b992SStephen Hemminger 263946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 264046e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 264146e6b992SStephen Hemminger 264246e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 264346e6b992SStephen Hemminger err = -EINVAL; 264446e6b992SStephen Hemminger goto errout; 264546e6b992SStephen Hemminger } 264646e6b992SStephen Hemminger 264746e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 264846e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 264946e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 265046e6b992SStephen Hemminger } 265146e6b992SStephen Hemminger } 265246e6b992SStephen Hemminger 26530157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 265493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 26550157f60cSPatrick McHardy 26560157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 26571889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 26581889b0e7SNicolas Dichtel 26590157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 26601889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2661ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 26621889b0e7SNicolas Dichtel dev->link_mode = value; 266393b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 26640157f60cSPatrick McHardy } 26650157f60cSPatrick McHardy 2666c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 26674f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2668c02db8c6SChris Wright struct nlattr *attr; 2669c02db8c6SChris Wright int rem; 26704f7d2cdfSDaniel Borkmann 2671c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 26724f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 26734f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2674253683bbSDavid Howells err = -EINVAL; 2675c02db8c6SChris Wright goto errout; 2676253683bbSDavid Howells } 26778cb08174SJohannes Berg err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 26788cb08174SJohannes Berg attr, 26798cb08174SJohannes Berg ifla_vf_policy, 26808cb08174SJohannes Berg NULL); 26814f7d2cdfSDaniel Borkmann if (err < 0) 26824f7d2cdfSDaniel Borkmann goto errout; 26834f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2684ebc08a6fSWilliams, Mitch A if (err < 0) 2685ebc08a6fSWilliams, Mitch A goto errout; 2686ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2687ebc08a6fSWilliams, Mitch A } 2688ebc08a6fSWilliams, Mitch A } 26890157f60cSPatrick McHardy err = 0; 26900157f60cSPatrick McHardy 269157b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 269257b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 269357b61080SScott Feldman struct nlattr *attr; 269457b61080SScott Feldman int vf; 269557b61080SScott Feldman int rem; 269657b61080SScott Feldman 269757b61080SScott Feldman err = -EOPNOTSUPP; 269857b61080SScott Feldman if (!ops->ndo_set_vf_port) 269957b61080SScott Feldman goto errout; 270057b61080SScott Feldman 270157b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2702035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2703035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2704035d210fSDaniel Borkmann err = -EINVAL; 2705035d210fSDaniel Borkmann goto errout; 2706035d210fSDaniel Borkmann } 27078cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 27088cb08174SJohannes Berg attr, 27098cb08174SJohannes Berg ifla_port_policy, 27108cb08174SJohannes Berg NULL); 271157b61080SScott Feldman if (err < 0) 271257b61080SScott Feldman goto errout; 271357b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 271457b61080SScott Feldman err = -EOPNOTSUPP; 271557b61080SScott Feldman goto errout; 271657b61080SScott Feldman } 271757b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 271857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 271957b61080SScott Feldman if (err < 0) 272057b61080SScott Feldman goto errout; 2721ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 272257b61080SScott Feldman } 272357b61080SScott Feldman } 272457b61080SScott Feldman err = 0; 272557b61080SScott Feldman 272657b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 272757b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 272857b61080SScott Feldman 27298cb08174SJohannes Berg err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 27308cb08174SJohannes Berg tb[IFLA_PORT_SELF], 27318cb08174SJohannes Berg ifla_port_policy, NULL); 273257b61080SScott Feldman if (err < 0) 273357b61080SScott Feldman goto errout; 273457b61080SScott Feldman 273557b61080SScott Feldman err = -EOPNOTSUPP; 273657b61080SScott Feldman if (ops->ndo_set_vf_port) 273757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 273857b61080SScott Feldman if (err < 0) 273957b61080SScott Feldman goto errout; 2740ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 274157b61080SScott Feldman } 2742f8ff182cSThomas Graf 2743f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2744f8ff182cSThomas Graf struct nlattr *af; 2745f8ff182cSThomas Graf int rem; 2746f8ff182cSThomas Graf 2747f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2748f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2749f8ff182cSThomas Graf 27505fa85a09SFlorian Westphal rcu_read_lock(); 27515fa85a09SFlorian Westphal 2752058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2753f8ff182cSThomas Graf 2754cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 27555fa85a09SFlorian Westphal if (err < 0) { 27565fa85a09SFlorian Westphal rcu_read_unlock(); 2757f8ff182cSThomas Graf goto errout; 27585fa85a09SFlorian Westphal } 2759f8ff182cSThomas Graf 27605fa85a09SFlorian Westphal rcu_read_unlock(); 2761ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2762f8ff182cSThomas Graf } 2763f8ff182cSThomas Graf } 276457b61080SScott Feldman err = 0; 276557b61080SScott Feldman 276688d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 276788d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 276888d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 276988d6378bSAnuradha Karuppiah if (err) 277088d6378bSAnuradha Karuppiah goto errout; 277188d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 277288d6378bSAnuradha Karuppiah } 277388d6378bSAnuradha Karuppiah 2774d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2775d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 277685de8576SDaniel Borkmann u32 xdp_flags = 0; 2777d1fdd913SBrenden Blanco 27788cb08174SJohannes Berg err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 27798cb08174SJohannes Berg tb[IFLA_XDP], 2780fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2781d1fdd913SBrenden Blanco if (err < 0) 2782d1fdd913SBrenden Blanco goto errout; 2783d1fdd913SBrenden Blanco 278458038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2785262d8625SBrenden Blanco err = -EINVAL; 2786262d8625SBrenden Blanco goto errout; 2787262d8625SBrenden Blanco } 278885de8576SDaniel Borkmann 278985de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 279085de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 279185de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 279285de8576SDaniel Borkmann err = -EINVAL; 279385de8576SDaniel Borkmann goto errout; 279485de8576SDaniel Borkmann } 2795ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 27960489df9aSDaniel Borkmann err = -EINVAL; 27970489df9aSDaniel Borkmann goto errout; 27980489df9aSDaniel Borkmann } 279985de8576SDaniel Borkmann } 280085de8576SDaniel Borkmann 2801d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2802ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 280385de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 280485de8576SDaniel Borkmann xdp_flags); 2805d1fdd913SBrenden Blanco if (err) 2806d1fdd913SBrenden Blanco goto errout; 2807d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2808d1fdd913SBrenden Blanco } 2809d1fdd913SBrenden Blanco } 2810d1fdd913SBrenden Blanco 28110157f60cSPatrick McHardy errout: 2812ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 281364ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2814ba998906SNicolas Dichtel netdev_state_change(dev); 2815ba998906SNicolas Dichtel 2816ba998906SNicolas Dichtel if (err < 0) 2817e87cc472SJoe 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", 2818e87cc472SJoe Perches dev->name); 2819ba998906SNicolas Dichtel } 28200157f60cSPatrick McHardy 28210157f60cSPatrick McHardy return err; 28220157f60cSPatrick McHardy } 28230157f60cSPatrick McHardy 2824cc6090e9SJiri Pirko static struct net_device *rtnl_dev_get(struct net *net, 2825cc6090e9SJiri Pirko struct nlattr *ifname_attr, 282676c9ac0eSJiri Pirko struct nlattr *altifname_attr, 2827cc6090e9SJiri Pirko char *ifname) 2828cc6090e9SJiri Pirko { 282976c9ac0eSJiri Pirko char buffer[ALTIFNAMSIZ]; 2830cc6090e9SJiri Pirko 2831cc6090e9SJiri Pirko if (!ifname) { 2832cc6090e9SJiri Pirko ifname = buffer; 2833cc6090e9SJiri Pirko if (ifname_attr) 2834cc6090e9SJiri Pirko nla_strlcpy(ifname, ifname_attr, IFNAMSIZ); 283576c9ac0eSJiri Pirko else if (altifname_attr) 283676c9ac0eSJiri Pirko nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ); 2837cc6090e9SJiri Pirko else 2838cc6090e9SJiri Pirko return NULL; 2839cc6090e9SJiri Pirko } 2840cc6090e9SJiri Pirko 2841cc6090e9SJiri Pirko return __dev_get_by_name(net, ifname); 2842cc6090e9SJiri Pirko } 2843cc6090e9SJiri Pirko 2844c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2845c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2846da5e0494SThomas Graf { 28473b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2848da5e0494SThomas Graf struct ifinfomsg *ifm; 2849da5e0494SThomas Graf struct net_device *dev; 28500157f60cSPatrick McHardy int err; 2851da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 28521da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 28531da177e4SLinus Torvalds 28548cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 28558cb08174SJohannes Berg ifla_policy, extack); 2856da5e0494SThomas Graf if (err < 0) 2857da5e0494SThomas Graf goto errout; 28581da177e4SLinus Torvalds 28594ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 28604ff66caeSChristian Brauner if (err < 0) 28614ff66caeSChristian Brauner goto errout; 28624ff66caeSChristian Brauner 28635176f91eSThomas Graf if (tb[IFLA_IFNAME]) 28645176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 286578e5b891SPatrick McHardy else 286678e5b891SPatrick McHardy ifname[0] = '\0'; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds err = -EINVAL; 2869da5e0494SThomas Graf ifm = nlmsg_data(nlh); 287051055be8SPatrick McHardy if (ifm->ifi_index > 0) 2871a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 287276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 287376c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 2874da5e0494SThomas Graf else 2875da5e0494SThomas Graf goto errout; 28761da177e4SLinus Torvalds 2877da5e0494SThomas Graf if (dev == NULL) { 2878da5e0494SThomas Graf err = -ENODEV; 2879da5e0494SThomas Graf goto errout; 2880da5e0494SThomas Graf } 28811da177e4SLinus Torvalds 2882ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2883da5e0494SThomas Graf errout: 28841da177e4SLinus Torvalds return err; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 288766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 288866400d54SWANG Cong { 288966400d54SWANG Cong struct net_device *dev, *aux; 289066400d54SWANG Cong LIST_HEAD(list_kill); 289166400d54SWANG Cong bool found = false; 289266400d54SWANG Cong 289366400d54SWANG Cong if (!group) 289466400d54SWANG Cong return -EPERM; 289566400d54SWANG Cong 289666400d54SWANG Cong for_each_netdev(net, dev) { 289766400d54SWANG Cong if (dev->group == group) { 289866400d54SWANG Cong const struct rtnl_link_ops *ops; 289966400d54SWANG Cong 290066400d54SWANG Cong found = true; 290166400d54SWANG Cong ops = dev->rtnl_link_ops; 290266400d54SWANG Cong if (!ops || !ops->dellink) 290366400d54SWANG Cong return -EOPNOTSUPP; 290466400d54SWANG Cong } 290566400d54SWANG Cong } 290666400d54SWANG Cong 290766400d54SWANG Cong if (!found) 290866400d54SWANG Cong return -ENODEV; 290966400d54SWANG Cong 291066400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 291166400d54SWANG Cong if (dev->group == group) { 291266400d54SWANG Cong const struct rtnl_link_ops *ops; 291366400d54SWANG Cong 291466400d54SWANG Cong ops = dev->rtnl_link_ops; 291566400d54SWANG Cong ops->dellink(dev, &list_kill); 291666400d54SWANG Cong } 291766400d54SWANG Cong } 291866400d54SWANG Cong unregister_netdevice_many(&list_kill); 291966400d54SWANG Cong 292066400d54SWANG Cong return 0; 292166400d54SWANG Cong } 292266400d54SWANG Cong 2923614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2924614732eaSThomas Graf { 2925614732eaSThomas Graf const struct rtnl_link_ops *ops; 2926614732eaSThomas Graf LIST_HEAD(list_kill); 2927614732eaSThomas Graf 2928614732eaSThomas Graf ops = dev->rtnl_link_ops; 2929614732eaSThomas Graf if (!ops || !ops->dellink) 2930614732eaSThomas Graf return -EOPNOTSUPP; 2931614732eaSThomas Graf 2932614732eaSThomas Graf ops->dellink(dev, &list_kill); 2933614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2934614732eaSThomas Graf 2935614732eaSThomas Graf return 0; 2936614732eaSThomas Graf } 2937614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2938614732eaSThomas Graf 2939c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2940c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 294138f7b870SPatrick McHardy { 29423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2943b61ad68aSChristian Brauner struct net *tgt_net = net; 2944b61ad68aSChristian Brauner struct net_device *dev = NULL; 294538f7b870SPatrick McHardy struct ifinfomsg *ifm; 294638f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 294738f7b870SPatrick McHardy int err; 2948b61ad68aSChristian Brauner int netnsid = -1; 294938f7b870SPatrick McHardy 29508cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 29518cb08174SJohannes Berg ifla_policy, extack); 295238f7b870SPatrick McHardy if (err < 0) 295338f7b870SPatrick McHardy return err; 295438f7b870SPatrick McHardy 29554ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 29564ff66caeSChristian Brauner if (err < 0) 29574ff66caeSChristian Brauner return err; 29584ff66caeSChristian Brauner 29597e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 29607e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 2961c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 2962b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2963b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2964b61ad68aSChristian Brauner } 2965b61ad68aSChristian Brauner 2966b61ad68aSChristian Brauner err = -EINVAL; 296738f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 296838f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2969b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 297076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 297176c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 297276c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 297366400d54SWANG Cong else if (tb[IFLA_GROUP]) 2974b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 297538f7b870SPatrick McHardy else 2976b61ad68aSChristian Brauner goto out; 297738f7b870SPatrick McHardy 2978b61ad68aSChristian Brauner if (!dev) { 2979b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2980b61ad68aSChristian Brauner err = -ENODEV; 298138f7b870SPatrick McHardy 2982b61ad68aSChristian Brauner goto out; 2983b61ad68aSChristian Brauner } 2984b61ad68aSChristian Brauner 2985b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2986b61ad68aSChristian Brauner 2987b61ad68aSChristian Brauner out: 2988b61ad68aSChristian Brauner if (netnsid >= 0) 2989b61ad68aSChristian Brauner put_net(tgt_net); 2990b61ad68aSChristian Brauner 2991b61ad68aSChristian Brauner return err; 299238f7b870SPatrick McHardy } 299338f7b870SPatrick McHardy 29943729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 29953729d502SPatrick McHardy { 29963729d502SPatrick McHardy unsigned int old_flags; 29973729d502SPatrick McHardy int err; 29983729d502SPatrick McHardy 29993729d502SPatrick McHardy old_flags = dev->flags; 30003729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 30016d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 30026d040321SPetr Machata NULL); 30033729d502SPatrick McHardy if (err < 0) 30043729d502SPatrick McHardy return err; 30053729d502SPatrick McHardy } 30063729d502SPatrick McHardy 30078d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 300856a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 30098d356b89SRoopa Prabhu } else { 30103729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 3011a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 30128d356b89SRoopa Prabhu } 30133729d502SPatrick McHardy return 0; 30143729d502SPatrick McHardy } 30153729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 30163729d502SPatrick McHardy 3017d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3018d0522f1cSDavid Ahern unsigned char name_assign_type, 3019d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 3020d0522f1cSDavid Ahern struct nlattr *tb[], 3021d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 3022e7199288SPavel Emelianov { 3023e7199288SPavel Emelianov struct net_device *dev; 3024d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 3025d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 3026e7199288SPavel Emelianov 302776ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 302876ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 302976ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 3030d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 303176ff5cc9SJiri Pirko 303276ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 303376ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 303476ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 3035d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 3036efacb309Sstephen hemminger 3037d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 3038d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 30390e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3040d0522f1cSDavid Ahern } 30410e1d6ecaSEric Dumazet 3042d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 3043d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 30440e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 3045d0522f1cSDavid Ahern } 30460e1d6ecaSEric Dumazet 30475517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 3048c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 3049e7199288SPavel Emelianov if (!dev) 3050d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 3051e7199288SPavel Emelianov 305281adee47SEric W. Biederman dev_net_set(dev, net); 305381adee47SEric W. Biederman dev->rtnl_link_ops = ops; 30543729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 305581adee47SEric W. Biederman 3056d836f5c6SEric Dumazet if (tb[IFLA_MTU]) { 3057d836f5c6SEric Dumazet u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3058d836f5c6SEric Dumazet int err; 3059d836f5c6SEric Dumazet 3060d836f5c6SEric Dumazet err = dev_validate_mtu(dev, mtu, extack); 3061d836f5c6SEric Dumazet if (err) { 3062d836f5c6SEric Dumazet free_netdev(dev); 3063d836f5c6SEric Dumazet return ERR_PTR(err); 3064d836f5c6SEric Dumazet } 3065d836f5c6SEric Dumazet dev->mtu = mtu; 3066d836f5c6SEric Dumazet } 30672afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 3068e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 3069e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 30702afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 30712afb9b53SJiri Pirko } 3072e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 3073e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3074e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 3075e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 3076e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3077e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 307893b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3079e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 3080e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3081ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 3082ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 308346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 308446e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 308546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 3086a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 3087e7199288SPavel Emelianov 3088e7199288SPavel Emelianov return dev; 3089e7199288SPavel Emelianov } 3090e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 3091e7199288SPavel Emelianov 309290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 309390f62cf3SEric W. Biederman struct net *net, int group, 3094e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 3095ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 3096e7ed828fSVlad Dogaru struct nlattr **tb) 3097e7ed828fSVlad Dogaru { 3098d079535dSWANG Cong struct net_device *dev, *aux; 3099e7ed828fSVlad Dogaru int err; 3100e7ed828fSVlad Dogaru 3101d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 3102e7ed828fSVlad Dogaru if (dev->group == group) { 3103ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 3104e7ed828fSVlad Dogaru if (err < 0) 3105e7ed828fSVlad Dogaru return err; 3106e7ed828fSVlad Dogaru } 3107e7ed828fSVlad Dogaru } 3108e7ed828fSVlad Dogaru 3109e7ed828fSVlad Dogaru return 0; 3110e7ed828fSVlad Dogaru } 3111e7ed828fSVlad Dogaru 3112a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3113a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 311438f7b870SPatrick McHardy { 3115420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3116420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 3117420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3118420d0318SJakub Kicinski const struct rtnl_link_ops *m_ops = NULL; 3119420d0318SJakub Kicinski struct net_device *master_dev = NULL; 31203b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 312138f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 312238f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 3123420d0318SJakub Kicinski struct net *dest_net, *link_net; 3124420d0318SJakub Kicinski struct nlattr **slave_data; 3125420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 3126420d0318SJakub Kicinski struct net_device *dev; 3127420d0318SJakub Kicinski struct ifinfomsg *ifm; 3128420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 3129420d0318SJakub Kicinski struct nlattr **data; 313038f7b870SPatrick McHardy int err; 313138f7b870SPatrick McHardy 313295a5afcaSJohannes Berg #ifdef CONFIG_MODULES 313338f7b870SPatrick McHardy replay: 31348072f085SThomas Graf #endif 31358cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 31368cb08174SJohannes Berg ifla_policy, extack); 313738f7b870SPatrick McHardy if (err < 0) 313838f7b870SPatrick McHardy return err; 313938f7b870SPatrick McHardy 31404ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 31414ff66caeSChristian Brauner if (err < 0) 31424ff66caeSChristian Brauner return err; 31434ff66caeSChristian Brauner 314438f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 314538f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 314638f7b870SPatrick McHardy else 314738f7b870SPatrick McHardy ifname[0] = '\0'; 314838f7b870SPatrick McHardy 314938f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 315038f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3151881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 315276c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 315376c9ac0eSJiri Pirko dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname); 315438f7b870SPatrick McHardy else 315538f7b870SPatrick McHardy dev = NULL; 315638f7b870SPatrick McHardy 3157ba7d49b1SJiri Pirko if (dev) { 3158ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3159ba7d49b1SJiri Pirko if (master_dev) 3160ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3161ba7d49b1SJiri Pirko } 3162ba7d49b1SJiri Pirko 3163e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 3164e0d087afSEric Dumazet if (err < 0) 31651840bb13SThomas Graf return err; 31661840bb13SThomas Graf 316738f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 31688cb08174SJohannes Berg err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 31698cb08174SJohannes Berg tb[IFLA_LINKINFO], 31708cb08174SJohannes Berg ifla_info_policy, NULL); 317138f7b870SPatrick McHardy if (err < 0) 317238f7b870SPatrick McHardy return err; 317338f7b870SPatrick McHardy } else 317438f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 317538f7b870SPatrick McHardy 317638f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 317738f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 317838f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 317938f7b870SPatrick McHardy } else { 318038f7b870SPatrick McHardy kind[0] = '\0'; 318138f7b870SPatrick McHardy ops = NULL; 318238f7b870SPatrick McHardy } 318338f7b870SPatrick McHardy 3184420d0318SJakub Kicinski data = NULL; 318538f7b870SPatrick McHardy if (ops) { 3186ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3187ccf8dbcdSKees Cook return -EINVAL; 3188ccf8dbcdSKees Cook 318938f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 31908cb08174SJohannes Berg err = nla_parse_nested_deprecated(attr, ops->maxtype, 319138f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 319268d57f3bSDavid Ahern ops->policy, extack); 319338f7b870SPatrick McHardy if (err < 0) 319438f7b870SPatrick McHardy return err; 319538f7b870SPatrick McHardy data = attr; 319638f7b870SPatrick McHardy } 319738f7b870SPatrick McHardy if (ops->validate) { 3198a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 319938f7b870SPatrick McHardy if (err < 0) 320038f7b870SPatrick McHardy return err; 320138f7b870SPatrick McHardy } 320238f7b870SPatrick McHardy } 320338f7b870SPatrick McHardy 3204420d0318SJakub Kicinski slave_data = NULL; 3205ba7d49b1SJiri Pirko if (m_ops) { 3206af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3207ccf8dbcdSKees Cook return -EINVAL; 3208ccf8dbcdSKees Cook 3209ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3210ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 32118cb08174SJohannes Berg err = nla_parse_nested_deprecated(slave_attr, 32128cb08174SJohannes Berg m_ops->slave_maxtype, 3213ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 32148cb08174SJohannes Berg m_ops->slave_policy, 32158cb08174SJohannes Berg extack); 3216ba7d49b1SJiri Pirko if (err < 0) 3217ba7d49b1SJiri Pirko return err; 3218ba7d49b1SJiri Pirko slave_data = slave_attr; 3219ba7d49b1SJiri Pirko } 3220ba7d49b1SJiri Pirko } 3221ba7d49b1SJiri Pirko 322238f7b870SPatrick McHardy if (dev) { 322390c325e3SNicolas Dichtel int status = 0; 322438f7b870SPatrick McHardy 322538f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 322638f7b870SPatrick McHardy return -EEXIST; 322738f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 322838f7b870SPatrick McHardy return -EOPNOTSUPP; 322938f7b870SPatrick McHardy 323038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 323138f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 323238f7b870SPatrick McHardy !ops->changelink) 323338f7b870SPatrick McHardy return -EOPNOTSUPP; 323438f7b870SPatrick McHardy 3235ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 323638f7b870SPatrick McHardy if (err < 0) 323738f7b870SPatrick McHardy return err; 3238ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 323938f7b870SPatrick McHardy } 324038f7b870SPatrick McHardy 3241ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3242ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3243ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3244ba7d49b1SJiri Pirko 3245420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3246420d0318SJakub Kicinski slave_data, extack); 3247ba7d49b1SJiri Pirko if (err < 0) 3248ba7d49b1SJiri Pirko return err; 3249ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3250ba7d49b1SJiri Pirko } 3251ba7d49b1SJiri Pirko 3252420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 325338f7b870SPatrick McHardy } 325438f7b870SPatrick McHardy 3255ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3256ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 325790f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3258ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3259ddf9f970SJakub Kicinski ifm, extack, tb); 326038f7b870SPatrick McHardy return -ENODEV; 3261ffa934f1SPatrick McHardy } 326238f7b870SPatrick McHardy 3263160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 326438f7b870SPatrick McHardy return -EOPNOTSUPP; 326538f7b870SPatrick McHardy 326638f7b870SPatrick McHardy if (!ops) { 326795a5afcaSJohannes Berg #ifdef CONFIG_MODULES 326838f7b870SPatrick McHardy if (kind[0]) { 326938f7b870SPatrick McHardy __rtnl_unlock(); 327038f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 327138f7b870SPatrick McHardy rtnl_lock(); 327238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 327338f7b870SPatrick McHardy if (ops) 327438f7b870SPatrick McHardy goto replay; 327538f7b870SPatrick McHardy } 327638f7b870SPatrick McHardy #endif 327768d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 327838f7b870SPatrick McHardy return -EOPNOTSUPP; 327938f7b870SPatrick McHardy } 328038f7b870SPatrick McHardy 3281b0ab2fabSJiri Pirko if (!ops->setup) 3282b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3283b0ab2fabSJiri Pirko 32845517750fSTom Gundersen if (!ifname[0]) { 328538f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 32865517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 32875517750fSTom Gundersen } 328838f7b870SPatrick McHardy 32895bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 329013ad1774SEric W. Biederman if (IS_ERR(dest_net)) 329113ad1774SEric W. Biederman return PTR_ERR(dest_net); 329213ad1774SEric W. Biederman 3293317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3294317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3295317f4810SNicolas Dichtel 3296317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3297317f4810SNicolas Dichtel if (!link_net) { 329868d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3299317f4810SNicolas Dichtel err = -EINVAL; 3300317f4810SNicolas Dichtel goto out; 3301317f4810SNicolas Dichtel } 330206615bedSEric W. Biederman err = -EPERM; 330306615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 330406615bedSEric W. Biederman goto out; 3305420d0318SJakub Kicinski } else { 3306420d0318SJakub Kicinski link_net = NULL; 3307317f4810SNicolas Dichtel } 3308317f4810SNicolas Dichtel 3309317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3310d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 33119c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3312e7199288SPavel Emelianov err = PTR_ERR(dev); 33139c7dafbfSPavel Emelyanov goto out; 33149c7dafbfSPavel Emelyanov } 33159c7dafbfSPavel Emelyanov 33169c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 33179c7dafbfSPavel Emelyanov 33180e0eee24SCong Wang if (ops->newlink) { 3319420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 33200e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3321e51fb152SCong Wang * and unregister it on failure after registration 3322e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 33230e0eee24SCong Wang */ 3324e51fb152SCong Wang if (err < 0) { 3325e51fb152SCong Wang /* If device is not registered at all, free it now */ 3326e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3327e51fb152SCong Wang free_netdev(dev); 33280e0eee24SCong Wang goto out; 3329e51fb152SCong Wang } 33300e0eee24SCong Wang } else { 33312d85cba2SPatrick McHardy err = register_netdevice(dev); 3332fce9b9beSDan Carpenter if (err < 0) { 333338f7b870SPatrick McHardy free_netdev(dev); 33343729d502SPatrick McHardy goto out; 3335fce9b9beSDan Carpenter } 33360e0eee24SCong Wang } 33373729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 333843638900SDavid S. Miller if (err < 0) 333943638900SDavid S. Miller goto out_unregister; 334043638900SDavid S. Miller if (link_net) { 334143638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 334243638900SDavid S. Miller if (err < 0) 334343638900SDavid S. Miller goto out_unregister; 334443638900SDavid S. Miller } 3345160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3346420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3347160ca014STheuns Verwoerd if (err) 3348160ca014STheuns Verwoerd goto out_unregister; 3349160ca014STheuns Verwoerd } 335043638900SDavid S. Miller out: 335143638900SDavid S. Miller if (link_net) 335243638900SDavid S. Miller put_net(link_net); 335343638900SDavid S. Miller put_net(dest_net); 335443638900SDavid S. Miller return err; 335543638900SDavid S. Miller out_unregister: 33567afb8886SWANG Cong if (ops->newlink) { 33577afb8886SWANG Cong LIST_HEAD(list_kill); 33587afb8886SWANG Cong 33597afb8886SWANG Cong ops->dellink(dev, &list_kill); 33607afb8886SWANG Cong unregister_netdevice_many(&list_kill); 33617afb8886SWANG Cong } else { 33623729d502SPatrick McHardy unregister_netdevice(dev); 33637afb8886SWANG Cong } 3364317f4810SNicolas Dichtel goto out; 3365317f4810SNicolas Dichtel } 336638f7b870SPatrick McHardy 3367a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3368a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3369a2939745SJakub Kicinski { 3370a2939745SJakub Kicinski struct nlattr **attr; 3371a2939745SJakub Kicinski int ret; 3372a2939745SJakub Kicinski 3373a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3374a2939745SJakub Kicinski if (!attr) 3375a2939745SJakub Kicinski return -ENOMEM; 3376a2939745SJakub Kicinski 3377a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3378a2939745SJakub Kicinski kfree(attr); 3379a2939745SJakub Kicinski return ret; 3380a2939745SJakub Kicinski } 3381a2939745SJakub Kicinski 33829b3757b0SJakub Kicinski static int rtnl_valid_getlink_req(struct sk_buff *skb, 33839b3757b0SJakub Kicinski const struct nlmsghdr *nlh, 33849b3757b0SJakub Kicinski struct nlattr **tb, 33859b3757b0SJakub Kicinski struct netlink_ext_ack *extack) 33869b3757b0SJakub Kicinski { 33879b3757b0SJakub Kicinski struct ifinfomsg *ifm; 33889b3757b0SJakub Kicinski int i, err; 33899b3757b0SJakub Kicinski 33909b3757b0SJakub Kicinski if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 33919b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for get link"); 33929b3757b0SJakub Kicinski return -EINVAL; 33939b3757b0SJakub Kicinski } 33949b3757b0SJakub Kicinski 33959b3757b0SJakub Kicinski if (!netlink_strict_get_check(skb)) 33968cb08174SJohannes Berg return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 33978cb08174SJohannes Berg ifla_policy, extack); 33989b3757b0SJakub Kicinski 33999b3757b0SJakub Kicinski ifm = nlmsg_data(nlh); 34009b3757b0SJakub Kicinski if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 34019b3757b0SJakub Kicinski ifm->ifi_change) { 34029b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 34039b3757b0SJakub Kicinski return -EINVAL; 34049b3757b0SJakub Kicinski } 34059b3757b0SJakub Kicinski 34068cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 34078cb08174SJohannes Berg ifla_policy, extack); 34089b3757b0SJakub Kicinski if (err) 34099b3757b0SJakub Kicinski return err; 34109b3757b0SJakub Kicinski 34119b3757b0SJakub Kicinski for (i = 0; i <= IFLA_MAX; i++) { 34129b3757b0SJakub Kicinski if (!tb[i]) 34139b3757b0SJakub Kicinski continue; 34149b3757b0SJakub Kicinski 34159b3757b0SJakub Kicinski switch (i) { 34169b3757b0SJakub Kicinski case IFLA_IFNAME: 341776c9ac0eSJiri Pirko case IFLA_ALT_IFNAME: 34189b3757b0SJakub Kicinski case IFLA_EXT_MASK: 34199b3757b0SJakub Kicinski case IFLA_TARGET_NETNSID: 34209b3757b0SJakub Kicinski break; 34219b3757b0SJakub Kicinski default: 34229b3757b0SJakub Kicinski NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 34239b3757b0SJakub Kicinski return -EINVAL; 34249b3757b0SJakub Kicinski } 34259b3757b0SJakub Kicinski } 34269b3757b0SJakub Kicinski 34279b3757b0SJakub Kicinski return 0; 34289b3757b0SJakub Kicinski } 34299b3757b0SJakub Kicinski 3430c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3431c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3432711e2c33SJean Tourrilhes { 34333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 343479e1ad14SJiri Benc struct net *tgt_net = net; 3435b60c5115SThomas Graf struct ifinfomsg *ifm; 3436b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3437b60c5115SThomas Graf struct net_device *dev = NULL; 3438b60c5115SThomas Graf struct sk_buff *nskb; 343979e1ad14SJiri Benc int netnsid = -1; 3440339bf98fSThomas Graf int err; 3441115c9b81SGreg Rose u32 ext_filter_mask = 0; 3442711e2c33SJean Tourrilhes 34439b3757b0SJakub Kicinski err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3444b60c5115SThomas Graf if (err < 0) 34459918f230SEric Sesterhenn return err; 3446b60c5115SThomas Graf 34474ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 34484ff66caeSChristian Brauner if (err < 0) 34494ff66caeSChristian Brauner return err; 34504ff66caeSChristian Brauner 34517e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 34527e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3453c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 345479e1ad14SJiri Benc if (IS_ERR(tgt_net)) 345579e1ad14SJiri Benc return PTR_ERR(tgt_net); 345679e1ad14SJiri Benc } 345779e1ad14SJiri Benc 3458115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3459115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3460115c9b81SGreg Rose 346179e1ad14SJiri Benc err = -EINVAL; 3462b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3463a3d12891SEric Dumazet if (ifm->ifi_index > 0) 346479e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 346576c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 346676c9ac0eSJiri Pirko dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME], 346776c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3468a3d12891SEric Dumazet else 346979e1ad14SJiri Benc goto out; 3470b60c5115SThomas Graf 347179e1ad14SJiri Benc err = -ENODEV; 3472a3d12891SEric Dumazet if (dev == NULL) 347379e1ad14SJiri Benc goto out; 3474a3d12891SEric Dumazet 347579e1ad14SJiri Benc err = -ENOBUFS; 3476115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3477a3d12891SEric Dumazet if (nskb == NULL) 347879e1ad14SJiri Benc goto out; 3479711e2c33SJean Tourrilhes 348079e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 348179e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 348279e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3483d4e4fdf9SGuillaume Nault 0, NULL, 0, netnsid, GFP_KERNEL); 348426932566SPatrick McHardy if (err < 0) { 348526932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 348626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 348726932566SPatrick McHardy kfree_skb(nskb); 3488a3d12891SEric Dumazet } else 348915e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 349079e1ad14SJiri Benc out: 349179e1ad14SJiri Benc if (netnsid >= 0) 349279e1ad14SJiri Benc put_net(tgt_net); 3493b60c5115SThomas Graf 3494711e2c33SJean Tourrilhes return err; 3495711e2c33SJean Tourrilhes } 3496711e2c33SJean Tourrilhes 349736fbf1e5SJiri Pirko static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 349836fbf1e5SJiri Pirko bool *changed, struct netlink_ext_ack *extack) 349936fbf1e5SJiri Pirko { 350036fbf1e5SJiri Pirko char *alt_ifname; 350136fbf1e5SJiri Pirko int err; 350236fbf1e5SJiri Pirko 350336fbf1e5SJiri Pirko err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 350436fbf1e5SJiri Pirko if (err) 350536fbf1e5SJiri Pirko return err; 350636fbf1e5SJiri Pirko 3507*44bfa9c5SEric Dumazet alt_ifname = nla_strdup(attr, GFP_KERNEL); 350836fbf1e5SJiri Pirko if (!alt_ifname) 350936fbf1e5SJiri Pirko return -ENOMEM; 3510*44bfa9c5SEric Dumazet 3511*44bfa9c5SEric Dumazet if (cmd == RTM_NEWLINKPROP) { 351236fbf1e5SJiri Pirko err = netdev_name_node_alt_create(dev, alt_ifname); 3513*44bfa9c5SEric Dumazet if (!err) 3514*44bfa9c5SEric Dumazet alt_ifname = NULL; 351536fbf1e5SJiri Pirko } else if (cmd == RTM_DELLINKPROP) { 351636fbf1e5SJiri Pirko err = netdev_name_node_alt_destroy(dev, alt_ifname); 351736fbf1e5SJiri Pirko } else { 3518*44bfa9c5SEric Dumazet WARN_ON_ONCE(1); 3519*44bfa9c5SEric Dumazet err = -EINVAL; 352036fbf1e5SJiri Pirko } 352136fbf1e5SJiri Pirko 3522*44bfa9c5SEric Dumazet kfree(alt_ifname); 3523*44bfa9c5SEric Dumazet if (!err) 352436fbf1e5SJiri Pirko *changed = true; 3525*44bfa9c5SEric Dumazet return err; 352636fbf1e5SJiri Pirko } 352736fbf1e5SJiri Pirko 352836fbf1e5SJiri Pirko static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 352936fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 353036fbf1e5SJiri Pirko { 353136fbf1e5SJiri Pirko struct net *net = sock_net(skb->sk); 353236fbf1e5SJiri Pirko struct nlattr *tb[IFLA_MAX + 1]; 353336fbf1e5SJiri Pirko struct net_device *dev; 353436fbf1e5SJiri Pirko struct ifinfomsg *ifm; 353536fbf1e5SJiri Pirko bool changed = false; 353636fbf1e5SJiri Pirko struct nlattr *attr; 353736fbf1e5SJiri Pirko int err, rem; 353836fbf1e5SJiri Pirko 353936fbf1e5SJiri Pirko err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 354036fbf1e5SJiri Pirko if (err) 354136fbf1e5SJiri Pirko return err; 354236fbf1e5SJiri Pirko 354336fbf1e5SJiri Pirko err = rtnl_ensure_unique_netns(tb, extack, true); 354436fbf1e5SJiri Pirko if (err) 354536fbf1e5SJiri Pirko return err; 354636fbf1e5SJiri Pirko 354736fbf1e5SJiri Pirko ifm = nlmsg_data(nlh); 3548cc6090e9SJiri Pirko if (ifm->ifi_index > 0) 354936fbf1e5SJiri Pirko dev = __dev_get_by_index(net, ifm->ifi_index); 355076c9ac0eSJiri Pirko else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 355176c9ac0eSJiri Pirko dev = rtnl_dev_get(net, tb[IFLA_IFNAME], 355276c9ac0eSJiri Pirko tb[IFLA_ALT_IFNAME], NULL); 3553cc6090e9SJiri Pirko else 355436fbf1e5SJiri Pirko return -EINVAL; 355536fbf1e5SJiri Pirko 355636fbf1e5SJiri Pirko if (!dev) 355736fbf1e5SJiri Pirko return -ENODEV; 355836fbf1e5SJiri Pirko 355936fbf1e5SJiri Pirko if (!tb[IFLA_PROP_LIST]) 356036fbf1e5SJiri Pirko return 0; 356136fbf1e5SJiri Pirko 356236fbf1e5SJiri Pirko nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 356336fbf1e5SJiri Pirko switch (nla_type(attr)) { 356436fbf1e5SJiri Pirko case IFLA_ALT_IFNAME: 356536fbf1e5SJiri Pirko err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 356636fbf1e5SJiri Pirko if (err) 356736fbf1e5SJiri Pirko return err; 356836fbf1e5SJiri Pirko break; 356936fbf1e5SJiri Pirko } 357036fbf1e5SJiri Pirko } 357136fbf1e5SJiri Pirko 357236fbf1e5SJiri Pirko if (changed) 357336fbf1e5SJiri Pirko netdev_state_change(dev); 357436fbf1e5SJiri Pirko return 0; 357536fbf1e5SJiri Pirko } 357636fbf1e5SJiri Pirko 357736fbf1e5SJiri Pirko static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 357836fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 357936fbf1e5SJiri Pirko { 358036fbf1e5SJiri Pirko return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 358136fbf1e5SJiri Pirko } 358236fbf1e5SJiri Pirko 358336fbf1e5SJiri Pirko static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 358436fbf1e5SJiri Pirko struct netlink_ext_ack *extack) 358536fbf1e5SJiri Pirko { 358636fbf1e5SJiri Pirko return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 358736fbf1e5SJiri Pirko } 358836fbf1e5SJiri Pirko 3589115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3590c7ac8679SGreg Rose { 3591115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3592115c9b81SGreg Rose struct net_device *dev; 3593115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3594115c9b81SGreg Rose u32 ext_filter_mask = 0; 3595115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3596e5eca6d4SMichal Schmidt int hdrlen; 3597115c9b81SGreg Rose 3598e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3599e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3600e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3601e5eca6d4SMichal Schmidt 36028cb08174SJohannes Berg if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3603115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3604115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3605a4b64fbeSEric Dumazet } 3606115c9b81SGreg Rose 3607115c9b81SGreg Rose if (!ext_filter_mask) 3608115c9b81SGreg Rose return NLMSG_GOODSIZE; 3609115c9b81SGreg Rose /* 3610115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3611115c9b81SGreg Rose * buffer size based upon the filter mask. 3612115c9b81SGreg Rose */ 36136853dd48SFlorian Westphal rcu_read_lock(); 36146853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3615115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3616115c9b81SGreg Rose if_nlmsg_size(dev, 3617115c9b81SGreg Rose ext_filter_mask)); 3618115c9b81SGreg Rose } 36196853dd48SFlorian Westphal rcu_read_unlock(); 3620115c9b81SGreg Rose 362193af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3622c7ac8679SGreg Rose } 3623c7ac8679SGreg Rose 362442bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 36251da177e4SLinus Torvalds { 36261da177e4SLinus Torvalds int idx; 36271da177e4SLinus Torvalds int s_idx = cb->family; 362887ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3629c63586dcSDavid Ahern int ret = 0; 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds if (s_idx == 0) 36321da177e4SLinus Torvalds s_idx = 1; 36336853dd48SFlorian Westphal 363425239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3635addf9b90SFlorian Westphal struct rtnl_link **tab; 3636addf9b90SFlorian Westphal struct rtnl_link *link; 36376853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 36386853dd48SFlorian Westphal 36391da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 36401da177e4SLinus Torvalds continue; 36416853dd48SFlorian Westphal 3642addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 36431da177e4SLinus Torvalds continue; 36446853dd48SFlorian Westphal 3645addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3646addf9b90SFlorian Westphal if (!tab) 3647addf9b90SFlorian Westphal continue; 3648addf9b90SFlorian Westphal 3649addf9b90SFlorian Westphal link = tab[type]; 3650addf9b90SFlorian Westphal if (!link) 3651addf9b90SFlorian Westphal continue; 3652addf9b90SFlorian Westphal 3653addf9b90SFlorian Westphal dumpit = link->dumpit; 36546853dd48SFlorian Westphal if (!dumpit) 36556853dd48SFlorian Westphal continue; 36566853dd48SFlorian Westphal 36570465277fSNicolas Dichtel if (idx > s_idx) { 36581da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 36590465277fSNicolas Dichtel cb->prev_seq = 0; 36600465277fSNicolas Dichtel cb->seq = 0; 36610465277fSNicolas Dichtel } 3662c63586dcSDavid Ahern ret = dumpit(skb, cb); 36635e1acb4aSAlexey Kodanev if (ret) 36641da177e4SLinus Torvalds break; 36651da177e4SLinus Torvalds } 36661da177e4SLinus Torvalds cb->family = idx; 36671da177e4SLinus Torvalds 3668c63586dcSDavid Ahern return skb->len ? : ret; 36691da177e4SLinus Torvalds } 36701da177e4SLinus Torvalds 3671395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 36723d3ea5afSVlad Yasevich unsigned int change, 367338e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 367438e01b30SNicolas Dichtel int new_ifindex) 36751da177e4SLinus Torvalds { 3676c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 36771da177e4SLinus Torvalds struct sk_buff *skb; 36780ec6d3f4SThomas Graf int err = -ENOBUFS; 3679c7ac8679SGreg Rose size_t if_info_size; 36801da177e4SLinus Torvalds 36817f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 36820ec6d3f4SThomas Graf if (skb == NULL) 36830ec6d3f4SThomas Graf goto errout; 36841da177e4SLinus Torvalds 368579e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 368679e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 3687d4e4fdf9SGuillaume Nault new_nsid, new_ifindex, -1, flags); 368826932566SPatrick McHardy if (err < 0) { 368926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 369026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 369126932566SPatrick McHardy kfree_skb(skb); 369226932566SPatrick McHardy goto errout; 369326932566SPatrick McHardy } 3694395eea6cSMahesh Bandewar return skb; 36950ec6d3f4SThomas Graf errout: 36960ec6d3f4SThomas Graf if (err < 0) 36974b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3698395eea6cSMahesh Bandewar return NULL; 3699395eea6cSMahesh Bandewar } 3700395eea6cSMahesh Bandewar 3701395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3702395eea6cSMahesh Bandewar { 3703395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3704395eea6cSMahesh Bandewar 3705395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3706395eea6cSMahesh Bandewar } 3707395eea6cSMahesh Bandewar 37083d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 37093d3ea5afSVlad Yasevich unsigned int change, u32 event, 371038e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3711395eea6cSMahesh Bandewar { 3712395eea6cSMahesh Bandewar struct sk_buff *skb; 3713395eea6cSMahesh Bandewar 3714ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3715ed2a80abSNicolas Dichtel return; 3716ed2a80abSNicolas Dichtel 371738e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 371838e01b30SNicolas Dichtel new_ifindex); 3719395eea6cSMahesh Bandewar if (skb) 3720395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 37211da177e4SLinus Torvalds } 37223d3ea5afSVlad Yasevich 37233d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 37243d3ea5afSVlad Yasevich gfp_t flags) 37253d3ea5afSVlad Yasevich { 372638e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 372738e01b30SNicolas Dichtel NULL, 0); 37283d3ea5afSVlad Yasevich } 37291da177e4SLinus Torvalds 37306621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 373138e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 37326621dd29SNicolas Dichtel { 37336621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 373438e01b30SNicolas Dichtel new_nsid, new_ifindex); 37356621dd29SNicolas Dichtel } 37366621dd29SNicolas Dichtel 3737d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3738d83b0603SJohn Fastabend struct net_device *dev, 37391e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 37401c104a6bSNicolas Dichtel int type, unsigned int flags, 3741b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3742d83b0603SJohn Fastabend { 3743d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3744d83b0603SJohn Fastabend struct ndmsg *ndm; 3745d83b0603SJohn Fastabend 37461c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3747d83b0603SJohn Fastabend if (!nlh) 3748d83b0603SJohn Fastabend return -EMSGSIZE; 3749d83b0603SJohn Fastabend 3750d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3751d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3752d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3753d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3754d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3755d83b0603SJohn Fastabend ndm->ndm_type = 0; 3756d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3757b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3758d83b0603SJohn Fastabend 3759d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3760d83b0603SJohn Fastabend goto nla_put_failure; 37611e53d5bbSHubert Sokolowski if (vid) 37621e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 37631e53d5bbSHubert Sokolowski goto nla_put_failure; 3764d83b0603SJohn Fastabend 3765053c095aSJohannes Berg nlmsg_end(skb, nlh); 3766053c095aSJohannes Berg return 0; 3767d83b0603SJohn Fastabend 3768d83b0603SJohn Fastabend nla_put_failure: 3769d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3770d83b0603SJohn Fastabend return -EMSGSIZE; 3771d83b0603SJohn Fastabend } 3772d83b0603SJohn Fastabend 37733ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 37743ff661c3SJohn Fastabend { 3775f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3776f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3777f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3778f82ef3e1SSabrina Dubroca 0; 37793ff661c3SJohn Fastabend } 37803ff661c3SJohn Fastabend 3781b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3782b3379041SHubert Sokolowski u16 ndm_state) 37833ff661c3SJohn Fastabend { 37843ff661c3SJohn Fastabend struct net *net = dev_net(dev); 37853ff661c3SJohn Fastabend struct sk_buff *skb; 37863ff661c3SJohn Fastabend int err = -ENOBUFS; 37873ff661c3SJohn Fastabend 37883ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 37893ff661c3SJohn Fastabend if (!skb) 37903ff661c3SJohn Fastabend goto errout; 37913ff661c3SJohn Fastabend 37921e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3793b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 37943ff661c3SJohn Fastabend if (err < 0) { 37953ff661c3SJohn Fastabend kfree_skb(skb); 37963ff661c3SJohn Fastabend goto errout; 37973ff661c3SJohn Fastabend } 37983ff661c3SJohn Fastabend 37993ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 38003ff661c3SJohn Fastabend return; 38013ff661c3SJohn Fastabend errout: 38023ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 38033ff661c3SJohn Fastabend } 38043ff661c3SJohn Fastabend 3805a986967eSBart Van Assche /* 3806090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3807090096bfSVlad Yasevich */ 3808090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3809090096bfSVlad Yasevich struct nlattr *tb[], 3810090096bfSVlad Yasevich struct net_device *dev, 3811f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3812090096bfSVlad Yasevich u16 flags) 3813090096bfSVlad Yasevich { 3814090096bfSVlad Yasevich int err = -EINVAL; 3815090096bfSVlad Yasevich 3816090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3817090096bfSVlad Yasevich * implement its own handler for this. 3818090096bfSVlad Yasevich */ 3819090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3820090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3821090096bfSVlad Yasevich return err; 3822090096bfSVlad Yasevich } 3823090096bfSVlad Yasevich 382465891feaSOr Gerlitz if (vid) { 382565891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 382665891feaSOr Gerlitz return err; 382765891feaSOr Gerlitz } 382865891feaSOr Gerlitz 3829090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3830090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3831090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3832090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3833090096bfSVlad Yasevich 3834090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3835090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3836090096bfSVlad Yasevich err = 0; 3837090096bfSVlad Yasevich 3838090096bfSVlad Yasevich return err; 3839090096bfSVlad Yasevich } 3840090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3841090096bfSVlad Yasevich 3842b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3843b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3844f6f6424bSJiri Pirko { 3845f6f6424bSJiri Pirko u16 vid = 0; 3846f6f6424bSJiri Pirko 3847f6f6424bSJiri Pirko if (vlan_attr) { 3848f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3849b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3850f6f6424bSJiri Pirko return -EINVAL; 3851f6f6424bSJiri Pirko } 3852f6f6424bSJiri Pirko 3853f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3854f6f6424bSJiri Pirko 3855f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3856b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3857f6f6424bSJiri Pirko return -EINVAL; 3858f6f6424bSJiri Pirko } 3859f6f6424bSJiri Pirko } 3860f6f6424bSJiri Pirko *p_vid = vid; 3861f6f6424bSJiri Pirko return 0; 3862f6f6424bSJiri Pirko } 3863f6f6424bSJiri Pirko 3864c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3865c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 386677162022SJohn Fastabend { 386777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 386877162022SJohn Fastabend struct ndmsg *ndm; 386977162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 387077162022SJohn Fastabend struct net_device *dev; 387177162022SJohn Fastabend u8 *addr; 3872f6f6424bSJiri Pirko u16 vid; 387377162022SJohn Fastabend int err; 387477162022SJohn Fastabend 38758cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 38768cb08174SJohannes Berg extack); 387777162022SJohn Fastabend if (err < 0) 387877162022SJohn Fastabend return err; 387977162022SJohn Fastabend 388077162022SJohn Fastabend ndm = nlmsg_data(nlh); 388177162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3882b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 388377162022SJohn Fastabend return -EINVAL; 388477162022SJohn Fastabend } 388577162022SJohn Fastabend 388677162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 388777162022SJohn Fastabend if (dev == NULL) { 3888b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 388977162022SJohn Fastabend return -ENODEV; 389077162022SJohn Fastabend } 389177162022SJohn Fastabend 389277162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3893b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 389477162022SJohn Fastabend return -EINVAL; 389577162022SJohn Fastabend } 389677162022SJohn Fastabend 3897da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 3898da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 3899da715775SIdo Schimmel return -EINVAL; 3900da715775SIdo Schimmel } 3901da715775SIdo Schimmel 390277162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 390377162022SJohn Fastabend 3904b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3905f6f6424bSJiri Pirko if (err) 3906f6f6424bSJiri Pirko return err; 3907f6f6424bSJiri Pirko 390877162022SJohn Fastabend err = -EOPNOTSUPP; 390977162022SJohn Fastabend 391077162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 391177162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 391277162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3913898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3914898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3915898e5061SJiri Pirko 3916f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 391787b0984eSPetr Machata nlh->nlmsg_flags, extack); 391877162022SJohn Fastabend if (err) 391977162022SJohn Fastabend goto out; 392077162022SJohn Fastabend else 392177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 392277162022SJohn Fastabend } 392377162022SJohn Fastabend 392477162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3925090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3926090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3927090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3928f6f6424bSJiri Pirko vid, 392987b0984eSPetr Machata nlh->nlmsg_flags, 393087b0984eSPetr Machata extack); 3931090096bfSVlad Yasevich else 3932f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 393377162022SJohn Fastabend nlh->nlmsg_flags); 393477162022SJohn Fastabend 39353ff661c3SJohn Fastabend if (!err) { 3936b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3937b3379041SHubert Sokolowski ndm->ndm_state); 393877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 393977162022SJohn Fastabend } 39403ff661c3SJohn Fastabend } 394177162022SJohn Fastabend out: 394277162022SJohn Fastabend return err; 394377162022SJohn Fastabend } 394477162022SJohn Fastabend 3945a986967eSBart Van Assche /* 3946090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3947090096bfSVlad Yasevich */ 3948090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3949090096bfSVlad Yasevich struct nlattr *tb[], 3950090096bfSVlad Yasevich struct net_device *dev, 3951f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3952090096bfSVlad Yasevich { 3953c8a89c4aSAlexander Duyck int err = -EINVAL; 3954090096bfSVlad Yasevich 3955090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3956090096bfSVlad Yasevich * implement its own handler for this. 3957090096bfSVlad Yasevich */ 395864535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3959090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3960c8a89c4aSAlexander Duyck return err; 3961090096bfSVlad Yasevich } 3962090096bfSVlad Yasevich 3963090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3964090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3965090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3966090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3967090096bfSVlad Yasevich 3968090096bfSVlad Yasevich return err; 3969090096bfSVlad Yasevich } 3970090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3971090096bfSVlad Yasevich 3972c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3973c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 397477162022SJohn Fastabend { 397577162022SJohn Fastabend struct net *net = sock_net(skb->sk); 397677162022SJohn Fastabend struct ndmsg *ndm; 39771690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 397877162022SJohn Fastabend struct net_device *dev; 397977162022SJohn Fastabend int err = -EINVAL; 398077162022SJohn Fastabend __u8 *addr; 3981f6f6424bSJiri Pirko u16 vid; 398277162022SJohn Fastabend 398390f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 39841690be63SVlad Yasevich return -EPERM; 39851690be63SVlad Yasevich 39868cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 39878cb08174SJohannes Berg extack); 39881690be63SVlad Yasevich if (err < 0) 39891690be63SVlad Yasevich return err; 399077162022SJohn Fastabend 399177162022SJohn Fastabend ndm = nlmsg_data(nlh); 399277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3993b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 399477162022SJohn Fastabend return -EINVAL; 399577162022SJohn Fastabend } 399677162022SJohn Fastabend 399777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 399877162022SJohn Fastabend if (dev == NULL) { 3999b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 400077162022SJohn Fastabend return -ENODEV; 400177162022SJohn Fastabend } 400277162022SJohn Fastabend 40031690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4004b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 400577162022SJohn Fastabend return -EINVAL; 400677162022SJohn Fastabend } 400777162022SJohn Fastabend 4008da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 4009da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4010da715775SIdo Schimmel return -EINVAL; 4011da715775SIdo Schimmel } 4012da715775SIdo Schimmel 40131690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 40141690be63SVlad Yasevich 4015b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4016f6f6424bSJiri Pirko if (err) 4017f6f6424bSJiri Pirko return err; 4018f6f6424bSJiri Pirko 401977162022SJohn Fastabend err = -EOPNOTSUPP; 402077162022SJohn Fastabend 402177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 402277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 402377162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 4024898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4025898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 402677162022SJohn Fastabend 4027898e5061SJiri Pirko if (ops->ndo_fdb_del) 4028f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 402977162022SJohn Fastabend 403077162022SJohn Fastabend if (err) 403177162022SJohn Fastabend goto out; 403277162022SJohn Fastabend else 403377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 403477162022SJohn Fastabend } 403577162022SJohn Fastabend 403677162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 4037090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 4038090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 4039f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 4040f6f6424bSJiri Pirko vid); 4041090096bfSVlad Yasevich else 4042f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 404377162022SJohn Fastabend 40443ff661c3SJohn Fastabend if (!err) { 4045b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4046b3379041SHubert Sokolowski ndm->ndm_state); 404777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 404877162022SJohn Fastabend } 40493ff661c3SJohn Fastabend } 405077162022SJohn Fastabend out: 405177162022SJohn Fastabend return err; 405277162022SJohn Fastabend } 405377162022SJohn Fastabend 4054d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 4055d83b0603SJohn Fastabend struct netlink_callback *cb, 4056d83b0603SJohn Fastabend struct net_device *dev, 4057d83b0603SJohn Fastabend int *idx, 4058d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 4059d83b0603SJohn Fastabend { 4060d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 4061d83b0603SJohn Fastabend int err; 406215e47304SEric W. Biederman u32 portid, seq; 4063d83b0603SJohn Fastabend 406415e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 4065d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 4066d83b0603SJohn Fastabend 4067d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 4068d297653dSRoopa Prabhu if (*idx < cb->args[2]) 4069d83b0603SJohn Fastabend goto skip; 4070d83b0603SJohn Fastabend 40711e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4072a7a558feSJohn Fastabend portid, seq, 40731c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 4074b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 4075d83b0603SJohn Fastabend if (err < 0) 4076d83b0603SJohn Fastabend return err; 4077d83b0603SJohn Fastabend skip: 4078d83b0603SJohn Fastabend *idx += 1; 4079d83b0603SJohn Fastabend } 4080d83b0603SJohn Fastabend return 0; 4081d83b0603SJohn Fastabend } 4082d83b0603SJohn Fastabend 4083d83b0603SJohn Fastabend /** 40842c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4085a986967eSBart Van Assche * @skb: socket buffer to store message in 4086a986967eSBart Van Assche * @cb: netlink callback 4087d83b0603SJohn Fastabend * @dev: netdevice 4088a986967eSBart Van Assche * @filter_dev: ignored 4089a986967eSBart Van Assche * @idx: the number of FDB table entries dumped is added to *@idx 4090d83b0603SJohn Fastabend * 4091d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 409291f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 4093d83b0603SJohn Fastabend */ 4094d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 4095d83b0603SJohn Fastabend struct netlink_callback *cb, 4096d83b0603SJohn Fastabend struct net_device *dev, 40975d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 4098d297653dSRoopa Prabhu int *idx) 4099d83b0603SJohn Fastabend { 4100d83b0603SJohn Fastabend int err; 4101d83b0603SJohn Fastabend 410268883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 410368883893SEric Dumazet return -EINVAL; 410468883893SEric Dumazet 4105d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 4106d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4107d83b0603SJohn Fastabend if (err) 4108d83b0603SJohn Fastabend goto out; 41092934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4110d83b0603SJohn Fastabend out: 4111d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 4112d297653dSRoopa Prabhu return err; 4113d83b0603SJohn Fastabend } 4114d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4115d83b0603SJohn Fastabend 41168c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 41178c6e137fSDavid Ahern int *br_idx, int *brport_idx, 41188c6e137fSDavid Ahern struct netlink_ext_ack *extack) 41198c6e137fSDavid Ahern { 41208c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 41218c6e137fSDavid Ahern struct ndmsg *ndm; 41228c6e137fSDavid Ahern int err, i; 41238c6e137fSDavid Ahern 41248c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 41258c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 41268c6e137fSDavid Ahern return -EINVAL; 41278c6e137fSDavid Ahern } 41288c6e137fSDavid Ahern 41298c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 41308c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 41318c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 41328b73018fSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 41338c6e137fSDavid Ahern return -EINVAL; 41348c6e137fSDavid Ahern } 41358c6e137fSDavid Ahern 41368cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 41378cb08174SJohannes Berg NDA_MAX, NULL, extack); 41388c6e137fSDavid Ahern if (err < 0) 41398c6e137fSDavid Ahern return err; 41408c6e137fSDavid Ahern 41418c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 41428c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 41438c6e137fSDavid Ahern if (!tb[i]) 41448c6e137fSDavid Ahern continue; 41458c6e137fSDavid Ahern 41468c6e137fSDavid Ahern switch (i) { 41478c6e137fSDavid Ahern case NDA_IFINDEX: 41488c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 41498c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 41508c6e137fSDavid Ahern return -EINVAL; 41518c6e137fSDavid Ahern } 41528c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 41538c6e137fSDavid Ahern break; 41548c6e137fSDavid Ahern case NDA_MASTER: 41558c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 41568c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 41578c6e137fSDavid Ahern return -EINVAL; 41588c6e137fSDavid Ahern } 41598c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 41608c6e137fSDavid Ahern break; 41618c6e137fSDavid Ahern default: 41628c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 41638c6e137fSDavid Ahern return -EINVAL; 41648c6e137fSDavid Ahern } 41658c6e137fSDavid Ahern } 41668c6e137fSDavid Ahern 41678c6e137fSDavid Ahern return 0; 41688c6e137fSDavid Ahern } 41698c6e137fSDavid Ahern 41708dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 41718dfbda19SDavid Ahern int *br_idx, int *brport_idx, 41728dfbda19SDavid Ahern struct netlink_ext_ack *extack) 41738dfbda19SDavid Ahern { 41748dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 41758dfbda19SDavid Ahern int err; 41768dfbda19SDavid Ahern 41778dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 41788dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 41798dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 41808dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 41818dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 41828dfbda19SDavid Ahern * with an optional attribute. 41838dfbda19SDavid Ahern */ 41848dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 41858dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 41868dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 41874565d7e5SDavid Ahern struct ifinfomsg *ifm; 41884565d7e5SDavid Ahern 41898cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 41908cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 41918cb08174SJohannes Berg extack); 41928dfbda19SDavid Ahern if (err < 0) { 41938dfbda19SDavid Ahern return -EINVAL; 41948dfbda19SDavid Ahern } else if (err == 0) { 41958dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 41968dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 41978dfbda19SDavid Ahern } 41988dfbda19SDavid Ahern 41994565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 42008dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 42018dfbda19SDavid Ahern } 42028dfbda19SDavid Ahern return 0; 42038dfbda19SDavid Ahern } 42048dfbda19SDavid Ahern 420577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 420677162022SJohn Fastabend { 420777162022SJohn Fastabend struct net_device *dev; 42085e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 42095e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 42105e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 42115e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 4212d297653dSRoopa Prabhu struct hlist_head *head; 42135e6d2435SJamal Hadi Salim int brport_idx = 0; 42145e6d2435SJamal Hadi Salim int br_idx = 0; 4215d297653dSRoopa Prabhu int h, s_h; 4216d297653dSRoopa Prabhu int idx = 0, s_idx; 4217d297653dSRoopa Prabhu int err = 0; 4218d297653dSRoopa Prabhu int fidx = 0; 421977162022SJohn Fastabend 42208c6e137fSDavid Ahern if (cb->strict_check) 42218c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 42228c6e137fSDavid Ahern cb->extack); 42238c6e137fSDavid Ahern else 42248dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 42258dfbda19SDavid Ahern cb->extack); 42268dfbda19SDavid Ahern if (err < 0) 42278dfbda19SDavid Ahern return err; 42285e6d2435SJamal Hadi Salim 42295e6d2435SJamal Hadi Salim if (br_idx) { 42305e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 42315e6d2435SJamal Hadi Salim if (!br_dev) 42325e6d2435SJamal Hadi Salim return -ENODEV; 42335e6d2435SJamal Hadi Salim 4234898e5061SJiri Pirko ops = br_dev->netdev_ops; 42355e6d2435SJamal Hadi Salim } 42365e6d2435SJamal Hadi Salim 4237d297653dSRoopa Prabhu s_h = cb->args[0]; 4238d297653dSRoopa Prabhu s_idx = cb->args[1]; 4239d297653dSRoopa Prabhu 4240d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4241d297653dSRoopa Prabhu idx = 0; 4242d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 4243d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 4244d297653dSRoopa Prabhu 42455e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 42465e6d2435SJamal Hadi Salim continue; 42475e6d2435SJamal Hadi Salim 42485e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 42495e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 42505e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 42515e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 42525e6d2435SJamal Hadi Salim } 42535e6d2435SJamal Hadi Salim } else { 42545e6d2435SJamal Hadi Salim if (dev != br_dev && 42555e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 42565e6d2435SJamal Hadi Salim continue; 42575e6d2435SJamal Hadi Salim 42585e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 42595e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 42605e6d2435SJamal Hadi Salim continue; 42615e6d2435SJamal Hadi Salim cops = ops; 42625e6d2435SJamal Hadi Salim } 42635e6d2435SJamal Hadi Salim 4264d297653dSRoopa Prabhu if (idx < s_idx) 4265d297653dSRoopa Prabhu goto cont; 4266d297653dSRoopa Prabhu 42675e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 4268d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 4269d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 4270d297653dSRoopa Prabhu br_dev, dev, 4271d297653dSRoopa Prabhu &fidx); 4272d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4273d297653dSRoopa Prabhu goto out; 427477162022SJohn Fastabend } 4275d297653dSRoopa Prabhu } 427677162022SJohn Fastabend 42775e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 4278d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4279d297653dSRoopa Prabhu dev, NULL, 4280d297653dSRoopa Prabhu &fidx); 42816cb69742SHubert Sokolowski else 4282d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4283d297653dSRoopa Prabhu &fidx); 4284d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4285d297653dSRoopa Prabhu goto out; 42865e6d2435SJamal Hadi Salim 42875e6d2435SJamal Hadi Salim cops = NULL; 4288d297653dSRoopa Prabhu 4289d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4290d297653dSRoopa Prabhu cb->args[2] = 0; 4291d297653dSRoopa Prabhu fidx = 0; 4292d297653dSRoopa Prabhu cont: 4293d297653dSRoopa Prabhu idx++; 4294d297653dSRoopa Prabhu } 429577162022SJohn Fastabend } 429677162022SJohn Fastabend 4297d297653dSRoopa Prabhu out: 4298d297653dSRoopa Prabhu cb->args[0] = h; 4299d297653dSRoopa Prabhu cb->args[1] = idx; 4300d297653dSRoopa Prabhu cb->args[2] = fidx; 4301d297653dSRoopa Prabhu 430277162022SJohn Fastabend return skb->len; 430377162022SJohn Fastabend } 430477162022SJohn Fastabend 43055b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 43065b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 43075b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 43085b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 43095b2f94b2SRoopa Prabhu { 43105b2f94b2SRoopa Prabhu struct ndmsg *ndm; 43115b2f94b2SRoopa Prabhu int err, i; 43125b2f94b2SRoopa Prabhu 43135b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 43145b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 43155b2f94b2SRoopa Prabhu return -EINVAL; 43165b2f94b2SRoopa Prabhu } 43175b2f94b2SRoopa Prabhu 43185b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 43195b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 43205b2f94b2SRoopa Prabhu ndm->ndm_type) { 43215b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 43225b2f94b2SRoopa Prabhu return -EINVAL; 43235b2f94b2SRoopa Prabhu } 43245b2f94b2SRoopa Prabhu 43255b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 43265b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 43275b2f94b2SRoopa Prabhu return -EINVAL; 43285b2f94b2SRoopa Prabhu } 43295b2f94b2SRoopa Prabhu 43308cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 43318cb08174SJohannes Berg NDA_MAX, nda_policy, extack); 43325b2f94b2SRoopa Prabhu if (err < 0) 43335b2f94b2SRoopa Prabhu return err; 43345b2f94b2SRoopa Prabhu 43355b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 43365b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 43375b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 43385b2f94b2SRoopa Prabhu if (!tb[i]) 43395b2f94b2SRoopa Prabhu continue; 43405b2f94b2SRoopa Prabhu 43415b2f94b2SRoopa Prabhu switch (i) { 43425b2f94b2SRoopa Prabhu case NDA_MASTER: 43435b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 43445b2f94b2SRoopa Prabhu break; 43455b2f94b2SRoopa Prabhu case NDA_LLADDR: 43465b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 43475b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 43485b2f94b2SRoopa Prabhu return -EINVAL; 43495b2f94b2SRoopa Prabhu } 43505b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 43515b2f94b2SRoopa Prabhu break; 43525b2f94b2SRoopa Prabhu case NDA_VLAN: 43535b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 43545b2f94b2SRoopa Prabhu if (err) 43555b2f94b2SRoopa Prabhu return err; 43565b2f94b2SRoopa Prabhu break; 43575b2f94b2SRoopa Prabhu case NDA_VNI: 43585b2f94b2SRoopa Prabhu break; 43595b2f94b2SRoopa Prabhu default: 43605b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 43615b2f94b2SRoopa Prabhu return -EINVAL; 43625b2f94b2SRoopa Prabhu } 43635b2f94b2SRoopa Prabhu } 43645b2f94b2SRoopa Prabhu 43655b2f94b2SRoopa Prabhu return 0; 43665b2f94b2SRoopa Prabhu } 43675b2f94b2SRoopa Prabhu 43685b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 43695b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 43705b2f94b2SRoopa Prabhu { 43715b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 43725b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 43735b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 43745b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 43755b2f94b2SRoopa Prabhu struct sk_buff *skb; 43765b2f94b2SRoopa Prabhu int brport_idx = 0; 43775b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 43785b2f94b2SRoopa Prabhu int br_idx = 0; 43795b2f94b2SRoopa Prabhu u8 *addr = NULL; 43805b2f94b2SRoopa Prabhu u16 vid = 0; 43815b2f94b2SRoopa Prabhu int err; 43825b2f94b2SRoopa Prabhu 43835b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 43845b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 43855b2f94b2SRoopa Prabhu if (err < 0) 43865b2f94b2SRoopa Prabhu return err; 43875b2f94b2SRoopa Prabhu 4388f989d03eSNikolay Aleksandrov if (!addr) { 4389f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4390f989d03eSNikolay Aleksandrov return -EINVAL; 4391f989d03eSNikolay Aleksandrov } 4392f989d03eSNikolay Aleksandrov 43935b2f94b2SRoopa Prabhu if (brport_idx) { 43945b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 43955b2f94b2SRoopa Prabhu if (!dev) { 43965b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 43975b2f94b2SRoopa Prabhu return -ENODEV; 43985b2f94b2SRoopa Prabhu } 43995b2f94b2SRoopa Prabhu } 44005b2f94b2SRoopa Prabhu 44015b2f94b2SRoopa Prabhu if (br_idx) { 44025b2f94b2SRoopa Prabhu if (dev) { 44035b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 44045b2f94b2SRoopa Prabhu return -EINVAL; 44055b2f94b2SRoopa Prabhu } 44065b2f94b2SRoopa Prabhu 44075b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 44085b2f94b2SRoopa Prabhu if (!br_dev) { 44095b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 44105b2f94b2SRoopa Prabhu return -EINVAL; 44115b2f94b2SRoopa Prabhu } 44125b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 44135b2f94b2SRoopa Prabhu } 44145b2f94b2SRoopa Prabhu 44155b2f94b2SRoopa Prabhu if (dev) { 44165b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 44175b2f94b2SRoopa Prabhu if (!(dev->priv_flags & IFF_BRIDGE_PORT)) { 44185b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 44195b2f94b2SRoopa Prabhu return -EINVAL; 44205b2f94b2SRoopa Prabhu } 44215b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 44225b2f94b2SRoopa Prabhu if (!br_dev) { 44235b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 44245b2f94b2SRoopa Prabhu return -EINVAL; 44255b2f94b2SRoopa Prabhu } 44265b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 44275b2f94b2SRoopa Prabhu } else { 44285b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 44295b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 44305b2f94b2SRoopa Prabhu return -EINVAL; 44315b2f94b2SRoopa Prabhu } 44325b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 44335b2f94b2SRoopa Prabhu } 44345b2f94b2SRoopa Prabhu } 44355b2f94b2SRoopa Prabhu 44365b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 44375b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 44385b2f94b2SRoopa Prabhu return -ENODEV; 44395b2f94b2SRoopa Prabhu } 44405b2f94b2SRoopa Prabhu 44415b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 44425b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 44435b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 44445b2f94b2SRoopa Prabhu } 44455b2f94b2SRoopa Prabhu 44465b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 44475b2f94b2SRoopa Prabhu if (!skb) 44485b2f94b2SRoopa Prabhu return -ENOBUFS; 44495b2f94b2SRoopa Prabhu 44505b2f94b2SRoopa Prabhu if (br_dev) 44515b2f94b2SRoopa Prabhu dev = br_dev; 44525b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 44535b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 44545b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 44555b2f94b2SRoopa Prabhu if (err) 44565b2f94b2SRoopa Prabhu goto out; 44575b2f94b2SRoopa Prabhu 44585b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 44595b2f94b2SRoopa Prabhu out: 44605b2f94b2SRoopa Prabhu kfree_skb(skb); 44615b2f94b2SRoopa Prabhu return err; 44625b2f94b2SRoopa Prabhu } 44635b2f94b2SRoopa Prabhu 44642c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 44652c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 44662c3c031cSScott Feldman { 44672c3c031cSScott Feldman if (mask & flag) 44682c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 44692c3c031cSScott Feldman return 0; 44702c3c031cSScott Feldman } 44712c3c031cSScott Feldman 4472815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 44732c3c031cSScott Feldman struct net_device *dev, u16 mode, 44747d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 44757d4f8d87SScott Feldman u32 filter_mask, 44767d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 44777d4f8d87SScott Feldman struct net_device *dev, 44787d4f8d87SScott Feldman u32 filter_mask)) 4479815cccbfSJohn Fastabend { 4480815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4481815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4482815cccbfSJohn Fastabend struct nlattr *br_afspec; 44832c3c031cSScott Feldman struct nlattr *protinfo; 4484815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4485898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 44867d4f8d87SScott Feldman int err = 0; 4487815cccbfSJohn Fastabend 448846c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4489815cccbfSJohn Fastabend if (nlh == NULL) 4490815cccbfSJohn Fastabend return -EMSGSIZE; 4491815cccbfSJohn Fastabend 4492815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4493815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4494815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4495815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4496815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4497815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4498815cccbfSJohn Fastabend ifm->ifi_change = 0; 4499815cccbfSJohn Fastabend 4500815cccbfSJohn Fastabend 4501815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4502815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4503815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4504898e5061SJiri Pirko (br_dev && 4505898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4506815cccbfSJohn Fastabend (dev->addr_len && 4507815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4508a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4509a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4510815cccbfSJohn Fastabend goto nla_put_failure; 4511815cccbfSJohn Fastabend 4512ae0be8deSMichal Kubecek br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4513815cccbfSJohn Fastabend if (!br_afspec) 4514815cccbfSJohn Fastabend goto nla_put_failure; 4515815cccbfSJohn Fastabend 45161d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4517815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4518815cccbfSJohn Fastabend goto nla_put_failure; 4519815cccbfSJohn Fastabend } 45201d460b98SRoopa Prabhu 45211d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 45221d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 45231d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 45241d460b98SRoopa Prabhu goto nla_put_failure; 45251d460b98SRoopa Prabhu } 45261d460b98SRoopa Prabhu } 45277d4f8d87SScott Feldman if (vlan_fill) { 45287d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 45297d4f8d87SScott Feldman if (err) { 45307d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 45317d4f8d87SScott Feldman goto nla_put_failure; 45327d4f8d87SScott Feldman } 45337d4f8d87SScott Feldman } 4534815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4535815cccbfSJohn Fastabend 4536ae0be8deSMichal Kubecek protinfo = nla_nest_start(skb, IFLA_PROTINFO); 45372c3c031cSScott Feldman if (!protinfo) 45382c3c031cSScott Feldman goto nla_put_failure; 45392c3c031cSScott Feldman 45402c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 45412c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 45422c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45432c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 45442c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45452c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 45462c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 45472c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45482c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 45492c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45502c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 45512c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45522c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 45532c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45542c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 45552c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 45562c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 45572c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 45582c3c031cSScott Feldman goto nla_put_failure; 45592c3c031cSScott Feldman } 45602c3c031cSScott Feldman 45612c3c031cSScott Feldman nla_nest_end(skb, protinfo); 45622c3c031cSScott Feldman 4563053c095aSJohannes Berg nlmsg_end(skb, nlh); 4564053c095aSJohannes Berg return 0; 4565815cccbfSJohn Fastabend nla_put_failure: 4566815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 45677d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4568815cccbfSJohn Fastabend } 45697d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4570815cccbfSJohn Fastabend 45712d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 45722d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 45732d011be8SDavid Ahern struct netlink_ext_ack *extack) 45742d011be8SDavid Ahern { 45752d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 45762d011be8SDavid Ahern int err, i; 45772d011be8SDavid Ahern 45782d011be8SDavid Ahern if (strict_check) { 45792d011be8SDavid Ahern struct ifinfomsg *ifm; 45802d011be8SDavid Ahern 45812d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 45822d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 45832d011be8SDavid Ahern return -EINVAL; 45842d011be8SDavid Ahern } 45852d011be8SDavid Ahern 45862d011be8SDavid Ahern ifm = nlmsg_data(nlh); 45872d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 45882d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 45892d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 45902d011be8SDavid Ahern return -EINVAL; 45912d011be8SDavid Ahern } 45922d011be8SDavid Ahern 45938cb08174SJohannes Berg err = nlmsg_parse_deprecated_strict(nlh, 45948cb08174SJohannes Berg sizeof(struct ifinfomsg), 45958cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 45968cb08174SJohannes Berg extack); 45972d011be8SDavid Ahern } else { 45988cb08174SJohannes Berg err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 45998cb08174SJohannes Berg tb, IFLA_MAX, ifla_policy, 46008cb08174SJohannes Berg extack); 46012d011be8SDavid Ahern } 46022d011be8SDavid Ahern if (err < 0) 46032d011be8SDavid Ahern return err; 46042d011be8SDavid Ahern 46052d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 46062d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 46072d011be8SDavid Ahern if (!tb[i]) 46082d011be8SDavid Ahern continue; 46092d011be8SDavid Ahern 46102d011be8SDavid Ahern switch (i) { 46112d011be8SDavid Ahern case IFLA_EXT_MASK: 46122d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 46132d011be8SDavid Ahern break; 46142d011be8SDavid Ahern default: 46152d011be8SDavid Ahern if (strict_check) { 46162d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 46172d011be8SDavid Ahern return -EINVAL; 46182d011be8SDavid Ahern } 46192d011be8SDavid Ahern } 46202d011be8SDavid Ahern } 46212d011be8SDavid Ahern 46222d011be8SDavid Ahern return 0; 46232d011be8SDavid Ahern } 46242d011be8SDavid Ahern 4625e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4626e5a55a89SJohn Fastabend { 46272d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4628e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4629e5a55a89SJohn Fastabend struct net_device *dev; 4630e5a55a89SJohn Fastabend int idx = 0; 4631e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 46322d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 46336cbdceebSVlad Yasevich u32 filter_mask = 0; 4634d64f69b0SRoopa Prabhu int err; 46356cbdceebSVlad Yasevich 46362d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 46372d011be8SDavid Ahern cb->extack); 46382d011be8SDavid Ahern if (err < 0 && cb->strict_check) 46392d011be8SDavid Ahern return err; 4640e5a55a89SJohn Fastabend 4641e5a55a89SJohn Fastabend rcu_read_lock(); 4642e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4643e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4644898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4645e5a55a89SJohn Fastabend 4646898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4647d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4648d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4649d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4650d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4651f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4652f6c5775fSDavid Ahern if (likely(skb->len)) 4653e5a55a89SJohn Fastabend break; 4654f6c5775fSDavid Ahern 4655f6c5775fSDavid Ahern goto out_err; 4656f6c5775fSDavid Ahern } 4657d64f69b0SRoopa Prabhu } 4658e5a55a89SJohn Fastabend idx++; 4659e5a55a89SJohn Fastabend } 4660e5a55a89SJohn Fastabend 4661e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4662d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4663d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4664d64f69b0SRoopa Prabhu seq, dev, 466546c264daSNicolas Dichtel filter_mask, 4666d64f69b0SRoopa Prabhu NLM_F_MULTI); 4667f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4668f6c5775fSDavid Ahern if (likely(skb->len)) 4669e5a55a89SJohn Fastabend break; 4670f6c5775fSDavid Ahern 4671f6c5775fSDavid Ahern goto out_err; 4672f6c5775fSDavid Ahern } 4673d64f69b0SRoopa Prabhu } 4674e5a55a89SJohn Fastabend idx++; 4675e5a55a89SJohn Fastabend } 4676e5a55a89SJohn Fastabend } 4677f6c5775fSDavid Ahern err = skb->len; 4678f6c5775fSDavid Ahern out_err: 4679e5a55a89SJohn Fastabend rcu_read_unlock(); 4680e5a55a89SJohn Fastabend cb->args[0] = idx; 4681e5a55a89SJohn Fastabend 4682f6c5775fSDavid Ahern return err; 4683e5a55a89SJohn Fastabend } 4684e5a55a89SJohn Fastabend 46852469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 46862469ffd7SJohn Fastabend { 46872469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 46882469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 46892469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 46902469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 46912469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 46922469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 46932469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 46942469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 46952469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 46962469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 46972469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 46982469ffd7SJohn Fastabend } 46992469ffd7SJohn Fastabend 470002dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 47012469ffd7SJohn Fastabend { 47022469ffd7SJohn Fastabend struct net *net = dev_net(dev); 47032469ffd7SJohn Fastabend struct sk_buff *skb; 47042469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 47052469ffd7SJohn Fastabend 470602dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 470702dba438SRoopa Prabhu return 0; 470802dba438SRoopa Prabhu 47092469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 47102469ffd7SJohn Fastabend if (!skb) { 47112469ffd7SJohn Fastabend err = -ENOMEM; 47122469ffd7SJohn Fastabend goto errout; 47132469ffd7SJohn Fastabend } 47142469ffd7SJohn Fastabend 471546c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4716c38e01b8SJohn Fastabend if (err < 0) 4717c38e01b8SJohn Fastabend goto errout; 47182469ffd7SJohn Fastabend 471959ccaaaaSRoopa Prabhu if (!skb->len) 472059ccaaaaSRoopa Prabhu goto errout; 472159ccaaaaSRoopa Prabhu 47222469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 47232469ffd7SJohn Fastabend return 0; 47242469ffd7SJohn Fastabend errout: 47252469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 47262469ffd7SJohn Fastabend kfree_skb(skb); 472759ccaaaaSRoopa Prabhu if (err) 47282469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 47292469ffd7SJohn Fastabend return err; 47302469ffd7SJohn Fastabend } 47312469ffd7SJohn Fastabend 4732c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4733c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4734e5a55a89SJohn Fastabend { 4735e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4736e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4737e5a55a89SJohn Fastabend struct net_device *dev; 47382469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 47392469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 47404de8b413SRosen, Rami u16 flags = 0; 4741c38e01b8SJohn Fastabend bool have_flags = false; 4742e5a55a89SJohn Fastabend 4743e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4744e5a55a89SJohn Fastabend return -EINVAL; 4745e5a55a89SJohn Fastabend 4746e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4747e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4748e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4749e5a55a89SJohn Fastabend 4750e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4751e5a55a89SJohn Fastabend if (!dev) { 4752b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4753e5a55a89SJohn Fastabend return -ENODEV; 4754e5a55a89SJohn Fastabend } 4755e5a55a89SJohn Fastabend 47562469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 47572469ffd7SJohn Fastabend if (br_spec) { 47582469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 47592469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 47606e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 47616e8d1c55SThomas Graf return -EINVAL; 47626e8d1c55SThomas Graf 4763c38e01b8SJohn Fastabend have_flags = true; 47642469ffd7SJohn Fastabend flags = nla_get_u16(attr); 47652469ffd7SJohn Fastabend break; 47662469ffd7SJohn Fastabend } 47672469ffd7SJohn Fastabend } 47682469ffd7SJohn Fastabend } 47692469ffd7SJohn Fastabend 47702469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4771898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4772898e5061SJiri Pirko 4773898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 47742469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4775e5a55a89SJohn Fastabend goto out; 4776e5a55a89SJohn Fastabend } 4777e5a55a89SJohn Fastabend 47782fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 47792fd527b7SPetr Machata extack); 47802469ffd7SJohn Fastabend if (err) 47812469ffd7SJohn Fastabend goto out; 47822469ffd7SJohn Fastabend 47832469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 47842469ffd7SJohn Fastabend } 47852469ffd7SJohn Fastabend 47862469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 47872469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 47882469ffd7SJohn Fastabend err = -EOPNOTSUPP; 47892469ffd7SJohn Fastabend else 4790add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 47912fd527b7SPetr Machata flags, 47922fd527b7SPetr Machata extack); 479302dba438SRoopa Prabhu if (!err) { 47942469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 479502dba438SRoopa Prabhu 479602dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 479702dba438SRoopa Prabhu * change 479802dba438SRoopa Prabhu */ 479902dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 480002dba438SRoopa Prabhu } 48012469ffd7SJohn Fastabend } 48022469ffd7SJohn Fastabend 4803c38e01b8SJohn Fastabend if (have_flags) 48042469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4805e5a55a89SJohn Fastabend out: 4806e5a55a89SJohn Fastabend return err; 4807e5a55a89SJohn Fastabend } 4808e5a55a89SJohn Fastabend 4809c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4810c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4811407af329SVlad Yasevich { 4812407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4813407af329SVlad Yasevich struct ifinfomsg *ifm; 4814407af329SVlad Yasevich struct net_device *dev; 4815407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4816407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 48174de8b413SRosen, Rami u16 flags = 0; 4818407af329SVlad Yasevich bool have_flags = false; 4819407af329SVlad Yasevich 4820407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4821407af329SVlad Yasevich return -EINVAL; 4822407af329SVlad Yasevich 4823407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4824407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4825407af329SVlad Yasevich return -EPFNOSUPPORT; 4826407af329SVlad Yasevich 4827407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4828407af329SVlad Yasevich if (!dev) { 4829b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4830407af329SVlad Yasevich return -ENODEV; 4831407af329SVlad Yasevich } 4832407af329SVlad Yasevich 4833407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4834407af329SVlad Yasevich if (br_spec) { 4835407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4836407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 48376e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 48386e8d1c55SThomas Graf return -EINVAL; 48396e8d1c55SThomas Graf 4840407af329SVlad Yasevich have_flags = true; 4841407af329SVlad Yasevich flags = nla_get_u16(attr); 4842407af329SVlad Yasevich break; 4843407af329SVlad Yasevich } 4844407af329SVlad Yasevich } 4845407af329SVlad Yasevich } 4846407af329SVlad Yasevich 4847407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4848407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4849407af329SVlad Yasevich 4850407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4851407af329SVlad Yasevich err = -EOPNOTSUPP; 4852407af329SVlad Yasevich goto out; 4853407af329SVlad Yasevich } 4854407af329SVlad Yasevich 4855add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4856407af329SVlad Yasevich if (err) 4857407af329SVlad Yasevich goto out; 4858407af329SVlad Yasevich 4859407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4860407af329SVlad Yasevich } 4861407af329SVlad Yasevich 4862407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4863407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4864407af329SVlad Yasevich err = -EOPNOTSUPP; 4865407af329SVlad Yasevich else 4866add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4867add511b3SRoopa Prabhu flags); 4868407af329SVlad Yasevich 486902dba438SRoopa Prabhu if (!err) { 4870407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 487102dba438SRoopa Prabhu 487202dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 487302dba438SRoopa Prabhu * change 487402dba438SRoopa Prabhu */ 487502dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 487602dba438SRoopa Prabhu } 4877407af329SVlad Yasevich } 4878407af329SVlad Yasevich 4879407af329SVlad Yasevich if (have_flags) 4880407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4881407af329SVlad Yasevich out: 4882407af329SVlad Yasevich return err; 4883407af329SVlad Yasevich } 4884407af329SVlad Yasevich 4885e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4886e8872a25SNikolay Aleksandrov { 4887e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4888e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4889e8872a25SNikolay Aleksandrov } 4890e8872a25SNikolay Aleksandrov 489169ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 489269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 489369ae6ad2SNogah Frankel { 489469ae6ad2SNogah Frankel switch (attr_id) { 489569ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 489669ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 489769ae6ad2SNogah Frankel } 489869ae6ad2SNogah Frankel 489969ae6ad2SNogah Frankel return 0; 490069ae6ad2SNogah Frankel } 490169ae6ad2SNogah Frankel 490269ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 490369ae6ad2SNogah Frankel int *prividx) 490469ae6ad2SNogah Frankel { 490569ae6ad2SNogah Frankel struct nlattr *attr = NULL; 490669ae6ad2SNogah Frankel int attr_id, size; 490769ae6ad2SNogah Frankel void *attr_data; 490869ae6ad2SNogah Frankel int err; 490969ae6ad2SNogah Frankel 491069ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 491169ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 491269ae6ad2SNogah Frankel return -ENODATA; 491369ae6ad2SNogah Frankel 491469ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 491569ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 491669ae6ad2SNogah Frankel if (attr_id < *prividx) 491769ae6ad2SNogah Frankel continue; 491869ae6ad2SNogah Frankel 491969ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 492069ae6ad2SNogah Frankel if (!size) 492169ae6ad2SNogah Frankel continue; 492269ae6ad2SNogah Frankel 49233df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 492469ae6ad2SNogah Frankel continue; 492569ae6ad2SNogah Frankel 492669ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 492769ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 492869ae6ad2SNogah Frankel if (!attr) 492969ae6ad2SNogah Frankel goto nla_put_failure; 493069ae6ad2SNogah Frankel 493169ae6ad2SNogah Frankel attr_data = nla_data(attr); 493269ae6ad2SNogah Frankel memset(attr_data, 0, size); 493369ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 493469ae6ad2SNogah Frankel attr_data); 493569ae6ad2SNogah Frankel if (err) 493669ae6ad2SNogah Frankel goto get_offload_stats_failure; 493769ae6ad2SNogah Frankel } 493869ae6ad2SNogah Frankel 493969ae6ad2SNogah Frankel if (!attr) 494069ae6ad2SNogah Frankel return -ENODATA; 494169ae6ad2SNogah Frankel 494269ae6ad2SNogah Frankel *prividx = 0; 494369ae6ad2SNogah Frankel return 0; 494469ae6ad2SNogah Frankel 494569ae6ad2SNogah Frankel nla_put_failure: 494669ae6ad2SNogah Frankel err = -EMSGSIZE; 494769ae6ad2SNogah Frankel get_offload_stats_failure: 494869ae6ad2SNogah Frankel *prividx = attr_id; 494969ae6ad2SNogah Frankel return err; 495069ae6ad2SNogah Frankel } 495169ae6ad2SNogah Frankel 495269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 495369ae6ad2SNogah Frankel { 495469ae6ad2SNogah Frankel int nla_size = 0; 495569ae6ad2SNogah Frankel int attr_id; 495669ae6ad2SNogah Frankel int size; 495769ae6ad2SNogah Frankel 495869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 495969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 496069ae6ad2SNogah Frankel return 0; 496169ae6ad2SNogah Frankel 496269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 496369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 49643df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 496569ae6ad2SNogah Frankel continue; 496669ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 496769ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 496869ae6ad2SNogah Frankel } 496969ae6ad2SNogah Frankel 497069ae6ad2SNogah Frankel if (nla_size != 0) 497169ae6ad2SNogah Frankel nla_size += nla_total_size(0); 497269ae6ad2SNogah Frankel 497369ae6ad2SNogah Frankel return nla_size; 497469ae6ad2SNogah Frankel } 497569ae6ad2SNogah Frankel 497610c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 497710c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4978e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4979e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 498010c9ead9SRoopa Prabhu { 498110c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 498210c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 498310c9ead9SRoopa Prabhu struct nlattr *attr; 4984e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 498569ae6ad2SNogah Frankel int err; 498610c9ead9SRoopa Prabhu 498710c9ead9SRoopa Prabhu ASSERT_RTNL(); 498810c9ead9SRoopa Prabhu 498910c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 499010c9ead9SRoopa Prabhu if (!nlh) 499110c9ead9SRoopa Prabhu return -EMSGSIZE; 499210c9ead9SRoopa Prabhu 499310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4994ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4995ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4996ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 499710c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 499810c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 499910c9ead9SRoopa Prabhu 5000e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 500110c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 500210c9ead9SRoopa Prabhu 500358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 500458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 500558414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 500610c9ead9SRoopa Prabhu if (!attr) 500710c9ead9SRoopa Prabhu goto nla_put_failure; 500810c9ead9SRoopa Prabhu 500910c9ead9SRoopa Prabhu sp = nla_data(attr); 501010c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 501110c9ead9SRoopa Prabhu } 501210c9ead9SRoopa Prabhu 501397a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 501497a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 501597a47facSNikolay Aleksandrov 501697a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 501797a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 5018ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 501997a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 502097a47facSNikolay Aleksandrov if (!attr) 502197a47facSNikolay Aleksandrov goto nla_put_failure; 502297a47facSNikolay Aleksandrov 502380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 502480e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 502580e73cc5SNikolay Aleksandrov if (err) 502680e73cc5SNikolay Aleksandrov goto nla_put_failure; 502780e73cc5SNikolay Aleksandrov *idxattr = 0; 502880e73cc5SNikolay Aleksandrov } 502980e73cc5SNikolay Aleksandrov } 503080e73cc5SNikolay Aleksandrov 503180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 503280e73cc5SNikolay Aleksandrov *idxattr)) { 503380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 503480e73cc5SNikolay Aleksandrov const struct net_device *master; 503580e73cc5SNikolay Aleksandrov 503680e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 503780e73cc5SNikolay Aleksandrov if (master) 503880e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 503980e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 504080e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5041ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 504280e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 504380e73cc5SNikolay Aleksandrov if (!attr) 504480e73cc5SNikolay Aleksandrov goto nla_put_failure; 504580e73cc5SNikolay Aleksandrov 504680e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 504797a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 504897a47facSNikolay Aleksandrov if (err) 504997a47facSNikolay Aleksandrov goto nla_put_failure; 505097a47facSNikolay Aleksandrov *idxattr = 0; 505197a47facSNikolay Aleksandrov } 505297a47facSNikolay Aleksandrov } 505397a47facSNikolay Aleksandrov 505469ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 505569ae6ad2SNogah Frankel *idxattr)) { 505669ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5057ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, 5058ae0be8deSMichal Kubecek IFLA_STATS_LINK_OFFLOAD_XSTATS); 505969ae6ad2SNogah Frankel if (!attr) 506069ae6ad2SNogah Frankel goto nla_put_failure; 506169ae6ad2SNogah Frankel 506269ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 506369ae6ad2SNogah Frankel if (err == -ENODATA) 506469ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 506569ae6ad2SNogah Frankel else 506669ae6ad2SNogah Frankel nla_nest_end(skb, attr); 506769ae6ad2SNogah Frankel 506869ae6ad2SNogah Frankel if (err && err != -ENODATA) 506969ae6ad2SNogah Frankel goto nla_put_failure; 507069ae6ad2SNogah Frankel *idxattr = 0; 507169ae6ad2SNogah Frankel } 507269ae6ad2SNogah Frankel 5073aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5074aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5075aefb4d4aSRobert Shearman 5076aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 5077ae0be8deSMichal Kubecek attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5078aefb4d4aSRobert Shearman if (!attr) 5079aefb4d4aSRobert Shearman goto nla_put_failure; 5080aefb4d4aSRobert Shearman 50815fa85a09SFlorian Westphal rcu_read_lock(); 50825fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5083aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 5084aefb4d4aSRobert Shearman struct nlattr *af; 5085aefb4d4aSRobert Shearman int err; 5086aefb4d4aSRobert Shearman 5087ae0be8deSMichal Kubecek af = nla_nest_start_noflag(skb, 5088ae0be8deSMichal Kubecek af_ops->family); 50895fa85a09SFlorian Westphal if (!af) { 50905fa85a09SFlorian Westphal rcu_read_unlock(); 5091aefb4d4aSRobert Shearman goto nla_put_failure; 50925fa85a09SFlorian Westphal } 5093aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 5094aefb4d4aSRobert Shearman 50955fa85a09SFlorian Westphal if (err == -ENODATA) { 5096aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 50975fa85a09SFlorian Westphal } else if (err < 0) { 50985fa85a09SFlorian Westphal rcu_read_unlock(); 5099aefb4d4aSRobert Shearman goto nla_put_failure; 51005fa85a09SFlorian Westphal } 5101aefb4d4aSRobert Shearman 5102aefb4d4aSRobert Shearman nla_nest_end(skb, af); 5103aefb4d4aSRobert Shearman } 5104aefb4d4aSRobert Shearman } 51055fa85a09SFlorian Westphal rcu_read_unlock(); 5106aefb4d4aSRobert Shearman 5107aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 5108aefb4d4aSRobert Shearman 5109aefb4d4aSRobert Shearman *idxattr = 0; 5110aefb4d4aSRobert Shearman } 5111aefb4d4aSRobert Shearman 511210c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 511310c9ead9SRoopa Prabhu 511410c9ead9SRoopa Prabhu return 0; 511510c9ead9SRoopa Prabhu 511610c9ead9SRoopa Prabhu nla_put_failure: 5117e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 5118e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 511910c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 5120e8872a25SNikolay Aleksandrov else 5121e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 512210c9ead9SRoopa Prabhu 512310c9ead9SRoopa Prabhu return -EMSGSIZE; 512410c9ead9SRoopa Prabhu } 512510c9ead9SRoopa Prabhu 512610c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 512710c9ead9SRoopa Prabhu u32 filter_mask) 512810c9ead9SRoopa Prabhu { 512910c9ead9SRoopa Prabhu size_t size = 0; 513010c9ead9SRoopa Prabhu 5131e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 513210c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 513310c9ead9SRoopa Prabhu 513497a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 513597a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 513680e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 513797a47facSNikolay Aleksandrov 513897a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 513980e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 514080e73cc5SNikolay Aleksandrov attr)); 514197a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 514297a47facSNikolay Aleksandrov size += nla_total_size(0); 514397a47facSNikolay Aleksandrov } 514497a47facSNikolay Aleksandrov } 514597a47facSNikolay Aleksandrov 514680e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 514780e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 514880e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 514980e73cc5SNikolay Aleksandrov const struct net_device *master; 515080e73cc5SNikolay Aleksandrov 515180e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 515280e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 515380e73cc5SNikolay Aleksandrov if (master) 515480e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 515580e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 515680e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 515780e73cc5SNikolay Aleksandrov 515880e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 515980e73cc5SNikolay Aleksandrov attr)); 516080e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 516180e73cc5SNikolay Aleksandrov size += nla_total_size(0); 516280e73cc5SNikolay Aleksandrov } 516380e73cc5SNikolay Aleksandrov } 516480e73cc5SNikolay Aleksandrov 516569ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 516669ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 516769ae6ad2SNogah Frankel 5168aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5169aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 5170aefb4d4aSRobert Shearman 5171aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 5172aefb4d4aSRobert Shearman size += nla_total_size(0); 5173aefb4d4aSRobert Shearman 51745fa85a09SFlorian Westphal rcu_read_lock(); 51755fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5176aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 5177aefb4d4aSRobert Shearman size += nla_total_size( 5178aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 5179aefb4d4aSRobert Shearman 5180aefb4d4aSRobert Shearman /* for AF_* */ 5181aefb4d4aSRobert Shearman size += nla_total_size(0); 5182aefb4d4aSRobert Shearman } 5183aefb4d4aSRobert Shearman } 51845fa85a09SFlorian Westphal rcu_read_unlock(); 5185aefb4d4aSRobert Shearman } 5186aefb4d4aSRobert Shearman 518710c9ead9SRoopa Prabhu return size; 518810c9ead9SRoopa Prabhu } 518910c9ead9SRoopa Prabhu 519051bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 519151bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 519251bc860dSJakub Kicinski { 519351bc860dSJakub Kicinski struct if_stats_msg *ifsm; 519451bc860dSJakub Kicinski 519569f23a09SEric Dumazet if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 519651bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 519751bc860dSJakub Kicinski return -EINVAL; 519851bc860dSJakub Kicinski } 519951bc860dSJakub Kicinski 520051bc860dSJakub Kicinski if (!strict_check) 520151bc860dSJakub Kicinski return 0; 520251bc860dSJakub Kicinski 520351bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 520451bc860dSJakub Kicinski 520551bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 520651bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 520751bc860dSJakub Kicinski */ 520851bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 520951bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 521051bc860dSJakub Kicinski return -EINVAL; 521151bc860dSJakub Kicinski } 521251bc860dSJakub Kicinski if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 521351bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 521451bc860dSJakub Kicinski return -EINVAL; 521551bc860dSJakub Kicinski } 52166300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 52176300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 52186300acb2SJakub Kicinski return -EINVAL; 52196300acb2SJakub Kicinski } 522051bc860dSJakub Kicinski 522151bc860dSJakub Kicinski return 0; 522251bc860dSJakub Kicinski } 522351bc860dSJakub Kicinski 5224c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5225c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 522610c9ead9SRoopa Prabhu { 522710c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 522810c9ead9SRoopa Prabhu struct net_device *dev = NULL; 5229e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 5230e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 523110c9ead9SRoopa Prabhu struct sk_buff *nskb; 523210c9ead9SRoopa Prabhu u32 filter_mask; 523310c9ead9SRoopa Prabhu int err; 523410c9ead9SRoopa Prabhu 523551bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 523651bc860dSJakub Kicinski false, extack); 523751bc860dSJakub Kicinski if (err) 523851bc860dSJakub Kicinski return err; 52394775cc1fSMathias Krause 524010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 524110c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 524210c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 524310c9ead9SRoopa Prabhu else 524410c9ead9SRoopa Prabhu return -EINVAL; 524510c9ead9SRoopa Prabhu 524610c9ead9SRoopa Prabhu if (!dev) 524710c9ead9SRoopa Prabhu return -ENODEV; 524810c9ead9SRoopa Prabhu 524910c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 525010c9ead9SRoopa Prabhu if (!filter_mask) 525110c9ead9SRoopa Prabhu return -EINVAL; 525210c9ead9SRoopa Prabhu 525310c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 525410c9ead9SRoopa Prabhu if (!nskb) 525510c9ead9SRoopa Prabhu return -ENOBUFS; 525610c9ead9SRoopa Prabhu 525710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 525810c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5259e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 526010c9ead9SRoopa Prabhu if (err < 0) { 526110c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 526210c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 526310c9ead9SRoopa Prabhu kfree_skb(nskb); 526410c9ead9SRoopa Prabhu } else { 526510c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 526610c9ead9SRoopa Prabhu } 526710c9ead9SRoopa Prabhu 526810c9ead9SRoopa Prabhu return err; 526910c9ead9SRoopa Prabhu } 527010c9ead9SRoopa Prabhu 527110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 527210c9ead9SRoopa Prabhu { 5273841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 5274e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 527510c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 527610c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 5277e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 5278e8872a25SNikolay Aleksandrov struct hlist_head *head; 5279e8872a25SNikolay Aleksandrov struct net_device *dev; 528010c9ead9SRoopa Prabhu u32 filter_mask = 0; 5281e8872a25SNikolay Aleksandrov int idx = 0; 528210c9ead9SRoopa Prabhu 528310c9ead9SRoopa Prabhu s_h = cb->args[0]; 528410c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5285e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5286e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 528710c9ead9SRoopa Prabhu 528810c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 528910c9ead9SRoopa Prabhu 529051bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 529151bc860dSJakub Kicinski if (err) 529251bc860dSJakub Kicinski return err; 52934775cc1fSMathias Krause 529410c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 5295841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 5296841891ecSDavid Ahern if (!filter_mask) { 5297841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5298841891ecSDavid Ahern return -EINVAL; 5299841891ecSDavid Ahern } 530010c9ead9SRoopa Prabhu 530110c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 530210c9ead9SRoopa Prabhu idx = 0; 530310c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 530410c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 530510c9ead9SRoopa Prabhu if (idx < s_idx) 530610c9ead9SRoopa Prabhu goto cont; 530710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 530810c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 530910c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 5310e8872a25SNikolay Aleksandrov flags, filter_mask, 5311e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 531210c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 531310c9ead9SRoopa Prabhu * we're in trouble 531410c9ead9SRoopa Prabhu */ 531510c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 531610c9ead9SRoopa Prabhu 531710c9ead9SRoopa Prabhu if (err < 0) 531810c9ead9SRoopa Prabhu goto out; 5319e8872a25SNikolay Aleksandrov s_prividx = 0; 5320e8872a25SNikolay Aleksandrov s_idxattr = 0; 532110c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 532210c9ead9SRoopa Prabhu cont: 532310c9ead9SRoopa Prabhu idx++; 532410c9ead9SRoopa Prabhu } 532510c9ead9SRoopa Prabhu } 532610c9ead9SRoopa Prabhu out: 5327e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5328e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 532910c9ead9SRoopa Prabhu cb->args[1] = idx; 533010c9ead9SRoopa Prabhu cb->args[0] = h; 533110c9ead9SRoopa Prabhu 533210c9ead9SRoopa Prabhu return skb->len; 533310c9ead9SRoopa Prabhu } 533410c9ead9SRoopa Prabhu 53351da177e4SLinus Torvalds /* Process one rtnetlink message. */ 53361da177e4SLinus Torvalds 53372d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 53382d4bc933SJohannes Berg struct netlink_ext_ack *extack) 53391da177e4SLinus Torvalds { 53403b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5341addf9b90SFlorian Westphal struct rtnl_link *link; 5342e4202511SFlorian Westphal struct module *owner; 53436853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5344e2849863SThomas Graf rtnl_doit_func doit; 534562256f98SFlorian Westphal unsigned int flags; 5346617cfc75SAlexander Kuleshov int kind; 53471da177e4SLinus Torvalds int family; 53481da177e4SLinus Torvalds int type; 53491da177e4SLinus Torvalds 53501da177e4SLinus Torvalds type = nlh->nlmsg_type; 53511da177e4SLinus Torvalds if (type > RTM_MAX) 5352038890feSThomas Graf return -EOPNOTSUPP; 53531da177e4SLinus Torvalds 53541da177e4SLinus Torvalds type -= RTM_BASE; 53551da177e4SLinus Torvalds 53561da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5357573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 53581da177e4SLinus Torvalds return 0; 53591da177e4SLinus Torvalds 5360573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 53611da177e4SLinus Torvalds kind = type&3; 53621da177e4SLinus Torvalds 536390f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 53641d00a4ebSThomas Graf return -EPERM; 53651da177e4SLinus Torvalds 53666853dd48SFlorian Westphal rcu_read_lock(); 5367b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 536897c53cacSDenis V. Lunev struct sock *rtnl; 5369e2849863SThomas Graf rtnl_dumpit_func dumpit; 5370c7ac8679SGreg Rose u16 min_dump_alloc = 0; 53711da177e4SLinus Torvalds 5372addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5373addf9b90SFlorian Westphal if (!link || !link->dumpit) { 53746853dd48SFlorian Westphal family = PF_UNSPEC; 5375addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5376addf9b90SFlorian Westphal if (!link || !link->dumpit) 53776853dd48SFlorian Westphal goto err_unlock; 53786853dd48SFlorian Westphal } 5379e4202511SFlorian Westphal owner = link->owner; 5380addf9b90SFlorian Westphal dumpit = link->dumpit; 53816853dd48SFlorian Westphal 53825c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5383e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 53841da177e4SLinus Torvalds 5385e4202511SFlorian Westphal err = 0; 5386e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5387e4202511SFlorian Westphal if (!try_module_get(owner)) 5388e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5389e4202511SFlorian Westphal 53906853dd48SFlorian Westphal rcu_read_unlock(); 53916853dd48SFlorian Westphal 539297c53cacSDenis V. Lunev rtnl = net->rtnl; 5393e4202511SFlorian Westphal if (err == 0) { 539480d326faSPablo Neira Ayuso struct netlink_dump_control c = { 539580d326faSPablo Neira Ayuso .dump = dumpit, 539680d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5397e4202511SFlorian Westphal .module = owner, 539880d326faSPablo Neira Ayuso }; 539980d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5400e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5401e4202511SFlorian Westphal * module if dump is still in progress. 5402e4202511SFlorian Westphal */ 5403e4202511SFlorian Westphal module_put(owner); 540480d326faSPablo Neira Ayuso } 54052907c35fSEric Dumazet return err; 54061da177e4SLinus Torvalds } 54071da177e4SLinus Torvalds 5408addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5409addf9b90SFlorian Westphal if (!link || !link->doit) { 54108caa38b5SFlorian Westphal family = PF_UNSPEC; 5411addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 5412addf9b90SFlorian Westphal if (!link || !link->doit) 5413addf9b90SFlorian Westphal goto out_unlock; 54148caa38b5SFlorian Westphal } 54158caa38b5SFlorian Westphal 5416e4202511SFlorian Westphal owner = link->owner; 5417e4202511SFlorian Westphal if (!try_module_get(owner)) { 5418e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5419e4202511SFlorian Westphal goto out_unlock; 5420e4202511SFlorian Westphal } 5421e4202511SFlorian Westphal 5422addf9b90SFlorian Westphal flags = link->flags; 542362256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5424addf9b90SFlorian Westphal doit = link->doit; 542562256f98SFlorian Westphal rcu_read_unlock(); 542662256f98SFlorian Westphal if (doit) 542762256f98SFlorian Westphal err = doit(skb, nlh, extack); 5428e4202511SFlorian Westphal module_put(owner); 542962256f98SFlorian Westphal return err; 543062256f98SFlorian Westphal } 54316853dd48SFlorian Westphal rcu_read_unlock(); 54326853dd48SFlorian Westphal 54330cc09020SFlorian Westphal rtnl_lock(); 5434addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5435addf9b90SFlorian Westphal if (link && link->doit) 5436addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 54370cc09020SFlorian Westphal rtnl_unlock(); 5438addf9b90SFlorian Westphal 5439e4202511SFlorian Westphal module_put(owner); 5440e4202511SFlorian Westphal 5441addf9b90SFlorian Westphal return err; 5442addf9b90SFlorian Westphal 5443addf9b90SFlorian Westphal out_unlock: 5444addf9b90SFlorian Westphal rcu_read_unlock(); 54450cc09020SFlorian Westphal return err; 54460cc09020SFlorian Westphal 54470cc09020SFlorian Westphal err_unlock: 54486853dd48SFlorian Westphal rcu_read_unlock(); 54490cc09020SFlorian Westphal return -EOPNOTSUPP; 54501da177e4SLinus Torvalds } 54511da177e4SLinus Torvalds 5452cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 54531da177e4SLinus Torvalds { 5454cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 54551da177e4SLinus Torvalds } 54561da177e4SLinus Torvalds 54575f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 54585f729eaaSJulien Gomes { 54595f729eaaSJulien Gomes switch (group) { 54605f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 54615f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 54625f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 54635f729eaaSJulien Gomes return -EPERM; 54645f729eaaSJulien Gomes break; 54655f729eaaSJulien Gomes } 54665f729eaaSJulien Gomes return 0; 54675f729eaaSJulien Gomes } 54685f729eaaSJulien Gomes 54691da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 54701da177e4SLinus Torvalds { 5471351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5472e9dc8653SEric W. Biederman 54731da177e4SLinus Torvalds switch (event) { 54745138e86fSVlad Yasevich case NETDEV_REBOOT: 54758a212589SXin Long case NETDEV_CHANGEMTU: 54763753654eSDavid Ahern case NETDEV_CHANGEADDR: 54775138e86fSVlad Yasevich case NETDEV_CHANGENAME: 54785138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 54795138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 5480e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 54815138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 5482dc709f37SXin Long case NETDEV_CHANGEUPPER: 54835138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 54845138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 5485eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 5486ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 54873d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 548838e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 54891da177e4SLinus Torvalds break; 54901da177e4SLinus Torvalds default: 54911da177e4SLinus Torvalds break; 54921da177e4SLinus Torvalds } 54931da177e4SLinus Torvalds return NOTIFY_DONE; 54941da177e4SLinus Torvalds } 54951da177e4SLinus Torvalds 54961da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 54971da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 54981da177e4SLinus Torvalds }; 54991da177e4SLinus Torvalds 550097c53cacSDenis V. Lunev 55012c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 550297c53cacSDenis V. Lunev { 550397c53cacSDenis V. Lunev struct sock *sk; 5504a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 5505a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 5506a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 5507a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 55089785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 55095f729eaaSJulien Gomes .bind = rtnetlink_bind, 5510a31f2d17SPablo Neira Ayuso }; 5511a31f2d17SPablo Neira Ayuso 55129f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 551397c53cacSDenis V. Lunev if (!sk) 551497c53cacSDenis V. Lunev return -ENOMEM; 551597c53cacSDenis V. Lunev net->rtnl = sk; 551697c53cacSDenis V. Lunev return 0; 551797c53cacSDenis V. Lunev } 551897c53cacSDenis V. Lunev 55192c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 552097c53cacSDenis V. Lunev { 5521b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 552297c53cacSDenis V. Lunev net->rtnl = NULL; 552397c53cacSDenis V. Lunev } 552497c53cacSDenis V. Lunev 552597c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 552697c53cacSDenis V. Lunev .init = rtnetlink_net_init, 552797c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 552897c53cacSDenis V. Lunev }; 552997c53cacSDenis V. Lunev 55301da177e4SLinus Torvalds void __init rtnetlink_init(void) 55311da177e4SLinus Torvalds { 553297c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 55331da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 553497c53cacSDenis V. Lunev 55351da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 5536340d17fcSThomas Graf 5537c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5538b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 5539b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5540b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5541b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5542687ad8ccSThomas Graf 5543b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5544b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5545b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 554677162022SJohn Fastabend 554736fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 554836fbf1e5SJiri Pirko rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 554936fbf1e5SJiri Pirko 5550b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5551b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 55525b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5553e5a55a89SJohn Fastabend 5554b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5555b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5556b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 555710c9ead9SRoopa Prabhu 555810c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5559b97bac64SFlorian Westphal 0); 55601da177e4SLinus Torvalds } 5561