11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Routing netlink socket interface: protocol independent part. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Fixes: 161da177e4SLinus Torvalds * Vitaly E. Lavrov RTA_OK arithmetics was wrong. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 19ee5d032fSJakub Kicinski #include <linux/bitops.h> 201da177e4SLinus Torvalds #include <linux/errno.h> 211da177e4SLinus Torvalds #include <linux/module.h> 221da177e4SLinus Torvalds #include <linux/types.h> 231da177e4SLinus Torvalds #include <linux/socket.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 251da177e4SLinus Torvalds #include <linux/timer.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/sockios.h> 281da177e4SLinus Torvalds #include <linux/net.h> 291da177e4SLinus Torvalds #include <linux/fcntl.h> 301da177e4SLinus Torvalds #include <linux/mm.h> 311da177e4SLinus Torvalds #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/capability.h> 341da177e4SLinus Torvalds #include <linux/skbuff.h> 351da177e4SLinus Torvalds #include <linux/init.h> 361da177e4SLinus Torvalds #include <linux/security.h> 376756ae4bSStephen Hemminger #include <linux/mutex.h> 381823730fSThomas Graf #include <linux/if_addr.h> 3977162022SJohn Fastabend #include <linux/if_bridge.h> 40f6f6424bSJiri Pirko #include <linux/if_vlan.h> 41ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 4277162022SJohn Fastabend #include <linux/etherdevice.h> 4358038695SMartin KaFai Lau #include <linux/bpf.h> 441da177e4SLinus Torvalds 457c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 4982f28412SJiri Pirko #include <net/switchdev.h> 501da177e4SLinus Torvalds #include <net/ip.h> 511da177e4SLinus Torvalds #include <net/protocol.h> 521da177e4SLinus Torvalds #include <net/arp.h> 531da177e4SLinus Torvalds #include <net/route.h> 541da177e4SLinus Torvalds #include <net/udp.h> 55ea697639SDaniel Borkmann #include <net/tcp.h> 561da177e4SLinus Torvalds #include <net/sock.h> 571da177e4SLinus Torvalds #include <net/pkt_sched.h> 5814c0b97dSThomas Graf #include <net/fib_rules.h> 59e2849863SThomas Graf #include <net/rtnetlink.h> 6030ffee84SJohannes Berg #include <net/net_namespace.h> 611da177e4SLinus Torvalds 62a428afe8SNikolay Aleksandrov #define RTNL_MAX_TYPE 50 63ccf8dbcdSKees Cook #define RTNL_SLAVE_MAX_TYPE 36 64ccf8dbcdSKees Cook 65e0d087afSEric Dumazet struct rtnl_link { 66e2849863SThomas Graf rtnl_doit_func doit; 67e2849863SThomas Graf rtnl_dumpit_func dumpit; 68e4202511SFlorian Westphal struct module *owner; 6962256f98SFlorian Westphal unsigned int flags; 70addf9b90SFlorian Westphal struct rcu_head rcu; 71e2849863SThomas Graf }; 72e2849863SThomas Graf 736756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds void rtnl_lock(void) 761da177e4SLinus Torvalds { 776756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 781da177e4SLinus Torvalds } 79e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 801da177e4SLinus Torvalds 8179ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 8279ffdfc6SKirill Tkhai { 8379ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 8479ffdfc6SKirill Tkhai } 8579ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 8679ffdfc6SKirill Tkhai 871b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 881b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 891b5c5493SEric Dumazet { 901b5c5493SEric Dumazet if (head && tail) { 911b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 921b5c5493SEric Dumazet defer_kfree_skb_list = head; 931b5c5493SEric Dumazet } 941b5c5493SEric Dumazet } 951b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 961b5c5493SEric Dumazet 976756ae4bSStephen Hemminger void __rtnl_unlock(void) 981da177e4SLinus Torvalds { 991b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 1001b5c5493SEric Dumazet 1011b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 1021b5c5493SEric Dumazet 1036756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1041b5c5493SEric Dumazet 1051b5c5493SEric Dumazet while (head) { 1061b5c5493SEric Dumazet struct sk_buff *next = head->next; 1071b5c5493SEric Dumazet 1081b5c5493SEric Dumazet kfree_skb(head); 1091b5c5493SEric Dumazet cond_resched(); 1101b5c5493SEric Dumazet head = next; 1111b5c5493SEric Dumazet } 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds void rtnl_unlock(void) 1151da177e4SLinus Torvalds { 11658ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1171da177e4SLinus Torvalds netdev_run_todo(); 1181da177e4SLinus Torvalds } 119e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1201da177e4SLinus Torvalds 1216756ae4bSStephen Hemminger int rtnl_trylock(void) 1226756ae4bSStephen Hemminger { 1236756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1246756ae4bSStephen Hemminger } 125e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1266756ae4bSStephen Hemminger 127c9c1014bSPatrick McHardy int rtnl_is_locked(void) 128c9c1014bSPatrick McHardy { 129c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 130c9c1014bSPatrick McHardy } 131e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 132c9c1014bSPatrick McHardy 1336f99528eSVlad Buslov bool refcount_dec_and_rtnl_lock(refcount_t *r) 1346f99528eSVlad Buslov { 1356f99528eSVlad Buslov return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 1366f99528eSVlad Buslov } 1376f99528eSVlad Buslov EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 1386f99528eSVlad Buslov 139a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1400cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 141a898def2SPaul E. McKenney { 142a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 143a898def2SPaul E. McKenney } 144a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 145a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 146a898def2SPaul E. McKenney 147b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 148e2849863SThomas Graf 149e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 150e2849863SThomas Graf { 151e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 152e2849863SThomas Graf 153e2849863SThomas Graf /* 154e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 155e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 156e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 157e2849863SThomas Graf */ 158e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 159e2849863SThomas Graf 160e2849863SThomas Graf return msgindex; 161e2849863SThomas Graf } 162e2849863SThomas Graf 163addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 164addf9b90SFlorian Westphal { 165addf9b90SFlorian Westphal struct rtnl_link **tab; 166addf9b90SFlorian Westphal 167addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 168addf9b90SFlorian Westphal protocol = PF_UNSPEC; 169addf9b90SFlorian Westphal 170addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 171addf9b90SFlorian Westphal if (!tab) 172addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 173addf9b90SFlorian Westphal 174addf9b90SFlorian Westphal return tab[msgtype]; 175addf9b90SFlorian Westphal } 176addf9b90SFlorian Westphal 177e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 178e4202511SFlorian Westphal int protocol, int msgtype, 179c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 180b97bac64SFlorian Westphal unsigned int flags) 181e2849863SThomas Graf { 182b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 183b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 184e2849863SThomas Graf int msgindex; 185addf9b90SFlorian Westphal int ret = -ENOBUFS; 186e2849863SThomas Graf 18725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 188e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 189e2849863SThomas Graf 190addf9b90SFlorian Westphal rtnl_lock(); 191addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 192e2849863SThomas Graf if (tab == NULL) { 193addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 194addf9b90SFlorian Westphal if (!tab) 195addf9b90SFlorian Westphal goto unlock; 196e2849863SThomas Graf 197addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1986853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 199e2849863SThomas Graf } 200e2849863SThomas Graf 201addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 202addf9b90SFlorian Westphal if (old) { 203addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 204addf9b90SFlorian Westphal if (!link) 205addf9b90SFlorian Westphal goto unlock; 206addf9b90SFlorian Westphal } else { 207addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 208addf9b90SFlorian Westphal if (!link) 209addf9b90SFlorian Westphal goto unlock; 210addf9b90SFlorian Westphal } 211e2849863SThomas Graf 212e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 213e4202511SFlorian Westphal link->owner = owner; 214e4202511SFlorian Westphal 215addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 216addf9b90SFlorian Westphal if (doit) 217addf9b90SFlorian Westphal link->doit = doit; 218addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 219addf9b90SFlorian Westphal if (dumpit) 220addf9b90SFlorian Westphal link->dumpit = dumpit; 221addf9b90SFlorian Westphal 222addf9b90SFlorian Westphal link->flags |= flags; 223addf9b90SFlorian Westphal 224addf9b90SFlorian Westphal /* publish protocol:msgtype */ 225addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 226addf9b90SFlorian Westphal ret = 0; 227addf9b90SFlorian Westphal if (old) 228addf9b90SFlorian Westphal kfree_rcu(old, rcu); 229addf9b90SFlorian Westphal unlock: 230addf9b90SFlorian Westphal rtnl_unlock(); 231addf9b90SFlorian Westphal return ret; 232e2849863SThomas Graf } 233e4202511SFlorian Westphal 234e4202511SFlorian Westphal /** 235e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 236e4202511SFlorian Westphal * 237e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 238e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 239e4202511SFlorian Westphal * @msgtype: rtnetlink message type 240e4202511SFlorian Westphal * @doit: Function pointer called for each request message 241e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 242e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 243e4202511SFlorian Westphal * 244e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 245e4202511SFlorian Westphal */ 246e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 247e4202511SFlorian Westphal int protocol, int msgtype, 248e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 249e4202511SFlorian Westphal unsigned int flags) 250e4202511SFlorian Westphal { 251e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 252e4202511SFlorian Westphal doit, dumpit, flags); 253e4202511SFlorian Westphal } 254e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 255e4202511SFlorian Westphal 256e4202511SFlorian Westphal /** 25716feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 258e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 259e4202511SFlorian Westphal * @msgtype: rtnetlink message type 260e4202511SFlorian Westphal * @doit: Function pointer called for each request message 261e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 262e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 263e4202511SFlorian Westphal * 264e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 265e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 266e4202511SFlorian Westphal * specified protocol family and message type is received. 267e4202511SFlorian Westphal * 268e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 269e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 270e4202511SFlorian Westphal * family exists. 271e2849863SThomas Graf */ 272e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 273c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 274b97bac64SFlorian Westphal unsigned int flags) 275e2849863SThomas Graf { 27616feebcfSFlorian Westphal int err; 27716feebcfSFlorian Westphal 27816feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27916feebcfSFlorian Westphal flags); 28016feebcfSFlorian Westphal if (err) 28116feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 28216feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 283e2849863SThomas Graf } 284e2849863SThomas Graf 285e2849863SThomas Graf /** 286e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 287e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 288e2849863SThomas Graf * @msgtype: rtnetlink message type 289e2849863SThomas Graf * 290e2849863SThomas Graf * Returns 0 on success or a negative error code. 291e2849863SThomas Graf */ 292e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 293e2849863SThomas Graf { 294addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 295e2849863SThomas Graf int msgindex; 296e2849863SThomas Graf 29725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 298e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 299e2849863SThomas Graf 3006853dd48SFlorian Westphal rtnl_lock(); 301addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 302addf9b90SFlorian Westphal if (!tab) { 3036853dd48SFlorian Westphal rtnl_unlock(); 304e2849863SThomas Graf return -ENOENT; 3056853dd48SFlorian Westphal } 306e2849863SThomas Graf 307addf9b90SFlorian Westphal link = tab[msgindex]; 308addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 3096853dd48SFlorian Westphal rtnl_unlock(); 310e2849863SThomas Graf 311addf9b90SFlorian Westphal kfree_rcu(link, rcu); 312addf9b90SFlorian Westphal 313e2849863SThomas Graf return 0; 314e2849863SThomas Graf } 315e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 316e2849863SThomas Graf 317e2849863SThomas Graf /** 318e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 319e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 320e2849863SThomas Graf * 321e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 322e2849863SThomas Graf * of a certain protocol family. 323e2849863SThomas Graf */ 324e2849863SThomas Graf void rtnl_unregister_all(int protocol) 325e2849863SThomas Graf { 326addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 327addf9b90SFlorian Westphal int msgindex; 328019a3169SFlorian Westphal 32925239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 330e2849863SThomas Graf 331019a3169SFlorian Westphal rtnl_lock(); 332addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 333f707ef61SSabrina Dubroca if (!tab) { 334f707ef61SSabrina Dubroca rtnl_unlock(); 335f707ef61SSabrina Dubroca return; 336f707ef61SSabrina Dubroca } 3376853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 338addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 339addf9b90SFlorian Westphal link = tab[msgindex]; 340addf9b90SFlorian Westphal if (!link) 341addf9b90SFlorian Westphal continue; 342addf9b90SFlorian Westphal 343addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 344addf9b90SFlorian Westphal kfree_rcu(link, rcu); 345addf9b90SFlorian Westphal } 346019a3169SFlorian Westphal rtnl_unlock(); 347019a3169SFlorian Westphal 3486853dd48SFlorian Westphal synchronize_net(); 3496853dd48SFlorian Westphal 350addf9b90SFlorian Westphal kfree(tab); 351e2849863SThomas Graf } 352e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3531da177e4SLinus Torvalds 35438f7b870SPatrick McHardy static LIST_HEAD(link_ops); 35538f7b870SPatrick McHardy 356c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 357c63044f0SEric Dumazet { 358c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 359c63044f0SEric Dumazet 360c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 361c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 362c63044f0SEric Dumazet return ops; 363c63044f0SEric Dumazet } 364c63044f0SEric Dumazet return NULL; 365c63044f0SEric Dumazet } 366c63044f0SEric Dumazet 36738f7b870SPatrick McHardy /** 36838f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 36938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 37038f7b870SPatrick McHardy * 37138f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 37238f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 37338f7b870SPatrick McHardy * must be called before registering the devices. 37438f7b870SPatrick McHardy * 37538f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 37638f7b870SPatrick McHardy */ 37738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 37838f7b870SPatrick McHardy { 379c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 380c63044f0SEric Dumazet return -EEXIST; 381c63044f0SEric Dumazet 382b0ab2fabSJiri Pirko /* The check for setup is here because if ops 383b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 384b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 385b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 386b0ab2fabSJiri Pirko */ 387b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 38823289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3892d85cba2SPatrick McHardy 39038f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 39138f7b870SPatrick McHardy return 0; 39238f7b870SPatrick McHardy } 39338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 39438f7b870SPatrick McHardy 39538f7b870SPatrick McHardy /** 39638f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 39738f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 39838f7b870SPatrick McHardy * 39938f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 40038f7b870SPatrick McHardy */ 40138f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 40238f7b870SPatrick McHardy { 40338f7b870SPatrick McHardy int err; 40438f7b870SPatrick McHardy 405ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 406ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 407ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 408ccf8dbcdSKees Cook return -EINVAL; 409ccf8dbcdSKees Cook 41038f7b870SPatrick McHardy rtnl_lock(); 41138f7b870SPatrick McHardy err = __rtnl_link_register(ops); 41238f7b870SPatrick McHardy rtnl_unlock(); 41338f7b870SPatrick McHardy return err; 41438f7b870SPatrick McHardy } 41538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 41638f7b870SPatrick McHardy 417669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 418669f87baSPavel Emelyanov { 419669f87baSPavel Emelyanov struct net_device *dev; 42023289a37SEric Dumazet LIST_HEAD(list_kill); 42123289a37SEric Dumazet 422669f87baSPavel Emelyanov for_each_netdev(net, dev) { 42323289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 42423289a37SEric Dumazet ops->dellink(dev, &list_kill); 425669f87baSPavel Emelyanov } 42623289a37SEric Dumazet unregister_netdevice_many(&list_kill); 427669f87baSPavel Emelyanov } 428669f87baSPavel Emelyanov 42938f7b870SPatrick McHardy /** 43038f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 43138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 43238f7b870SPatrick McHardy * 433554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 434554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 435554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 43638f7b870SPatrick McHardy */ 43738f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 43838f7b870SPatrick McHardy { 439881d966bSEric W. Biederman struct net *net; 4402d85cba2SPatrick McHardy 441881d966bSEric W. Biederman for_each_net(net) { 442669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 443881d966bSEric W. Biederman } 44438f7b870SPatrick McHardy list_del(&ops->list); 44538f7b870SPatrick McHardy } 44638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 44738f7b870SPatrick McHardy 448200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 449200b916fSCong Wang * devices unregistering in any network namespace. 450200b916fSCong Wang */ 451200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 452200b916fSCong Wang { 453200b916fSCong Wang struct net *net; 454200b916fSCong Wang bool unregistering; 455ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 456200b916fSCong Wang 457ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 458200b916fSCong Wang for (;;) { 459200b916fSCong Wang unregistering = false; 460200b916fSCong Wang rtnl_lock(); 461f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 462f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 463f0b07bb1SKirill Tkhai */ 464200b916fSCong Wang for_each_net(net) { 465200b916fSCong Wang if (net->dev_unreg_count > 0) { 466200b916fSCong Wang unregistering = true; 467200b916fSCong Wang break; 468200b916fSCong Wang } 469200b916fSCong Wang } 470200b916fSCong Wang if (!unregistering) 471200b916fSCong Wang break; 472200b916fSCong Wang __rtnl_unlock(); 473ff960a73SPeter Zijlstra 474ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 475200b916fSCong Wang } 476ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 477200b916fSCong Wang } 478200b916fSCong Wang 47938f7b870SPatrick McHardy /** 48038f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 48138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 48238f7b870SPatrick McHardy */ 48338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 48438f7b870SPatrick McHardy { 4858518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4864420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 487200b916fSCong Wang rtnl_lock_unregistering_all(); 48838f7b870SPatrick McHardy __rtnl_link_unregister(ops); 48938f7b870SPatrick McHardy rtnl_unlock(); 4904420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 49138f7b870SPatrick McHardy } 49238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 49338f7b870SPatrick McHardy 494ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 495ba7d49b1SJiri Pirko { 496ba7d49b1SJiri Pirko struct net_device *master_dev; 497ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4988515ae38SFlorian Westphal size_t size = 0; 499ba7d49b1SJiri Pirko 5008515ae38SFlorian Westphal rcu_read_lock(); 5018515ae38SFlorian Westphal 5028515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 503ba7d49b1SJiri Pirko if (!master_dev) 5048515ae38SFlorian Westphal goto out; 5058515ae38SFlorian Westphal 506ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5076049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5088515ae38SFlorian Westphal goto out; 509ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5108515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 511ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5128515ae38SFlorian Westphal 5138515ae38SFlorian Westphal out: 5148515ae38SFlorian Westphal rcu_read_unlock(); 5158515ae38SFlorian Westphal return size; 516ba7d49b1SJiri Pirko } 517ba7d49b1SJiri Pirko 51838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 51938f7b870SPatrick McHardy { 52038f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 52138f7b870SPatrick McHardy size_t size; 52238f7b870SPatrick McHardy 52338f7b870SPatrick McHardy if (!ops) 52438f7b870SPatrick McHardy return 0; 52538f7b870SPatrick McHardy 526369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 527369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 52838f7b870SPatrick McHardy 52938f7b870SPatrick McHardy if (ops->get_size) 53038f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 531369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 53238f7b870SPatrick McHardy ops->get_size(dev); 53338f7b870SPatrick McHardy 53438f7b870SPatrick McHardy if (ops->get_xstats_size) 535369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 536369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 53738f7b870SPatrick McHardy 538ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 539ba7d49b1SJiri Pirko 54038f7b870SPatrick McHardy return size; 54138f7b870SPatrick McHardy } 54238f7b870SPatrick McHardy 543f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 544f8ff182cSThomas Graf 545f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 546f8ff182cSThomas Graf { 547f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 548f8ff182cSThomas Graf 5495fa85a09SFlorian Westphal list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 550f8ff182cSThomas Graf if (ops->family == family) 551f8ff182cSThomas Graf return ops; 552f8ff182cSThomas Graf } 553f8ff182cSThomas Graf 554f8ff182cSThomas Graf return NULL; 555f8ff182cSThomas Graf } 556f8ff182cSThomas Graf 557f8ff182cSThomas Graf /** 558f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 559f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 560f8ff182cSThomas Graf * 561f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 562f8ff182cSThomas Graf */ 5633678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 564f8ff182cSThomas Graf { 565f8ff182cSThomas Graf rtnl_lock(); 5665fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 567f8ff182cSThomas Graf rtnl_unlock(); 568f8ff182cSThomas Graf } 569f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 570f8ff182cSThomas Graf 571f8ff182cSThomas Graf /** 572f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 573f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 574f8ff182cSThomas Graf */ 575f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 576f8ff182cSThomas Graf { 577f8ff182cSThomas Graf rtnl_lock(); 5785fa85a09SFlorian Westphal list_del_rcu(&ops->list); 579f8ff182cSThomas Graf rtnl_unlock(); 5805fa85a09SFlorian Westphal 5815fa85a09SFlorian Westphal synchronize_rcu(); 582f8ff182cSThomas Graf } 583f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 584f8ff182cSThomas Graf 585b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 586b1974ed0SArad, Ronen u32 ext_filter_mask) 587f8ff182cSThomas Graf { 588f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 589f8ff182cSThomas Graf size_t size; 590f8ff182cSThomas Graf 591f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 592f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 593f8ff182cSThomas Graf 5945fa85a09SFlorian Westphal rcu_read_lock(); 5955fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 596f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 597f8ff182cSThomas Graf /* AF_* + nested data */ 598f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 599b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 600f8ff182cSThomas Graf } 601f8ff182cSThomas Graf } 6025fa85a09SFlorian Westphal rcu_read_unlock(); 603f8ff182cSThomas Graf 604f8ff182cSThomas Graf return size; 605f8ff182cSThomas Graf } 606f8ff182cSThomas Graf 607ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 608ba7d49b1SJiri Pirko { 609ba7d49b1SJiri Pirko struct net_device *master_dev; 6104c82a95eSFlorian Westphal bool ret = false; 611ba7d49b1SJiri Pirko 6124c82a95eSFlorian Westphal rcu_read_lock(); 6134c82a95eSFlorian Westphal 6144c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 615813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6164c82a95eSFlorian Westphal ret = true; 6174c82a95eSFlorian Westphal rcu_read_unlock(); 6184c82a95eSFlorian Westphal return ret; 619ba7d49b1SJiri Pirko } 620ba7d49b1SJiri Pirko 621ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 622ba7d49b1SJiri Pirko const struct net_device *dev) 623ba7d49b1SJiri Pirko { 624ba7d49b1SJiri Pirko struct net_device *master_dev; 625ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 626ba7d49b1SJiri Pirko struct nlattr *slave_data; 627ba7d49b1SJiri Pirko int err; 628ba7d49b1SJiri Pirko 629ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 630ba7d49b1SJiri Pirko if (!master_dev) 631ba7d49b1SJiri Pirko return 0; 632ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 633ba7d49b1SJiri Pirko if (!ops) 634ba7d49b1SJiri Pirko return 0; 635ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 636ba7d49b1SJiri Pirko return -EMSGSIZE; 637ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 638ba7d49b1SJiri Pirko slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 639ba7d49b1SJiri Pirko if (!slave_data) 640ba7d49b1SJiri Pirko return -EMSGSIZE; 641ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 642ba7d49b1SJiri Pirko if (err < 0) 643ba7d49b1SJiri Pirko goto err_cancel_slave_data; 644ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 645ba7d49b1SJiri Pirko } 646ba7d49b1SJiri Pirko return 0; 647ba7d49b1SJiri Pirko 648ba7d49b1SJiri Pirko err_cancel_slave_data: 649ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 650ba7d49b1SJiri Pirko return err; 651ba7d49b1SJiri Pirko } 652ba7d49b1SJiri Pirko 653ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 654ba7d49b1SJiri Pirko const struct net_device *dev) 65538f7b870SPatrick McHardy { 65638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 657ba7d49b1SJiri Pirko struct nlattr *data; 658ba7d49b1SJiri Pirko int err; 659ba7d49b1SJiri Pirko 660ba7d49b1SJiri Pirko if (!ops) 661ba7d49b1SJiri Pirko return 0; 662ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 663ba7d49b1SJiri Pirko return -EMSGSIZE; 664ba7d49b1SJiri Pirko if (ops->fill_xstats) { 665ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 666ba7d49b1SJiri Pirko if (err < 0) 667ba7d49b1SJiri Pirko return err; 668ba7d49b1SJiri Pirko } 669ba7d49b1SJiri Pirko if (ops->fill_info) { 670ba7d49b1SJiri Pirko data = nla_nest_start(skb, IFLA_INFO_DATA); 671ba7d49b1SJiri Pirko if (data == NULL) 672ba7d49b1SJiri Pirko return -EMSGSIZE; 673ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 674ba7d49b1SJiri Pirko if (err < 0) 675ba7d49b1SJiri Pirko goto err_cancel_data; 676ba7d49b1SJiri Pirko nla_nest_end(skb, data); 677ba7d49b1SJiri Pirko } 678ba7d49b1SJiri Pirko return 0; 679ba7d49b1SJiri Pirko 680ba7d49b1SJiri Pirko err_cancel_data: 681ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 682ba7d49b1SJiri Pirko return err; 683ba7d49b1SJiri Pirko } 684ba7d49b1SJiri Pirko 685ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 686ba7d49b1SJiri Pirko { 687ba7d49b1SJiri Pirko struct nlattr *linkinfo; 68838f7b870SPatrick McHardy int err = -EMSGSIZE; 68938f7b870SPatrick McHardy 69038f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 69138f7b870SPatrick McHardy if (linkinfo == NULL) 69238f7b870SPatrick McHardy goto out; 69338f7b870SPatrick McHardy 694ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 69538f7b870SPatrick McHardy if (err < 0) 69638f7b870SPatrick McHardy goto err_cancel_link; 697ba7d49b1SJiri Pirko 698ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 69938f7b870SPatrick McHardy if (err < 0) 700ba7d49b1SJiri Pirko goto err_cancel_link; 70138f7b870SPatrick McHardy 70238f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 70338f7b870SPatrick McHardy return 0; 70438f7b870SPatrick McHardy 70538f7b870SPatrick McHardy err_cancel_link: 70638f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 70738f7b870SPatrick McHardy out: 70838f7b870SPatrick McHardy return err; 70938f7b870SPatrick McHardy } 71038f7b870SPatrick McHardy 71195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7121da177e4SLinus Torvalds { 71397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7141da177e4SLinus Torvalds int err = 0; 7151da177e4SLinus Torvalds 716ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 7171da177e4SLinus Torvalds if (echo) 71863354797SReshetova, Elena refcount_inc(&skb->users); 7191da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 7201da177e4SLinus Torvalds if (echo) 7211da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 7221da177e4SLinus Torvalds return err; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 72597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7262942e900SThomas Graf { 72797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72897c53cacSDenis V. Lunev 7292942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7302942e900SThomas Graf } 731e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7322942e900SThomas Graf 7331ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 73497676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 73597676b6bSThomas Graf { 73697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73797676b6bSThomas Graf int report = 0; 73897676b6bSThomas Graf 73997676b6bSThomas Graf if (nlh) 74097676b6bSThomas Graf report = nlmsg_report(nlh); 74197676b6bSThomas Graf 7421ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 74397676b6bSThomas Graf } 744e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 74597676b6bSThomas Graf 74697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 74797676b6bSThomas Graf { 74897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 74997c53cacSDenis V. Lunev 75097676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 75197676b6bSThomas Graf } 752e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 75397676b6bSThomas Graf 7541da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7551da177e4SLinus Torvalds { 7562d7202bfSThomas Graf struct nlattr *mx; 7572d7202bfSThomas Graf int i, valid = 0; 7581da177e4SLinus Torvalds 7592d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 7602d7202bfSThomas Graf if (mx == NULL) 7612d7202bfSThomas Graf return -ENOBUFS; 7622d7202bfSThomas Graf 7631da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7642d7202bfSThomas Graf if (metrics[i]) { 765ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 766ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 767ea697639SDaniel Borkmann 768ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 769ea697639SDaniel Borkmann if (!name) 770ea697639SDaniel Borkmann continue; 771ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 772ea697639SDaniel Borkmann goto nla_put_failure; 773c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 774c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 775c3a8d947SDaniel Borkmann 776f8edcd12SPhil Sutter if (!user_features) 777f8edcd12SPhil Sutter continue; 778c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 779c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 780c3a8d947SDaniel Borkmann goto nla_put_failure; 781ea697639SDaniel Borkmann } else { 782a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 783a6574349SDavid S. Miller goto nla_put_failure; 7841da177e4SLinus Torvalds } 785ea697639SDaniel Borkmann valid++; 786ea697639SDaniel Borkmann } 7872d7202bfSThomas Graf } 7881da177e4SLinus Torvalds 789a57d27fcSDavid S. Miller if (!valid) { 790a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 791a57d27fcSDavid S. Miller return 0; 792a57d27fcSDavid S. Miller } 7932d7202bfSThomas Graf 7942d7202bfSThomas Graf return nla_nest_end(skb, mx); 7952d7202bfSThomas Graf 7962d7202bfSThomas Graf nla_put_failure: 797bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 798bc3ed28cSThomas Graf return -EMSGSIZE; 7991da177e4SLinus Torvalds } 800e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 8011da177e4SLinus Torvalds 802e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 80387a50699SDavid S. Miller long expires, u32 error) 804e3703b3dSThomas Graf { 805e3703b3dSThomas Graf struct rta_cacheinfo ci = { 806e3703b3dSThomas Graf .rta_error = error, 807e3703b3dSThomas Graf .rta_id = id, 808e3703b3dSThomas Graf }; 809e3703b3dSThomas Graf 8103940746dSDavid Ahern if (dst) { 8113940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8123940746dSDavid Ahern ci.rta_used = dst->__use; 8133940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8143940746dSDavid Ahern } 8158253947eSLi Wei if (expires) { 8168253947eSLi Wei unsigned long clock; 817e3703b3dSThomas Graf 8188253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8198253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8208253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8218253947eSLi Wei } 822e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 823e3703b3dSThomas Graf } 824e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8251da177e4SLinus Torvalds 82693b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 827b00055aaSStefan Rompf { 828b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 829b00055aaSStefan Rompf 830b00055aaSStefan Rompf switch (transition) { 831b00055aaSStefan Rompf case IF_OPER_UP: 832b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 833b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 834b00055aaSStefan Rompf !netif_dormant(dev)) 835b00055aaSStefan Rompf operstate = IF_OPER_UP; 836b00055aaSStefan Rompf break; 837b00055aaSStefan Rompf 838b00055aaSStefan Rompf case IF_OPER_DORMANT: 839b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 840b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 841b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 842b00055aaSStefan Rompf break; 8433ff50b79SStephen Hemminger } 844b00055aaSStefan Rompf 845b00055aaSStefan Rompf if (dev->operstate != operstate) { 846b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 847b00055aaSStefan Rompf dev->operstate = operstate; 848b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 849b00055aaSStefan Rompf netdev_state_change(dev); 85093b2d4a2SDavid S. Miller } 851b00055aaSStefan Rompf } 852b00055aaSStefan Rompf 853b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 854b1beb681SJiri Benc { 855b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 856b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 857b1beb681SJiri Benc } 858b1beb681SJiri Benc 8593729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8603729d502SPatrick McHardy const struct ifinfomsg *ifm) 8613729d502SPatrick McHardy { 8623729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8633729d502SPatrick McHardy 8643729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8653729d502SPatrick McHardy if (ifm->ifi_change) 8663729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 867b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8683729d502SPatrick McHardy 8693729d502SPatrick McHardy return flags; 8703729d502SPatrick McHardy } 8713729d502SPatrick McHardy 872b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 873be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8741da177e4SLinus Torvalds { 875b60c5115SThomas Graf a->rx_packets = b->rx_packets; 876b60c5115SThomas Graf a->tx_packets = b->tx_packets; 877b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 878b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 879b60c5115SThomas Graf a->rx_errors = b->rx_errors; 880b60c5115SThomas Graf a->tx_errors = b->tx_errors; 881b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 882b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 883b60c5115SThomas Graf 884b60c5115SThomas Graf a->multicast = b->multicast; 885b60c5115SThomas Graf a->collisions = b->collisions; 886b60c5115SThomas Graf 887b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 888b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 889b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 890b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 891b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 892b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 893b60c5115SThomas Graf 894b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 895b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 896b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 897b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 898b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 899b60c5115SThomas Graf 900b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 901b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 9026e7333d3SJarod Wilson 9036e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 90410708f37SJan Engelhardt } 90510708f37SJan Engelhardt 906c02db8c6SChris Wright /* All VF info */ 907115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 908115c9b81SGreg Rose u32 ext_filter_mask) 909ebc08a6fSWilliams, Mitch A { 9109af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 911c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9127e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 913045de01aSScott Feldman size += num_vfs * 9147e75f74aSSabrina Dubroca (nla_total_size(0) + 9157e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 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 983115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 984115c9b81SGreg Rose u32 ext_filter_mask) 985339bf98fSThomas Graf { 986339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 987339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9880b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 989339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 990270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 991339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 99235c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 993339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 994339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 995339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 996339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 997339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 998339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 999339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 10009a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 1001edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 100276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 100376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 10046919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 10056919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1006339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 100738f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10082d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1009d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1010db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1011115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1012115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1013115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1014c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1015f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1016b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 101782f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 101888d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1019c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1020b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10213d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10226621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 102338e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 102403ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 10257e4a8d5aSChristian Brauner + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1026b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1027b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10283e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10293e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 103079e1ad14SJiri Benc + 0; 1031339bf98fSThomas Graf } 1032339bf98fSThomas Graf 103357b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 103457b61080SScott Feldman { 103557b61080SScott Feldman struct nlattr *vf_ports; 103657b61080SScott Feldman struct nlattr *vf_port; 103757b61080SScott Feldman int vf; 103857b61080SScott Feldman int err; 103957b61080SScott Feldman 104057b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 104157b61080SScott Feldman if (!vf_ports) 104257b61080SScott Feldman return -EMSGSIZE; 104357b61080SScott Feldman 104457b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 104557b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10468ca94183SScott Feldman if (!vf_port) 10478ca94183SScott Feldman goto nla_put_failure; 1048a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1049a6574349SDavid S. Miller goto nla_put_failure; 105057b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10518ca94183SScott Feldman if (err == -EMSGSIZE) 10528ca94183SScott Feldman goto nla_put_failure; 105357b61080SScott Feldman if (err) { 105457b61080SScott Feldman nla_nest_cancel(skb, vf_port); 105557b61080SScott Feldman continue; 105657b61080SScott Feldman } 105757b61080SScott Feldman nla_nest_end(skb, vf_port); 105857b61080SScott Feldman } 105957b61080SScott Feldman 106057b61080SScott Feldman nla_nest_end(skb, vf_ports); 106157b61080SScott Feldman 106257b61080SScott Feldman return 0; 10638ca94183SScott Feldman 10648ca94183SScott Feldman nla_put_failure: 10658ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10668ca94183SScott Feldman return -EMSGSIZE; 106757b61080SScott Feldman } 106857b61080SScott Feldman 106957b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 107057b61080SScott Feldman { 107157b61080SScott Feldman struct nlattr *port_self; 107257b61080SScott Feldman int err; 107357b61080SScott Feldman 107457b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 107557b61080SScott Feldman if (!port_self) 107657b61080SScott Feldman return -EMSGSIZE; 107757b61080SScott Feldman 107857b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 107957b61080SScott Feldman if (err) { 108057b61080SScott Feldman nla_nest_cancel(skb, port_self); 10818ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 108257b61080SScott Feldman } 108357b61080SScott Feldman 108457b61080SScott Feldman nla_nest_end(skb, port_self); 108557b61080SScott Feldman 108657b61080SScott Feldman return 0; 108757b61080SScott Feldman } 108857b61080SScott Feldman 1089c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1090c53864fdSDavid Gibson u32 ext_filter_mask) 109157b61080SScott Feldman { 109257b61080SScott Feldman int err; 109357b61080SScott Feldman 1094c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1095c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 109657b61080SScott Feldman return 0; 109757b61080SScott Feldman 109857b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 109957b61080SScott Feldman if (err) 110057b61080SScott Feldman return err; 110157b61080SScott Feldman 110257b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 110357b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 110457b61080SScott Feldman if (err) 110557b61080SScott Feldman return err; 110657b61080SScott Feldman } 110757b61080SScott Feldman 110857b61080SScott Feldman return 0; 110957b61080SScott Feldman } 111057b61080SScott Feldman 111166cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 111266cae9edSJiri Pirko { 111366cae9edSJiri Pirko int err; 111402637fceSJiri Pirko struct netdev_phys_item_id ppid; 111566cae9edSJiri Pirko 111666cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 111766cae9edSJiri Pirko if (err) { 111866cae9edSJiri Pirko if (err == -EOPNOTSUPP) 111966cae9edSJiri Pirko return 0; 112066cae9edSJiri Pirko return err; 112166cae9edSJiri Pirko } 112266cae9edSJiri Pirko 112366cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 112466cae9edSJiri Pirko return -EMSGSIZE; 112566cae9edSJiri Pirko 112666cae9edSJiri Pirko return 0; 112766cae9edSJiri Pirko } 112866cae9edSJiri Pirko 1129db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1130db24a904SDavid Ahern { 1131db24a904SDavid Ahern char name[IFNAMSIZ]; 1132db24a904SDavid Ahern int err; 1133db24a904SDavid Ahern 1134db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1135db24a904SDavid Ahern if (err) { 1136db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1137db24a904SDavid Ahern return 0; 1138db24a904SDavid Ahern return err; 1139db24a904SDavid Ahern } 1140db24a904SDavid Ahern 114177ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1142db24a904SDavid Ahern return -EMSGSIZE; 1143db24a904SDavid Ahern 1144db24a904SDavid Ahern return 0; 1145db24a904SDavid Ahern } 1146db24a904SDavid Ahern 114782f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 114882f28412SJiri Pirko { 114982f28412SJiri Pirko int err; 1150f8e20a9fSScott Feldman struct switchdev_attr attr = { 11516ff64f6fSIdo Schimmel .orig_dev = dev, 11521f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1153f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1154f8e20a9fSScott Feldman }; 115582f28412SJiri Pirko 1156f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 115782f28412SJiri Pirko if (err) { 115882f28412SJiri Pirko if (err == -EOPNOTSUPP) 115982f28412SJiri Pirko return 0; 116082f28412SJiri Pirko return err; 116182f28412SJiri Pirko } 116282f28412SJiri Pirko 116342275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 116442275bd8SScott Feldman attr.u.ppid.id)) 116582f28412SJiri Pirko return -EMSGSIZE; 116682f28412SJiri Pirko 116782f28412SJiri Pirko return 0; 116882f28412SJiri Pirko } 116982f28412SJiri Pirko 1170b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1171b22b941bSHannes Frederic Sowa struct net_device *dev) 1172b22b941bSHannes Frederic Sowa { 1173550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1174b22b941bSHannes Frederic Sowa struct nlattr *attr; 1175b22b941bSHannes Frederic Sowa 117658414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 117758414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1178b22b941bSHannes Frederic Sowa if (!attr) 1179b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1180b22b941bSHannes Frederic Sowa 1181550bce59SRoopa Prabhu sp = nla_data(attr); 1182550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1183550bce59SRoopa Prabhu 1184550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1185550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1186550bce59SRoopa Prabhu if (!attr) 1187550bce59SRoopa Prabhu return -EMSGSIZE; 1188550bce59SRoopa Prabhu 1189550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1190b22b941bSHannes Frederic Sowa 1191b22b941bSHannes Frederic Sowa return 0; 1192b22b941bSHannes Frederic Sowa } 1193b22b941bSHannes Frederic Sowa 1194b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1195b22b941bSHannes Frederic Sowa struct net_device *dev, 1196b22b941bSHannes Frederic Sowa int vfs_num, 1197b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1198b22b941bSHannes Frederic Sowa { 1199b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 120079aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1201b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 120279aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1203b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1204b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1205b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1206b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1207b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1208b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1209b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1210b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1211b22b941bSHannes Frederic Sowa 12120eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12130eed9cf5SMintz, Yuval 1214b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1215b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1216b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1217b22b941bSHannes Frederic Sowa * didn't report anything. 1218b22b941bSHannes Frederic Sowa */ 1219b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1220b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1221b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1222b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1223b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1224b22b941bSHannes Frederic Sowa */ 1225b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 122679aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 122779aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1228b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1229b22b941bSHannes Frederic Sowa return 0; 1230b22b941bSHannes Frederic Sowa 1231775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1232775f4f05SDan Carpenter 1233b22b941bSHannes Frederic Sowa vf_mac.vf = 1234b22b941bSHannes Frederic Sowa vf_vlan.vf = 123579aab093SMoshe Shemesh vf_vlan_info.vf = 1236b22b941bSHannes Frederic Sowa vf_rate.vf = 1237b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1238b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1239b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1240b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1241b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1242b22b941bSHannes Frederic Sowa 1243b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1244b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1245b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 124679aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 124779aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 124879aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1249b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1250b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1251b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1252b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1253b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1254b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1255b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1256b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 125779aab093SMoshe Shemesh if (!vf) 125879aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1259b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1260b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1261b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1262b22b941bSHannes Frederic Sowa &vf_rate) || 1263b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1264b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1265b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1266b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1267b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1268b22b941bSHannes Frederic Sowa &vf_linkstate) || 1269b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1270b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1271b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1272b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1273b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 127479aab093SMoshe Shemesh goto nla_put_vf_failure; 127579aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 127679aab093SMoshe Shemesh if (!vfvlanlist) 127779aab093SMoshe Shemesh goto nla_put_vf_failure; 127879aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 127979aab093SMoshe Shemesh &vf_vlan_info)) { 128079aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 128179aab093SMoshe Shemesh goto nla_put_vf_failure; 128279aab093SMoshe Shemesh } 128379aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1284b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1285b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1286b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1287b22b941bSHannes Frederic Sowa &vf_stats); 1288b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 128979aab093SMoshe Shemesh if (!vfstats) 129079aab093SMoshe Shemesh goto nla_put_vf_failure; 1291343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1292343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1293343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1294343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1295343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1296343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1297343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1298343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1299343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1300343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1301343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1302c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1303c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1304c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1305c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1306c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 130779aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 130879aab093SMoshe Shemesh goto nla_put_vf_failure; 130979aab093SMoshe Shemesh } 1310b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1311b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1312b22b941bSHannes Frederic Sowa return 0; 131379aab093SMoshe Shemesh 131479aab093SMoshe Shemesh nla_put_vf_failure: 131579aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 131679aab093SMoshe Shemesh nla_put_vfinfo_failure: 131779aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 131879aab093SMoshe Shemesh return -EMSGSIZE; 1319b22b941bSHannes Frederic Sowa } 1320b22b941bSHannes Frederic Sowa 1321250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1322250fc3dfSFlorian Westphal struct net_device *dev, 1323250fc3dfSFlorian Westphal u32 ext_filter_mask) 1324250fc3dfSFlorian Westphal { 1325250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1326250fc3dfSFlorian Westphal int i, num_vfs; 1327250fc3dfSFlorian Westphal 1328250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1329250fc3dfSFlorian Westphal return 0; 1330250fc3dfSFlorian Westphal 1331250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1332250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1333250fc3dfSFlorian Westphal return -EMSGSIZE; 1334250fc3dfSFlorian Westphal 1335250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1336250fc3dfSFlorian Westphal return 0; 1337250fc3dfSFlorian Westphal 1338250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1339250fc3dfSFlorian Westphal if (!vfinfo) 1340250fc3dfSFlorian Westphal return -EMSGSIZE; 1341250fc3dfSFlorian Westphal 1342250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1343250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1344250fc3dfSFlorian Westphal return -EMSGSIZE; 1345250fc3dfSFlorian Westphal } 1346250fc3dfSFlorian Westphal 1347250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1348250fc3dfSFlorian Westphal return 0; 1349250fc3dfSFlorian Westphal } 1350250fc3dfSFlorian Westphal 1351b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1352b22b941bSHannes Frederic Sowa { 13535f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13545f8e4474SKangjie Lu 13555f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13565f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13575f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13585f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13595f8e4474SKangjie Lu map.irq = dev->irq; 13605f8e4474SKangjie Lu map.dma = dev->dma; 13615f8e4474SKangjie Lu map.port = dev->if_port; 13625f8e4474SKangjie Lu 1363270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1364b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1365b22b941bSHannes Frederic Sowa 1366b22b941bSHannes Frederic Sowa return 0; 1367b22b941bSHannes Frederic Sowa } 1368b22b941bSHannes Frederic Sowa 1369a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1370d67b9cd2SDaniel Borkmann { 137158038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1372d67b9cd2SDaniel Borkmann 1373d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1374d67b9cd2SDaniel Borkmann 137558038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1376a25717d2SJakub Kicinski if (!generic_xdp_prog) 1377a25717d2SJakub Kicinski return 0; 1378a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 137958038695SMartin KaFai Lau } 1380ce158e58SJakub Kicinski 1381a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1382a25717d2SJakub Kicinski { 1383a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1384a25717d2SJakub Kicinski } 1385118b4aa2SJakub Kicinski 1386a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1387a25717d2SJakub Kicinski { 1388a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1389a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1390a25717d2SJakub Kicinski } 1391a25717d2SJakub Kicinski 1392a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1393a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1394a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1395a25717d2SJakub Kicinski { 1396a25717d2SJakub Kicinski u32 curr_id; 1397a25717d2SJakub Kicinski int err; 1398a25717d2SJakub Kicinski 1399a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1400a25717d2SJakub Kicinski if (!curr_id) 1401a25717d2SJakub Kicinski return 0; 1402a25717d2SJakub Kicinski 1403a25717d2SJakub Kicinski *prog_id = curr_id; 1404a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1405a25717d2SJakub Kicinski if (err) 1406a25717d2SJakub Kicinski return err; 1407a25717d2SJakub Kicinski 1408a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1409a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1410a25717d2SJakub Kicinski else 1411a25717d2SJakub Kicinski *mode = tgt_mode; 1412a25717d2SJakub Kicinski 1413a25717d2SJakub Kicinski return 0; 1414d67b9cd2SDaniel Borkmann } 1415d67b9cd2SDaniel Borkmann 1416d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1417d1fdd913SBrenden Blanco { 1418d1fdd913SBrenden Blanco struct nlattr *xdp; 141958038695SMartin KaFai Lau u32 prog_id; 1420d1fdd913SBrenden Blanco int err; 14214f91da26SJakub Kicinski u8 mode; 1422d1fdd913SBrenden Blanco 1423d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1424d1fdd913SBrenden Blanco if (!xdp) 1425d1fdd913SBrenden Blanco return -EMSGSIZE; 1426b5cdae32SDavid S. Miller 1427a25717d2SJakub Kicinski prog_id = 0; 1428a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1429202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1430202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1431202aabe8SJakub Kicinski if (err) 1432a25717d2SJakub Kicinski goto err_cancel; 1433202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1434202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1435202aabe8SJakub Kicinski if (err) 1436a25717d2SJakub Kicinski goto err_cancel; 1437202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1438202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1439202aabe8SJakub Kicinski if (err) 1440a25717d2SJakub Kicinski goto err_cancel; 1441a25717d2SJakub Kicinski 14424f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1443d1fdd913SBrenden Blanco if (err) 1444d1fdd913SBrenden Blanco goto err_cancel; 1445d1fdd913SBrenden Blanco 1446a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 144758038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 144858038695SMartin KaFai Lau if (err) 144958038695SMartin KaFai Lau goto err_cancel; 145058038695SMartin KaFai Lau } 145158038695SMartin KaFai Lau 1452d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1453d1fdd913SBrenden Blanco return 0; 1454d1fdd913SBrenden Blanco 1455d1fdd913SBrenden Blanco err_cancel: 1456d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1457d1fdd913SBrenden Blanco return err; 1458d1fdd913SBrenden Blanco } 1459d1fdd913SBrenden Blanco 14603d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14613d3ea5afSVlad Yasevich { 14623d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14633d3ea5afSVlad Yasevich 14643d3ea5afSVlad Yasevich switch (event) { 14653d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14673d3ea5afSVlad Yasevich break; 14683d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14703d3ea5afSVlad Yasevich break; 14713d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14733d3ea5afSVlad Yasevich break; 14743d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14753d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14763d3ea5afSVlad Yasevich break; 14773d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14783d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14793d3ea5afSVlad Yasevich break; 14803d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14813d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14823d3ea5afSVlad Yasevich break; 14833d3ea5afSVlad Yasevich default: 14843d3ea5afSVlad Yasevich break; 14853d3ea5afSVlad Yasevich } 14863d3ea5afSVlad Yasevich 14873d3ea5afSVlad Yasevich return rtnl_event_type; 14883d3ea5afSVlad Yasevich } 14893d3ea5afSVlad Yasevich 149079110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 149179110a04SFlorian Westphal { 149279110a04SFlorian Westphal const struct net_device *upper_dev; 149379110a04SFlorian Westphal int ret = 0; 149479110a04SFlorian Westphal 149579110a04SFlorian Westphal rcu_read_lock(); 149679110a04SFlorian Westphal 149779110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 149879110a04SFlorian Westphal if (upper_dev) 149979110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 150079110a04SFlorian Westphal 150179110a04SFlorian Westphal rcu_read_unlock(); 150279110a04SFlorian Westphal return ret; 150379110a04SFlorian Westphal } 150479110a04SFlorian Westphal 150579110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 150679110a04SFlorian Westphal { 150779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 150879110a04SFlorian Westphal 150979110a04SFlorian Westphal if (dev->ifindex == ifindex) 151079110a04SFlorian Westphal return 0; 151179110a04SFlorian Westphal 151279110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 151379110a04SFlorian Westphal } 151479110a04SFlorian Westphal 15156c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15166c557001SFlorian Westphal struct net_device *dev) 15176c557001SFlorian Westphal { 15186c557001SFlorian Westphal char buf[IFALIASZ]; 15196c557001SFlorian Westphal int ret; 15206c557001SFlorian Westphal 15216c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15226c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15236c557001SFlorian Westphal } 15246c557001SFlorian Westphal 1525b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 152679e1ad14SJiri Benc const struct net_device *dev, 152779e1ad14SJiri Benc struct net *src_net) 1528b1e66b9aSFlorian Westphal { 1529b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1530b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1531b1e66b9aSFlorian Westphal 1532b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 153379e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1534b1e66b9aSFlorian Westphal 1535b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1536b1e66b9aSFlorian Westphal return -EMSGSIZE; 1537b1e66b9aSFlorian Westphal } 1538b1e66b9aSFlorian Westphal } 1539b1e66b9aSFlorian Westphal 1540b1e66b9aSFlorian Westphal return 0; 1541b1e66b9aSFlorian Westphal } 1542b1e66b9aSFlorian Westphal 1543070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1544070cbf5bSFlorian Westphal const struct net_device *dev, 1545070cbf5bSFlorian Westphal u32 ext_filter_mask) 1546070cbf5bSFlorian Westphal { 1547070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1548070cbf5bSFlorian Westphal struct nlattr *af_spec; 1549070cbf5bSFlorian Westphal 1550070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1551070cbf5bSFlorian Westphal if (!af_spec) 1552070cbf5bSFlorian Westphal return -EMSGSIZE; 1553070cbf5bSFlorian Westphal 15545fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1555070cbf5bSFlorian Westphal struct nlattr *af; 1556070cbf5bSFlorian Westphal int err; 1557070cbf5bSFlorian Westphal 1558070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1559070cbf5bSFlorian Westphal continue; 1560070cbf5bSFlorian Westphal 1561070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1562070cbf5bSFlorian Westphal if (!af) 1563070cbf5bSFlorian Westphal return -EMSGSIZE; 1564070cbf5bSFlorian Westphal 1565070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1566070cbf5bSFlorian Westphal /* 1567070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1568070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1569070cbf5bSFlorian Westphal * means we should trim the attribute header and 1570070cbf5bSFlorian Westphal * continue. 1571070cbf5bSFlorian Westphal */ 1572070cbf5bSFlorian Westphal if (err == -ENODATA) 1573070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1574070cbf5bSFlorian Westphal else if (err < 0) 1575070cbf5bSFlorian Westphal return -EMSGSIZE; 1576070cbf5bSFlorian Westphal 1577070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1578070cbf5bSFlorian Westphal } 1579070cbf5bSFlorian Westphal 1580070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1581070cbf5bSFlorian Westphal return 0; 1582070cbf5bSFlorian Westphal } 1583070cbf5bSFlorian Westphal 158479e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 158579e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1586575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15873d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 158838e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 158938e01b30SNicolas Dichtel int tgt_netnsid) 1590b60c5115SThomas Graf { 1591b60c5115SThomas Graf struct ifinfomsg *ifm; 15921da177e4SLinus Torvalds struct nlmsghdr *nlh; 15931da177e4SLinus Torvalds 15942907c35fSEric Dumazet ASSERT_RTNL(); 1595b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1596b60c5115SThomas Graf if (nlh == NULL) 159726932566SPatrick McHardy return -EMSGSIZE; 15981da177e4SLinus Torvalds 1599b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1600b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1601b60c5115SThomas Graf ifm->__ifi_pad = 0; 1602b60c5115SThomas Graf ifm->ifi_type = dev->type; 1603b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1604b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1605b60c5115SThomas Graf ifm->ifi_change = change; 16061da177e4SLinus Torvalds 16077e4a8d5aSChristian Brauner if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 160879e1ad14SJiri Benc goto nla_put_failure; 160979e1ad14SJiri Benc 1610a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1611a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1612a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1613a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1614a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1615a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 16163e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 16173e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1618a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1619edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 162076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1621c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1622c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 16231d69c2b3SMark A. Greer #ifdef CONFIG_RPS 162476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 16251d69c2b3SMark A. Greer #endif 162679110a04SFlorian Westphal nla_put_iflink(skb, dev) || 162779110a04SFlorian Westphal put_master_ifindex(skb, dev) || 16289a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1629a6574349SDavid S. Miller (dev->qdisc && 1630a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 16316c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 16322d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1633b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1634b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1635b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1636b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1637b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1638b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1639b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1640a6574349SDavid S. Miller goto nla_put_failure; 16410b815a1aSStephen Hemminger 16423d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 16433d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 16443d3ea5afSVlad Yasevich goto nla_put_failure; 16453d3ea5afSVlad Yasevich } 16463d3ea5afSVlad Yasevich 1647b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1648a6574349SDavid S. Miller goto nla_put_failure; 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds if (dev->addr_len) { 1651a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1652a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1653a6574349SDavid S. Miller goto nla_put_failure; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 165666cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 165766cae9edSJiri Pirko goto nla_put_failure; 165866cae9edSJiri Pirko 1659db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1660db24a904SDavid Ahern goto nla_put_failure; 1661db24a904SDavid Ahern 166282f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 166382f28412SJiri Pirko goto nla_put_failure; 166482f28412SJiri Pirko 1665b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1666b60c5115SThomas Graf goto nla_put_failure; 1667b60c5115SThomas Graf 1668250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1669a6574349SDavid S. Miller goto nla_put_failure; 167057b61080SScott Feldman 1671c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 167257b61080SScott Feldman goto nla_put_failure; 167357b61080SScott Feldman 1674d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1675d1fdd913SBrenden Blanco goto nla_put_failure; 1676d1fdd913SBrenden Blanco 1677ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 167838f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 167938f7b870SPatrick McHardy goto nla_put_failure; 168038f7b870SPatrick McHardy } 168138f7b870SPatrick McHardy 168279e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1683d37512a2SNicolas Dichtel goto nla_put_failure; 1684d37512a2SNicolas Dichtel 16856621dd29SNicolas Dichtel if (new_nsid && 16866621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16876621dd29SNicolas Dichtel goto nla_put_failure; 168838e01b30SNicolas Dichtel if (new_ifindex && 168938e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 169038e01b30SNicolas Dichtel goto nla_put_failure; 169138e01b30SNicolas Dichtel 16926621dd29SNicolas Dichtel 16935fa85a09SFlorian Westphal rcu_read_lock(); 1694070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16955fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16965fa85a09SFlorian Westphal rcu_read_unlock(); 1697f8ff182cSThomas Graf 1698053c095aSJohannes Berg nlmsg_end(skb, nlh); 1699053c095aSJohannes Berg return 0; 1700b60c5115SThomas Graf 17015fa85a09SFlorian Westphal nla_put_failure_rcu: 17025fa85a09SFlorian Westphal rcu_read_unlock(); 1703b60c5115SThomas Graf nla_put_failure: 170426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 170526932566SPatrick McHardy return -EMSGSIZE; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 1708f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1709f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1710f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1711f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1712f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1713f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1714f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1715f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1716f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1717f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1718f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1719f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1720f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1721f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1722f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1723f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 17242459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 17252459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 17262459b4c6SNicolas Dichtel */ 17272459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1728f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1729f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1730f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1731f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1732f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1733f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1734f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1735f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 173646e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 173746e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 173802637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 17392d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 174082f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1741317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 174288d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1743d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 17443d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1745db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 17467e4a8d5aSChristian Brauner [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1747b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1748b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 17493e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 17503e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 1751f7b12606SJiri Pirko }; 1752f7b12606SJiri Pirko 1753f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1754f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1755f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1756f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1757f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1758f7b12606SJiri Pirko }; 1759f7b12606SJiri Pirko 1760f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1761364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1762364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 176379aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1764364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1765364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1766364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1767364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 176801a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17693b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1770dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1771cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1772cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17733b766cd8SEran Ben Elisha }; 17743b766cd8SEran Ben Elisha 1775f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1776f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1777f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1778f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1779f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1780f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1781f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1782f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1783f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1784f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1785025331dfSDaniel Borkmann 1786025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1787025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1788025331dfSDaniel Borkmann * combination with structs. 1789025331dfSDaniel Borkmann */ 1790025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1791025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1792f7b12606SJiri Pirko }; 1793f7b12606SJiri Pirko 1794d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1795d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1796d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 179785de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 179858038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1799d1fdd913SBrenden Blanco }; 1800d1fdd913SBrenden Blanco 1801dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1802dc599f76SDavid Ahern { 1803dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1804dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1805dc599f76SDavid Ahern 1806fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1807fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1808dc599f76SDavid Ahern return NULL; 1809dc599f76SDavid Ahern 1810dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1811dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1812dc599f76SDavid Ahern 1813dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1814dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1815dc599f76SDavid Ahern } 1816dc599f76SDavid Ahern 1817dc599f76SDavid Ahern return ops; 1818dc599f76SDavid Ahern } 1819dc599f76SDavid Ahern 1820dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1821dc599f76SDavid Ahern { 1822dc599f76SDavid Ahern struct net_device *master; 1823dc599f76SDavid Ahern 1824dc599f76SDavid Ahern if (!master_idx) 1825dc599f76SDavid Ahern return false; 1826dc599f76SDavid Ahern 1827dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1828dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1829dc599f76SDavid Ahern return true; 1830dc599f76SDavid Ahern 1831dc599f76SDavid Ahern return false; 1832dc599f76SDavid Ahern } 1833dc599f76SDavid Ahern 1834dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1835dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1836dc599f76SDavid Ahern { 1837dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1838dc599f76SDavid Ahern return true; 1839dc599f76SDavid Ahern 1840dc599f76SDavid Ahern return false; 1841dc599f76SDavid Ahern } 1842dc599f76SDavid Ahern 1843dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1844dc599f76SDavid Ahern int master_idx, 1845dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1846dc599f76SDavid Ahern { 1847dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1848dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1849dc599f76SDavid Ahern return true; 1850dc599f76SDavid Ahern 1851dc599f76SDavid Ahern return false; 1852dc599f76SDavid Ahern } 1853dc599f76SDavid Ahern 1854c383edc4SChristian Brauner /** 1855c383edc4SChristian Brauner * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 1856c383edc4SChristian Brauner * @sk: netlink socket 1857c383edc4SChristian Brauner * @netnsid: network namespace identifier 1858c383edc4SChristian Brauner * 1859c383edc4SChristian Brauner * Returns the network namespace identified by netnsid on success or an error 1860c383edc4SChristian Brauner * pointer on failure. 1861c383edc4SChristian Brauner */ 1862c383edc4SChristian Brauner struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 186379e1ad14SJiri Benc { 186479e1ad14SJiri Benc struct net *net; 186579e1ad14SJiri Benc 1866f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 186779e1ad14SJiri Benc if (!net) 186879e1ad14SJiri Benc return ERR_PTR(-EINVAL); 186979e1ad14SJiri Benc 187079e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 187179e1ad14SJiri Benc * the user namespace owning the target net ns. 187279e1ad14SJiri Benc */ 1873f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 187479e1ad14SJiri Benc put_net(net); 187579e1ad14SJiri Benc return ERR_PTR(-EACCES); 187679e1ad14SJiri Benc } 187779e1ad14SJiri Benc return net; 187879e1ad14SJiri Benc } 1879c383edc4SChristian Brauner EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 188079e1ad14SJiri Benc 1881905cf0abSDavid Ahern static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 1882905cf0abSDavid Ahern bool strict_check, struct nlattr **tb, 1883905cf0abSDavid Ahern struct netlink_ext_ack *extack) 1884905cf0abSDavid Ahern { 1885905cf0abSDavid Ahern int hdrlen; 1886905cf0abSDavid Ahern 1887905cf0abSDavid Ahern if (strict_check) { 1888905cf0abSDavid Ahern struct ifinfomsg *ifm; 1889905cf0abSDavid Ahern 1890905cf0abSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 1891905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 1892905cf0abSDavid Ahern return -EINVAL; 1893905cf0abSDavid Ahern } 1894905cf0abSDavid Ahern 1895905cf0abSDavid Ahern ifm = nlmsg_data(nlh); 1896905cf0abSDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 1897905cf0abSDavid Ahern ifm->ifi_change) { 1898905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 1899905cf0abSDavid Ahern return -EINVAL; 1900905cf0abSDavid Ahern } 1901905cf0abSDavid Ahern if (ifm->ifi_index) { 1902905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 1903905cf0abSDavid Ahern return -EINVAL; 1904905cf0abSDavid Ahern } 1905905cf0abSDavid Ahern 1906905cf0abSDavid Ahern return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 1907905cf0abSDavid Ahern ifla_policy, extack); 1908905cf0abSDavid Ahern } 1909905cf0abSDavid Ahern 1910905cf0abSDavid Ahern /* A hack to preserve kernel<->userspace interface. 1911905cf0abSDavid Ahern * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1912905cf0abSDavid Ahern * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1913905cf0abSDavid Ahern * what iproute2 < v3.9.0 used. 1914905cf0abSDavid Ahern * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1915905cf0abSDavid Ahern * attribute, its netlink message is shorter than struct ifinfomsg. 1916905cf0abSDavid Ahern */ 1917905cf0abSDavid Ahern hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 1918905cf0abSDavid Ahern sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1919905cf0abSDavid Ahern 1920905cf0abSDavid Ahern return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack); 1921905cf0abSDavid Ahern } 1922905cf0abSDavid Ahern 1923b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 19241da177e4SLinus Torvalds { 1925905cf0abSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 1926905cf0abSDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 19273b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 192879e1ad14SJiri Benc struct net *tgt_net = net; 19297c28bd0bSEric Dumazet int h, s_h; 19307c28bd0bSEric Dumazet int idx = 0, s_idx; 19311da177e4SLinus Torvalds struct net_device *dev; 19327c28bd0bSEric Dumazet struct hlist_head *head; 1933115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1934115c9b81SGreg Rose u32 ext_filter_mask = 0; 1935dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1936dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1937dc599f76SDavid Ahern int master_idx = 0; 193879e1ad14SJiri Benc int netnsid = -1; 1939905cf0abSDavid Ahern int err, i; 19401da177e4SLinus Torvalds 19417c28bd0bSEric Dumazet s_h = cb->args[0]; 19427c28bd0bSEric Dumazet s_idx = cb->args[1]; 19437c28bd0bSEric Dumazet 1944905cf0abSDavid Ahern err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 1945905cf0abSDavid Ahern if (err < 0) { 1946905cf0abSDavid Ahern if (cb->strict_check) 1947905cf0abSDavid Ahern return err; 1948e5eca6d4SMichal Schmidt 1949905cf0abSDavid Ahern goto walk_entries; 195079e1ad14SJiri Benc } 195179e1ad14SJiri Benc 1952905cf0abSDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 1953905cf0abSDavid Ahern if (!tb[i]) 1954905cf0abSDavid Ahern continue; 1955dc599f76SDavid Ahern 1956905cf0abSDavid Ahern /* new attributes should only be added with strict checking */ 1957905cf0abSDavid Ahern switch (i) { 1958905cf0abSDavid Ahern case IFLA_TARGET_NETNSID: 1959905cf0abSDavid Ahern netnsid = nla_get_s32(tb[i]); 1960905cf0abSDavid Ahern tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 1961905cf0abSDavid Ahern if (IS_ERR(tgt_net)) { 1962905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 1963905cf0abSDavid Ahern return PTR_ERR(tgt_net); 1964905cf0abSDavid Ahern } 1965905cf0abSDavid Ahern break; 1966905cf0abSDavid Ahern case IFLA_EXT_MASK: 1967905cf0abSDavid Ahern ext_filter_mask = nla_get_u32(tb[i]); 1968905cf0abSDavid Ahern break; 1969905cf0abSDavid Ahern case IFLA_MASTER: 1970905cf0abSDavid Ahern master_idx = nla_get_u32(tb[i]); 1971905cf0abSDavid Ahern break; 1972905cf0abSDavid Ahern case IFLA_LINKINFO: 1973905cf0abSDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[i]); 1974905cf0abSDavid Ahern break; 1975905cf0abSDavid Ahern default: 1976905cf0abSDavid Ahern if (cb->strict_check) { 1977905cf0abSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 1978905cf0abSDavid Ahern return -EINVAL; 1979905cf0abSDavid Ahern } 1980905cf0abSDavid Ahern } 1981905cf0abSDavid Ahern } 1982dc599f76SDavid Ahern 1983dc599f76SDavid Ahern if (master_idx || kind_ops) 1984dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1985115c9b81SGreg Rose 1986905cf0abSDavid Ahern walk_entries: 19877c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 19887562f876SPavel Emelianov idx = 0; 198979e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1990cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1991dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 19923f0ae05dSZhang Shengju goto cont; 19931da177e4SLinus Torvalds if (idx < s_idx) 19947562f876SPavel Emelianov goto cont; 199579e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 199679e1ad14SJiri Benc RTM_NEWLINK, 199715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 1998905cf0abSDavid Ahern nlh->nlmsg_seq, 0, flags, 199938e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 200079e1ad14SJiri Benc netnsid); 2001973462bbSDavid Gibson 2002f6c5775fSDavid Ahern if (err < 0) { 2003f6c5775fSDavid Ahern if (likely(skb->len)) 20047c28bd0bSEric Dumazet goto out; 20054e985adaSThomas Graf 2006f6c5775fSDavid Ahern goto out_err; 2007f6c5775fSDavid Ahern } 20087562f876SPavel Emelianov cont: 20097562f876SPavel Emelianov idx++; 20101da177e4SLinus Torvalds } 20117c28bd0bSEric Dumazet } 20127c28bd0bSEric Dumazet out: 2013f6c5775fSDavid Ahern err = skb->len; 2014f6c5775fSDavid Ahern out_err: 20157c28bd0bSEric Dumazet cb->args[1] = idx; 20167c28bd0bSEric Dumazet cb->args[0] = h; 2017d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 2018d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 201979e1ad14SJiri Benc if (netnsid >= 0) 202079e1ad14SJiri Benc put_net(tgt_net); 20211da177e4SLinus Torvalds 2022f6c5775fSDavid Ahern return err; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 2025fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2026fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 2027f7b12606SJiri Pirko { 2028fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 2029f7b12606SJiri Pirko } 2030f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 203157b61080SScott Feldman 203281adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 203381adee47SEric W. Biederman { 203481adee47SEric W. Biederman struct net *net; 203581adee47SEric W. Biederman /* Examine the link attributes and figure out which 203681adee47SEric W. Biederman * network namespace we are talking about. 203781adee47SEric W. Biederman */ 203881adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 203981adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2040f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 2041f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 204281adee47SEric W. Biederman else 204381adee47SEric W. Biederman net = get_net(src_net); 204481adee47SEric W. Biederman return net; 204581adee47SEric W. Biederman } 204681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 204781adee47SEric W. Biederman 20487c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 20497c4f63baSChristian Brauner * examining the link attributes in the following order: 20507c4f63baSChristian Brauner * 20517c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 20527c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 20537e4a8d5aSChristian Brauner * 3. IFLA_TARGET_NETNSID 20547c4f63baSChristian Brauner */ 20557c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 20567c4f63baSChristian Brauner struct nlattr *tb[]) 20577c4f63baSChristian Brauner { 20587c4f63baSChristian Brauner struct net *net; 20597c4f63baSChristian Brauner 20607c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 20617c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 20627c4f63baSChristian Brauner 20637e4a8d5aSChristian Brauner if (!tb[IFLA_TARGET_NETNSID]) 20647c4f63baSChristian Brauner return get_net(src_net); 20657c4f63baSChristian Brauner 20667e4a8d5aSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 20677c4f63baSChristian Brauner if (!net) 20687c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 20697c4f63baSChristian Brauner 20707c4f63baSChristian Brauner return net; 20717c4f63baSChristian Brauner } 20727c4f63baSChristian Brauner 20737c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 20747c4f63baSChristian Brauner struct net *src_net, 20757c4f63baSChristian Brauner struct nlattr *tb[], int cap) 20767c4f63baSChristian Brauner { 20777c4f63baSChristian Brauner struct net *net; 20787c4f63baSChristian Brauner 20797c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 20807c4f63baSChristian Brauner if (IS_ERR(net)) 20817c4f63baSChristian Brauner return net; 20827c4f63baSChristian Brauner 20837c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 20847c4f63baSChristian Brauner put_net(net); 20857c4f63baSChristian Brauner return ERR_PTR(-EPERM); 20867c4f63baSChristian Brauner } 20877c4f63baSChristian Brauner 20887c4f63baSChristian Brauner return net; 20897c4f63baSChristian Brauner } 20907c4f63baSChristian Brauner 20914ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 20924ff66caeSChristian Brauner * potentially referring to different network namespaces. 20934ff66caeSChristian Brauner */ 20944ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 20954ff66caeSChristian Brauner struct netlink_ext_ack *extack, 20964ff66caeSChristian Brauner bool netns_id_only) 20974ff66caeSChristian Brauner { 20984ff66caeSChristian Brauner 20994ff66caeSChristian Brauner if (netns_id_only) { 21004ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 21014ff66caeSChristian Brauner return 0; 21024ff66caeSChristian Brauner 21034ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 21044ff66caeSChristian Brauner return -EOPNOTSUPP; 21054ff66caeSChristian Brauner } 21064ff66caeSChristian Brauner 21077e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 21084ff66caeSChristian Brauner goto invalid_attr; 21094ff66caeSChristian Brauner 21107e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 21114ff66caeSChristian Brauner goto invalid_attr; 21124ff66caeSChristian Brauner 21137e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 21144ff66caeSChristian Brauner goto invalid_attr; 21154ff66caeSChristian Brauner 21164ff66caeSChristian Brauner return 0; 21174ff66caeSChristian Brauner 21184ff66caeSChristian Brauner invalid_attr: 21194ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 21204ff66caeSChristian Brauner return -EINVAL; 21214ff66caeSChristian Brauner } 21224ff66caeSChristian Brauner 21231840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 21241840bb13SThomas Graf { 21251840bb13SThomas Graf if (dev) { 21261840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 21271840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 21281840bb13SThomas Graf return -EINVAL; 21291840bb13SThomas Graf 21301840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 21311840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 21321840bb13SThomas Graf return -EINVAL; 21331840bb13SThomas Graf } 21341840bb13SThomas Graf 2135cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2136cf7afbfeSThomas Graf struct nlattr *af; 2137cf7afbfeSThomas Graf int rem, err; 2138cf7afbfeSThomas Graf 2139cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2140cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2141cf7afbfeSThomas Graf 21425fa85a09SFlorian Westphal rcu_read_lock(); 21435fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 21445fa85a09SFlorian Westphal if (!af_ops) { 21455fa85a09SFlorian Westphal rcu_read_unlock(); 2146cf7afbfeSThomas Graf return -EAFNOSUPPORT; 21475fa85a09SFlorian Westphal } 2148cf7afbfeSThomas Graf 21495fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 21505fa85a09SFlorian Westphal rcu_read_unlock(); 2151cf7afbfeSThomas Graf return -EOPNOTSUPP; 21525fa85a09SFlorian Westphal } 2153cf7afbfeSThomas Graf 2154cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 21556d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 21565fa85a09SFlorian Westphal if (err < 0) { 21575fa85a09SFlorian Westphal rcu_read_unlock(); 2158cf7afbfeSThomas Graf return err; 2159cf7afbfeSThomas Graf } 2160cf7afbfeSThomas Graf } 21615fa85a09SFlorian Westphal 21625fa85a09SFlorian Westphal rcu_read_unlock(); 21635fa85a09SFlorian Westphal } 2164cf7afbfeSThomas Graf } 2165cf7afbfeSThomas Graf 21661840bb13SThomas Graf return 0; 21671840bb13SThomas Graf } 21681840bb13SThomas Graf 2169cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2170cc8e27ccSEli Cohen int guid_type) 2171cc8e27ccSEli Cohen { 2172cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2173cc8e27ccSEli Cohen 2174cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2175cc8e27ccSEli Cohen } 2176cc8e27ccSEli Cohen 2177cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2178cc8e27ccSEli Cohen { 2179cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2180cc8e27ccSEli Cohen return -EOPNOTSUPP; 2181cc8e27ccSEli Cohen 2182cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2183cc8e27ccSEli Cohen } 2184cc8e27ccSEli Cohen 21854f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2186c02db8c6SChris Wright { 2187c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 21884f7d2cdfSDaniel Borkmann int err = -EINVAL; 2189c02db8c6SChris Wright 21904f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 21914f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 21924f7d2cdfSDaniel Borkmann 2193c02db8c6SChris Wright err = -EOPNOTSUPP; 2194c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2195c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2196c02db8c6SChris Wright ivm->mac); 21974f7d2cdfSDaniel Borkmann if (err < 0) 21984f7d2cdfSDaniel Borkmann return err; 2199c02db8c6SChris Wright } 22004f7d2cdfSDaniel Borkmann 22014f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 22024f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 22034f7d2cdfSDaniel Borkmann 2204c02db8c6SChris Wright err = -EOPNOTSUPP; 2205c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 22064f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 220779aab093SMoshe Shemesh ivv->qos, 220879aab093SMoshe Shemesh htons(ETH_P_8021Q)); 220979aab093SMoshe Shemesh if (err < 0) 221079aab093SMoshe Shemesh return err; 221179aab093SMoshe Shemesh } 221279aab093SMoshe Shemesh 221379aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 221479aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 221579aab093SMoshe Shemesh struct nlattr *attr; 221679aab093SMoshe Shemesh int rem, len = 0; 221779aab093SMoshe Shemesh 221879aab093SMoshe Shemesh err = -EOPNOTSUPP; 221979aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 222079aab093SMoshe Shemesh return err; 222179aab093SMoshe Shemesh 222279aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 222379aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 222479aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 222579aab093SMoshe Shemesh return -EINVAL; 222679aab093SMoshe Shemesh } 222779aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 222879aab093SMoshe Shemesh return -EOPNOTSUPP; 222979aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 223079aab093SMoshe Shemesh 223179aab093SMoshe Shemesh len++; 223279aab093SMoshe Shemesh } 2233fa34cd94SArnd Bergmann if (len == 0) 2234fa34cd94SArnd Bergmann return -EINVAL; 2235fa34cd94SArnd Bergmann 223679aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 223779aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 22384f7d2cdfSDaniel Borkmann if (err < 0) 22394f7d2cdfSDaniel Borkmann return err; 2240c02db8c6SChris Wright } 22414f7d2cdfSDaniel Borkmann 22424f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 22434f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2244ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 22454f7d2cdfSDaniel Borkmann 2246c02db8c6SChris Wright err = -EOPNOTSUPP; 2247ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 22484f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 22494f7d2cdfSDaniel Borkmann if (err < 0) 22504f7d2cdfSDaniel Borkmann return err; 22514f7d2cdfSDaniel Borkmann 2252ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2253ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2254ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2255ed616689SSucheta Chakraborty ivf.min_tx_rate, 2256c02db8c6SChris Wright ivt->rate); 22574f7d2cdfSDaniel Borkmann if (err < 0) 22584f7d2cdfSDaniel Borkmann return err; 2259c02db8c6SChris Wright } 22604f7d2cdfSDaniel Borkmann 22614f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 22624f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 22634f7d2cdfSDaniel Borkmann 2264ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2265ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2266ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2267ed616689SSucheta Chakraborty ivt->min_tx_rate, 2268ed616689SSucheta Chakraborty ivt->max_tx_rate); 22694f7d2cdfSDaniel Borkmann if (err < 0) 22704f7d2cdfSDaniel Borkmann return err; 2271ed616689SSucheta Chakraborty } 22724f7d2cdfSDaniel Borkmann 22734f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 22744f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 22754f7d2cdfSDaniel Borkmann 22765f8444a3SGreg Rose err = -EOPNOTSUPP; 22775f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 22785f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 22795f8444a3SGreg Rose ivs->setting); 22804f7d2cdfSDaniel Borkmann if (err < 0) 22814f7d2cdfSDaniel Borkmann return err; 22825f8444a3SGreg Rose } 22834f7d2cdfSDaniel Borkmann 22844f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 22854f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 22864f7d2cdfSDaniel Borkmann 22871d8faf48SRony Efraim err = -EOPNOTSUPP; 22881d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 22891d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 22901d8faf48SRony Efraim ivl->link_state); 22914f7d2cdfSDaniel Borkmann if (err < 0) 22924f7d2cdfSDaniel Borkmann return err; 22931d8faf48SRony Efraim } 22944f7d2cdfSDaniel Borkmann 22954f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 229601a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 229701a3d796SVlad Zolotarov 229801a3d796SVlad Zolotarov err = -EOPNOTSUPP; 22994f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 230001a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 23014f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 230201a3d796SVlad Zolotarov ivrssq_en->setting); 23034f7d2cdfSDaniel Borkmann if (err < 0) 23044f7d2cdfSDaniel Borkmann return err; 230501a3d796SVlad Zolotarov } 23064f7d2cdfSDaniel Borkmann 2307dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2308dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2309dd461d6aSHiroshi Shimamoto 2310dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2311dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2312dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2313dd461d6aSHiroshi Shimamoto if (err < 0) 2314dd461d6aSHiroshi Shimamoto return err; 2315dd461d6aSHiroshi Shimamoto } 2316dd461d6aSHiroshi Shimamoto 2317cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2318cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2319cc8e27ccSEli Cohen 2320cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2321cc8e27ccSEli Cohen return -EOPNOTSUPP; 2322cc8e27ccSEli Cohen 2323cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2324cc8e27ccSEli Cohen } 2325cc8e27ccSEli Cohen 2326cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2327cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2328cc8e27ccSEli Cohen 2329cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2330cc8e27ccSEli Cohen return -EOPNOTSUPP; 2331cc8e27ccSEli Cohen 2332cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2333cc8e27ccSEli Cohen } 2334cc8e27ccSEli Cohen 2335c02db8c6SChris Wright return err; 2336c02db8c6SChris Wright } 2337c02db8c6SChris Wright 233833eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 233933eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2340fbaec0eaSJiri Pirko { 2341898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2342fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2343fbaec0eaSJiri Pirko int err; 2344fbaec0eaSJiri Pirko 2345898e5061SJiri Pirko if (upper_dev) { 2346898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2347fbaec0eaSJiri Pirko return 0; 2348898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2349fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2350898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2351fbaec0eaSJiri Pirko if (err) 2352fbaec0eaSJiri Pirko return err; 2353fbaec0eaSJiri Pirko } else { 2354fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2355fbaec0eaSJiri Pirko } 2356fbaec0eaSJiri Pirko } 2357fbaec0eaSJiri Pirko 2358fbaec0eaSJiri Pirko if (ifindex) { 2359898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2360898e5061SJiri Pirko if (!upper_dev) 2361fbaec0eaSJiri Pirko return -EINVAL; 2362898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2363fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 236433eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2365fbaec0eaSJiri Pirko if (err) 2366fbaec0eaSJiri Pirko return err; 2367fbaec0eaSJiri Pirko } else { 2368fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2369fbaec0eaSJiri Pirko } 2370fbaec0eaSJiri Pirko } 2371fbaec0eaSJiri Pirko return 0; 2372fbaec0eaSJiri Pirko } 2373fbaec0eaSJiri Pirko 237490c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2375ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2376ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 237790f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 237890f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2379ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 238090c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 23810157f60cSPatrick McHardy { 2382d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 23830157f60cSPatrick McHardy int err; 23840157f60cSPatrick McHardy 2385644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2386644c7eebSEric Dumazet if (err < 0) 2387644c7eebSEric Dumazet return err; 2388644c7eebSEric Dumazet 23897e4a8d5aSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 23907c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 23917c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2392d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2393d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2394d8a5ec67SEric W. Biederman goto errout; 2395d8a5ec67SEric W. Biederman } 23967c4f63baSChristian Brauner 2397d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2398d8a5ec67SEric W. Biederman put_net(net); 2399d8a5ec67SEric W. Biederman if (err) 2400d8a5ec67SEric W. Biederman goto errout; 240190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2402d8a5ec67SEric W. Biederman } 2403d8a5ec67SEric W. Biederman 24040157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 24050157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 24060157f60cSPatrick McHardy struct ifmap k_map; 24070157f60cSPatrick McHardy 2408d314774cSStephen Hemminger if (!ops->ndo_set_config) { 24090157f60cSPatrick McHardy err = -EOPNOTSUPP; 24100157f60cSPatrick McHardy goto errout; 24110157f60cSPatrick McHardy } 24120157f60cSPatrick McHardy 24130157f60cSPatrick McHardy if (!netif_device_present(dev)) { 24140157f60cSPatrick McHardy err = -ENODEV; 24150157f60cSPatrick McHardy goto errout; 24160157f60cSPatrick McHardy } 24170157f60cSPatrick McHardy 24180157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 24190157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 24200157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 24210157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 24220157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 24230157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 24240157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 24250157f60cSPatrick McHardy 2426d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 24270157f60cSPatrick McHardy if (err < 0) 24280157f60cSPatrick McHardy goto errout; 24290157f60cSPatrick McHardy 2430ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24310157f60cSPatrick McHardy } 24320157f60cSPatrick McHardy 24330157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 24340157f60cSPatrick McHardy struct sockaddr *sa; 24350157f60cSPatrick McHardy int len; 24360157f60cSPatrick McHardy 2437153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2438153711f9SWANG Cong sizeof(*sa)); 24390157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 24400157f60cSPatrick McHardy if (!sa) { 24410157f60cSPatrick McHardy err = -ENOMEM; 24420157f60cSPatrick McHardy goto errout; 24430157f60cSPatrick McHardy } 24440157f60cSPatrick McHardy sa->sa_family = dev->type; 24450157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 24460157f60cSPatrick McHardy dev->addr_len); 24473a37a963SPetr Machata err = dev_set_mac_address(dev, sa, extack); 24480157f60cSPatrick McHardy kfree(sa); 24490157f60cSPatrick McHardy if (err) 24500157f60cSPatrick McHardy goto errout; 245190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24520157f60cSPatrick McHardy } 24530157f60cSPatrick McHardy 24540157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 24557a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 24560157f60cSPatrick McHardy if (err < 0) 24570157f60cSPatrick McHardy goto errout; 245890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24590157f60cSPatrick McHardy } 24600157f60cSPatrick McHardy 2461cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2462cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2463ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2464cbda10faSVlad Dogaru } 2465cbda10faSVlad Dogaru 24660157f60cSPatrick McHardy /* 24670157f60cSPatrick McHardy * Interface selected by interface index but interface 24680157f60cSPatrick McHardy * name provided implies that a name change has been 24690157f60cSPatrick McHardy * requested. 24700157f60cSPatrick McHardy */ 24710157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 24720157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 24730157f60cSPatrick McHardy if (err < 0) 24740157f60cSPatrick McHardy goto errout; 247590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24760157f60cSPatrick McHardy } 24770157f60cSPatrick McHardy 24780b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 24790b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 24800b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 24810b815a1aSStephen Hemminger if (err < 0) 24820b815a1aSStephen Hemminger goto errout; 2483ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24840b815a1aSStephen Hemminger } 24850b815a1aSStephen Hemminger 24860157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 24870157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2488e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 24890157f60cSPatrick McHardy } 24900157f60cSPatrick McHardy 24910157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 2492567c5e13SPetr Machata err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2493567c5e13SPetr Machata extack); 24945f9021cfSJohannes Berg if (err < 0) 24955f9021cfSJohannes Berg goto errout; 24960157f60cSPatrick McHardy } 24970157f60cSPatrick McHardy 2498fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 249933eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2500fbaec0eaSJiri Pirko if (err) 2501fbaec0eaSJiri Pirko goto errout; 250290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2503fbaec0eaSJiri Pirko } 2504fbaec0eaSJiri Pirko 25059a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 25069a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 25079a57247fSJiri Pirko if (err) 25089a57247fSJiri Pirko goto errout; 250990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25109a57247fSJiri Pirko } 25119a57247fSJiri Pirko 25125d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 25130cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 25145d1180fcSNicolas Dichtel 25156a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 25166a643ddbSCong Wang if (err) 251708294a26SJason Wang goto errout; 25182d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 251908294a26SJason Wang } 25200157f60cSPatrick McHardy 252146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 252246e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 252346e6b992SStephen Hemminger 252446e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 252546e6b992SStephen Hemminger err = -EINVAL; 252646e6b992SStephen Hemminger goto errout; 252746e6b992SStephen Hemminger } 252846e6b992SStephen Hemminger 252946e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 253046e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 253146e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 253246e6b992SStephen Hemminger } 253346e6b992SStephen Hemminger } 253446e6b992SStephen Hemminger 253546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 253646e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 253746e6b992SStephen Hemminger 253846e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 253946e6b992SStephen Hemminger err = -EINVAL; 254046e6b992SStephen Hemminger goto errout; 254146e6b992SStephen Hemminger } 254246e6b992SStephen Hemminger 254346e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 254446e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 254546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 254646e6b992SStephen Hemminger } 254746e6b992SStephen Hemminger } 254846e6b992SStephen Hemminger 25490157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 255093b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 25510157f60cSPatrick McHardy 25520157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 25531889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 25541889b0e7SNicolas Dichtel 25550157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 25561889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2557ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 25581889b0e7SNicolas Dichtel dev->link_mode = value; 255993b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 25600157f60cSPatrick McHardy } 25610157f60cSPatrick McHardy 2562c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 25634f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2564c02db8c6SChris Wright struct nlattr *attr; 2565c02db8c6SChris Wright int rem; 25664f7d2cdfSDaniel Borkmann 2567c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 25684f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 25694f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2570253683bbSDavid Howells err = -EINVAL; 2571c02db8c6SChris Wright goto errout; 2572253683bbSDavid Howells } 25734f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2574fceb6435SJohannes Berg ifla_vf_policy, NULL); 25754f7d2cdfSDaniel Borkmann if (err < 0) 25764f7d2cdfSDaniel Borkmann goto errout; 25774f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2578ebc08a6fSWilliams, Mitch A if (err < 0) 2579ebc08a6fSWilliams, Mitch A goto errout; 2580ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2581ebc08a6fSWilliams, Mitch A } 2582ebc08a6fSWilliams, Mitch A } 25830157f60cSPatrick McHardy err = 0; 25840157f60cSPatrick McHardy 258557b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 258657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 258757b61080SScott Feldman struct nlattr *attr; 258857b61080SScott Feldman int vf; 258957b61080SScott Feldman int rem; 259057b61080SScott Feldman 259157b61080SScott Feldman err = -EOPNOTSUPP; 259257b61080SScott Feldman if (!ops->ndo_set_vf_port) 259357b61080SScott Feldman goto errout; 259457b61080SScott Feldman 259557b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2596035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2597035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2598035d210fSDaniel Borkmann err = -EINVAL; 2599035d210fSDaniel Borkmann goto errout; 2600035d210fSDaniel Borkmann } 2601035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2602fceb6435SJohannes Berg ifla_port_policy, NULL); 260357b61080SScott Feldman if (err < 0) 260457b61080SScott Feldman goto errout; 260557b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 260657b61080SScott Feldman err = -EOPNOTSUPP; 260757b61080SScott Feldman goto errout; 260857b61080SScott Feldman } 260957b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 261057b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 261157b61080SScott Feldman if (err < 0) 261257b61080SScott Feldman goto errout; 2613ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 261457b61080SScott Feldman } 261557b61080SScott Feldman } 261657b61080SScott Feldman err = 0; 261757b61080SScott Feldman 261857b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 261957b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 262057b61080SScott Feldman 262157b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2622fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2623fceb6435SJohannes Berg NULL); 262457b61080SScott Feldman if (err < 0) 262557b61080SScott Feldman goto errout; 262657b61080SScott Feldman 262757b61080SScott Feldman err = -EOPNOTSUPP; 262857b61080SScott Feldman if (ops->ndo_set_vf_port) 262957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 263057b61080SScott Feldman if (err < 0) 263157b61080SScott Feldman goto errout; 2632ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 263357b61080SScott Feldman } 2634f8ff182cSThomas Graf 2635f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2636f8ff182cSThomas Graf struct nlattr *af; 2637f8ff182cSThomas Graf int rem; 2638f8ff182cSThomas Graf 2639f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2640f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2641f8ff182cSThomas Graf 26425fa85a09SFlorian Westphal rcu_read_lock(); 26435fa85a09SFlorian Westphal 2644058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2645f8ff182cSThomas Graf 2646cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 26475fa85a09SFlorian Westphal if (err < 0) { 26485fa85a09SFlorian Westphal rcu_read_unlock(); 2649f8ff182cSThomas Graf goto errout; 26505fa85a09SFlorian Westphal } 2651f8ff182cSThomas Graf 26525fa85a09SFlorian Westphal rcu_read_unlock(); 2653ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2654f8ff182cSThomas Graf } 2655f8ff182cSThomas Graf } 265657b61080SScott Feldman err = 0; 265757b61080SScott Feldman 265888d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 265988d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 266088d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 266188d6378bSAnuradha Karuppiah if (err) 266288d6378bSAnuradha Karuppiah goto errout; 266388d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 266488d6378bSAnuradha Karuppiah } 266588d6378bSAnuradha Karuppiah 2666d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2667d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 266885de8576SDaniel Borkmann u32 xdp_flags = 0; 2669d1fdd913SBrenden Blanco 2670d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2671fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2672d1fdd913SBrenden Blanco if (err < 0) 2673d1fdd913SBrenden Blanco goto errout; 2674d1fdd913SBrenden Blanco 267558038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2676262d8625SBrenden Blanco err = -EINVAL; 2677262d8625SBrenden Blanco goto errout; 2678262d8625SBrenden Blanco } 267985de8576SDaniel Borkmann 268085de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 268185de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 268285de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 268385de8576SDaniel Borkmann err = -EINVAL; 268485de8576SDaniel Borkmann goto errout; 268585de8576SDaniel Borkmann } 2686ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 26870489df9aSDaniel Borkmann err = -EINVAL; 26880489df9aSDaniel Borkmann goto errout; 26890489df9aSDaniel Borkmann } 269085de8576SDaniel Borkmann } 269185de8576SDaniel Borkmann 2692d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2693ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 269485de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 269585de8576SDaniel Borkmann xdp_flags); 2696d1fdd913SBrenden Blanco if (err) 2697d1fdd913SBrenden Blanco goto errout; 2698d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2699d1fdd913SBrenden Blanco } 2700d1fdd913SBrenden Blanco } 2701d1fdd913SBrenden Blanco 27020157f60cSPatrick McHardy errout: 2703ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 270464ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2705ba998906SNicolas Dichtel netdev_state_change(dev); 2706ba998906SNicolas Dichtel 2707ba998906SNicolas Dichtel if (err < 0) 2708e87cc472SJoe 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", 2709e87cc472SJoe Perches dev->name); 2710ba998906SNicolas Dichtel } 27110157f60cSPatrick McHardy 27120157f60cSPatrick McHardy return err; 27130157f60cSPatrick McHardy } 27140157f60cSPatrick McHardy 2715c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2716c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2717da5e0494SThomas Graf { 27183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2719da5e0494SThomas Graf struct ifinfomsg *ifm; 2720da5e0494SThomas Graf struct net_device *dev; 27210157f60cSPatrick McHardy int err; 2722da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 27231da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 27241da177e4SLinus Torvalds 2725c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2726c21ef3e3SDavid Ahern extack); 2727da5e0494SThomas Graf if (err < 0) 2728da5e0494SThomas Graf goto errout; 27291da177e4SLinus Torvalds 27304ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 27314ff66caeSChristian Brauner if (err < 0) 27324ff66caeSChristian Brauner goto errout; 27334ff66caeSChristian Brauner 27345176f91eSThomas Graf if (tb[IFLA_IFNAME]) 27355176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 273678e5b891SPatrick McHardy else 273778e5b891SPatrick McHardy ifname[0] = '\0'; 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds err = -EINVAL; 2740da5e0494SThomas Graf ifm = nlmsg_data(nlh); 274151055be8SPatrick McHardy if (ifm->ifi_index > 0) 2742a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2743da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2744a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2745da5e0494SThomas Graf else 2746da5e0494SThomas Graf goto errout; 27471da177e4SLinus Torvalds 2748da5e0494SThomas Graf if (dev == NULL) { 2749da5e0494SThomas Graf err = -ENODEV; 2750da5e0494SThomas Graf goto errout; 2751da5e0494SThomas Graf } 27521da177e4SLinus Torvalds 2753ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2754da5e0494SThomas Graf errout: 27551da177e4SLinus Torvalds return err; 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 275866400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 275966400d54SWANG Cong { 276066400d54SWANG Cong struct net_device *dev, *aux; 276166400d54SWANG Cong LIST_HEAD(list_kill); 276266400d54SWANG Cong bool found = false; 276366400d54SWANG Cong 276466400d54SWANG Cong if (!group) 276566400d54SWANG Cong return -EPERM; 276666400d54SWANG Cong 276766400d54SWANG Cong for_each_netdev(net, dev) { 276866400d54SWANG Cong if (dev->group == group) { 276966400d54SWANG Cong const struct rtnl_link_ops *ops; 277066400d54SWANG Cong 277166400d54SWANG Cong found = true; 277266400d54SWANG Cong ops = dev->rtnl_link_ops; 277366400d54SWANG Cong if (!ops || !ops->dellink) 277466400d54SWANG Cong return -EOPNOTSUPP; 277566400d54SWANG Cong } 277666400d54SWANG Cong } 277766400d54SWANG Cong 277866400d54SWANG Cong if (!found) 277966400d54SWANG Cong return -ENODEV; 278066400d54SWANG Cong 278166400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 278266400d54SWANG Cong if (dev->group == group) { 278366400d54SWANG Cong const struct rtnl_link_ops *ops; 278466400d54SWANG Cong 278566400d54SWANG Cong ops = dev->rtnl_link_ops; 278666400d54SWANG Cong ops->dellink(dev, &list_kill); 278766400d54SWANG Cong } 278866400d54SWANG Cong } 278966400d54SWANG Cong unregister_netdevice_many(&list_kill); 279066400d54SWANG Cong 279166400d54SWANG Cong return 0; 279266400d54SWANG Cong } 279366400d54SWANG Cong 2794614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2795614732eaSThomas Graf { 2796614732eaSThomas Graf const struct rtnl_link_ops *ops; 2797614732eaSThomas Graf LIST_HEAD(list_kill); 2798614732eaSThomas Graf 2799614732eaSThomas Graf ops = dev->rtnl_link_ops; 2800614732eaSThomas Graf if (!ops || !ops->dellink) 2801614732eaSThomas Graf return -EOPNOTSUPP; 2802614732eaSThomas Graf 2803614732eaSThomas Graf ops->dellink(dev, &list_kill); 2804614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2805614732eaSThomas Graf 2806614732eaSThomas Graf return 0; 2807614732eaSThomas Graf } 2808614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2809614732eaSThomas Graf 2810c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2811c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 281238f7b870SPatrick McHardy { 28133b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2814b61ad68aSChristian Brauner struct net *tgt_net = net; 2815b61ad68aSChristian Brauner struct net_device *dev = NULL; 281638f7b870SPatrick McHardy struct ifinfomsg *ifm; 281738f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 281838f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 281938f7b870SPatrick McHardy int err; 2820b61ad68aSChristian Brauner int netnsid = -1; 282138f7b870SPatrick McHardy 2822c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 282338f7b870SPatrick McHardy if (err < 0) 282438f7b870SPatrick McHardy return err; 282538f7b870SPatrick McHardy 28264ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 28274ff66caeSChristian Brauner if (err < 0) 28284ff66caeSChristian Brauner return err; 28294ff66caeSChristian Brauner 283038f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 283138f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 283238f7b870SPatrick McHardy 28337e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 28347e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 2835c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 2836b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2837b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2838b61ad68aSChristian Brauner } 2839b61ad68aSChristian Brauner 2840b61ad68aSChristian Brauner err = -EINVAL; 284138f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 284238f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2843b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 284438f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2845b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 284666400d54SWANG Cong else if (tb[IFLA_GROUP]) 2847b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 284838f7b870SPatrick McHardy else 2849b61ad68aSChristian Brauner goto out; 285038f7b870SPatrick McHardy 2851b61ad68aSChristian Brauner if (!dev) { 2852b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2853b61ad68aSChristian Brauner err = -ENODEV; 285438f7b870SPatrick McHardy 2855b61ad68aSChristian Brauner goto out; 2856b61ad68aSChristian Brauner } 2857b61ad68aSChristian Brauner 2858b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2859b61ad68aSChristian Brauner 2860b61ad68aSChristian Brauner out: 2861b61ad68aSChristian Brauner if (netnsid >= 0) 2862b61ad68aSChristian Brauner put_net(tgt_net); 2863b61ad68aSChristian Brauner 2864b61ad68aSChristian Brauner return err; 286538f7b870SPatrick McHardy } 286638f7b870SPatrick McHardy 28673729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 28683729d502SPatrick McHardy { 28693729d502SPatrick McHardy unsigned int old_flags; 28703729d502SPatrick McHardy int err; 28713729d502SPatrick McHardy 28723729d502SPatrick McHardy old_flags = dev->flags; 28733729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 28746d040321SPetr Machata err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 28756d040321SPetr Machata NULL); 28763729d502SPatrick McHardy if (err < 0) 28773729d502SPatrick McHardy return err; 28783729d502SPatrick McHardy } 28793729d502SPatrick McHardy 28808d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 288156a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 28828d356b89SRoopa Prabhu } else { 28833729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2884a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 28858d356b89SRoopa Prabhu } 28863729d502SPatrick McHardy return 0; 28873729d502SPatrick McHardy } 28883729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 28893729d502SPatrick McHardy 2890d0522f1cSDavid Ahern struct net_device *rtnl_create_link(struct net *net, const char *ifname, 2891d0522f1cSDavid Ahern unsigned char name_assign_type, 2892d0522f1cSDavid Ahern const struct rtnl_link_ops *ops, 2893d0522f1cSDavid Ahern struct nlattr *tb[], 2894d0522f1cSDavid Ahern struct netlink_ext_ack *extack) 2895e7199288SPavel Emelianov { 2896e7199288SPavel Emelianov struct net_device *dev; 2897d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2898d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2899e7199288SPavel Emelianov 290076ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 290176ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 290276ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2903d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 290476ff5cc9SJiri Pirko 290576ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 290676ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 290776ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2908d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2909efacb309Sstephen hemminger 2910d0522f1cSDavid Ahern if (num_tx_queues < 1 || num_tx_queues > 4096) { 2911d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 29120e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2913d0522f1cSDavid Ahern } 29140e1d6ecaSEric Dumazet 2915d0522f1cSDavid Ahern if (num_rx_queues < 1 || num_rx_queues > 4096) { 2916d0522f1cSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 29170e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2918d0522f1cSDavid Ahern } 29190e1d6ecaSEric Dumazet 29205517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2921c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2922e7199288SPavel Emelianov if (!dev) 2923d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2924e7199288SPavel Emelianov 292581adee47SEric W. Biederman dev_net_set(dev, net); 292681adee47SEric W. Biederman dev->rtnl_link_ops = ops; 29273729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 292881adee47SEric W. Biederman 2929e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2930e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 29312afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2932e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2933e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 29342afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 29352afb9b53SJiri Pirko } 2936e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2937e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2938e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2939e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2940e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2941e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 294293b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2943e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2944e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2945ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2946ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 294746e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 294846e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 294946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2950a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2951e7199288SPavel Emelianov 2952e7199288SPavel Emelianov return dev; 2953e7199288SPavel Emelianov } 2954e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2955e7199288SPavel Emelianov 295690f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 295790f62cf3SEric W. Biederman struct net *net, int group, 2958e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2959ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2960e7ed828fSVlad Dogaru struct nlattr **tb) 2961e7ed828fSVlad Dogaru { 2962d079535dSWANG Cong struct net_device *dev, *aux; 2963e7ed828fSVlad Dogaru int err; 2964e7ed828fSVlad Dogaru 2965d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2966e7ed828fSVlad Dogaru if (dev->group == group) { 2967ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2968e7ed828fSVlad Dogaru if (err < 0) 2969e7ed828fSVlad Dogaru return err; 2970e7ed828fSVlad Dogaru } 2971e7ed828fSVlad Dogaru } 2972e7ed828fSVlad Dogaru 2973e7ed828fSVlad Dogaru return 0; 2974e7ed828fSVlad Dogaru } 2975e7ed828fSVlad Dogaru 2976a2939745SJakub Kicinski static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2977a2939745SJakub Kicinski struct nlattr **attr, struct netlink_ext_ack *extack) 297838f7b870SPatrick McHardy { 2979420d0318SJakub Kicinski struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2980420d0318SJakub Kicinski unsigned char name_assign_type = NET_NAME_USER; 2981420d0318SJakub Kicinski struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 2982420d0318SJakub Kicinski const struct rtnl_link_ops *m_ops = NULL; 2983420d0318SJakub Kicinski struct net_device *master_dev = NULL; 29843b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 298538f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 298638f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX + 1]; 2987420d0318SJakub Kicinski struct net *dest_net, *link_net; 2988420d0318SJakub Kicinski struct nlattr **slave_data; 2989420d0318SJakub Kicinski char kind[MODULE_NAME_LEN]; 2990420d0318SJakub Kicinski struct net_device *dev; 2991420d0318SJakub Kicinski struct ifinfomsg *ifm; 2992420d0318SJakub Kicinski char ifname[IFNAMSIZ]; 2993420d0318SJakub Kicinski struct nlattr **data; 299438f7b870SPatrick McHardy int err; 299538f7b870SPatrick McHardy 299695a5afcaSJohannes Berg #ifdef CONFIG_MODULES 299738f7b870SPatrick McHardy replay: 29988072f085SThomas Graf #endif 2999c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 300038f7b870SPatrick McHardy if (err < 0) 300138f7b870SPatrick McHardy return err; 300238f7b870SPatrick McHardy 30034ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 30044ff66caeSChristian Brauner if (err < 0) 30054ff66caeSChristian Brauner return err; 30064ff66caeSChristian Brauner 300738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 300838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 300938f7b870SPatrick McHardy else 301038f7b870SPatrick McHardy ifname[0] = '\0'; 301138f7b870SPatrick McHardy 301238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 301338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3014881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3015e7ed828fSVlad Dogaru else { 3016e7ed828fSVlad Dogaru if (ifname[0]) 3017881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 301838f7b870SPatrick McHardy else 301938f7b870SPatrick McHardy dev = NULL; 3020e7ed828fSVlad Dogaru } 302138f7b870SPatrick McHardy 3022ba7d49b1SJiri Pirko if (dev) { 3023ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3024ba7d49b1SJiri Pirko if (master_dev) 3025ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3026ba7d49b1SJiri Pirko } 3027ba7d49b1SJiri Pirko 3028e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 3029e0d087afSEric Dumazet if (err < 0) 30301840bb13SThomas Graf return err; 30311840bb13SThomas Graf 303238f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 303338f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 3034fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 3035fceb6435SJohannes Berg NULL); 303638f7b870SPatrick McHardy if (err < 0) 303738f7b870SPatrick McHardy return err; 303838f7b870SPatrick McHardy } else 303938f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 304038f7b870SPatrick McHardy 304138f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 304238f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 304338f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 304438f7b870SPatrick McHardy } else { 304538f7b870SPatrick McHardy kind[0] = '\0'; 304638f7b870SPatrick McHardy ops = NULL; 304738f7b870SPatrick McHardy } 304838f7b870SPatrick McHardy 3049420d0318SJakub Kicinski data = NULL; 305038f7b870SPatrick McHardy if (ops) { 3051ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3052ccf8dbcdSKees Cook return -EINVAL; 3053ccf8dbcdSKees Cook 305438f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 305538f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 305638f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 305768d57f3bSDavid Ahern ops->policy, extack); 305838f7b870SPatrick McHardy if (err < 0) 305938f7b870SPatrick McHardy return err; 306038f7b870SPatrick McHardy data = attr; 306138f7b870SPatrick McHardy } 306238f7b870SPatrick McHardy if (ops->validate) { 3063a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 306438f7b870SPatrick McHardy if (err < 0) 306538f7b870SPatrick McHardy return err; 306638f7b870SPatrick McHardy } 306738f7b870SPatrick McHardy } 306838f7b870SPatrick McHardy 3069420d0318SJakub Kicinski slave_data = NULL; 3070ba7d49b1SJiri Pirko if (m_ops) { 3071af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3072ccf8dbcdSKees Cook return -EINVAL; 3073ccf8dbcdSKees Cook 3074ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3075ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3076420d0318SJakub Kicinski err = nla_parse_nested(slave_attr, m_ops->slave_maxtype, 3077ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 3078420d0318SJakub Kicinski m_ops->slave_policy, extack); 3079ba7d49b1SJiri Pirko if (err < 0) 3080ba7d49b1SJiri Pirko return err; 3081ba7d49b1SJiri Pirko slave_data = slave_attr; 3082ba7d49b1SJiri Pirko } 3083ba7d49b1SJiri Pirko } 3084ba7d49b1SJiri Pirko 308538f7b870SPatrick McHardy if (dev) { 308690c325e3SNicolas Dichtel int status = 0; 308738f7b870SPatrick McHardy 308838f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 308938f7b870SPatrick McHardy return -EEXIST; 309038f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 309138f7b870SPatrick McHardy return -EOPNOTSUPP; 309238f7b870SPatrick McHardy 309338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 309438f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 309538f7b870SPatrick McHardy !ops->changelink) 309638f7b870SPatrick McHardy return -EOPNOTSUPP; 309738f7b870SPatrick McHardy 3098ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 309938f7b870SPatrick McHardy if (err < 0) 310038f7b870SPatrick McHardy return err; 3101ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 310238f7b870SPatrick McHardy } 310338f7b870SPatrick McHardy 3104ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3105ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3106ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3107ba7d49b1SJiri Pirko 3108420d0318SJakub Kicinski err = m_ops->slave_changelink(master_dev, dev, tb, 3109420d0318SJakub Kicinski slave_data, extack); 3110ba7d49b1SJiri Pirko if (err < 0) 3111ba7d49b1SJiri Pirko return err; 3112ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3113ba7d49b1SJiri Pirko } 3114ba7d49b1SJiri Pirko 3115420d0318SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 311638f7b870SPatrick McHardy } 311738f7b870SPatrick McHardy 3118ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3119ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 312090f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3121ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3122ddf9f970SJakub Kicinski ifm, extack, tb); 312338f7b870SPatrick McHardy return -ENODEV; 3124ffa934f1SPatrick McHardy } 312538f7b870SPatrick McHardy 3126160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 312738f7b870SPatrick McHardy return -EOPNOTSUPP; 312838f7b870SPatrick McHardy 312938f7b870SPatrick McHardy if (!ops) { 313095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 313138f7b870SPatrick McHardy if (kind[0]) { 313238f7b870SPatrick McHardy __rtnl_unlock(); 313338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 313438f7b870SPatrick McHardy rtnl_lock(); 313538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 313638f7b870SPatrick McHardy if (ops) 313738f7b870SPatrick McHardy goto replay; 313838f7b870SPatrick McHardy } 313938f7b870SPatrick McHardy #endif 314068d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown device type"); 314138f7b870SPatrick McHardy return -EOPNOTSUPP; 314238f7b870SPatrick McHardy } 314338f7b870SPatrick McHardy 3144b0ab2fabSJiri Pirko if (!ops->setup) 3145b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3146b0ab2fabSJiri Pirko 31475517750fSTom Gundersen if (!ifname[0]) { 314838f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 31495517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 31505517750fSTom Gundersen } 315138f7b870SPatrick McHardy 31525bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 315313ad1774SEric W. Biederman if (IS_ERR(dest_net)) 315413ad1774SEric W. Biederman return PTR_ERR(dest_net); 315513ad1774SEric W. Biederman 3156317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3157317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3158317f4810SNicolas Dichtel 3159317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3160317f4810SNicolas Dichtel if (!link_net) { 316168d57f3bSDavid Ahern NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3162317f4810SNicolas Dichtel err = -EINVAL; 3163317f4810SNicolas Dichtel goto out; 3164317f4810SNicolas Dichtel } 316506615bedSEric W. Biederman err = -EPERM; 316606615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 316706615bedSEric W. Biederman goto out; 3168420d0318SJakub Kicinski } else { 3169420d0318SJakub Kicinski link_net = NULL; 3170317f4810SNicolas Dichtel } 3171317f4810SNicolas Dichtel 3172317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3173d0522f1cSDavid Ahern name_assign_type, ops, tb, extack); 31749c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3175e7199288SPavel Emelianov err = PTR_ERR(dev); 31769c7dafbfSPavel Emelyanov goto out; 31779c7dafbfSPavel Emelyanov } 31789c7dafbfSPavel Emelyanov 31799c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 31809c7dafbfSPavel Emelyanov 31810e0eee24SCong Wang if (ops->newlink) { 3182420d0318SJakub Kicinski err = ops->newlink(link_net ? : net, dev, tb, data, extack); 31830e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3184e51fb152SCong Wang * and unregister it on failure after registration 3185e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 31860e0eee24SCong Wang */ 3187e51fb152SCong Wang if (err < 0) { 3188e51fb152SCong Wang /* If device is not registered at all, free it now */ 3189e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3190e51fb152SCong Wang free_netdev(dev); 31910e0eee24SCong Wang goto out; 3192e51fb152SCong Wang } 31930e0eee24SCong Wang } else { 31942d85cba2SPatrick McHardy err = register_netdevice(dev); 3195fce9b9beSDan Carpenter if (err < 0) { 319638f7b870SPatrick McHardy free_netdev(dev); 31973729d502SPatrick McHardy goto out; 3198fce9b9beSDan Carpenter } 31990e0eee24SCong Wang } 32003729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 320143638900SDavid S. Miller if (err < 0) 320243638900SDavid S. Miller goto out_unregister; 320343638900SDavid S. Miller if (link_net) { 320443638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 320543638900SDavid S. Miller if (err < 0) 320643638900SDavid S. Miller goto out_unregister; 320743638900SDavid S. Miller } 3208160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 3209420d0318SJakub Kicinski err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3210160ca014STheuns Verwoerd if (err) 3211160ca014STheuns Verwoerd goto out_unregister; 3212160ca014STheuns Verwoerd } 321343638900SDavid S. Miller out: 321443638900SDavid S. Miller if (link_net) 321543638900SDavid S. Miller put_net(link_net); 321643638900SDavid S. Miller put_net(dest_net); 321743638900SDavid S. Miller return err; 321843638900SDavid S. Miller out_unregister: 32197afb8886SWANG Cong if (ops->newlink) { 32207afb8886SWANG Cong LIST_HEAD(list_kill); 32217afb8886SWANG Cong 32227afb8886SWANG Cong ops->dellink(dev, &list_kill); 32237afb8886SWANG Cong unregister_netdevice_many(&list_kill); 32247afb8886SWANG Cong } else { 32253729d502SPatrick McHardy unregister_netdevice(dev); 32267afb8886SWANG Cong } 3227317f4810SNicolas Dichtel goto out; 3228317f4810SNicolas Dichtel } 322938f7b870SPatrick McHardy 3230a2939745SJakub Kicinski static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3231a2939745SJakub Kicinski struct netlink_ext_ack *extack) 3232a2939745SJakub Kicinski { 3233a2939745SJakub Kicinski struct nlattr **attr; 3234a2939745SJakub Kicinski int ret; 3235a2939745SJakub Kicinski 3236a2939745SJakub Kicinski attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3237a2939745SJakub Kicinski if (!attr) 3238a2939745SJakub Kicinski return -ENOMEM; 3239a2939745SJakub Kicinski 3240a2939745SJakub Kicinski ret = __rtnl_newlink(skb, nlh, attr, extack); 3241a2939745SJakub Kicinski kfree(attr); 3242a2939745SJakub Kicinski return ret; 3243a2939745SJakub Kicinski } 3244a2939745SJakub Kicinski 3245c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3246c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3247711e2c33SJean Tourrilhes { 32483b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 324979e1ad14SJiri Benc struct net *tgt_net = net; 3250b60c5115SThomas Graf struct ifinfomsg *ifm; 3251a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3252b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3253b60c5115SThomas Graf struct net_device *dev = NULL; 3254b60c5115SThomas Graf struct sk_buff *nskb; 325579e1ad14SJiri Benc int netnsid = -1; 3256339bf98fSThomas Graf int err; 3257115c9b81SGreg Rose u32 ext_filter_mask = 0; 3258711e2c33SJean Tourrilhes 3259c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3260b60c5115SThomas Graf if (err < 0) 32619918f230SEric Sesterhenn return err; 3262b60c5115SThomas Graf 32634ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32644ff66caeSChristian Brauner if (err < 0) 32654ff66caeSChristian Brauner return err; 32664ff66caeSChristian Brauner 32677e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32687e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3269c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 327079e1ad14SJiri Benc if (IS_ERR(tgt_net)) 327179e1ad14SJiri Benc return PTR_ERR(tgt_net); 327279e1ad14SJiri Benc } 327379e1ad14SJiri Benc 3274a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3275a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3276a3d12891SEric Dumazet 3277115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3278115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3279115c9b81SGreg Rose 328079e1ad14SJiri Benc err = -EINVAL; 3281b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3282a3d12891SEric Dumazet if (ifm->ifi_index > 0) 328379e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3284a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 328579e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3286a3d12891SEric Dumazet else 328779e1ad14SJiri Benc goto out; 3288b60c5115SThomas Graf 328979e1ad14SJiri Benc err = -ENODEV; 3290a3d12891SEric Dumazet if (dev == NULL) 329179e1ad14SJiri Benc goto out; 3292a3d12891SEric Dumazet 329379e1ad14SJiri Benc err = -ENOBUFS; 3294115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3295a3d12891SEric Dumazet if (nskb == NULL) 329679e1ad14SJiri Benc goto out; 3297711e2c33SJean Tourrilhes 329879e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 329979e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 330079e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 330138e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 330226932566SPatrick McHardy if (err < 0) { 330326932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 330426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 330526932566SPatrick McHardy kfree_skb(nskb); 3306a3d12891SEric Dumazet } else 330715e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 330879e1ad14SJiri Benc out: 330979e1ad14SJiri Benc if (netnsid >= 0) 331079e1ad14SJiri Benc put_net(tgt_net); 3311b60c5115SThomas Graf 3312711e2c33SJean Tourrilhes return err; 3313711e2c33SJean Tourrilhes } 3314711e2c33SJean Tourrilhes 3315115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3316c7ac8679SGreg Rose { 3317115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3318115c9b81SGreg Rose struct net_device *dev; 3319115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3320115c9b81SGreg Rose u32 ext_filter_mask = 0; 3321115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3322e5eca6d4SMichal Schmidt int hdrlen; 3323115c9b81SGreg Rose 3324e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3325e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3326e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3327e5eca6d4SMichal Schmidt 3328fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3329115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3330115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3331a4b64fbeSEric Dumazet } 3332115c9b81SGreg Rose 3333115c9b81SGreg Rose if (!ext_filter_mask) 3334115c9b81SGreg Rose return NLMSG_GOODSIZE; 3335115c9b81SGreg Rose /* 3336115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3337115c9b81SGreg Rose * buffer size based upon the filter mask. 3338115c9b81SGreg Rose */ 33396853dd48SFlorian Westphal rcu_read_lock(); 33406853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3341115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3342115c9b81SGreg Rose if_nlmsg_size(dev, 3343115c9b81SGreg Rose ext_filter_mask)); 3344115c9b81SGreg Rose } 33456853dd48SFlorian Westphal rcu_read_unlock(); 3346115c9b81SGreg Rose 334793af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3348c7ac8679SGreg Rose } 3349c7ac8679SGreg Rose 335042bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 33511da177e4SLinus Torvalds { 33521da177e4SLinus Torvalds int idx; 33531da177e4SLinus Torvalds int s_idx = cb->family; 335487ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 3355c63586dcSDavid Ahern int ret = 0; 33561da177e4SLinus Torvalds 33571da177e4SLinus Torvalds if (s_idx == 0) 33581da177e4SLinus Torvalds s_idx = 1; 33596853dd48SFlorian Westphal 336025239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3361addf9b90SFlorian Westphal struct rtnl_link **tab; 3362addf9b90SFlorian Westphal struct rtnl_link *link; 33636853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 33646853dd48SFlorian Westphal 33651da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 33661da177e4SLinus Torvalds continue; 33676853dd48SFlorian Westphal 3368addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 33691da177e4SLinus Torvalds continue; 33706853dd48SFlorian Westphal 3371addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3372addf9b90SFlorian Westphal if (!tab) 3373addf9b90SFlorian Westphal continue; 3374addf9b90SFlorian Westphal 3375addf9b90SFlorian Westphal link = tab[type]; 3376addf9b90SFlorian Westphal if (!link) 3377addf9b90SFlorian Westphal continue; 3378addf9b90SFlorian Westphal 3379addf9b90SFlorian Westphal dumpit = link->dumpit; 33806853dd48SFlorian Westphal if (!dumpit) 33816853dd48SFlorian Westphal continue; 33826853dd48SFlorian Westphal 33830465277fSNicolas Dichtel if (idx > s_idx) { 33841da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 33850465277fSNicolas Dichtel cb->prev_seq = 0; 33860465277fSNicolas Dichtel cb->seq = 0; 33870465277fSNicolas Dichtel } 3388c63586dcSDavid Ahern ret = dumpit(skb, cb); 33895e1acb4aSAlexey Kodanev if (ret) 33901da177e4SLinus Torvalds break; 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds cb->family = idx; 33931da177e4SLinus Torvalds 3394c63586dcSDavid Ahern return skb->len ? : ret; 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds 3397395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 33983d3ea5afSVlad Yasevich unsigned int change, 339938e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 340038e01b30SNicolas Dichtel int new_ifindex) 34011da177e4SLinus Torvalds { 3402c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 34031da177e4SLinus Torvalds struct sk_buff *skb; 34040ec6d3f4SThomas Graf int err = -ENOBUFS; 3405c7ac8679SGreg Rose size_t if_info_size; 34061da177e4SLinus Torvalds 34077f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 34080ec6d3f4SThomas Graf if (skb == NULL) 34090ec6d3f4SThomas Graf goto errout; 34101da177e4SLinus Torvalds 341179e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 341279e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 341338e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 341426932566SPatrick McHardy if (err < 0) { 341526932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 341626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 341726932566SPatrick McHardy kfree_skb(skb); 341826932566SPatrick McHardy goto errout; 341926932566SPatrick McHardy } 3420395eea6cSMahesh Bandewar return skb; 34210ec6d3f4SThomas Graf errout: 34220ec6d3f4SThomas Graf if (err < 0) 34234b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3424395eea6cSMahesh Bandewar return NULL; 3425395eea6cSMahesh Bandewar } 3426395eea6cSMahesh Bandewar 3427395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3428395eea6cSMahesh Bandewar { 3429395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3430395eea6cSMahesh Bandewar 3431395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3432395eea6cSMahesh Bandewar } 3433395eea6cSMahesh Bandewar 34343d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 34353d3ea5afSVlad Yasevich unsigned int change, u32 event, 343638e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3437395eea6cSMahesh Bandewar { 3438395eea6cSMahesh Bandewar struct sk_buff *skb; 3439395eea6cSMahesh Bandewar 3440ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3441ed2a80abSNicolas Dichtel return; 3442ed2a80abSNicolas Dichtel 344338e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 344438e01b30SNicolas Dichtel new_ifindex); 3445395eea6cSMahesh Bandewar if (skb) 3446395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 34471da177e4SLinus Torvalds } 34483d3ea5afSVlad Yasevich 34493d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 34503d3ea5afSVlad Yasevich gfp_t flags) 34513d3ea5afSVlad Yasevich { 345238e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 345338e01b30SNicolas Dichtel NULL, 0); 34543d3ea5afSVlad Yasevich } 34551da177e4SLinus Torvalds 34566621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 345738e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 34586621dd29SNicolas Dichtel { 34596621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 346038e01b30SNicolas Dichtel new_nsid, new_ifindex); 34616621dd29SNicolas Dichtel } 34626621dd29SNicolas Dichtel 3463d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3464d83b0603SJohn Fastabend struct net_device *dev, 34651e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 34661c104a6bSNicolas Dichtel int type, unsigned int flags, 3467b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3468d83b0603SJohn Fastabend { 3469d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3470d83b0603SJohn Fastabend struct ndmsg *ndm; 3471d83b0603SJohn Fastabend 34721c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3473d83b0603SJohn Fastabend if (!nlh) 3474d83b0603SJohn Fastabend return -EMSGSIZE; 3475d83b0603SJohn Fastabend 3476d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3477d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3478d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3479d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3480d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3481d83b0603SJohn Fastabend ndm->ndm_type = 0; 3482d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3483b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3484d83b0603SJohn Fastabend 3485d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3486d83b0603SJohn Fastabend goto nla_put_failure; 34871e53d5bbSHubert Sokolowski if (vid) 34881e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 34891e53d5bbSHubert Sokolowski goto nla_put_failure; 3490d83b0603SJohn Fastabend 3491053c095aSJohannes Berg nlmsg_end(skb, nlh); 3492053c095aSJohannes Berg return 0; 3493d83b0603SJohn Fastabend 3494d83b0603SJohn Fastabend nla_put_failure: 3495d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3496d83b0603SJohn Fastabend return -EMSGSIZE; 3497d83b0603SJohn Fastabend } 3498d83b0603SJohn Fastabend 34993ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 35003ff661c3SJohn Fastabend { 3501f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3502f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3503f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3504f82ef3e1SSabrina Dubroca 0; 35053ff661c3SJohn Fastabend } 35063ff661c3SJohn Fastabend 3507b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3508b3379041SHubert Sokolowski u16 ndm_state) 35093ff661c3SJohn Fastabend { 35103ff661c3SJohn Fastabend struct net *net = dev_net(dev); 35113ff661c3SJohn Fastabend struct sk_buff *skb; 35123ff661c3SJohn Fastabend int err = -ENOBUFS; 35133ff661c3SJohn Fastabend 35143ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 35153ff661c3SJohn Fastabend if (!skb) 35163ff661c3SJohn Fastabend goto errout; 35173ff661c3SJohn Fastabend 35181e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3519b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 35203ff661c3SJohn Fastabend if (err < 0) { 35213ff661c3SJohn Fastabend kfree_skb(skb); 35223ff661c3SJohn Fastabend goto errout; 35233ff661c3SJohn Fastabend } 35243ff661c3SJohn Fastabend 35253ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 35263ff661c3SJohn Fastabend return; 35273ff661c3SJohn Fastabend errout: 35283ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 35293ff661c3SJohn Fastabend } 35303ff661c3SJohn Fastabend 3531090096bfSVlad Yasevich /** 3532090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3533090096bfSVlad Yasevich */ 3534090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3535090096bfSVlad Yasevich struct nlattr *tb[], 3536090096bfSVlad Yasevich struct net_device *dev, 3537f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3538090096bfSVlad Yasevich u16 flags) 3539090096bfSVlad Yasevich { 3540090096bfSVlad Yasevich int err = -EINVAL; 3541090096bfSVlad Yasevich 3542090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3543090096bfSVlad Yasevich * implement its own handler for this. 3544090096bfSVlad Yasevich */ 3545090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3546090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3547090096bfSVlad Yasevich return err; 3548090096bfSVlad Yasevich } 3549090096bfSVlad Yasevich 355065891feaSOr Gerlitz if (vid) { 355165891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 355265891feaSOr Gerlitz return err; 355365891feaSOr Gerlitz } 355465891feaSOr Gerlitz 3555090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3556090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3557090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3558090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3559090096bfSVlad Yasevich 3560090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3561090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3562090096bfSVlad Yasevich err = 0; 3563090096bfSVlad Yasevich 3564090096bfSVlad Yasevich return err; 3565090096bfSVlad Yasevich } 3566090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3567090096bfSVlad Yasevich 3568b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3569b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3570f6f6424bSJiri Pirko { 3571f6f6424bSJiri Pirko u16 vid = 0; 3572f6f6424bSJiri Pirko 3573f6f6424bSJiri Pirko if (vlan_attr) { 3574f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3575b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3576f6f6424bSJiri Pirko return -EINVAL; 3577f6f6424bSJiri Pirko } 3578f6f6424bSJiri Pirko 3579f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3580f6f6424bSJiri Pirko 3581f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3582b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3583f6f6424bSJiri Pirko return -EINVAL; 3584f6f6424bSJiri Pirko } 3585f6f6424bSJiri Pirko } 3586f6f6424bSJiri Pirko *p_vid = vid; 3587f6f6424bSJiri Pirko return 0; 3588f6f6424bSJiri Pirko } 3589f6f6424bSJiri Pirko 3590c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3591c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 359277162022SJohn Fastabend { 359377162022SJohn Fastabend struct net *net = sock_net(skb->sk); 359477162022SJohn Fastabend struct ndmsg *ndm; 359577162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 359677162022SJohn Fastabend struct net_device *dev; 359777162022SJohn Fastabend u8 *addr; 3598f6f6424bSJiri Pirko u16 vid; 359977162022SJohn Fastabend int err; 360077162022SJohn Fastabend 3601c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 360277162022SJohn Fastabend if (err < 0) 360377162022SJohn Fastabend return err; 360477162022SJohn Fastabend 360577162022SJohn Fastabend ndm = nlmsg_data(nlh); 360677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3607b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 360877162022SJohn Fastabend return -EINVAL; 360977162022SJohn Fastabend } 361077162022SJohn Fastabend 361177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 361277162022SJohn Fastabend if (dev == NULL) { 3613b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 361477162022SJohn Fastabend return -ENODEV; 361577162022SJohn Fastabend } 361677162022SJohn Fastabend 361777162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3618b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 361977162022SJohn Fastabend return -EINVAL; 362077162022SJohn Fastabend } 362177162022SJohn Fastabend 3622da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 3623da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 3624da715775SIdo Schimmel return -EINVAL; 3625da715775SIdo Schimmel } 3626da715775SIdo Schimmel 362777162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 362877162022SJohn Fastabend 3629b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3630f6f6424bSJiri Pirko if (err) 3631f6f6424bSJiri Pirko return err; 3632f6f6424bSJiri Pirko 363377162022SJohn Fastabend err = -EOPNOTSUPP; 363477162022SJohn Fastabend 363577162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 363677162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 363777162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3638898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3639898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3640898e5061SJiri Pirko 3641f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 364287b0984eSPetr Machata nlh->nlmsg_flags, extack); 364377162022SJohn Fastabend if (err) 364477162022SJohn Fastabend goto out; 364577162022SJohn Fastabend else 364677162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 364777162022SJohn Fastabend } 364877162022SJohn Fastabend 364977162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3650090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3651090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3652090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3653f6f6424bSJiri Pirko vid, 365487b0984eSPetr Machata nlh->nlmsg_flags, 365587b0984eSPetr Machata extack); 3656090096bfSVlad Yasevich else 3657f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 365877162022SJohn Fastabend nlh->nlmsg_flags); 365977162022SJohn Fastabend 36603ff661c3SJohn Fastabend if (!err) { 3661b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3662b3379041SHubert Sokolowski ndm->ndm_state); 366377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 366477162022SJohn Fastabend } 36653ff661c3SJohn Fastabend } 366677162022SJohn Fastabend out: 366777162022SJohn Fastabend return err; 366877162022SJohn Fastabend } 366977162022SJohn Fastabend 3670090096bfSVlad Yasevich /** 3671090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3672090096bfSVlad Yasevich */ 3673090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3674090096bfSVlad Yasevich struct nlattr *tb[], 3675090096bfSVlad Yasevich struct net_device *dev, 3676f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3677090096bfSVlad Yasevich { 3678c8a89c4aSAlexander Duyck int err = -EINVAL; 3679090096bfSVlad Yasevich 3680090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3681090096bfSVlad Yasevich * implement its own handler for this. 3682090096bfSVlad Yasevich */ 368364535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3684090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3685c8a89c4aSAlexander Duyck return err; 3686090096bfSVlad Yasevich } 3687090096bfSVlad Yasevich 3688090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3689090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3690090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3691090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3692090096bfSVlad Yasevich 3693090096bfSVlad Yasevich return err; 3694090096bfSVlad Yasevich } 3695090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3696090096bfSVlad Yasevich 3697c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3698c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 369977162022SJohn Fastabend { 370077162022SJohn Fastabend struct net *net = sock_net(skb->sk); 370177162022SJohn Fastabend struct ndmsg *ndm; 37021690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 370377162022SJohn Fastabend struct net_device *dev; 370477162022SJohn Fastabend int err = -EINVAL; 370577162022SJohn Fastabend __u8 *addr; 3706f6f6424bSJiri Pirko u16 vid; 370777162022SJohn Fastabend 370890f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 37091690be63SVlad Yasevich return -EPERM; 37101690be63SVlad Yasevich 3711c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 37121690be63SVlad Yasevich if (err < 0) 37131690be63SVlad Yasevich return err; 371477162022SJohn Fastabend 371577162022SJohn Fastabend ndm = nlmsg_data(nlh); 371677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3717b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 371877162022SJohn Fastabend return -EINVAL; 371977162022SJohn Fastabend } 372077162022SJohn Fastabend 372177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 372277162022SJohn Fastabend if (dev == NULL) { 3723b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 372477162022SJohn Fastabend return -ENODEV; 372577162022SJohn Fastabend } 372677162022SJohn Fastabend 37271690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3728b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 372977162022SJohn Fastabend return -EINVAL; 373077162022SJohn Fastabend } 373177162022SJohn Fastabend 3732da715775SIdo Schimmel if (dev->type != ARPHRD_ETHER) { 3733da715775SIdo Schimmel NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 3734da715775SIdo Schimmel return -EINVAL; 3735da715775SIdo Schimmel } 3736da715775SIdo Schimmel 37371690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 37381690be63SVlad Yasevich 3739b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3740f6f6424bSJiri Pirko if (err) 3741f6f6424bSJiri Pirko return err; 3742f6f6424bSJiri Pirko 374377162022SJohn Fastabend err = -EOPNOTSUPP; 374477162022SJohn Fastabend 374577162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 374677162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 374777162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3748898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3749898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 375077162022SJohn Fastabend 3751898e5061SJiri Pirko if (ops->ndo_fdb_del) 3752f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 375377162022SJohn Fastabend 375477162022SJohn Fastabend if (err) 375577162022SJohn Fastabend goto out; 375677162022SJohn Fastabend else 375777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 375877162022SJohn Fastabend } 375977162022SJohn Fastabend 376077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3761090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3762090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3763f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3764f6f6424bSJiri Pirko vid); 3765090096bfSVlad Yasevich else 3766f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 376777162022SJohn Fastabend 37683ff661c3SJohn Fastabend if (!err) { 3769b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3770b3379041SHubert Sokolowski ndm->ndm_state); 377177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 377277162022SJohn Fastabend } 37733ff661c3SJohn Fastabend } 377477162022SJohn Fastabend out: 377577162022SJohn Fastabend return err; 377677162022SJohn Fastabend } 377777162022SJohn Fastabend 3778d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3779d83b0603SJohn Fastabend struct netlink_callback *cb, 3780d83b0603SJohn Fastabend struct net_device *dev, 3781d83b0603SJohn Fastabend int *idx, 3782d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3783d83b0603SJohn Fastabend { 3784d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3785d83b0603SJohn Fastabend int err; 378615e47304SEric W. Biederman u32 portid, seq; 3787d83b0603SJohn Fastabend 378815e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3789d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3790d83b0603SJohn Fastabend 3791d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3792d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3793d83b0603SJohn Fastabend goto skip; 3794d83b0603SJohn Fastabend 37951e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3796a7a558feSJohn Fastabend portid, seq, 37971c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3798b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3799d83b0603SJohn Fastabend if (err < 0) 3800d83b0603SJohn Fastabend return err; 3801d83b0603SJohn Fastabend skip: 3802d83b0603SJohn Fastabend *idx += 1; 3803d83b0603SJohn Fastabend } 3804d83b0603SJohn Fastabend return 0; 3805d83b0603SJohn Fastabend } 3806d83b0603SJohn Fastabend 3807d83b0603SJohn Fastabend /** 38082c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3809d83b0603SJohn Fastabend * @nlh: netlink message header 3810d83b0603SJohn Fastabend * @dev: netdevice 3811d83b0603SJohn Fastabend * 3812d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 381391f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3814d83b0603SJohn Fastabend */ 3815d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3816d83b0603SJohn Fastabend struct netlink_callback *cb, 3817d83b0603SJohn Fastabend struct net_device *dev, 38185d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3819d297653dSRoopa Prabhu int *idx) 3820d83b0603SJohn Fastabend { 3821d83b0603SJohn Fastabend int err; 3822d83b0603SJohn Fastabend 382368883893SEric Dumazet if (dev->type != ARPHRD_ETHER) 382468883893SEric Dumazet return -EINVAL; 382568883893SEric Dumazet 3826d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3827d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3828d83b0603SJohn Fastabend if (err) 3829d83b0603SJohn Fastabend goto out; 38302934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3831d83b0603SJohn Fastabend out: 3832d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3833d297653dSRoopa Prabhu return err; 3834d83b0603SJohn Fastabend } 3835d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3836d83b0603SJohn Fastabend 38378c6e137fSDavid Ahern static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 38388c6e137fSDavid Ahern int *br_idx, int *brport_idx, 38398c6e137fSDavid Ahern struct netlink_ext_ack *extack) 38408c6e137fSDavid Ahern { 38418c6e137fSDavid Ahern struct nlattr *tb[NDA_MAX + 1]; 38428c6e137fSDavid Ahern struct ndmsg *ndm; 38438c6e137fSDavid Ahern int err, i; 38448c6e137fSDavid Ahern 38458c6e137fSDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 38468c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 38478c6e137fSDavid Ahern return -EINVAL; 38488c6e137fSDavid Ahern } 38498c6e137fSDavid Ahern 38508c6e137fSDavid Ahern ndm = nlmsg_data(nlh); 38518c6e137fSDavid Ahern if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 38528c6e137fSDavid Ahern ndm->ndm_flags || ndm->ndm_type) { 38538c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request"); 38548c6e137fSDavid Ahern return -EINVAL; 38558c6e137fSDavid Ahern } 38568c6e137fSDavid Ahern 38578c6e137fSDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX, 38588c6e137fSDavid Ahern NULL, extack); 38598c6e137fSDavid Ahern if (err < 0) 38608c6e137fSDavid Ahern return err; 38618c6e137fSDavid Ahern 38628c6e137fSDavid Ahern *brport_idx = ndm->ndm_ifindex; 38638c6e137fSDavid Ahern for (i = 0; i <= NDA_MAX; ++i) { 38648c6e137fSDavid Ahern if (!tb[i]) 38658c6e137fSDavid Ahern continue; 38668c6e137fSDavid Ahern 38678c6e137fSDavid Ahern switch (i) { 38688c6e137fSDavid Ahern case NDA_IFINDEX: 38698c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 38708c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 38718c6e137fSDavid Ahern return -EINVAL; 38728c6e137fSDavid Ahern } 38738c6e137fSDavid Ahern *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 38748c6e137fSDavid Ahern break; 38758c6e137fSDavid Ahern case NDA_MASTER: 38768c6e137fSDavid Ahern if (nla_len(tb[i]) != sizeof(u32)) { 38778c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 38788c6e137fSDavid Ahern return -EINVAL; 38798c6e137fSDavid Ahern } 38808c6e137fSDavid Ahern *br_idx = nla_get_u32(tb[NDA_MASTER]); 38818c6e137fSDavid Ahern break; 38828c6e137fSDavid Ahern default: 38838c6e137fSDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 38848c6e137fSDavid Ahern return -EINVAL; 38858c6e137fSDavid Ahern } 38868c6e137fSDavid Ahern } 38878c6e137fSDavid Ahern 38888c6e137fSDavid Ahern return 0; 38898c6e137fSDavid Ahern } 38908c6e137fSDavid Ahern 38918dfbda19SDavid Ahern static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 38928dfbda19SDavid Ahern int *br_idx, int *brport_idx, 38938dfbda19SDavid Ahern struct netlink_ext_ack *extack) 38948dfbda19SDavid Ahern { 38958dfbda19SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 38968dfbda19SDavid Ahern int err; 38978dfbda19SDavid Ahern 38988dfbda19SDavid Ahern /* A hack to preserve kernel<->userspace interface. 38998dfbda19SDavid Ahern * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 39008dfbda19SDavid Ahern * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 39018dfbda19SDavid Ahern * So, check for ndmsg with an optional u32 attribute (not used here). 39028dfbda19SDavid Ahern * Fortunately these sizes don't conflict with the size of ifinfomsg 39038dfbda19SDavid Ahern * with an optional attribute. 39048dfbda19SDavid Ahern */ 39058dfbda19SDavid Ahern if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 39068dfbda19SDavid Ahern (nlmsg_len(nlh) != sizeof(struct ndmsg) + 39078dfbda19SDavid Ahern nla_attr_size(sizeof(u32)))) { 39084565d7e5SDavid Ahern struct ifinfomsg *ifm; 39094565d7e5SDavid Ahern 39108dfbda19SDavid Ahern err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, 39118dfbda19SDavid Ahern ifla_policy, extack); 39128dfbda19SDavid Ahern if (err < 0) { 39138dfbda19SDavid Ahern return -EINVAL; 39148dfbda19SDavid Ahern } else if (err == 0) { 39158dfbda19SDavid Ahern if (tb[IFLA_MASTER]) 39168dfbda19SDavid Ahern *br_idx = nla_get_u32(tb[IFLA_MASTER]); 39178dfbda19SDavid Ahern } 39188dfbda19SDavid Ahern 39194565d7e5SDavid Ahern ifm = nlmsg_data(nlh); 39208dfbda19SDavid Ahern *brport_idx = ifm->ifi_index; 39218dfbda19SDavid Ahern } 39228dfbda19SDavid Ahern return 0; 39238dfbda19SDavid Ahern } 39248dfbda19SDavid Ahern 392577162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 392677162022SJohn Fastabend { 392777162022SJohn Fastabend struct net_device *dev; 39285e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 39295e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 39305e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 39315e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3932d297653dSRoopa Prabhu struct hlist_head *head; 39335e6d2435SJamal Hadi Salim int brport_idx = 0; 39345e6d2435SJamal Hadi Salim int br_idx = 0; 3935d297653dSRoopa Prabhu int h, s_h; 3936d297653dSRoopa Prabhu int idx = 0, s_idx; 3937d297653dSRoopa Prabhu int err = 0; 3938d297653dSRoopa Prabhu int fidx = 0; 393977162022SJohn Fastabend 39408c6e137fSDavid Ahern if (cb->strict_check) 39418c6e137fSDavid Ahern err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 39428c6e137fSDavid Ahern cb->extack); 39438c6e137fSDavid Ahern else 39448dfbda19SDavid Ahern err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 39458dfbda19SDavid Ahern cb->extack); 39468dfbda19SDavid Ahern if (err < 0) 39478dfbda19SDavid Ahern return err; 39485e6d2435SJamal Hadi Salim 39495e6d2435SJamal Hadi Salim if (br_idx) { 39505e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 39515e6d2435SJamal Hadi Salim if (!br_dev) 39525e6d2435SJamal Hadi Salim return -ENODEV; 39535e6d2435SJamal Hadi Salim 3954898e5061SJiri Pirko ops = br_dev->netdev_ops; 39555e6d2435SJamal Hadi Salim } 39565e6d2435SJamal Hadi Salim 3957d297653dSRoopa Prabhu s_h = cb->args[0]; 3958d297653dSRoopa Prabhu s_idx = cb->args[1]; 3959d297653dSRoopa Prabhu 3960d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3961d297653dSRoopa Prabhu idx = 0; 3962d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3963d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3964d297653dSRoopa Prabhu 39655e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 39665e6d2435SJamal Hadi Salim continue; 39675e6d2435SJamal Hadi Salim 39685e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 39695e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 39705e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 39715e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 39725e6d2435SJamal Hadi Salim } 39735e6d2435SJamal Hadi Salim } else { 39745e6d2435SJamal Hadi Salim if (dev != br_dev && 39755e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 39765e6d2435SJamal Hadi Salim continue; 39775e6d2435SJamal Hadi Salim 39785e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 39795e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 39805e6d2435SJamal Hadi Salim continue; 39815e6d2435SJamal Hadi Salim cops = ops; 39825e6d2435SJamal Hadi Salim } 39835e6d2435SJamal Hadi Salim 3984d297653dSRoopa Prabhu if (idx < s_idx) 3985d297653dSRoopa Prabhu goto cont; 3986d297653dSRoopa Prabhu 39875e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3988d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3989d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3990d297653dSRoopa Prabhu br_dev, dev, 3991d297653dSRoopa Prabhu &fidx); 3992d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3993d297653dSRoopa Prabhu goto out; 399477162022SJohn Fastabend } 3995d297653dSRoopa Prabhu } 399677162022SJohn Fastabend 39975e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3998d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3999d297653dSRoopa Prabhu dev, NULL, 4000d297653dSRoopa Prabhu &fidx); 40016cb69742SHubert Sokolowski else 4002d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4003d297653dSRoopa Prabhu &fidx); 4004d297653dSRoopa Prabhu if (err == -EMSGSIZE) 4005d297653dSRoopa Prabhu goto out; 40065e6d2435SJamal Hadi Salim 40075e6d2435SJamal Hadi Salim cops = NULL; 4008d297653dSRoopa Prabhu 4009d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 4010d297653dSRoopa Prabhu cb->args[2] = 0; 4011d297653dSRoopa Prabhu fidx = 0; 4012d297653dSRoopa Prabhu cont: 4013d297653dSRoopa Prabhu idx++; 4014d297653dSRoopa Prabhu } 401577162022SJohn Fastabend } 401677162022SJohn Fastabend 4017d297653dSRoopa Prabhu out: 4018d297653dSRoopa Prabhu cb->args[0] = h; 4019d297653dSRoopa Prabhu cb->args[1] = idx; 4020d297653dSRoopa Prabhu cb->args[2] = fidx; 4021d297653dSRoopa Prabhu 402277162022SJohn Fastabend return skb->len; 402377162022SJohn Fastabend } 402477162022SJohn Fastabend 40255b2f94b2SRoopa Prabhu static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 40265b2f94b2SRoopa Prabhu struct nlattr **tb, u8 *ndm_flags, 40275b2f94b2SRoopa Prabhu int *br_idx, int *brport_idx, u8 **addr, 40285b2f94b2SRoopa Prabhu u16 *vid, struct netlink_ext_ack *extack) 40295b2f94b2SRoopa Prabhu { 40305b2f94b2SRoopa Prabhu struct ndmsg *ndm; 40315b2f94b2SRoopa Prabhu int err, i; 40325b2f94b2SRoopa Prabhu 40335b2f94b2SRoopa Prabhu if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 40345b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 40355b2f94b2SRoopa Prabhu return -EINVAL; 40365b2f94b2SRoopa Prabhu } 40375b2f94b2SRoopa Prabhu 40385b2f94b2SRoopa Prabhu ndm = nlmsg_data(nlh); 40395b2f94b2SRoopa Prabhu if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 40405b2f94b2SRoopa Prabhu ndm->ndm_type) { 40415b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 40425b2f94b2SRoopa Prabhu return -EINVAL; 40435b2f94b2SRoopa Prabhu } 40445b2f94b2SRoopa Prabhu 40455b2f94b2SRoopa Prabhu if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 40465b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 40475b2f94b2SRoopa Prabhu return -EINVAL; 40485b2f94b2SRoopa Prabhu } 40495b2f94b2SRoopa Prabhu 40505b2f94b2SRoopa Prabhu err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX, 40515b2f94b2SRoopa Prabhu nda_policy, extack); 40525b2f94b2SRoopa Prabhu if (err < 0) 40535b2f94b2SRoopa Prabhu return err; 40545b2f94b2SRoopa Prabhu 40555b2f94b2SRoopa Prabhu *ndm_flags = ndm->ndm_flags; 40565b2f94b2SRoopa Prabhu *brport_idx = ndm->ndm_ifindex; 40575b2f94b2SRoopa Prabhu for (i = 0; i <= NDA_MAX; ++i) { 40585b2f94b2SRoopa Prabhu if (!tb[i]) 40595b2f94b2SRoopa Prabhu continue; 40605b2f94b2SRoopa Prabhu 40615b2f94b2SRoopa Prabhu switch (i) { 40625b2f94b2SRoopa Prabhu case NDA_MASTER: 40635b2f94b2SRoopa Prabhu *br_idx = nla_get_u32(tb[i]); 40645b2f94b2SRoopa Prabhu break; 40655b2f94b2SRoopa Prabhu case NDA_LLADDR: 40665b2f94b2SRoopa Prabhu if (nla_len(tb[i]) != ETH_ALEN) { 40675b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 40685b2f94b2SRoopa Prabhu return -EINVAL; 40695b2f94b2SRoopa Prabhu } 40705b2f94b2SRoopa Prabhu *addr = nla_data(tb[i]); 40715b2f94b2SRoopa Prabhu break; 40725b2f94b2SRoopa Prabhu case NDA_VLAN: 40735b2f94b2SRoopa Prabhu err = fdb_vid_parse(tb[i], vid, extack); 40745b2f94b2SRoopa Prabhu if (err) 40755b2f94b2SRoopa Prabhu return err; 40765b2f94b2SRoopa Prabhu break; 40775b2f94b2SRoopa Prabhu case NDA_VNI: 40785b2f94b2SRoopa Prabhu break; 40795b2f94b2SRoopa Prabhu default: 40805b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 40815b2f94b2SRoopa Prabhu return -EINVAL; 40825b2f94b2SRoopa Prabhu } 40835b2f94b2SRoopa Prabhu } 40845b2f94b2SRoopa Prabhu 40855b2f94b2SRoopa Prabhu return 0; 40865b2f94b2SRoopa Prabhu } 40875b2f94b2SRoopa Prabhu 40885b2f94b2SRoopa Prabhu static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 40895b2f94b2SRoopa Prabhu struct netlink_ext_ack *extack) 40905b2f94b2SRoopa Prabhu { 40915b2f94b2SRoopa Prabhu struct net_device *dev = NULL, *br_dev = NULL; 40925b2f94b2SRoopa Prabhu const struct net_device_ops *ops = NULL; 40935b2f94b2SRoopa Prabhu struct net *net = sock_net(in_skb->sk); 40945b2f94b2SRoopa Prabhu struct nlattr *tb[NDA_MAX + 1]; 40955b2f94b2SRoopa Prabhu struct sk_buff *skb; 40965b2f94b2SRoopa Prabhu int brport_idx = 0; 40975b2f94b2SRoopa Prabhu u8 ndm_flags = 0; 40985b2f94b2SRoopa Prabhu int br_idx = 0; 40995b2f94b2SRoopa Prabhu u8 *addr = NULL; 41005b2f94b2SRoopa Prabhu u16 vid = 0; 41015b2f94b2SRoopa Prabhu int err; 41025b2f94b2SRoopa Prabhu 41035b2f94b2SRoopa Prabhu err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 41045b2f94b2SRoopa Prabhu &brport_idx, &addr, &vid, extack); 41055b2f94b2SRoopa Prabhu if (err < 0) 41065b2f94b2SRoopa Prabhu return err; 41075b2f94b2SRoopa Prabhu 4108f989d03eSNikolay Aleksandrov if (!addr) { 4109f989d03eSNikolay Aleksandrov NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4110f989d03eSNikolay Aleksandrov return -EINVAL; 4111f989d03eSNikolay Aleksandrov } 4112f989d03eSNikolay Aleksandrov 41135b2f94b2SRoopa Prabhu if (brport_idx) { 41145b2f94b2SRoopa Prabhu dev = __dev_get_by_index(net, brport_idx); 41155b2f94b2SRoopa Prabhu if (!dev) { 41165b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 41175b2f94b2SRoopa Prabhu return -ENODEV; 41185b2f94b2SRoopa Prabhu } 41195b2f94b2SRoopa Prabhu } 41205b2f94b2SRoopa Prabhu 41215b2f94b2SRoopa Prabhu if (br_idx) { 41225b2f94b2SRoopa Prabhu if (dev) { 41235b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 41245b2f94b2SRoopa Prabhu return -EINVAL; 41255b2f94b2SRoopa Prabhu } 41265b2f94b2SRoopa Prabhu 41275b2f94b2SRoopa Prabhu br_dev = __dev_get_by_index(net, br_idx); 41285b2f94b2SRoopa Prabhu if (!br_dev) { 41295b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 41305b2f94b2SRoopa Prabhu return -EINVAL; 41315b2f94b2SRoopa Prabhu } 41325b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 41335b2f94b2SRoopa Prabhu } 41345b2f94b2SRoopa Prabhu 41355b2f94b2SRoopa Prabhu if (dev) { 41365b2f94b2SRoopa Prabhu if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 41375b2f94b2SRoopa Prabhu if (!(dev->priv_flags & IFF_BRIDGE_PORT)) { 41385b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 41395b2f94b2SRoopa Prabhu return -EINVAL; 41405b2f94b2SRoopa Prabhu } 41415b2f94b2SRoopa Prabhu br_dev = netdev_master_upper_dev_get(dev); 41425b2f94b2SRoopa Prabhu if (!br_dev) { 41435b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Master of device not found"); 41445b2f94b2SRoopa Prabhu return -EINVAL; 41455b2f94b2SRoopa Prabhu } 41465b2f94b2SRoopa Prabhu ops = br_dev->netdev_ops; 41475b2f94b2SRoopa Prabhu } else { 41485b2f94b2SRoopa Prabhu if (!(ndm_flags & NTF_SELF)) { 41495b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 41505b2f94b2SRoopa Prabhu return -EINVAL; 41515b2f94b2SRoopa Prabhu } 41525b2f94b2SRoopa Prabhu ops = dev->netdev_ops; 41535b2f94b2SRoopa Prabhu } 41545b2f94b2SRoopa Prabhu } 41555b2f94b2SRoopa Prabhu 41565b2f94b2SRoopa Prabhu if (!br_dev && !dev) { 41575b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "No device specified"); 41585b2f94b2SRoopa Prabhu return -ENODEV; 41595b2f94b2SRoopa Prabhu } 41605b2f94b2SRoopa Prabhu 41615b2f94b2SRoopa Prabhu if (!ops || !ops->ndo_fdb_get) { 41625b2f94b2SRoopa Prabhu NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 41635b2f94b2SRoopa Prabhu return -EOPNOTSUPP; 41645b2f94b2SRoopa Prabhu } 41655b2f94b2SRoopa Prabhu 41665b2f94b2SRoopa Prabhu skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 41675b2f94b2SRoopa Prabhu if (!skb) 41685b2f94b2SRoopa Prabhu return -ENOBUFS; 41695b2f94b2SRoopa Prabhu 41705b2f94b2SRoopa Prabhu if (br_dev) 41715b2f94b2SRoopa Prabhu dev = br_dev; 41725b2f94b2SRoopa Prabhu err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 41735b2f94b2SRoopa Prabhu NETLINK_CB(in_skb).portid, 41745b2f94b2SRoopa Prabhu nlh->nlmsg_seq, extack); 41755b2f94b2SRoopa Prabhu if (err) 41765b2f94b2SRoopa Prabhu goto out; 41775b2f94b2SRoopa Prabhu 41785b2f94b2SRoopa Prabhu return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 41795b2f94b2SRoopa Prabhu out: 41805b2f94b2SRoopa Prabhu kfree_skb(skb); 41815b2f94b2SRoopa Prabhu return err; 41825b2f94b2SRoopa Prabhu } 41835b2f94b2SRoopa Prabhu 41842c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 41852c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 41862c3c031cSScott Feldman { 41872c3c031cSScott Feldman if (mask & flag) 41882c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 41892c3c031cSScott Feldman return 0; 41902c3c031cSScott Feldman } 41912c3c031cSScott Feldman 4192815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 41932c3c031cSScott Feldman struct net_device *dev, u16 mode, 41947d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 41957d4f8d87SScott Feldman u32 filter_mask, 41967d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 41977d4f8d87SScott Feldman struct net_device *dev, 41987d4f8d87SScott Feldman u32 filter_mask)) 4199815cccbfSJohn Fastabend { 4200815cccbfSJohn Fastabend struct nlmsghdr *nlh; 4201815cccbfSJohn Fastabend struct ifinfomsg *ifm; 4202815cccbfSJohn Fastabend struct nlattr *br_afspec; 42032c3c031cSScott Feldman struct nlattr *protinfo; 4204815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4205898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 42067d4f8d87SScott Feldman int err = 0; 4207815cccbfSJohn Fastabend 420846c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4209815cccbfSJohn Fastabend if (nlh == NULL) 4210815cccbfSJohn Fastabend return -EMSGSIZE; 4211815cccbfSJohn Fastabend 4212815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 4213815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 4214815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 4215815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 4216815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 4217815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 4218815cccbfSJohn Fastabend ifm->ifi_change = 0; 4219815cccbfSJohn Fastabend 4220815cccbfSJohn Fastabend 4221815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4222815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4223815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4224898e5061SJiri Pirko (br_dev && 4225898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4226815cccbfSJohn Fastabend (dev->addr_len && 4227815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4228a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 4229a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4230815cccbfSJohn Fastabend goto nla_put_failure; 4231815cccbfSJohn Fastabend 4232815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 4233815cccbfSJohn Fastabend if (!br_afspec) 4234815cccbfSJohn Fastabend goto nla_put_failure; 4235815cccbfSJohn Fastabend 42361d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4237815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 4238815cccbfSJohn Fastabend goto nla_put_failure; 4239815cccbfSJohn Fastabend } 42401d460b98SRoopa Prabhu 42411d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 42421d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 42431d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 42441d460b98SRoopa Prabhu goto nla_put_failure; 42451d460b98SRoopa Prabhu } 42461d460b98SRoopa Prabhu } 42477d4f8d87SScott Feldman if (vlan_fill) { 42487d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 42497d4f8d87SScott Feldman if (err) { 42507d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 42517d4f8d87SScott Feldman goto nla_put_failure; 42527d4f8d87SScott Feldman } 42537d4f8d87SScott Feldman } 4254815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 4255815cccbfSJohn Fastabend 42562c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 42572c3c031cSScott Feldman if (!protinfo) 42582c3c031cSScott Feldman goto nla_put_failure; 42592c3c031cSScott Feldman 42602c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 42612c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 42622c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42632c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 42642c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42652c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 42662c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 42672c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42682c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 42692c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42702c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 42712c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42722c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 42732c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42742c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 42752c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 42762c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 42772c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 42782c3c031cSScott Feldman goto nla_put_failure; 42792c3c031cSScott Feldman } 42802c3c031cSScott Feldman 42812c3c031cSScott Feldman nla_nest_end(skb, protinfo); 42822c3c031cSScott Feldman 4283053c095aSJohannes Berg nlmsg_end(skb, nlh); 4284053c095aSJohannes Berg return 0; 4285815cccbfSJohn Fastabend nla_put_failure: 4286815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 42877d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4288815cccbfSJohn Fastabend } 42897d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4290815cccbfSJohn Fastabend 42912d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 42922d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 42932d011be8SDavid Ahern struct netlink_ext_ack *extack) 42942d011be8SDavid Ahern { 42952d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 42962d011be8SDavid Ahern int err, i; 42972d011be8SDavid Ahern 42982d011be8SDavid Ahern if (strict_check) { 42992d011be8SDavid Ahern struct ifinfomsg *ifm; 43002d011be8SDavid Ahern 43012d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 43022d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 43032d011be8SDavid Ahern return -EINVAL; 43042d011be8SDavid Ahern } 43052d011be8SDavid Ahern 43062d011be8SDavid Ahern ifm = nlmsg_data(nlh); 43072d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 43082d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 43092d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 43102d011be8SDavid Ahern return -EINVAL; 43112d011be8SDavid Ahern } 43122d011be8SDavid Ahern 43132d011be8SDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb, 43142d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 43152d011be8SDavid Ahern } else { 43162d011be8SDavid Ahern err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, 43172d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 43182d011be8SDavid Ahern } 43192d011be8SDavid Ahern if (err < 0) 43202d011be8SDavid Ahern return err; 43212d011be8SDavid Ahern 43222d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 43232d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 43242d011be8SDavid Ahern if (!tb[i]) 43252d011be8SDavid Ahern continue; 43262d011be8SDavid Ahern 43272d011be8SDavid Ahern switch (i) { 43282d011be8SDavid Ahern case IFLA_EXT_MASK: 43292d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 43302d011be8SDavid Ahern break; 43312d011be8SDavid Ahern default: 43322d011be8SDavid Ahern if (strict_check) { 43332d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 43342d011be8SDavid Ahern return -EINVAL; 43352d011be8SDavid Ahern } 43362d011be8SDavid Ahern } 43372d011be8SDavid Ahern } 43382d011be8SDavid Ahern 43392d011be8SDavid Ahern return 0; 43402d011be8SDavid Ahern } 43412d011be8SDavid Ahern 4342e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4343e5a55a89SJohn Fastabend { 43442d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4345e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4346e5a55a89SJohn Fastabend struct net_device *dev; 4347e5a55a89SJohn Fastabend int idx = 0; 4348e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 43492d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 43506cbdceebSVlad Yasevich u32 filter_mask = 0; 4351d64f69b0SRoopa Prabhu int err; 43526cbdceebSVlad Yasevich 43532d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 43542d011be8SDavid Ahern cb->extack); 43552d011be8SDavid Ahern if (err < 0 && cb->strict_check) 43562d011be8SDavid Ahern return err; 4357e5a55a89SJohn Fastabend 4358e5a55a89SJohn Fastabend rcu_read_lock(); 4359e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4360e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4361898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4362e5a55a89SJohn Fastabend 4363898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4364d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4365d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4366d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4367d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4368f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4369f6c5775fSDavid Ahern if (likely(skb->len)) 4370e5a55a89SJohn Fastabend break; 4371f6c5775fSDavid Ahern 4372f6c5775fSDavid Ahern goto out_err; 4373f6c5775fSDavid Ahern } 4374d64f69b0SRoopa Prabhu } 4375e5a55a89SJohn Fastabend idx++; 4376e5a55a89SJohn Fastabend } 4377e5a55a89SJohn Fastabend 4378e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4379d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4380d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4381d64f69b0SRoopa Prabhu seq, dev, 438246c264daSNicolas Dichtel filter_mask, 4383d64f69b0SRoopa Prabhu NLM_F_MULTI); 4384f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4385f6c5775fSDavid Ahern if (likely(skb->len)) 4386e5a55a89SJohn Fastabend break; 4387f6c5775fSDavid Ahern 4388f6c5775fSDavid Ahern goto out_err; 4389f6c5775fSDavid Ahern } 4390d64f69b0SRoopa Prabhu } 4391e5a55a89SJohn Fastabend idx++; 4392e5a55a89SJohn Fastabend } 4393e5a55a89SJohn Fastabend } 4394f6c5775fSDavid Ahern err = skb->len; 4395f6c5775fSDavid Ahern out_err: 4396e5a55a89SJohn Fastabend rcu_read_unlock(); 4397e5a55a89SJohn Fastabend cb->args[0] = idx; 4398e5a55a89SJohn Fastabend 4399f6c5775fSDavid Ahern return err; 4400e5a55a89SJohn Fastabend } 4401e5a55a89SJohn Fastabend 44022469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 44032469ffd7SJohn Fastabend { 44042469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 44052469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 44062469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 44072469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 44082469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 44092469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 44102469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 44112469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 44122469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 44132469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 44142469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 44152469ffd7SJohn Fastabend } 44162469ffd7SJohn Fastabend 441702dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 44182469ffd7SJohn Fastabend { 44192469ffd7SJohn Fastabend struct net *net = dev_net(dev); 44202469ffd7SJohn Fastabend struct sk_buff *skb; 44212469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 44222469ffd7SJohn Fastabend 442302dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 442402dba438SRoopa Prabhu return 0; 442502dba438SRoopa Prabhu 44262469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 44272469ffd7SJohn Fastabend if (!skb) { 44282469ffd7SJohn Fastabend err = -ENOMEM; 44292469ffd7SJohn Fastabend goto errout; 44302469ffd7SJohn Fastabend } 44312469ffd7SJohn Fastabend 443246c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4433c38e01b8SJohn Fastabend if (err < 0) 4434c38e01b8SJohn Fastabend goto errout; 44352469ffd7SJohn Fastabend 443659ccaaaaSRoopa Prabhu if (!skb->len) 443759ccaaaaSRoopa Prabhu goto errout; 443859ccaaaaSRoopa Prabhu 44392469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 44402469ffd7SJohn Fastabend return 0; 44412469ffd7SJohn Fastabend errout: 44422469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 44432469ffd7SJohn Fastabend kfree_skb(skb); 444459ccaaaaSRoopa Prabhu if (err) 44452469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 44462469ffd7SJohn Fastabend return err; 44472469ffd7SJohn Fastabend } 44482469ffd7SJohn Fastabend 4449c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4450c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4451e5a55a89SJohn Fastabend { 4452e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4453e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4454e5a55a89SJohn Fastabend struct net_device *dev; 44552469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 44562469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 44574de8b413SRosen, Rami u16 flags = 0; 4458c38e01b8SJohn Fastabend bool have_flags = false; 4459e5a55a89SJohn Fastabend 4460e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4461e5a55a89SJohn Fastabend return -EINVAL; 4462e5a55a89SJohn Fastabend 4463e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4464e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4465e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4466e5a55a89SJohn Fastabend 4467e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4468e5a55a89SJohn Fastabend if (!dev) { 4469b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4470e5a55a89SJohn Fastabend return -ENODEV; 4471e5a55a89SJohn Fastabend } 4472e5a55a89SJohn Fastabend 44732469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 44742469ffd7SJohn Fastabend if (br_spec) { 44752469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 44762469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 44776e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 44786e8d1c55SThomas Graf return -EINVAL; 44796e8d1c55SThomas Graf 4480c38e01b8SJohn Fastabend have_flags = true; 44812469ffd7SJohn Fastabend flags = nla_get_u16(attr); 44822469ffd7SJohn Fastabend break; 44832469ffd7SJohn Fastabend } 44842469ffd7SJohn Fastabend } 44852469ffd7SJohn Fastabend } 44862469ffd7SJohn Fastabend 44872469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4488898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4489898e5061SJiri Pirko 4490898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 44912469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4492e5a55a89SJohn Fastabend goto out; 4493e5a55a89SJohn Fastabend } 4494e5a55a89SJohn Fastabend 44952fd527b7SPetr Machata err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 44962fd527b7SPetr Machata extack); 44972469ffd7SJohn Fastabend if (err) 44982469ffd7SJohn Fastabend goto out; 44992469ffd7SJohn Fastabend 45002469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 45012469ffd7SJohn Fastabend } 45022469ffd7SJohn Fastabend 45032469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 45042469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 45052469ffd7SJohn Fastabend err = -EOPNOTSUPP; 45062469ffd7SJohn Fastabend else 4507add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 45082fd527b7SPetr Machata flags, 45092fd527b7SPetr Machata extack); 451002dba438SRoopa Prabhu if (!err) { 45112469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 451202dba438SRoopa Prabhu 451302dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 451402dba438SRoopa Prabhu * change 451502dba438SRoopa Prabhu */ 451602dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 451702dba438SRoopa Prabhu } 45182469ffd7SJohn Fastabend } 45192469ffd7SJohn Fastabend 4520c38e01b8SJohn Fastabend if (have_flags) 45212469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4522e5a55a89SJohn Fastabend out: 4523e5a55a89SJohn Fastabend return err; 4524e5a55a89SJohn Fastabend } 4525e5a55a89SJohn Fastabend 4526c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4527c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4528407af329SVlad Yasevich { 4529407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4530407af329SVlad Yasevich struct ifinfomsg *ifm; 4531407af329SVlad Yasevich struct net_device *dev; 4532407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4533407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 45344de8b413SRosen, Rami u16 flags = 0; 4535407af329SVlad Yasevich bool have_flags = false; 4536407af329SVlad Yasevich 4537407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4538407af329SVlad Yasevich return -EINVAL; 4539407af329SVlad Yasevich 4540407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4541407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4542407af329SVlad Yasevich return -EPFNOSUPPORT; 4543407af329SVlad Yasevich 4544407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4545407af329SVlad Yasevich if (!dev) { 4546b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4547407af329SVlad Yasevich return -ENODEV; 4548407af329SVlad Yasevich } 4549407af329SVlad Yasevich 4550407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4551407af329SVlad Yasevich if (br_spec) { 4552407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4553407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 45546e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 45556e8d1c55SThomas Graf return -EINVAL; 45566e8d1c55SThomas Graf 4557407af329SVlad Yasevich have_flags = true; 4558407af329SVlad Yasevich flags = nla_get_u16(attr); 4559407af329SVlad Yasevich break; 4560407af329SVlad Yasevich } 4561407af329SVlad Yasevich } 4562407af329SVlad Yasevich } 4563407af329SVlad Yasevich 4564407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4565407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4566407af329SVlad Yasevich 4567407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4568407af329SVlad Yasevich err = -EOPNOTSUPP; 4569407af329SVlad Yasevich goto out; 4570407af329SVlad Yasevich } 4571407af329SVlad Yasevich 4572add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4573407af329SVlad Yasevich if (err) 4574407af329SVlad Yasevich goto out; 4575407af329SVlad Yasevich 4576407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4577407af329SVlad Yasevich } 4578407af329SVlad Yasevich 4579407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4580407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4581407af329SVlad Yasevich err = -EOPNOTSUPP; 4582407af329SVlad Yasevich else 4583add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4584add511b3SRoopa Prabhu flags); 4585407af329SVlad Yasevich 458602dba438SRoopa Prabhu if (!err) { 4587407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 458802dba438SRoopa Prabhu 458902dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 459002dba438SRoopa Prabhu * change 459102dba438SRoopa Prabhu */ 459202dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 459302dba438SRoopa Prabhu } 4594407af329SVlad Yasevich } 4595407af329SVlad Yasevich 4596407af329SVlad Yasevich if (have_flags) 4597407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4598407af329SVlad Yasevich out: 4599407af329SVlad Yasevich return err; 4600407af329SVlad Yasevich } 4601407af329SVlad Yasevich 4602e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4603e8872a25SNikolay Aleksandrov { 4604e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4605e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4606e8872a25SNikolay Aleksandrov } 4607e8872a25SNikolay Aleksandrov 460869ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 460969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 461069ae6ad2SNogah Frankel { 461169ae6ad2SNogah Frankel switch (attr_id) { 461269ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 461369ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 461469ae6ad2SNogah Frankel } 461569ae6ad2SNogah Frankel 461669ae6ad2SNogah Frankel return 0; 461769ae6ad2SNogah Frankel } 461869ae6ad2SNogah Frankel 461969ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 462069ae6ad2SNogah Frankel int *prividx) 462169ae6ad2SNogah Frankel { 462269ae6ad2SNogah Frankel struct nlattr *attr = NULL; 462369ae6ad2SNogah Frankel int attr_id, size; 462469ae6ad2SNogah Frankel void *attr_data; 462569ae6ad2SNogah Frankel int err; 462669ae6ad2SNogah Frankel 462769ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 462869ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 462969ae6ad2SNogah Frankel return -ENODATA; 463069ae6ad2SNogah Frankel 463169ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 463269ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 463369ae6ad2SNogah Frankel if (attr_id < *prividx) 463469ae6ad2SNogah Frankel continue; 463569ae6ad2SNogah Frankel 463669ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 463769ae6ad2SNogah Frankel if (!size) 463869ae6ad2SNogah Frankel continue; 463969ae6ad2SNogah Frankel 46403df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 464169ae6ad2SNogah Frankel continue; 464269ae6ad2SNogah Frankel 464369ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 464469ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 464569ae6ad2SNogah Frankel if (!attr) 464669ae6ad2SNogah Frankel goto nla_put_failure; 464769ae6ad2SNogah Frankel 464869ae6ad2SNogah Frankel attr_data = nla_data(attr); 464969ae6ad2SNogah Frankel memset(attr_data, 0, size); 465069ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 465169ae6ad2SNogah Frankel attr_data); 465269ae6ad2SNogah Frankel if (err) 465369ae6ad2SNogah Frankel goto get_offload_stats_failure; 465469ae6ad2SNogah Frankel } 465569ae6ad2SNogah Frankel 465669ae6ad2SNogah Frankel if (!attr) 465769ae6ad2SNogah Frankel return -ENODATA; 465869ae6ad2SNogah Frankel 465969ae6ad2SNogah Frankel *prividx = 0; 466069ae6ad2SNogah Frankel return 0; 466169ae6ad2SNogah Frankel 466269ae6ad2SNogah Frankel nla_put_failure: 466369ae6ad2SNogah Frankel err = -EMSGSIZE; 466469ae6ad2SNogah Frankel get_offload_stats_failure: 466569ae6ad2SNogah Frankel *prividx = attr_id; 466669ae6ad2SNogah Frankel return err; 466769ae6ad2SNogah Frankel } 466869ae6ad2SNogah Frankel 466969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 467069ae6ad2SNogah Frankel { 467169ae6ad2SNogah Frankel int nla_size = 0; 467269ae6ad2SNogah Frankel int attr_id; 467369ae6ad2SNogah Frankel int size; 467469ae6ad2SNogah Frankel 467569ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 467669ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 467769ae6ad2SNogah Frankel return 0; 467869ae6ad2SNogah Frankel 467969ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 468069ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 46813df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 468269ae6ad2SNogah Frankel continue; 468369ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 468469ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 468569ae6ad2SNogah Frankel } 468669ae6ad2SNogah Frankel 468769ae6ad2SNogah Frankel if (nla_size != 0) 468869ae6ad2SNogah Frankel nla_size += nla_total_size(0); 468969ae6ad2SNogah Frankel 469069ae6ad2SNogah Frankel return nla_size; 469169ae6ad2SNogah Frankel } 469269ae6ad2SNogah Frankel 469310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 469410c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4695e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4696e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 469710c9ead9SRoopa Prabhu { 469810c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 469910c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 470010c9ead9SRoopa Prabhu struct nlattr *attr; 4701e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 470269ae6ad2SNogah Frankel int err; 470310c9ead9SRoopa Prabhu 470410c9ead9SRoopa Prabhu ASSERT_RTNL(); 470510c9ead9SRoopa Prabhu 470610c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 470710c9ead9SRoopa Prabhu if (!nlh) 470810c9ead9SRoopa Prabhu return -EMSGSIZE; 470910c9ead9SRoopa Prabhu 471010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4711ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4712ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4713ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 471410c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 471510c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 471610c9ead9SRoopa Prabhu 4717e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 471810c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 471910c9ead9SRoopa Prabhu 472058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 472158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 472258414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 472310c9ead9SRoopa Prabhu if (!attr) 472410c9ead9SRoopa Prabhu goto nla_put_failure; 472510c9ead9SRoopa Prabhu 472610c9ead9SRoopa Prabhu sp = nla_data(attr); 472710c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 472810c9ead9SRoopa Prabhu } 472910c9ead9SRoopa Prabhu 473097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 473197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 473297a47facSNikolay Aleksandrov 473397a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 473497a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 473597a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 473697a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 473797a47facSNikolay Aleksandrov if (!attr) 473897a47facSNikolay Aleksandrov goto nla_put_failure; 473997a47facSNikolay Aleksandrov 474080e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 474180e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 474280e73cc5SNikolay Aleksandrov if (err) 474380e73cc5SNikolay Aleksandrov goto nla_put_failure; 474480e73cc5SNikolay Aleksandrov *idxattr = 0; 474580e73cc5SNikolay Aleksandrov } 474680e73cc5SNikolay Aleksandrov } 474780e73cc5SNikolay Aleksandrov 474880e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 474980e73cc5SNikolay Aleksandrov *idxattr)) { 475080e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 475180e73cc5SNikolay Aleksandrov const struct net_device *master; 475280e73cc5SNikolay Aleksandrov 475380e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 475480e73cc5SNikolay Aleksandrov if (master) 475580e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 475680e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 475780e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 475880e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 475980e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 476080e73cc5SNikolay Aleksandrov if (!attr) 476180e73cc5SNikolay Aleksandrov goto nla_put_failure; 476280e73cc5SNikolay Aleksandrov 476380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 476497a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 476597a47facSNikolay Aleksandrov if (err) 476697a47facSNikolay Aleksandrov goto nla_put_failure; 476797a47facSNikolay Aleksandrov *idxattr = 0; 476897a47facSNikolay Aleksandrov } 476997a47facSNikolay Aleksandrov } 477097a47facSNikolay Aleksandrov 477169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 477269ae6ad2SNogah Frankel *idxattr)) { 477369ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 477469ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 477569ae6ad2SNogah Frankel if (!attr) 477669ae6ad2SNogah Frankel goto nla_put_failure; 477769ae6ad2SNogah Frankel 477869ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 477969ae6ad2SNogah Frankel if (err == -ENODATA) 478069ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 478169ae6ad2SNogah Frankel else 478269ae6ad2SNogah Frankel nla_nest_end(skb, attr); 478369ae6ad2SNogah Frankel 478469ae6ad2SNogah Frankel if (err && err != -ENODATA) 478569ae6ad2SNogah Frankel goto nla_put_failure; 478669ae6ad2SNogah Frankel *idxattr = 0; 478769ae6ad2SNogah Frankel } 478869ae6ad2SNogah Frankel 4789aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4790aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4791aefb4d4aSRobert Shearman 4792aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4793aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4794aefb4d4aSRobert Shearman if (!attr) 4795aefb4d4aSRobert Shearman goto nla_put_failure; 4796aefb4d4aSRobert Shearman 47975fa85a09SFlorian Westphal rcu_read_lock(); 47985fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4799aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4800aefb4d4aSRobert Shearman struct nlattr *af; 4801aefb4d4aSRobert Shearman int err; 4802aefb4d4aSRobert Shearman 4803aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 48045fa85a09SFlorian Westphal if (!af) { 48055fa85a09SFlorian Westphal rcu_read_unlock(); 4806aefb4d4aSRobert Shearman goto nla_put_failure; 48075fa85a09SFlorian Westphal } 4808aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4809aefb4d4aSRobert Shearman 48105fa85a09SFlorian Westphal if (err == -ENODATA) { 4811aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 48125fa85a09SFlorian Westphal } else if (err < 0) { 48135fa85a09SFlorian Westphal rcu_read_unlock(); 4814aefb4d4aSRobert Shearman goto nla_put_failure; 48155fa85a09SFlorian Westphal } 4816aefb4d4aSRobert Shearman 4817aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4818aefb4d4aSRobert Shearman } 4819aefb4d4aSRobert Shearman } 48205fa85a09SFlorian Westphal rcu_read_unlock(); 4821aefb4d4aSRobert Shearman 4822aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4823aefb4d4aSRobert Shearman 4824aefb4d4aSRobert Shearman *idxattr = 0; 4825aefb4d4aSRobert Shearman } 4826aefb4d4aSRobert Shearman 482710c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 482810c9ead9SRoopa Prabhu 482910c9ead9SRoopa Prabhu return 0; 483010c9ead9SRoopa Prabhu 483110c9ead9SRoopa Prabhu nla_put_failure: 4832e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4833e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 483410c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4835e8872a25SNikolay Aleksandrov else 4836e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 483710c9ead9SRoopa Prabhu 483810c9ead9SRoopa Prabhu return -EMSGSIZE; 483910c9ead9SRoopa Prabhu } 484010c9ead9SRoopa Prabhu 484110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 484210c9ead9SRoopa Prabhu u32 filter_mask) 484310c9ead9SRoopa Prabhu { 484410c9ead9SRoopa Prabhu size_t size = 0; 484510c9ead9SRoopa Prabhu 4846e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 484710c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 484810c9ead9SRoopa Prabhu 484997a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 485097a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 485180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 485297a47facSNikolay Aleksandrov 485397a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 485480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 485580e73cc5SNikolay Aleksandrov attr)); 485697a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 485797a47facSNikolay Aleksandrov size += nla_total_size(0); 485897a47facSNikolay Aleksandrov } 485997a47facSNikolay Aleksandrov } 486097a47facSNikolay Aleksandrov 486180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 486280e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 486380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 486480e73cc5SNikolay Aleksandrov const struct net_device *master; 486580e73cc5SNikolay Aleksandrov 486680e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 486780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 486880e73cc5SNikolay Aleksandrov if (master) 486980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 487080e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 487180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 487280e73cc5SNikolay Aleksandrov 487380e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 487480e73cc5SNikolay Aleksandrov attr)); 487580e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 487680e73cc5SNikolay Aleksandrov size += nla_total_size(0); 487780e73cc5SNikolay Aleksandrov } 487880e73cc5SNikolay Aleksandrov } 487980e73cc5SNikolay Aleksandrov 488069ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 488169ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 488269ae6ad2SNogah Frankel 4883aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4884aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4885aefb4d4aSRobert Shearman 4886aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4887aefb4d4aSRobert Shearman size += nla_total_size(0); 4888aefb4d4aSRobert Shearman 48895fa85a09SFlorian Westphal rcu_read_lock(); 48905fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4891aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4892aefb4d4aSRobert Shearman size += nla_total_size( 4893aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4894aefb4d4aSRobert Shearman 4895aefb4d4aSRobert Shearman /* for AF_* */ 4896aefb4d4aSRobert Shearman size += nla_total_size(0); 4897aefb4d4aSRobert Shearman } 4898aefb4d4aSRobert Shearman } 48995fa85a09SFlorian Westphal rcu_read_unlock(); 4900aefb4d4aSRobert Shearman } 4901aefb4d4aSRobert Shearman 490210c9ead9SRoopa Prabhu return size; 490310c9ead9SRoopa Prabhu } 490410c9ead9SRoopa Prabhu 490551bc860dSJakub Kicinski static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 490651bc860dSJakub Kicinski bool is_dump, struct netlink_ext_ack *extack) 490751bc860dSJakub Kicinski { 490851bc860dSJakub Kicinski struct if_stats_msg *ifsm; 490951bc860dSJakub Kicinski 491051bc860dSJakub Kicinski if (nlh->nlmsg_len < sizeof(*ifsm)) { 491151bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 491251bc860dSJakub Kicinski return -EINVAL; 491351bc860dSJakub Kicinski } 491451bc860dSJakub Kicinski 491551bc860dSJakub Kicinski if (!strict_check) 491651bc860dSJakub Kicinski return 0; 491751bc860dSJakub Kicinski 491851bc860dSJakub Kicinski ifsm = nlmsg_data(nlh); 491951bc860dSJakub Kicinski 492051bc860dSJakub Kicinski /* only requests using strict checks can pass data to influence 492151bc860dSJakub Kicinski * the dump. The legacy exception is filter_mask. 492251bc860dSJakub Kicinski */ 492351bc860dSJakub Kicinski if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 492451bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 492551bc860dSJakub Kicinski return -EINVAL; 492651bc860dSJakub Kicinski } 492751bc860dSJakub Kicinski if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 492851bc860dSJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 492951bc860dSJakub Kicinski return -EINVAL; 493051bc860dSJakub Kicinski } 4931*6300acb2SJakub Kicinski if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 4932*6300acb2SJakub Kicinski NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 4933*6300acb2SJakub Kicinski return -EINVAL; 4934*6300acb2SJakub Kicinski } 493551bc860dSJakub Kicinski 493651bc860dSJakub Kicinski return 0; 493751bc860dSJakub Kicinski } 493851bc860dSJakub Kicinski 4939c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4940c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 494110c9ead9SRoopa Prabhu { 494210c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 494310c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4944e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4945e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 494610c9ead9SRoopa Prabhu struct sk_buff *nskb; 494710c9ead9SRoopa Prabhu u32 filter_mask; 494810c9ead9SRoopa Prabhu int err; 494910c9ead9SRoopa Prabhu 495051bc860dSJakub Kicinski err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 495151bc860dSJakub Kicinski false, extack); 495251bc860dSJakub Kicinski if (err) 495351bc860dSJakub Kicinski return err; 49544775cc1fSMathias Krause 495510c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 495610c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 495710c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 495810c9ead9SRoopa Prabhu else 495910c9ead9SRoopa Prabhu return -EINVAL; 496010c9ead9SRoopa Prabhu 496110c9ead9SRoopa Prabhu if (!dev) 496210c9ead9SRoopa Prabhu return -ENODEV; 496310c9ead9SRoopa Prabhu 496410c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 496510c9ead9SRoopa Prabhu if (!filter_mask) 496610c9ead9SRoopa Prabhu return -EINVAL; 496710c9ead9SRoopa Prabhu 496810c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 496910c9ead9SRoopa Prabhu if (!nskb) 497010c9ead9SRoopa Prabhu return -ENOBUFS; 497110c9ead9SRoopa Prabhu 497210c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 497310c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4974e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 497510c9ead9SRoopa Prabhu if (err < 0) { 497610c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 497710c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 497810c9ead9SRoopa Prabhu kfree_skb(nskb); 497910c9ead9SRoopa Prabhu } else { 498010c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 498110c9ead9SRoopa Prabhu } 498210c9ead9SRoopa Prabhu 498310c9ead9SRoopa Prabhu return err; 498410c9ead9SRoopa Prabhu } 498510c9ead9SRoopa Prabhu 498610c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 498710c9ead9SRoopa Prabhu { 4988841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 4989e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 499010c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 499110c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4992e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4993e8872a25SNikolay Aleksandrov struct hlist_head *head; 4994e8872a25SNikolay Aleksandrov struct net_device *dev; 499510c9ead9SRoopa Prabhu u32 filter_mask = 0; 4996e8872a25SNikolay Aleksandrov int idx = 0; 499710c9ead9SRoopa Prabhu 499810c9ead9SRoopa Prabhu s_h = cb->args[0]; 499910c9ead9SRoopa Prabhu s_idx = cb->args[1]; 5000e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 5001e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 500210c9ead9SRoopa Prabhu 500310c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 500410c9ead9SRoopa Prabhu 500551bc860dSJakub Kicinski err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 500651bc860dSJakub Kicinski if (err) 500751bc860dSJakub Kicinski return err; 50084775cc1fSMathias Krause 500910c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 5010841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 5011841891ecSDavid Ahern if (!filter_mask) { 5012841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5013841891ecSDavid Ahern return -EINVAL; 5014841891ecSDavid Ahern } 501510c9ead9SRoopa Prabhu 501610c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 501710c9ead9SRoopa Prabhu idx = 0; 501810c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 501910c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 502010c9ead9SRoopa Prabhu if (idx < s_idx) 502110c9ead9SRoopa Prabhu goto cont; 502210c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 502310c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 502410c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 5025e8872a25SNikolay Aleksandrov flags, filter_mask, 5026e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 502710c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 502810c9ead9SRoopa Prabhu * we're in trouble 502910c9ead9SRoopa Prabhu */ 503010c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 503110c9ead9SRoopa Prabhu 503210c9ead9SRoopa Prabhu if (err < 0) 503310c9ead9SRoopa Prabhu goto out; 5034e8872a25SNikolay Aleksandrov s_prividx = 0; 5035e8872a25SNikolay Aleksandrov s_idxattr = 0; 503610c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 503710c9ead9SRoopa Prabhu cont: 503810c9ead9SRoopa Prabhu idx++; 503910c9ead9SRoopa Prabhu } 504010c9ead9SRoopa Prabhu } 504110c9ead9SRoopa Prabhu out: 5042e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 5043e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 504410c9ead9SRoopa Prabhu cb->args[1] = idx; 504510c9ead9SRoopa Prabhu cb->args[0] = h; 504610c9ead9SRoopa Prabhu 504710c9ead9SRoopa Prabhu return skb->len; 504810c9ead9SRoopa Prabhu } 504910c9ead9SRoopa Prabhu 50501da177e4SLinus Torvalds /* Process one rtnetlink message. */ 50511da177e4SLinus Torvalds 50522d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 50532d4bc933SJohannes Berg struct netlink_ext_ack *extack) 50541da177e4SLinus Torvalds { 50553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 5056addf9b90SFlorian Westphal struct rtnl_link *link; 5057e4202511SFlorian Westphal struct module *owner; 50586853dd48SFlorian Westphal int err = -EOPNOTSUPP; 5059e2849863SThomas Graf rtnl_doit_func doit; 506062256f98SFlorian Westphal unsigned int flags; 5061617cfc75SAlexander Kuleshov int kind; 50621da177e4SLinus Torvalds int family; 50631da177e4SLinus Torvalds int type; 50641da177e4SLinus Torvalds 50651da177e4SLinus Torvalds type = nlh->nlmsg_type; 50661da177e4SLinus Torvalds if (type > RTM_MAX) 5067038890feSThomas Graf return -EOPNOTSUPP; 50681da177e4SLinus Torvalds 50691da177e4SLinus Torvalds type -= RTM_BASE; 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 5072573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 50731da177e4SLinus Torvalds return 0; 50741da177e4SLinus Torvalds 5075573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 50761da177e4SLinus Torvalds kind = type&3; 50771da177e4SLinus Torvalds 507890f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 50791d00a4ebSThomas Graf return -EPERM; 50801da177e4SLinus Torvalds 50816853dd48SFlorian Westphal rcu_read_lock(); 5082b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 508397c53cacSDenis V. Lunev struct sock *rtnl; 5084e2849863SThomas Graf rtnl_dumpit_func dumpit; 5085c7ac8679SGreg Rose u16 min_dump_alloc = 0; 50861da177e4SLinus Torvalds 5087addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5088addf9b90SFlorian Westphal if (!link || !link->dumpit) { 50896853dd48SFlorian Westphal family = PF_UNSPEC; 5090addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5091addf9b90SFlorian Westphal if (!link || !link->dumpit) 50926853dd48SFlorian Westphal goto err_unlock; 50936853dd48SFlorian Westphal } 5094e4202511SFlorian Westphal owner = link->owner; 5095addf9b90SFlorian Westphal dumpit = link->dumpit; 50966853dd48SFlorian Westphal 50975c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 5098e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 50991da177e4SLinus Torvalds 5100e4202511SFlorian Westphal err = 0; 5101e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 5102e4202511SFlorian Westphal if (!try_module_get(owner)) 5103e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5104e4202511SFlorian Westphal 51056853dd48SFlorian Westphal rcu_read_unlock(); 51066853dd48SFlorian Westphal 510797c53cacSDenis V. Lunev rtnl = net->rtnl; 5108e4202511SFlorian Westphal if (err == 0) { 510980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 511080d326faSPablo Neira Ayuso .dump = dumpit, 511180d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 5112e4202511SFlorian Westphal .module = owner, 511380d326faSPablo Neira Ayuso }; 511480d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 5115e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 5116e4202511SFlorian Westphal * module if dump is still in progress. 5117e4202511SFlorian Westphal */ 5118e4202511SFlorian Westphal module_put(owner); 511980d326faSPablo Neira Ayuso } 51202907c35fSEric Dumazet return err; 51211da177e4SLinus Torvalds } 51221da177e4SLinus Torvalds 5123addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5124addf9b90SFlorian Westphal if (!link || !link->doit) { 51258caa38b5SFlorian Westphal family = PF_UNSPEC; 5126addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 5127addf9b90SFlorian Westphal if (!link || !link->doit) 5128addf9b90SFlorian Westphal goto out_unlock; 51298caa38b5SFlorian Westphal } 51308caa38b5SFlorian Westphal 5131e4202511SFlorian Westphal owner = link->owner; 5132e4202511SFlorian Westphal if (!try_module_get(owner)) { 5133e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 5134e4202511SFlorian Westphal goto out_unlock; 5135e4202511SFlorian Westphal } 5136e4202511SFlorian Westphal 5137addf9b90SFlorian Westphal flags = link->flags; 513862256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5139addf9b90SFlorian Westphal doit = link->doit; 514062256f98SFlorian Westphal rcu_read_unlock(); 514162256f98SFlorian Westphal if (doit) 514262256f98SFlorian Westphal err = doit(skb, nlh, extack); 5143e4202511SFlorian Westphal module_put(owner); 514462256f98SFlorian Westphal return err; 514562256f98SFlorian Westphal } 51466853dd48SFlorian Westphal rcu_read_unlock(); 51476853dd48SFlorian Westphal 51480cc09020SFlorian Westphal rtnl_lock(); 5149addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 5150addf9b90SFlorian Westphal if (link && link->doit) 5151addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 51520cc09020SFlorian Westphal rtnl_unlock(); 5153addf9b90SFlorian Westphal 5154e4202511SFlorian Westphal module_put(owner); 5155e4202511SFlorian Westphal 5156addf9b90SFlorian Westphal return err; 5157addf9b90SFlorian Westphal 5158addf9b90SFlorian Westphal out_unlock: 5159addf9b90SFlorian Westphal rcu_read_unlock(); 51600cc09020SFlorian Westphal return err; 51610cc09020SFlorian Westphal 51620cc09020SFlorian Westphal err_unlock: 51636853dd48SFlorian Westphal rcu_read_unlock(); 51640cc09020SFlorian Westphal return -EOPNOTSUPP; 51651da177e4SLinus Torvalds } 51661da177e4SLinus Torvalds 5167cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 51681da177e4SLinus Torvalds { 5169cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 51701da177e4SLinus Torvalds } 51711da177e4SLinus Torvalds 51725f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 51735f729eaaSJulien Gomes { 51745f729eaaSJulien Gomes switch (group) { 51755f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 51765f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 51775f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 51785f729eaaSJulien Gomes return -EPERM; 51795f729eaaSJulien Gomes break; 51805f729eaaSJulien Gomes } 51815f729eaaSJulien Gomes return 0; 51825f729eaaSJulien Gomes } 51835f729eaaSJulien Gomes 51841da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 51851da177e4SLinus Torvalds { 5186351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5187e9dc8653SEric W. Biederman 51881da177e4SLinus Torvalds switch (event) { 51895138e86fSVlad Yasevich case NETDEV_REBOOT: 51908a212589SXin Long case NETDEV_CHANGEMTU: 51913753654eSDavid Ahern case NETDEV_CHANGEADDR: 51925138e86fSVlad Yasevich case NETDEV_CHANGENAME: 51935138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 51945138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 5195e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 51965138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 5197dc709f37SXin Long case NETDEV_CHANGEUPPER: 51985138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 51995138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 5200eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 5201ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 52023d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 520338e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 52041da177e4SLinus Torvalds break; 52051da177e4SLinus Torvalds default: 52061da177e4SLinus Torvalds break; 52071da177e4SLinus Torvalds } 52081da177e4SLinus Torvalds return NOTIFY_DONE; 52091da177e4SLinus Torvalds } 52101da177e4SLinus Torvalds 52111da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 52121da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 52131da177e4SLinus Torvalds }; 52141da177e4SLinus Torvalds 521597c53cacSDenis V. Lunev 52162c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 521797c53cacSDenis V. Lunev { 521897c53cacSDenis V. Lunev struct sock *sk; 5219a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 5220a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 5221a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 5222a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 52239785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 52245f729eaaSJulien Gomes .bind = rtnetlink_bind, 5225a31f2d17SPablo Neira Ayuso }; 5226a31f2d17SPablo Neira Ayuso 52279f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 522897c53cacSDenis V. Lunev if (!sk) 522997c53cacSDenis V. Lunev return -ENOMEM; 523097c53cacSDenis V. Lunev net->rtnl = sk; 523197c53cacSDenis V. Lunev return 0; 523297c53cacSDenis V. Lunev } 523397c53cacSDenis V. Lunev 52342c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 523597c53cacSDenis V. Lunev { 5236b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 523797c53cacSDenis V. Lunev net->rtnl = NULL; 523897c53cacSDenis V. Lunev } 523997c53cacSDenis V. Lunev 524097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 524197c53cacSDenis V. Lunev .init = rtnetlink_net_init, 524297c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 524397c53cacSDenis V. Lunev }; 524497c53cacSDenis V. Lunev 52451da177e4SLinus Torvalds void __init rtnetlink_init(void) 52461da177e4SLinus Torvalds { 524797c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 52481da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 524997c53cacSDenis V. Lunev 52501da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 5251340d17fcSThomas Graf 5252c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5253b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 5254b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5255b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5256b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5257687ad8ccSThomas Graf 5258b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5259b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5260b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 526177162022SJohn Fastabend 5262b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5263b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 52645b2f94b2SRoopa Prabhu rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5265e5a55a89SJohn Fastabend 5266b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5267b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5268b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 526910c9ead9SRoopa Prabhu 527010c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5271b97bac64SFlorian Westphal 0); 52721da177e4SLinus Torvalds } 5273