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 62ccf8dbcdSKees Cook #define RTNL_MAX_TYPE 48 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); 2447e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 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) { 24923729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 24935f9021cfSJohannes Berg if (err < 0) 24945f9021cfSJohannes Berg goto errout; 24950157f60cSPatrick McHardy } 24960157f60cSPatrick McHardy 2497fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 249833eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2499fbaec0eaSJiri Pirko if (err) 2500fbaec0eaSJiri Pirko goto errout; 250190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2502fbaec0eaSJiri Pirko } 2503fbaec0eaSJiri Pirko 25049a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 25059a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 25069a57247fSJiri Pirko if (err) 25079a57247fSJiri Pirko goto errout; 250890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 25099a57247fSJiri Pirko } 25109a57247fSJiri Pirko 25115d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 25120cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 25135d1180fcSNicolas Dichtel 25146a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 25156a643ddbSCong Wang if (err) 251608294a26SJason Wang goto errout; 25172d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 251808294a26SJason Wang } 25190157f60cSPatrick McHardy 252046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 252146e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 252246e6b992SStephen Hemminger 252346e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 252446e6b992SStephen Hemminger err = -EINVAL; 252546e6b992SStephen Hemminger goto errout; 252646e6b992SStephen Hemminger } 252746e6b992SStephen Hemminger 252846e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 252946e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 253046e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 253146e6b992SStephen Hemminger } 253246e6b992SStephen Hemminger } 253346e6b992SStephen Hemminger 253446e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 253546e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 253646e6b992SStephen Hemminger 253746e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 253846e6b992SStephen Hemminger err = -EINVAL; 253946e6b992SStephen Hemminger goto errout; 254046e6b992SStephen Hemminger } 254146e6b992SStephen Hemminger 254246e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 254346e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 254446e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 254546e6b992SStephen Hemminger } 254646e6b992SStephen Hemminger } 254746e6b992SStephen Hemminger 25480157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 254993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 25500157f60cSPatrick McHardy 25510157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 25521889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 25531889b0e7SNicolas Dichtel 25540157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 25551889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2556ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 25571889b0e7SNicolas Dichtel dev->link_mode = value; 255893b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 25590157f60cSPatrick McHardy } 25600157f60cSPatrick McHardy 2561c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 25624f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2563c02db8c6SChris Wright struct nlattr *attr; 2564c02db8c6SChris Wright int rem; 25654f7d2cdfSDaniel Borkmann 2566c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 25674f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 25684f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2569253683bbSDavid Howells err = -EINVAL; 2570c02db8c6SChris Wright goto errout; 2571253683bbSDavid Howells } 25724f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2573fceb6435SJohannes Berg ifla_vf_policy, NULL); 25744f7d2cdfSDaniel Borkmann if (err < 0) 25754f7d2cdfSDaniel Borkmann goto errout; 25764f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2577ebc08a6fSWilliams, Mitch A if (err < 0) 2578ebc08a6fSWilliams, Mitch A goto errout; 2579ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2580ebc08a6fSWilliams, Mitch A } 2581ebc08a6fSWilliams, Mitch A } 25820157f60cSPatrick McHardy err = 0; 25830157f60cSPatrick McHardy 258457b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 258557b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 258657b61080SScott Feldman struct nlattr *attr; 258757b61080SScott Feldman int vf; 258857b61080SScott Feldman int rem; 258957b61080SScott Feldman 259057b61080SScott Feldman err = -EOPNOTSUPP; 259157b61080SScott Feldman if (!ops->ndo_set_vf_port) 259257b61080SScott Feldman goto errout; 259357b61080SScott Feldman 259457b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2595035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2596035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2597035d210fSDaniel Borkmann err = -EINVAL; 2598035d210fSDaniel Borkmann goto errout; 2599035d210fSDaniel Borkmann } 2600035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2601fceb6435SJohannes Berg ifla_port_policy, NULL); 260257b61080SScott Feldman if (err < 0) 260357b61080SScott Feldman goto errout; 260457b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 260557b61080SScott Feldman err = -EOPNOTSUPP; 260657b61080SScott Feldman goto errout; 260757b61080SScott Feldman } 260857b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 260957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 261057b61080SScott Feldman if (err < 0) 261157b61080SScott Feldman goto errout; 2612ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 261357b61080SScott Feldman } 261457b61080SScott Feldman } 261557b61080SScott Feldman err = 0; 261657b61080SScott Feldman 261757b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 261857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 261957b61080SScott Feldman 262057b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2621fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2622fceb6435SJohannes Berg NULL); 262357b61080SScott Feldman if (err < 0) 262457b61080SScott Feldman goto errout; 262557b61080SScott Feldman 262657b61080SScott Feldman err = -EOPNOTSUPP; 262757b61080SScott Feldman if (ops->ndo_set_vf_port) 262857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 262957b61080SScott Feldman if (err < 0) 263057b61080SScott Feldman goto errout; 2631ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 263257b61080SScott Feldman } 2633f8ff182cSThomas Graf 2634f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2635f8ff182cSThomas Graf struct nlattr *af; 2636f8ff182cSThomas Graf int rem; 2637f8ff182cSThomas Graf 2638f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2639f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2640f8ff182cSThomas Graf 26415fa85a09SFlorian Westphal rcu_read_lock(); 26425fa85a09SFlorian Westphal 2643058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2644f8ff182cSThomas Graf 2645cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 26465fa85a09SFlorian Westphal if (err < 0) { 26475fa85a09SFlorian Westphal rcu_read_unlock(); 2648f8ff182cSThomas Graf goto errout; 26495fa85a09SFlorian Westphal } 2650f8ff182cSThomas Graf 26515fa85a09SFlorian Westphal rcu_read_unlock(); 2652ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2653f8ff182cSThomas Graf } 2654f8ff182cSThomas Graf } 265557b61080SScott Feldman err = 0; 265657b61080SScott Feldman 265788d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 265888d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 265988d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 266088d6378bSAnuradha Karuppiah if (err) 266188d6378bSAnuradha Karuppiah goto errout; 266288d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 266388d6378bSAnuradha Karuppiah } 266488d6378bSAnuradha Karuppiah 2665d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2666d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 266785de8576SDaniel Borkmann u32 xdp_flags = 0; 2668d1fdd913SBrenden Blanco 2669d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2670fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2671d1fdd913SBrenden Blanco if (err < 0) 2672d1fdd913SBrenden Blanco goto errout; 2673d1fdd913SBrenden Blanco 267458038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2675262d8625SBrenden Blanco err = -EINVAL; 2676262d8625SBrenden Blanco goto errout; 2677262d8625SBrenden Blanco } 267885de8576SDaniel Borkmann 267985de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 268085de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 268185de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 268285de8576SDaniel Borkmann err = -EINVAL; 268385de8576SDaniel Borkmann goto errout; 268485de8576SDaniel Borkmann } 2685ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 26860489df9aSDaniel Borkmann err = -EINVAL; 26870489df9aSDaniel Borkmann goto errout; 26880489df9aSDaniel Borkmann } 268985de8576SDaniel Borkmann } 269085de8576SDaniel Borkmann 2691d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2692ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 269385de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 269485de8576SDaniel Borkmann xdp_flags); 2695d1fdd913SBrenden Blanco if (err) 2696d1fdd913SBrenden Blanco goto errout; 2697d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2698d1fdd913SBrenden Blanco } 2699d1fdd913SBrenden Blanco } 2700d1fdd913SBrenden Blanco 27010157f60cSPatrick McHardy errout: 2702ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 270364ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2704ba998906SNicolas Dichtel netdev_state_change(dev); 2705ba998906SNicolas Dichtel 2706ba998906SNicolas Dichtel if (err < 0) 2707e87cc472SJoe 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", 2708e87cc472SJoe Perches dev->name); 2709ba998906SNicolas Dichtel } 27100157f60cSPatrick McHardy 27110157f60cSPatrick McHardy return err; 27120157f60cSPatrick McHardy } 27130157f60cSPatrick McHardy 2714c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2715c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2716da5e0494SThomas Graf { 27173b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2718da5e0494SThomas Graf struct ifinfomsg *ifm; 2719da5e0494SThomas Graf struct net_device *dev; 27200157f60cSPatrick McHardy int err; 2721da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 27221da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 27231da177e4SLinus Torvalds 2724c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2725c21ef3e3SDavid Ahern extack); 2726da5e0494SThomas Graf if (err < 0) 2727da5e0494SThomas Graf goto errout; 27281da177e4SLinus Torvalds 27294ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 27304ff66caeSChristian Brauner if (err < 0) 27314ff66caeSChristian Brauner goto errout; 27324ff66caeSChristian Brauner 27335176f91eSThomas Graf if (tb[IFLA_IFNAME]) 27345176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 273578e5b891SPatrick McHardy else 273678e5b891SPatrick McHardy ifname[0] = '\0'; 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds err = -EINVAL; 2739da5e0494SThomas Graf ifm = nlmsg_data(nlh); 274051055be8SPatrick McHardy if (ifm->ifi_index > 0) 2741a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2742da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2743a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2744da5e0494SThomas Graf else 2745da5e0494SThomas Graf goto errout; 27461da177e4SLinus Torvalds 2747da5e0494SThomas Graf if (dev == NULL) { 2748da5e0494SThomas Graf err = -ENODEV; 2749da5e0494SThomas Graf goto errout; 2750da5e0494SThomas Graf } 27511da177e4SLinus Torvalds 2752ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2753da5e0494SThomas Graf errout: 27541da177e4SLinus Torvalds return err; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 275766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 275866400d54SWANG Cong { 275966400d54SWANG Cong struct net_device *dev, *aux; 276066400d54SWANG Cong LIST_HEAD(list_kill); 276166400d54SWANG Cong bool found = false; 276266400d54SWANG Cong 276366400d54SWANG Cong if (!group) 276466400d54SWANG Cong return -EPERM; 276566400d54SWANG Cong 276666400d54SWANG Cong for_each_netdev(net, dev) { 276766400d54SWANG Cong if (dev->group == group) { 276866400d54SWANG Cong const struct rtnl_link_ops *ops; 276966400d54SWANG Cong 277066400d54SWANG Cong found = true; 277166400d54SWANG Cong ops = dev->rtnl_link_ops; 277266400d54SWANG Cong if (!ops || !ops->dellink) 277366400d54SWANG Cong return -EOPNOTSUPP; 277466400d54SWANG Cong } 277566400d54SWANG Cong } 277666400d54SWANG Cong 277766400d54SWANG Cong if (!found) 277866400d54SWANG Cong return -ENODEV; 277966400d54SWANG Cong 278066400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 278166400d54SWANG Cong if (dev->group == group) { 278266400d54SWANG Cong const struct rtnl_link_ops *ops; 278366400d54SWANG Cong 278466400d54SWANG Cong ops = dev->rtnl_link_ops; 278566400d54SWANG Cong ops->dellink(dev, &list_kill); 278666400d54SWANG Cong } 278766400d54SWANG Cong } 278866400d54SWANG Cong unregister_netdevice_many(&list_kill); 278966400d54SWANG Cong 279066400d54SWANG Cong return 0; 279166400d54SWANG Cong } 279266400d54SWANG Cong 2793614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2794614732eaSThomas Graf { 2795614732eaSThomas Graf const struct rtnl_link_ops *ops; 2796614732eaSThomas Graf LIST_HEAD(list_kill); 2797614732eaSThomas Graf 2798614732eaSThomas Graf ops = dev->rtnl_link_ops; 2799614732eaSThomas Graf if (!ops || !ops->dellink) 2800614732eaSThomas Graf return -EOPNOTSUPP; 2801614732eaSThomas Graf 2802614732eaSThomas Graf ops->dellink(dev, &list_kill); 2803614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2804614732eaSThomas Graf 2805614732eaSThomas Graf return 0; 2806614732eaSThomas Graf } 2807614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2808614732eaSThomas Graf 2809c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2810c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 281138f7b870SPatrick McHardy { 28123b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2813b61ad68aSChristian Brauner struct net *tgt_net = net; 2814b61ad68aSChristian Brauner struct net_device *dev = NULL; 281538f7b870SPatrick McHardy struct ifinfomsg *ifm; 281638f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 281738f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 281838f7b870SPatrick McHardy int err; 2819b61ad68aSChristian Brauner int netnsid = -1; 282038f7b870SPatrick McHardy 2821c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 282238f7b870SPatrick McHardy if (err < 0) 282338f7b870SPatrick McHardy return err; 282438f7b870SPatrick McHardy 28254ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 28264ff66caeSChristian Brauner if (err < 0) 28274ff66caeSChristian Brauner return err; 28284ff66caeSChristian Brauner 282938f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 283038f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 283138f7b870SPatrick McHardy 28327e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 28337e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 2834c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 2835b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2836b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2837b61ad68aSChristian Brauner } 2838b61ad68aSChristian Brauner 2839b61ad68aSChristian Brauner err = -EINVAL; 284038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 284138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2842b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 284338f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2844b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 284566400d54SWANG Cong else if (tb[IFLA_GROUP]) 2846b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 284738f7b870SPatrick McHardy else 2848b61ad68aSChristian Brauner goto out; 284938f7b870SPatrick McHardy 2850b61ad68aSChristian Brauner if (!dev) { 2851b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2852b61ad68aSChristian Brauner err = -ENODEV; 285338f7b870SPatrick McHardy 2854b61ad68aSChristian Brauner goto out; 2855b61ad68aSChristian Brauner } 2856b61ad68aSChristian Brauner 2857b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2858b61ad68aSChristian Brauner 2859b61ad68aSChristian Brauner out: 2860b61ad68aSChristian Brauner if (netnsid >= 0) 2861b61ad68aSChristian Brauner put_net(tgt_net); 2862b61ad68aSChristian Brauner 2863b61ad68aSChristian Brauner return err; 286438f7b870SPatrick McHardy } 286538f7b870SPatrick McHardy 28663729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 28673729d502SPatrick McHardy { 28683729d502SPatrick McHardy unsigned int old_flags; 28693729d502SPatrick McHardy int err; 28703729d502SPatrick McHardy 28713729d502SPatrick McHardy old_flags = dev->flags; 28723729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 28733729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 28743729d502SPatrick McHardy if (err < 0) 28753729d502SPatrick McHardy return err; 28763729d502SPatrick McHardy } 28773729d502SPatrick McHardy 28788d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 287956a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 28808d356b89SRoopa Prabhu } else { 28813729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2882a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 28838d356b89SRoopa Prabhu } 28843729d502SPatrick McHardy return 0; 28853729d502SPatrick McHardy } 28863729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 28873729d502SPatrick McHardy 2888c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 288978ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 28905517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2891e7199288SPavel Emelianov { 2892e7199288SPavel Emelianov struct net_device *dev; 2893d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2894d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2895e7199288SPavel Emelianov 289676ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 289776ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 289876ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2899d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 290076ff5cc9SJiri Pirko 290176ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 290276ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 290376ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2904d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2905efacb309Sstephen hemminger 29060e1d6ecaSEric Dumazet if (num_tx_queues < 1 || num_tx_queues > 4096) 29070e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 29080e1d6ecaSEric Dumazet 29090e1d6ecaSEric Dumazet if (num_rx_queues < 1 || num_rx_queues > 4096) 29100e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 29110e1d6ecaSEric Dumazet 29125517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2913c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2914e7199288SPavel Emelianov if (!dev) 2915d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2916e7199288SPavel Emelianov 291781adee47SEric W. Biederman dev_net_set(dev, net); 291881adee47SEric W. Biederman dev->rtnl_link_ops = ops; 29193729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 292081adee47SEric W. Biederman 2921e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2922e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 29232afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2924e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2925e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 29262afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 29272afb9b53SJiri Pirko } 2928e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2929e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2930e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2931e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2932e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2933e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 293493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2935e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2936e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2937ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2938ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 293946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 294046e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 294146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2942a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2943e7199288SPavel Emelianov 2944e7199288SPavel Emelianov return dev; 2945e7199288SPavel Emelianov } 2946e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2947e7199288SPavel Emelianov 294890f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 294990f62cf3SEric W. Biederman struct net *net, int group, 2950e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2951ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2952e7ed828fSVlad Dogaru struct nlattr **tb) 2953e7ed828fSVlad Dogaru { 2954d079535dSWANG Cong struct net_device *dev, *aux; 2955e7ed828fSVlad Dogaru int err; 2956e7ed828fSVlad Dogaru 2957d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2958e7ed828fSVlad Dogaru if (dev->group == group) { 2959ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2960e7ed828fSVlad Dogaru if (err < 0) 2961e7ed828fSVlad Dogaru return err; 2962e7ed828fSVlad Dogaru } 2963e7ed828fSVlad Dogaru } 2964e7ed828fSVlad Dogaru 2965e7ed828fSVlad Dogaru return 0; 2966e7ed828fSVlad Dogaru } 2967e7ed828fSVlad Dogaru 2968c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2969c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 297038f7b870SPatrick McHardy { 29713b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 297238f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2973ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 297438f7b870SPatrick McHardy struct net_device *dev; 2975ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 297638f7b870SPatrick McHardy struct ifinfomsg *ifm; 297738f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 297838f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 297938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 298038f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 29815517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 298238f7b870SPatrick McHardy int err; 298338f7b870SPatrick McHardy 298495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 298538f7b870SPatrick McHardy replay: 29868072f085SThomas Graf #endif 2987c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 298838f7b870SPatrick McHardy if (err < 0) 298938f7b870SPatrick McHardy return err; 299038f7b870SPatrick McHardy 29914ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 29924ff66caeSChristian Brauner if (err < 0) 29934ff66caeSChristian Brauner return err; 29944ff66caeSChristian Brauner 299538f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 299638f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 299738f7b870SPatrick McHardy else 299838f7b870SPatrick McHardy ifname[0] = '\0'; 299938f7b870SPatrick McHardy 300038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 300138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 3002881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 3003e7ed828fSVlad Dogaru else { 3004e7ed828fSVlad Dogaru if (ifname[0]) 3005881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 300638f7b870SPatrick McHardy else 300738f7b870SPatrick McHardy dev = NULL; 3008e7ed828fSVlad Dogaru } 300938f7b870SPatrick McHardy 3010ba7d49b1SJiri Pirko if (dev) { 3011ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 3012ba7d49b1SJiri Pirko if (master_dev) 3013ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 3014ba7d49b1SJiri Pirko } 3015ba7d49b1SJiri Pirko 3016e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 3017e0d087afSEric Dumazet if (err < 0) 30181840bb13SThomas Graf return err; 30191840bb13SThomas Graf 302038f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 302138f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 3022fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 3023fceb6435SJohannes Berg NULL); 302438f7b870SPatrick McHardy if (err < 0) 302538f7b870SPatrick McHardy return err; 302638f7b870SPatrick McHardy } else 302738f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 302838f7b870SPatrick McHardy 302938f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 303038f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 303138f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 303238f7b870SPatrick McHardy } else { 303338f7b870SPatrick McHardy kind[0] = '\0'; 303438f7b870SPatrick McHardy ops = NULL; 303538f7b870SPatrick McHardy } 303638f7b870SPatrick McHardy 303738f7b870SPatrick McHardy if (1) { 3038ccf8dbcdSKees Cook struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3039ccf8dbcdSKees Cook struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3040ba7d49b1SJiri Pirko struct nlattr **data = NULL; 3041ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 3042317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 304338f7b870SPatrick McHardy 304438f7b870SPatrick McHardy if (ops) { 3045ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 3046ccf8dbcdSKees Cook return -EINVAL; 3047ccf8dbcdSKees Cook 304838f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 304938f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 305038f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 3051fceb6435SJohannes Berg ops->policy, NULL); 305238f7b870SPatrick McHardy if (err < 0) 305338f7b870SPatrick McHardy return err; 305438f7b870SPatrick McHardy data = attr; 305538f7b870SPatrick McHardy } 305638f7b870SPatrick McHardy if (ops->validate) { 3057a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 305838f7b870SPatrick McHardy if (err < 0) 305938f7b870SPatrick McHardy return err; 306038f7b870SPatrick McHardy } 306138f7b870SPatrick McHardy } 306238f7b870SPatrick McHardy 3063ba7d49b1SJiri Pirko if (m_ops) { 3064af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3065ccf8dbcdSKees Cook return -EINVAL; 3066ccf8dbcdSKees Cook 3067ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3068ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3069ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 3070ba7d49b1SJiri Pirko m_ops->slave_maxtype, 3071ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 3072fceb6435SJohannes Berg m_ops->slave_policy, 3073fceb6435SJohannes Berg NULL); 3074ba7d49b1SJiri Pirko if (err < 0) 3075ba7d49b1SJiri Pirko return err; 3076ba7d49b1SJiri Pirko slave_data = slave_attr; 3077ba7d49b1SJiri Pirko } 3078ba7d49b1SJiri Pirko } 3079ba7d49b1SJiri Pirko 308038f7b870SPatrick McHardy if (dev) { 308190c325e3SNicolas Dichtel int status = 0; 308238f7b870SPatrick McHardy 308338f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 308438f7b870SPatrick McHardy return -EEXIST; 308538f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 308638f7b870SPatrick McHardy return -EOPNOTSUPP; 308738f7b870SPatrick McHardy 308838f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 308938f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 309038f7b870SPatrick McHardy !ops->changelink) 309138f7b870SPatrick McHardy return -EOPNOTSUPP; 309238f7b870SPatrick McHardy 3093ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 309438f7b870SPatrick McHardy if (err < 0) 309538f7b870SPatrick McHardy return err; 3096ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 309738f7b870SPatrick McHardy } 309838f7b870SPatrick McHardy 3099ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3100ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3101ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3102ba7d49b1SJiri Pirko 3103ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 310417dd0ec4SMatthias Schiffer tb, slave_data, 310517dd0ec4SMatthias Schiffer extack); 3106ba7d49b1SJiri Pirko if (err < 0) 3107ba7d49b1SJiri Pirko return err; 3108ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3109ba7d49b1SJiri Pirko } 3110ba7d49b1SJiri Pirko 3111ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 3112ddf9f970SJakub Kicinski status); 311338f7b870SPatrick McHardy } 311438f7b870SPatrick McHardy 3115ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3116ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 311790f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3118ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3119ddf9f970SJakub Kicinski ifm, extack, tb); 312038f7b870SPatrick McHardy return -ENODEV; 3121ffa934f1SPatrick McHardy } 312238f7b870SPatrick McHardy 3123160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 312438f7b870SPatrick McHardy return -EOPNOTSUPP; 312538f7b870SPatrick McHardy 312638f7b870SPatrick McHardy if (!ops) { 312795a5afcaSJohannes Berg #ifdef CONFIG_MODULES 312838f7b870SPatrick McHardy if (kind[0]) { 312938f7b870SPatrick McHardy __rtnl_unlock(); 313038f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 313138f7b870SPatrick McHardy rtnl_lock(); 313238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 313338f7b870SPatrick McHardy if (ops) 313438f7b870SPatrick McHardy goto replay; 313538f7b870SPatrick McHardy } 313638f7b870SPatrick McHardy #endif 313738f7b870SPatrick McHardy return -EOPNOTSUPP; 313838f7b870SPatrick McHardy } 313938f7b870SPatrick McHardy 3140b0ab2fabSJiri Pirko if (!ops->setup) 3141b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3142b0ab2fabSJiri Pirko 31435517750fSTom Gundersen if (!ifname[0]) { 314438f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 31455517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 31465517750fSTom Gundersen } 314738f7b870SPatrick McHardy 31485bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 314913ad1774SEric W. Biederman if (IS_ERR(dest_net)) 315013ad1774SEric W. Biederman return PTR_ERR(dest_net); 315113ad1774SEric W. Biederman 3152317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3153317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3154317f4810SNicolas Dichtel 3155317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3156317f4810SNicolas Dichtel if (!link_net) { 3157317f4810SNicolas Dichtel err = -EINVAL; 3158317f4810SNicolas Dichtel goto out; 3159317f4810SNicolas Dichtel } 316006615bedSEric W. Biederman err = -EPERM; 316106615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 316206615bedSEric W. Biederman goto out; 3163317f4810SNicolas Dichtel } 3164317f4810SNicolas Dichtel 3165317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3166317f4810SNicolas Dichtel name_assign_type, ops, tb); 31679c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3168e7199288SPavel Emelianov err = PTR_ERR(dev); 31699c7dafbfSPavel Emelyanov goto out; 31709c7dafbfSPavel Emelyanov } 31719c7dafbfSPavel Emelyanov 31729c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 31739c7dafbfSPavel Emelyanov 31740e0eee24SCong Wang if (ops->newlink) { 31757a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 31767a3f4a18SMatthias Schiffer extack); 31770e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3178e51fb152SCong Wang * and unregister it on failure after registration 3179e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 31800e0eee24SCong Wang */ 3181e51fb152SCong Wang if (err < 0) { 3182e51fb152SCong Wang /* If device is not registered at all, free it now */ 3183e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3184e51fb152SCong Wang free_netdev(dev); 31850e0eee24SCong Wang goto out; 3186e51fb152SCong Wang } 31870e0eee24SCong Wang } else { 31882d85cba2SPatrick McHardy err = register_netdevice(dev); 3189fce9b9beSDan Carpenter if (err < 0) { 319038f7b870SPatrick McHardy free_netdev(dev); 31913729d502SPatrick McHardy goto out; 3192fce9b9beSDan Carpenter } 31930e0eee24SCong Wang } 31943729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 319543638900SDavid S. Miller if (err < 0) 319643638900SDavid S. Miller goto out_unregister; 319743638900SDavid S. Miller if (link_net) { 319843638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 319943638900SDavid S. Miller if (err < 0) 320043638900SDavid S. Miller goto out_unregister; 320143638900SDavid S. Miller } 3202160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 320333eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 320433eaf2a6SDavid Ahern extack); 3205160ca014STheuns Verwoerd if (err) 3206160ca014STheuns Verwoerd goto out_unregister; 3207160ca014STheuns Verwoerd } 320843638900SDavid S. Miller out: 320943638900SDavid S. Miller if (link_net) 321043638900SDavid S. Miller put_net(link_net); 321143638900SDavid S. Miller put_net(dest_net); 321243638900SDavid S. Miller return err; 321343638900SDavid S. Miller out_unregister: 32147afb8886SWANG Cong if (ops->newlink) { 32157afb8886SWANG Cong LIST_HEAD(list_kill); 32167afb8886SWANG Cong 32177afb8886SWANG Cong ops->dellink(dev, &list_kill); 32187afb8886SWANG Cong unregister_netdevice_many(&list_kill); 32197afb8886SWANG Cong } else { 32203729d502SPatrick McHardy unregister_netdevice(dev); 32217afb8886SWANG Cong } 3222317f4810SNicolas Dichtel goto out; 3223317f4810SNicolas Dichtel } 322438f7b870SPatrick McHardy } 322538f7b870SPatrick McHardy 3226c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3227c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3228711e2c33SJean Tourrilhes { 32293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 323079e1ad14SJiri Benc struct net *tgt_net = net; 3231b60c5115SThomas Graf struct ifinfomsg *ifm; 3232a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3233b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3234b60c5115SThomas Graf struct net_device *dev = NULL; 3235b60c5115SThomas Graf struct sk_buff *nskb; 323679e1ad14SJiri Benc int netnsid = -1; 3237339bf98fSThomas Graf int err; 3238115c9b81SGreg Rose u32 ext_filter_mask = 0; 3239711e2c33SJean Tourrilhes 3240c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3241b60c5115SThomas Graf if (err < 0) 32429918f230SEric Sesterhenn return err; 3243b60c5115SThomas Graf 32444ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 32454ff66caeSChristian Brauner if (err < 0) 32464ff66caeSChristian Brauner return err; 32474ff66caeSChristian Brauner 32487e4a8d5aSChristian Brauner if (tb[IFLA_TARGET_NETNSID]) { 32497e4a8d5aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3250c383edc4SChristian Brauner tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 325179e1ad14SJiri Benc if (IS_ERR(tgt_net)) 325279e1ad14SJiri Benc return PTR_ERR(tgt_net); 325379e1ad14SJiri Benc } 325479e1ad14SJiri Benc 3255a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3256a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3257a3d12891SEric Dumazet 3258115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3259115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3260115c9b81SGreg Rose 326179e1ad14SJiri Benc err = -EINVAL; 3262b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3263a3d12891SEric Dumazet if (ifm->ifi_index > 0) 326479e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3265a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 326679e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3267a3d12891SEric Dumazet else 326879e1ad14SJiri Benc goto out; 3269b60c5115SThomas Graf 327079e1ad14SJiri Benc err = -ENODEV; 3271a3d12891SEric Dumazet if (dev == NULL) 327279e1ad14SJiri Benc goto out; 3273a3d12891SEric Dumazet 327479e1ad14SJiri Benc err = -ENOBUFS; 3275115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3276a3d12891SEric Dumazet if (nskb == NULL) 327779e1ad14SJiri Benc goto out; 3278711e2c33SJean Tourrilhes 327979e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 328079e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 328179e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 328238e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 328326932566SPatrick McHardy if (err < 0) { 328426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 328526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 328626932566SPatrick McHardy kfree_skb(nskb); 3287a3d12891SEric Dumazet } else 328815e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 328979e1ad14SJiri Benc out: 329079e1ad14SJiri Benc if (netnsid >= 0) 329179e1ad14SJiri Benc put_net(tgt_net); 3292b60c5115SThomas Graf 3293711e2c33SJean Tourrilhes return err; 3294711e2c33SJean Tourrilhes } 3295711e2c33SJean Tourrilhes 3296115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3297c7ac8679SGreg Rose { 3298115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3299115c9b81SGreg Rose struct net_device *dev; 3300115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3301115c9b81SGreg Rose u32 ext_filter_mask = 0; 3302115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3303e5eca6d4SMichal Schmidt int hdrlen; 3304115c9b81SGreg Rose 3305e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3306e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3307e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3308e5eca6d4SMichal Schmidt 3309fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3310115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3311115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3312a4b64fbeSEric Dumazet } 3313115c9b81SGreg Rose 3314115c9b81SGreg Rose if (!ext_filter_mask) 3315115c9b81SGreg Rose return NLMSG_GOODSIZE; 3316115c9b81SGreg Rose /* 3317115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3318115c9b81SGreg Rose * buffer size based upon the filter mask. 3319115c9b81SGreg Rose */ 33206853dd48SFlorian Westphal rcu_read_lock(); 33216853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3322115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3323115c9b81SGreg Rose if_nlmsg_size(dev, 3324115c9b81SGreg Rose ext_filter_mask)); 3325115c9b81SGreg Rose } 33266853dd48SFlorian Westphal rcu_read_unlock(); 3327115c9b81SGreg Rose 332893af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3329c7ac8679SGreg Rose } 3330c7ac8679SGreg Rose 333142bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 33321da177e4SLinus Torvalds { 33331da177e4SLinus Torvalds int idx; 33341da177e4SLinus Torvalds int s_idx = cb->family; 333587ccbb1fSChristian Brauner int type = cb->nlh->nlmsg_type - RTM_BASE; 33361da177e4SLinus Torvalds 33371da177e4SLinus Torvalds if (s_idx == 0) 33381da177e4SLinus Torvalds s_idx = 1; 33396853dd48SFlorian Westphal 334025239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3341addf9b90SFlorian Westphal struct rtnl_link **tab; 3342addf9b90SFlorian Westphal struct rtnl_link *link; 33436853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 33446853dd48SFlorian Westphal 33451da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 33461da177e4SLinus Torvalds continue; 33476853dd48SFlorian Westphal 3348addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 33491da177e4SLinus Torvalds continue; 33506853dd48SFlorian Westphal 3351addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3352addf9b90SFlorian Westphal if (!tab) 3353addf9b90SFlorian Westphal continue; 3354addf9b90SFlorian Westphal 3355addf9b90SFlorian Westphal link = tab[type]; 3356addf9b90SFlorian Westphal if (!link) 3357addf9b90SFlorian Westphal continue; 3358addf9b90SFlorian Westphal 3359addf9b90SFlorian Westphal dumpit = link->dumpit; 33606853dd48SFlorian Westphal if (!dumpit) 33616853dd48SFlorian Westphal continue; 33626853dd48SFlorian Westphal 33630465277fSNicolas Dichtel if (idx > s_idx) { 33641da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 33650465277fSNicolas Dichtel cb->prev_seq = 0; 33660465277fSNicolas Dichtel cb->seq = 0; 33670465277fSNicolas Dichtel } 33686853dd48SFlorian Westphal if (dumpit(skb, cb)) 33691da177e4SLinus Torvalds break; 33701da177e4SLinus Torvalds } 33711da177e4SLinus Torvalds cb->family = idx; 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds return skb->len; 33741da177e4SLinus Torvalds } 33751da177e4SLinus Torvalds 3376395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 33773d3ea5afSVlad Yasevich unsigned int change, 337838e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 337938e01b30SNicolas Dichtel int new_ifindex) 33801da177e4SLinus Torvalds { 3381c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 33821da177e4SLinus Torvalds struct sk_buff *skb; 33830ec6d3f4SThomas Graf int err = -ENOBUFS; 3384c7ac8679SGreg Rose size_t if_info_size; 33851da177e4SLinus Torvalds 33867f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 33870ec6d3f4SThomas Graf if (skb == NULL) 33880ec6d3f4SThomas Graf goto errout; 33891da177e4SLinus Torvalds 339079e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 339179e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 339238e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 339326932566SPatrick McHardy if (err < 0) { 339426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 339526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 339626932566SPatrick McHardy kfree_skb(skb); 339726932566SPatrick McHardy goto errout; 339826932566SPatrick McHardy } 3399395eea6cSMahesh Bandewar return skb; 34000ec6d3f4SThomas Graf errout: 34010ec6d3f4SThomas Graf if (err < 0) 34024b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3403395eea6cSMahesh Bandewar return NULL; 3404395eea6cSMahesh Bandewar } 3405395eea6cSMahesh Bandewar 3406395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3407395eea6cSMahesh Bandewar { 3408395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3409395eea6cSMahesh Bandewar 3410395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3411395eea6cSMahesh Bandewar } 3412395eea6cSMahesh Bandewar 34133d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 34143d3ea5afSVlad Yasevich unsigned int change, u32 event, 341538e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3416395eea6cSMahesh Bandewar { 3417395eea6cSMahesh Bandewar struct sk_buff *skb; 3418395eea6cSMahesh Bandewar 3419ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3420ed2a80abSNicolas Dichtel return; 3421ed2a80abSNicolas Dichtel 342238e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 342338e01b30SNicolas Dichtel new_ifindex); 3424395eea6cSMahesh Bandewar if (skb) 3425395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 34261da177e4SLinus Torvalds } 34273d3ea5afSVlad Yasevich 34283d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 34293d3ea5afSVlad Yasevich gfp_t flags) 34303d3ea5afSVlad Yasevich { 343138e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 343238e01b30SNicolas Dichtel NULL, 0); 34333d3ea5afSVlad Yasevich } 34341da177e4SLinus Torvalds 34356621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 343638e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 34376621dd29SNicolas Dichtel { 34386621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 343938e01b30SNicolas Dichtel new_nsid, new_ifindex); 34406621dd29SNicolas Dichtel } 34416621dd29SNicolas Dichtel 3442d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3443d83b0603SJohn Fastabend struct net_device *dev, 34441e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 34451c104a6bSNicolas Dichtel int type, unsigned int flags, 3446b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3447d83b0603SJohn Fastabend { 3448d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3449d83b0603SJohn Fastabend struct ndmsg *ndm; 3450d83b0603SJohn Fastabend 34511c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3452d83b0603SJohn Fastabend if (!nlh) 3453d83b0603SJohn Fastabend return -EMSGSIZE; 3454d83b0603SJohn Fastabend 3455d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3456d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3457d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3458d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3459d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3460d83b0603SJohn Fastabend ndm->ndm_type = 0; 3461d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3462b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3463d83b0603SJohn Fastabend 3464d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3465d83b0603SJohn Fastabend goto nla_put_failure; 34661e53d5bbSHubert Sokolowski if (vid) 34671e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 34681e53d5bbSHubert Sokolowski goto nla_put_failure; 3469d83b0603SJohn Fastabend 3470053c095aSJohannes Berg nlmsg_end(skb, nlh); 3471053c095aSJohannes Berg return 0; 3472d83b0603SJohn Fastabend 3473d83b0603SJohn Fastabend nla_put_failure: 3474d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3475d83b0603SJohn Fastabend return -EMSGSIZE; 3476d83b0603SJohn Fastabend } 3477d83b0603SJohn Fastabend 34783ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 34793ff661c3SJohn Fastabend { 3480f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3481f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3482f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3483f82ef3e1SSabrina Dubroca 0; 34843ff661c3SJohn Fastabend } 34853ff661c3SJohn Fastabend 3486b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3487b3379041SHubert Sokolowski u16 ndm_state) 34883ff661c3SJohn Fastabend { 34893ff661c3SJohn Fastabend struct net *net = dev_net(dev); 34903ff661c3SJohn Fastabend struct sk_buff *skb; 34913ff661c3SJohn Fastabend int err = -ENOBUFS; 34923ff661c3SJohn Fastabend 34933ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 34943ff661c3SJohn Fastabend if (!skb) 34953ff661c3SJohn Fastabend goto errout; 34963ff661c3SJohn Fastabend 34971e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3498b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 34993ff661c3SJohn Fastabend if (err < 0) { 35003ff661c3SJohn Fastabend kfree_skb(skb); 35013ff661c3SJohn Fastabend goto errout; 35023ff661c3SJohn Fastabend } 35033ff661c3SJohn Fastabend 35043ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 35053ff661c3SJohn Fastabend return; 35063ff661c3SJohn Fastabend errout: 35073ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 35083ff661c3SJohn Fastabend } 35093ff661c3SJohn Fastabend 3510090096bfSVlad Yasevich /** 3511090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3512090096bfSVlad Yasevich */ 3513090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3514090096bfSVlad Yasevich struct nlattr *tb[], 3515090096bfSVlad Yasevich struct net_device *dev, 3516f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3517090096bfSVlad Yasevich u16 flags) 3518090096bfSVlad Yasevich { 3519090096bfSVlad Yasevich int err = -EINVAL; 3520090096bfSVlad Yasevich 3521090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3522090096bfSVlad Yasevich * implement its own handler for this. 3523090096bfSVlad Yasevich */ 3524090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3525090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3526090096bfSVlad Yasevich return err; 3527090096bfSVlad Yasevich } 3528090096bfSVlad Yasevich 352965891feaSOr Gerlitz if (vid) { 353065891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 353165891feaSOr Gerlitz return err; 353265891feaSOr Gerlitz } 353365891feaSOr Gerlitz 3534090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3535090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3536090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3537090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3538090096bfSVlad Yasevich 3539090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3540090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3541090096bfSVlad Yasevich err = 0; 3542090096bfSVlad Yasevich 3543090096bfSVlad Yasevich return err; 3544090096bfSVlad Yasevich } 3545090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3546090096bfSVlad Yasevich 3547b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3548b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3549f6f6424bSJiri Pirko { 3550f6f6424bSJiri Pirko u16 vid = 0; 3551f6f6424bSJiri Pirko 3552f6f6424bSJiri Pirko if (vlan_attr) { 3553f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3554b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3555f6f6424bSJiri Pirko return -EINVAL; 3556f6f6424bSJiri Pirko } 3557f6f6424bSJiri Pirko 3558f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3559f6f6424bSJiri Pirko 3560f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3561b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3562f6f6424bSJiri Pirko return -EINVAL; 3563f6f6424bSJiri Pirko } 3564f6f6424bSJiri Pirko } 3565f6f6424bSJiri Pirko *p_vid = vid; 3566f6f6424bSJiri Pirko return 0; 3567f6f6424bSJiri Pirko } 3568f6f6424bSJiri Pirko 3569c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3570c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 357177162022SJohn Fastabend { 357277162022SJohn Fastabend struct net *net = sock_net(skb->sk); 357377162022SJohn Fastabend struct ndmsg *ndm; 357477162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 357577162022SJohn Fastabend struct net_device *dev; 357677162022SJohn Fastabend u8 *addr; 3577f6f6424bSJiri Pirko u16 vid; 357877162022SJohn Fastabend int err; 357977162022SJohn Fastabend 3580c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 358177162022SJohn Fastabend if (err < 0) 358277162022SJohn Fastabend return err; 358377162022SJohn Fastabend 358477162022SJohn Fastabend ndm = nlmsg_data(nlh); 358577162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3586b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 358777162022SJohn Fastabend return -EINVAL; 358877162022SJohn Fastabend } 358977162022SJohn Fastabend 359077162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 359177162022SJohn Fastabend if (dev == NULL) { 3592b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 359377162022SJohn Fastabend return -ENODEV; 359477162022SJohn Fastabend } 359577162022SJohn Fastabend 359677162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3597b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 359877162022SJohn Fastabend return -EINVAL; 359977162022SJohn Fastabend } 360077162022SJohn Fastabend 360177162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 360277162022SJohn Fastabend 3603b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3604f6f6424bSJiri Pirko if (err) 3605f6f6424bSJiri Pirko return err; 3606f6f6424bSJiri Pirko 360777162022SJohn Fastabend err = -EOPNOTSUPP; 360877162022SJohn Fastabend 360977162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 361077162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 361177162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3612898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3613898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3614898e5061SJiri Pirko 3615f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3616f6f6424bSJiri Pirko nlh->nlmsg_flags); 361777162022SJohn Fastabend if (err) 361877162022SJohn Fastabend goto out; 361977162022SJohn Fastabend else 362077162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 362177162022SJohn Fastabend } 362277162022SJohn Fastabend 362377162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3624090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3625090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3626090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3627f6f6424bSJiri Pirko vid, 3628090096bfSVlad Yasevich nlh->nlmsg_flags); 3629090096bfSVlad Yasevich else 3630f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 363177162022SJohn Fastabend nlh->nlmsg_flags); 363277162022SJohn Fastabend 36333ff661c3SJohn Fastabend if (!err) { 3634b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3635b3379041SHubert Sokolowski ndm->ndm_state); 363677162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 363777162022SJohn Fastabend } 36383ff661c3SJohn Fastabend } 363977162022SJohn Fastabend out: 364077162022SJohn Fastabend return err; 364177162022SJohn Fastabend } 364277162022SJohn Fastabend 3643090096bfSVlad Yasevich /** 3644090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3645090096bfSVlad Yasevich */ 3646090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3647090096bfSVlad Yasevich struct nlattr *tb[], 3648090096bfSVlad Yasevich struct net_device *dev, 3649f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3650090096bfSVlad Yasevich { 3651c8a89c4aSAlexander Duyck int err = -EINVAL; 3652090096bfSVlad Yasevich 3653090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3654090096bfSVlad Yasevich * implement its own handler for this. 3655090096bfSVlad Yasevich */ 365664535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3657090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3658c8a89c4aSAlexander Duyck return err; 3659090096bfSVlad Yasevich } 3660090096bfSVlad Yasevich 3661090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3662090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3663090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3664090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3665090096bfSVlad Yasevich 3666090096bfSVlad Yasevich return err; 3667090096bfSVlad Yasevich } 3668090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3669090096bfSVlad Yasevich 3670c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3671c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 367277162022SJohn Fastabend { 367377162022SJohn Fastabend struct net *net = sock_net(skb->sk); 367477162022SJohn Fastabend struct ndmsg *ndm; 36751690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 367677162022SJohn Fastabend struct net_device *dev; 367777162022SJohn Fastabend int err = -EINVAL; 367877162022SJohn Fastabend __u8 *addr; 3679f6f6424bSJiri Pirko u16 vid; 368077162022SJohn Fastabend 368190f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 36821690be63SVlad Yasevich return -EPERM; 36831690be63SVlad Yasevich 3684c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 36851690be63SVlad Yasevich if (err < 0) 36861690be63SVlad Yasevich return err; 368777162022SJohn Fastabend 368877162022SJohn Fastabend ndm = nlmsg_data(nlh); 368977162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3690b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 369177162022SJohn Fastabend return -EINVAL; 369277162022SJohn Fastabend } 369377162022SJohn Fastabend 369477162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 369577162022SJohn Fastabend if (dev == NULL) { 3696b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 369777162022SJohn Fastabend return -ENODEV; 369877162022SJohn Fastabend } 369977162022SJohn Fastabend 37001690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3701b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 370277162022SJohn Fastabend return -EINVAL; 370377162022SJohn Fastabend } 370477162022SJohn Fastabend 37051690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 37061690be63SVlad Yasevich 3707b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3708f6f6424bSJiri Pirko if (err) 3709f6f6424bSJiri Pirko return err; 3710f6f6424bSJiri Pirko 371177162022SJohn Fastabend err = -EOPNOTSUPP; 371277162022SJohn Fastabend 371377162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 371477162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 371577162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3716898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3717898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 371877162022SJohn Fastabend 3719898e5061SJiri Pirko if (ops->ndo_fdb_del) 3720f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 372177162022SJohn Fastabend 372277162022SJohn Fastabend if (err) 372377162022SJohn Fastabend goto out; 372477162022SJohn Fastabend else 372577162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 372677162022SJohn Fastabend } 372777162022SJohn Fastabend 372877162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3729090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3730090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3731f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3732f6f6424bSJiri Pirko vid); 3733090096bfSVlad Yasevich else 3734f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 373577162022SJohn Fastabend 37363ff661c3SJohn Fastabend if (!err) { 3737b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3738b3379041SHubert Sokolowski ndm->ndm_state); 373977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 374077162022SJohn Fastabend } 37413ff661c3SJohn Fastabend } 374277162022SJohn Fastabend out: 374377162022SJohn Fastabend return err; 374477162022SJohn Fastabend } 374577162022SJohn Fastabend 3746d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3747d83b0603SJohn Fastabend struct netlink_callback *cb, 3748d83b0603SJohn Fastabend struct net_device *dev, 3749d83b0603SJohn Fastabend int *idx, 3750d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3751d83b0603SJohn Fastabend { 3752d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3753d83b0603SJohn Fastabend int err; 375415e47304SEric W. Biederman u32 portid, seq; 3755d83b0603SJohn Fastabend 375615e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3757d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3758d83b0603SJohn Fastabend 3759d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3760d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3761d83b0603SJohn Fastabend goto skip; 3762d83b0603SJohn Fastabend 37631e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3764a7a558feSJohn Fastabend portid, seq, 37651c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3766b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3767d83b0603SJohn Fastabend if (err < 0) 3768d83b0603SJohn Fastabend return err; 3769d83b0603SJohn Fastabend skip: 3770d83b0603SJohn Fastabend *idx += 1; 3771d83b0603SJohn Fastabend } 3772d83b0603SJohn Fastabend return 0; 3773d83b0603SJohn Fastabend } 3774d83b0603SJohn Fastabend 3775d83b0603SJohn Fastabend /** 37762c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3777d83b0603SJohn Fastabend * @nlh: netlink message header 3778d83b0603SJohn Fastabend * @dev: netdevice 3779d83b0603SJohn Fastabend * 3780d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 378191f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3782d83b0603SJohn Fastabend */ 3783d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3784d83b0603SJohn Fastabend struct netlink_callback *cb, 3785d83b0603SJohn Fastabend struct net_device *dev, 37865d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3787d297653dSRoopa Prabhu int *idx) 3788d83b0603SJohn Fastabend { 3789d83b0603SJohn Fastabend int err; 3790d83b0603SJohn Fastabend 3791d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3792d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3793d83b0603SJohn Fastabend if (err) 3794d83b0603SJohn Fastabend goto out; 37952934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3796d83b0603SJohn Fastabend out: 3797d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3798d297653dSRoopa Prabhu return err; 3799d83b0603SJohn Fastabend } 3800d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3801d83b0603SJohn Fastabend 380277162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 380377162022SJohn Fastabend { 380477162022SJohn Fastabend struct net_device *dev; 38055e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 38065e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 38075e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 38085e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 38095e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 38105e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3811d297653dSRoopa Prabhu struct hlist_head *head; 38125e6d2435SJamal Hadi Salim int brport_idx = 0; 38135e6d2435SJamal Hadi Salim int br_idx = 0; 3814d297653dSRoopa Prabhu int h, s_h; 3815d297653dSRoopa Prabhu int idx = 0, s_idx; 3816d297653dSRoopa Prabhu int err = 0; 3817d297653dSRoopa Prabhu int fidx = 0; 381877162022SJohn Fastabend 3819bd961c9bSMauricio Faria de Oliveira /* A hack to preserve kernel<->userspace interface. 3820bd961c9bSMauricio Faria de Oliveira * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 3821bd961c9bSMauricio Faria de Oliveira * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 3822bd961c9bSMauricio Faria de Oliveira * So, check for ndmsg with an optional u32 attribute (not used here). 3823bd961c9bSMauricio Faria de Oliveira * Fortunately these sizes don't conflict with the size of ifinfomsg 3824bd961c9bSMauricio Faria de Oliveira * with an optional attribute. 3825bd961c9bSMauricio Faria de Oliveira */ 3826bd961c9bSMauricio Faria de Oliveira if (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) && 3827bd961c9bSMauricio Faria de Oliveira (nlmsg_len(cb->nlh) != sizeof(struct ndmsg) + 3828bd961c9bSMauricio Faria de Oliveira nla_attr_size(sizeof(u32)))) { 38290ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 3830dac9c979SDavid Ahern IFLA_MAX, ifla_policy, cb->extack); 38310ff50e83SAlexander Potapenko if (err < 0) { 38320ff50e83SAlexander Potapenko return -EINVAL; 38330ff50e83SAlexander Potapenko } else if (err == 0) { 38345e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 38355e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 38365e6d2435SJamal Hadi Salim } 383777162022SJohn Fastabend 38385e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 3839bd961c9bSMauricio Faria de Oliveira } 38405e6d2435SJamal Hadi Salim 38415e6d2435SJamal Hadi Salim if (br_idx) { 38425e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 38435e6d2435SJamal Hadi Salim if (!br_dev) 38445e6d2435SJamal Hadi Salim return -ENODEV; 38455e6d2435SJamal Hadi Salim 3846898e5061SJiri Pirko ops = br_dev->netdev_ops; 38475e6d2435SJamal Hadi Salim } 38485e6d2435SJamal Hadi Salim 3849d297653dSRoopa Prabhu s_h = cb->args[0]; 3850d297653dSRoopa Prabhu s_idx = cb->args[1]; 3851d297653dSRoopa Prabhu 3852d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3853d297653dSRoopa Prabhu idx = 0; 3854d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3855d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3856d297653dSRoopa Prabhu 38575e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 38585e6d2435SJamal Hadi Salim continue; 38595e6d2435SJamal Hadi Salim 38605e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 38615e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 38625e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 38635e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 38645e6d2435SJamal Hadi Salim } 38655e6d2435SJamal Hadi Salim } else { 38665e6d2435SJamal Hadi Salim if (dev != br_dev && 38675e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 38685e6d2435SJamal Hadi Salim continue; 38695e6d2435SJamal Hadi Salim 38705e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 38715e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 38725e6d2435SJamal Hadi Salim continue; 38735e6d2435SJamal Hadi Salim cops = ops; 38745e6d2435SJamal Hadi Salim } 38755e6d2435SJamal Hadi Salim 3876d297653dSRoopa Prabhu if (idx < s_idx) 3877d297653dSRoopa Prabhu goto cont; 3878d297653dSRoopa Prabhu 38795e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3880d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3881d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3882d297653dSRoopa Prabhu br_dev, dev, 3883d297653dSRoopa Prabhu &fidx); 3884d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3885d297653dSRoopa Prabhu goto out; 388677162022SJohn Fastabend } 3887d297653dSRoopa Prabhu } 388877162022SJohn Fastabend 38895e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3890d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3891d297653dSRoopa Prabhu dev, NULL, 3892d297653dSRoopa Prabhu &fidx); 38936cb69742SHubert Sokolowski else 3894d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3895d297653dSRoopa Prabhu &fidx); 3896d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3897d297653dSRoopa Prabhu goto out; 38985e6d2435SJamal Hadi Salim 38995e6d2435SJamal Hadi Salim cops = NULL; 3900d297653dSRoopa Prabhu 3901d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3902d297653dSRoopa Prabhu cb->args[2] = 0; 3903d297653dSRoopa Prabhu fidx = 0; 3904d297653dSRoopa Prabhu cont: 3905d297653dSRoopa Prabhu idx++; 3906d297653dSRoopa Prabhu } 390777162022SJohn Fastabend } 390877162022SJohn Fastabend 3909d297653dSRoopa Prabhu out: 3910d297653dSRoopa Prabhu cb->args[0] = h; 3911d297653dSRoopa Prabhu cb->args[1] = idx; 3912d297653dSRoopa Prabhu cb->args[2] = fidx; 3913d297653dSRoopa Prabhu 391477162022SJohn Fastabend return skb->len; 391577162022SJohn Fastabend } 391677162022SJohn Fastabend 39172c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 39182c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 39192c3c031cSScott Feldman { 39202c3c031cSScott Feldman if (mask & flag) 39212c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 39222c3c031cSScott Feldman return 0; 39232c3c031cSScott Feldman } 39242c3c031cSScott Feldman 3925815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 39262c3c031cSScott Feldman struct net_device *dev, u16 mode, 39277d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 39287d4f8d87SScott Feldman u32 filter_mask, 39297d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 39307d4f8d87SScott Feldman struct net_device *dev, 39317d4f8d87SScott Feldman u32 filter_mask)) 3932815cccbfSJohn Fastabend { 3933815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3934815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3935815cccbfSJohn Fastabend struct nlattr *br_afspec; 39362c3c031cSScott Feldman struct nlattr *protinfo; 3937815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3938898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 39397d4f8d87SScott Feldman int err = 0; 3940815cccbfSJohn Fastabend 394146c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3942815cccbfSJohn Fastabend if (nlh == NULL) 3943815cccbfSJohn Fastabend return -EMSGSIZE; 3944815cccbfSJohn Fastabend 3945815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3946815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3947815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3948815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3949815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3950815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3951815cccbfSJohn Fastabend ifm->ifi_change = 0; 3952815cccbfSJohn Fastabend 3953815cccbfSJohn Fastabend 3954815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3955815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3956815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3957898e5061SJiri Pirko (br_dev && 3958898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3959815cccbfSJohn Fastabend (dev->addr_len && 3960815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3961a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3962a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3963815cccbfSJohn Fastabend goto nla_put_failure; 3964815cccbfSJohn Fastabend 3965815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3966815cccbfSJohn Fastabend if (!br_afspec) 3967815cccbfSJohn Fastabend goto nla_put_failure; 3968815cccbfSJohn Fastabend 39691d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3970815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3971815cccbfSJohn Fastabend goto nla_put_failure; 3972815cccbfSJohn Fastabend } 39731d460b98SRoopa Prabhu 39741d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 39751d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 39761d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 39771d460b98SRoopa Prabhu goto nla_put_failure; 39781d460b98SRoopa Prabhu } 39791d460b98SRoopa Prabhu } 39807d4f8d87SScott Feldman if (vlan_fill) { 39817d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 39827d4f8d87SScott Feldman if (err) { 39837d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 39847d4f8d87SScott Feldman goto nla_put_failure; 39857d4f8d87SScott Feldman } 39867d4f8d87SScott Feldman } 3987815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3988815cccbfSJohn Fastabend 39892c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 39902c3c031cSScott Feldman if (!protinfo) 39912c3c031cSScott Feldman goto nla_put_failure; 39922c3c031cSScott Feldman 39932c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 39942c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 39952c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39962c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 39972c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39982c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 39992c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 40002c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 40012c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 40022c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 40032c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 40042c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 40052c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 40062c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 40072c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 40082c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 40092c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 40102c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 40112c3c031cSScott Feldman goto nla_put_failure; 40122c3c031cSScott Feldman } 40132c3c031cSScott Feldman 40142c3c031cSScott Feldman nla_nest_end(skb, protinfo); 40152c3c031cSScott Feldman 4016053c095aSJohannes Berg nlmsg_end(skb, nlh); 4017053c095aSJohannes Berg return 0; 4018815cccbfSJohn Fastabend nla_put_failure: 4019815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 40207d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 4021815cccbfSJohn Fastabend } 40227d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4023815cccbfSJohn Fastabend 40242d011be8SDavid Ahern static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 40252d011be8SDavid Ahern bool strict_check, u32 *filter_mask, 40262d011be8SDavid Ahern struct netlink_ext_ack *extack) 40272d011be8SDavid Ahern { 40282d011be8SDavid Ahern struct nlattr *tb[IFLA_MAX+1]; 40292d011be8SDavid Ahern int err, i; 40302d011be8SDavid Ahern 40312d011be8SDavid Ahern if (strict_check) { 40322d011be8SDavid Ahern struct ifinfomsg *ifm; 40332d011be8SDavid Ahern 40342d011be8SDavid Ahern if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 40352d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 40362d011be8SDavid Ahern return -EINVAL; 40372d011be8SDavid Ahern } 40382d011be8SDavid Ahern 40392d011be8SDavid Ahern ifm = nlmsg_data(nlh); 40402d011be8SDavid Ahern if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 40412d011be8SDavid Ahern ifm->ifi_change || ifm->ifi_index) { 40422d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 40432d011be8SDavid Ahern return -EINVAL; 40442d011be8SDavid Ahern } 40452d011be8SDavid Ahern 40462d011be8SDavid Ahern err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb, 40472d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 40482d011be8SDavid Ahern } else { 40492d011be8SDavid Ahern err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, 40502d011be8SDavid Ahern IFLA_MAX, ifla_policy, extack); 40512d011be8SDavid Ahern } 40522d011be8SDavid Ahern if (err < 0) 40532d011be8SDavid Ahern return err; 40542d011be8SDavid Ahern 40552d011be8SDavid Ahern /* new attributes should only be added with strict checking */ 40562d011be8SDavid Ahern for (i = 0; i <= IFLA_MAX; ++i) { 40572d011be8SDavid Ahern if (!tb[i]) 40582d011be8SDavid Ahern continue; 40592d011be8SDavid Ahern 40602d011be8SDavid Ahern switch (i) { 40612d011be8SDavid Ahern case IFLA_EXT_MASK: 40622d011be8SDavid Ahern *filter_mask = nla_get_u32(tb[i]); 40632d011be8SDavid Ahern break; 40642d011be8SDavid Ahern default: 40652d011be8SDavid Ahern if (strict_check) { 40662d011be8SDavid Ahern NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 40672d011be8SDavid Ahern return -EINVAL; 40682d011be8SDavid Ahern } 40692d011be8SDavid Ahern } 40702d011be8SDavid Ahern } 40712d011be8SDavid Ahern 40722d011be8SDavid Ahern return 0; 40732d011be8SDavid Ahern } 40742d011be8SDavid Ahern 4075e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4076e5a55a89SJohn Fastabend { 40772d011be8SDavid Ahern const struct nlmsghdr *nlh = cb->nlh; 4078e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4079e5a55a89SJohn Fastabend struct net_device *dev; 4080e5a55a89SJohn Fastabend int idx = 0; 4081e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 40822d011be8SDavid Ahern u32 seq = nlh->nlmsg_seq; 40836cbdceebSVlad Yasevich u32 filter_mask = 0; 4084d64f69b0SRoopa Prabhu int err; 40856cbdceebSVlad Yasevich 40862d011be8SDavid Ahern err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 40872d011be8SDavid Ahern cb->extack); 40882d011be8SDavid Ahern if (err < 0 && cb->strict_check) 40892d011be8SDavid Ahern return err; 4090e5a55a89SJohn Fastabend 4091e5a55a89SJohn Fastabend rcu_read_lock(); 4092e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 4093e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 4094898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4095e5a55a89SJohn Fastabend 4096898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4097d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4098d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 4099d64f69b0SRoopa Prabhu skb, portid, seq, dev, 4100d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 4101f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4102f6c5775fSDavid Ahern if (likely(skb->len)) 4103e5a55a89SJohn Fastabend break; 4104f6c5775fSDavid Ahern 4105f6c5775fSDavid Ahern goto out_err; 4106f6c5775fSDavid Ahern } 4107d64f69b0SRoopa Prabhu } 4108e5a55a89SJohn Fastabend idx++; 4109e5a55a89SJohn Fastabend } 4110e5a55a89SJohn Fastabend 4111e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 4112d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 4113d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 4114d64f69b0SRoopa Prabhu seq, dev, 411546c264daSNicolas Dichtel filter_mask, 4116d64f69b0SRoopa Prabhu NLM_F_MULTI); 4117f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 4118f6c5775fSDavid Ahern if (likely(skb->len)) 4119e5a55a89SJohn Fastabend break; 4120f6c5775fSDavid Ahern 4121f6c5775fSDavid Ahern goto out_err; 4122f6c5775fSDavid Ahern } 4123d64f69b0SRoopa Prabhu } 4124e5a55a89SJohn Fastabend idx++; 4125e5a55a89SJohn Fastabend } 4126e5a55a89SJohn Fastabend } 4127f6c5775fSDavid Ahern err = skb->len; 4128f6c5775fSDavid Ahern out_err: 4129e5a55a89SJohn Fastabend rcu_read_unlock(); 4130e5a55a89SJohn Fastabend cb->args[0] = idx; 4131e5a55a89SJohn Fastabend 4132f6c5775fSDavid Ahern return err; 4133e5a55a89SJohn Fastabend } 4134e5a55a89SJohn Fastabend 41352469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 41362469ffd7SJohn Fastabend { 41372469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 41382469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 41392469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 41402469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 41412469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 41422469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 41432469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 41442469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 41452469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 41462469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 41472469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 41482469ffd7SJohn Fastabend } 41492469ffd7SJohn Fastabend 415002dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 41512469ffd7SJohn Fastabend { 41522469ffd7SJohn Fastabend struct net *net = dev_net(dev); 41532469ffd7SJohn Fastabend struct sk_buff *skb; 41542469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 41552469ffd7SJohn Fastabend 415602dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 415702dba438SRoopa Prabhu return 0; 415802dba438SRoopa Prabhu 41592469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 41602469ffd7SJohn Fastabend if (!skb) { 41612469ffd7SJohn Fastabend err = -ENOMEM; 41622469ffd7SJohn Fastabend goto errout; 41632469ffd7SJohn Fastabend } 41642469ffd7SJohn Fastabend 416546c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4166c38e01b8SJohn Fastabend if (err < 0) 4167c38e01b8SJohn Fastabend goto errout; 41682469ffd7SJohn Fastabend 416959ccaaaaSRoopa Prabhu if (!skb->len) 417059ccaaaaSRoopa Prabhu goto errout; 417159ccaaaaSRoopa Prabhu 41722469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 41732469ffd7SJohn Fastabend return 0; 41742469ffd7SJohn Fastabend errout: 41752469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 41762469ffd7SJohn Fastabend kfree_skb(skb); 417759ccaaaaSRoopa Prabhu if (err) 41782469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 41792469ffd7SJohn Fastabend return err; 41802469ffd7SJohn Fastabend } 41812469ffd7SJohn Fastabend 4182c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4183c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4184e5a55a89SJohn Fastabend { 4185e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4186e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4187e5a55a89SJohn Fastabend struct net_device *dev; 41882469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 41892469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 41904de8b413SRosen, Rami u16 flags = 0; 4191c38e01b8SJohn Fastabend bool have_flags = false; 4192e5a55a89SJohn Fastabend 4193e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4194e5a55a89SJohn Fastabend return -EINVAL; 4195e5a55a89SJohn Fastabend 4196e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4197e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4198e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4199e5a55a89SJohn Fastabend 4200e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4201e5a55a89SJohn Fastabend if (!dev) { 4202b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4203e5a55a89SJohn Fastabend return -ENODEV; 4204e5a55a89SJohn Fastabend } 4205e5a55a89SJohn Fastabend 42062469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 42072469ffd7SJohn Fastabend if (br_spec) { 42082469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 42092469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 42106e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 42116e8d1c55SThomas Graf return -EINVAL; 42126e8d1c55SThomas Graf 4213c38e01b8SJohn Fastabend have_flags = true; 42142469ffd7SJohn Fastabend flags = nla_get_u16(attr); 42152469ffd7SJohn Fastabend break; 42162469ffd7SJohn Fastabend } 42172469ffd7SJohn Fastabend } 42182469ffd7SJohn Fastabend } 42192469ffd7SJohn Fastabend 42202469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4221898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4222898e5061SJiri Pirko 4223898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 42242469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4225e5a55a89SJohn Fastabend goto out; 4226e5a55a89SJohn Fastabend } 4227e5a55a89SJohn Fastabend 4228add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 42292469ffd7SJohn Fastabend if (err) 42302469ffd7SJohn Fastabend goto out; 42312469ffd7SJohn Fastabend 42322469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 42332469ffd7SJohn Fastabend } 42342469ffd7SJohn Fastabend 42352469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 42362469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 42372469ffd7SJohn Fastabend err = -EOPNOTSUPP; 42382469ffd7SJohn Fastabend else 4239add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4240add511b3SRoopa Prabhu flags); 424102dba438SRoopa Prabhu if (!err) { 42422469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 424302dba438SRoopa Prabhu 424402dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 424502dba438SRoopa Prabhu * change 424602dba438SRoopa Prabhu */ 424702dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 424802dba438SRoopa Prabhu } 42492469ffd7SJohn Fastabend } 42502469ffd7SJohn Fastabend 4251c38e01b8SJohn Fastabend if (have_flags) 42522469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4253e5a55a89SJohn Fastabend out: 4254e5a55a89SJohn Fastabend return err; 4255e5a55a89SJohn Fastabend } 4256e5a55a89SJohn Fastabend 4257c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4258c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4259407af329SVlad Yasevich { 4260407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4261407af329SVlad Yasevich struct ifinfomsg *ifm; 4262407af329SVlad Yasevich struct net_device *dev; 4263407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4264407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 42654de8b413SRosen, Rami u16 flags = 0; 4266407af329SVlad Yasevich bool have_flags = false; 4267407af329SVlad Yasevich 4268407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4269407af329SVlad Yasevich return -EINVAL; 4270407af329SVlad Yasevich 4271407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4272407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4273407af329SVlad Yasevich return -EPFNOSUPPORT; 4274407af329SVlad Yasevich 4275407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4276407af329SVlad Yasevich if (!dev) { 4277b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4278407af329SVlad Yasevich return -ENODEV; 4279407af329SVlad Yasevich } 4280407af329SVlad Yasevich 4281407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4282407af329SVlad Yasevich if (br_spec) { 4283407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4284407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 42856e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 42866e8d1c55SThomas Graf return -EINVAL; 42876e8d1c55SThomas Graf 4288407af329SVlad Yasevich have_flags = true; 4289407af329SVlad Yasevich flags = nla_get_u16(attr); 4290407af329SVlad Yasevich break; 4291407af329SVlad Yasevich } 4292407af329SVlad Yasevich } 4293407af329SVlad Yasevich } 4294407af329SVlad Yasevich 4295407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4296407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4297407af329SVlad Yasevich 4298407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4299407af329SVlad Yasevich err = -EOPNOTSUPP; 4300407af329SVlad Yasevich goto out; 4301407af329SVlad Yasevich } 4302407af329SVlad Yasevich 4303add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4304407af329SVlad Yasevich if (err) 4305407af329SVlad Yasevich goto out; 4306407af329SVlad Yasevich 4307407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4308407af329SVlad Yasevich } 4309407af329SVlad Yasevich 4310407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4311407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4312407af329SVlad Yasevich err = -EOPNOTSUPP; 4313407af329SVlad Yasevich else 4314add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4315add511b3SRoopa Prabhu flags); 4316407af329SVlad Yasevich 431702dba438SRoopa Prabhu if (!err) { 4318407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 431902dba438SRoopa Prabhu 432002dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 432102dba438SRoopa Prabhu * change 432202dba438SRoopa Prabhu */ 432302dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 432402dba438SRoopa Prabhu } 4325407af329SVlad Yasevich } 4326407af329SVlad Yasevich 4327407af329SVlad Yasevich if (have_flags) 4328407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4329407af329SVlad Yasevich out: 4330407af329SVlad Yasevich return err; 4331407af329SVlad Yasevich } 4332407af329SVlad Yasevich 4333e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4334e8872a25SNikolay Aleksandrov { 4335e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4336e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4337e8872a25SNikolay Aleksandrov } 4338e8872a25SNikolay Aleksandrov 433969ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 434069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 434169ae6ad2SNogah Frankel { 434269ae6ad2SNogah Frankel switch (attr_id) { 434369ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 434469ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 434569ae6ad2SNogah Frankel } 434669ae6ad2SNogah Frankel 434769ae6ad2SNogah Frankel return 0; 434869ae6ad2SNogah Frankel } 434969ae6ad2SNogah Frankel 435069ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 435169ae6ad2SNogah Frankel int *prividx) 435269ae6ad2SNogah Frankel { 435369ae6ad2SNogah Frankel struct nlattr *attr = NULL; 435469ae6ad2SNogah Frankel int attr_id, size; 435569ae6ad2SNogah Frankel void *attr_data; 435669ae6ad2SNogah Frankel int err; 435769ae6ad2SNogah Frankel 435869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 435969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 436069ae6ad2SNogah Frankel return -ENODATA; 436169ae6ad2SNogah Frankel 436269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 436369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 436469ae6ad2SNogah Frankel if (attr_id < *prividx) 436569ae6ad2SNogah Frankel continue; 436669ae6ad2SNogah Frankel 436769ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 436869ae6ad2SNogah Frankel if (!size) 436969ae6ad2SNogah Frankel continue; 437069ae6ad2SNogah Frankel 43713df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 437269ae6ad2SNogah Frankel continue; 437369ae6ad2SNogah Frankel 437469ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 437569ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 437669ae6ad2SNogah Frankel if (!attr) 437769ae6ad2SNogah Frankel goto nla_put_failure; 437869ae6ad2SNogah Frankel 437969ae6ad2SNogah Frankel attr_data = nla_data(attr); 438069ae6ad2SNogah Frankel memset(attr_data, 0, size); 438169ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 438269ae6ad2SNogah Frankel attr_data); 438369ae6ad2SNogah Frankel if (err) 438469ae6ad2SNogah Frankel goto get_offload_stats_failure; 438569ae6ad2SNogah Frankel } 438669ae6ad2SNogah Frankel 438769ae6ad2SNogah Frankel if (!attr) 438869ae6ad2SNogah Frankel return -ENODATA; 438969ae6ad2SNogah Frankel 439069ae6ad2SNogah Frankel *prividx = 0; 439169ae6ad2SNogah Frankel return 0; 439269ae6ad2SNogah Frankel 439369ae6ad2SNogah Frankel nla_put_failure: 439469ae6ad2SNogah Frankel err = -EMSGSIZE; 439569ae6ad2SNogah Frankel get_offload_stats_failure: 439669ae6ad2SNogah Frankel *prividx = attr_id; 439769ae6ad2SNogah Frankel return err; 439869ae6ad2SNogah Frankel } 439969ae6ad2SNogah Frankel 440069ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 440169ae6ad2SNogah Frankel { 440269ae6ad2SNogah Frankel int nla_size = 0; 440369ae6ad2SNogah Frankel int attr_id; 440469ae6ad2SNogah Frankel int size; 440569ae6ad2SNogah Frankel 440669ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 440769ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 440869ae6ad2SNogah Frankel return 0; 440969ae6ad2SNogah Frankel 441069ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 441169ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 44123df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 441369ae6ad2SNogah Frankel continue; 441469ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 441569ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 441669ae6ad2SNogah Frankel } 441769ae6ad2SNogah Frankel 441869ae6ad2SNogah Frankel if (nla_size != 0) 441969ae6ad2SNogah Frankel nla_size += nla_total_size(0); 442069ae6ad2SNogah Frankel 442169ae6ad2SNogah Frankel return nla_size; 442269ae6ad2SNogah Frankel } 442369ae6ad2SNogah Frankel 442410c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 442510c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4426e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4427e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 442810c9ead9SRoopa Prabhu { 442910c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 443010c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 443110c9ead9SRoopa Prabhu struct nlattr *attr; 4432e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 443369ae6ad2SNogah Frankel int err; 443410c9ead9SRoopa Prabhu 443510c9ead9SRoopa Prabhu ASSERT_RTNL(); 443610c9ead9SRoopa Prabhu 443710c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 443810c9ead9SRoopa Prabhu if (!nlh) 443910c9ead9SRoopa Prabhu return -EMSGSIZE; 444010c9ead9SRoopa Prabhu 444110c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4442ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4443ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4444ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 444510c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 444610c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 444710c9ead9SRoopa Prabhu 4448e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 444910c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 445010c9ead9SRoopa Prabhu 445158414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 445258414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 445358414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 445410c9ead9SRoopa Prabhu if (!attr) 445510c9ead9SRoopa Prabhu goto nla_put_failure; 445610c9ead9SRoopa Prabhu 445710c9ead9SRoopa Prabhu sp = nla_data(attr); 445810c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 445910c9ead9SRoopa Prabhu } 446010c9ead9SRoopa Prabhu 446197a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 446297a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 446397a47facSNikolay Aleksandrov 446497a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 446597a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 446697a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 446797a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 446897a47facSNikolay Aleksandrov if (!attr) 446997a47facSNikolay Aleksandrov goto nla_put_failure; 447097a47facSNikolay Aleksandrov 447180e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 447280e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 447380e73cc5SNikolay Aleksandrov if (err) 447480e73cc5SNikolay Aleksandrov goto nla_put_failure; 447580e73cc5SNikolay Aleksandrov *idxattr = 0; 447680e73cc5SNikolay Aleksandrov } 447780e73cc5SNikolay Aleksandrov } 447880e73cc5SNikolay Aleksandrov 447980e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 448080e73cc5SNikolay Aleksandrov *idxattr)) { 448180e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 448280e73cc5SNikolay Aleksandrov const struct net_device *master; 448380e73cc5SNikolay Aleksandrov 448480e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 448580e73cc5SNikolay Aleksandrov if (master) 448680e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 448780e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 448880e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 448980e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 449080e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 449180e73cc5SNikolay Aleksandrov if (!attr) 449280e73cc5SNikolay Aleksandrov goto nla_put_failure; 449380e73cc5SNikolay Aleksandrov 449480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 449597a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 449697a47facSNikolay Aleksandrov if (err) 449797a47facSNikolay Aleksandrov goto nla_put_failure; 449897a47facSNikolay Aleksandrov *idxattr = 0; 449997a47facSNikolay Aleksandrov } 450097a47facSNikolay Aleksandrov } 450197a47facSNikolay Aleksandrov 450269ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 450369ae6ad2SNogah Frankel *idxattr)) { 450469ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 450569ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 450669ae6ad2SNogah Frankel if (!attr) 450769ae6ad2SNogah Frankel goto nla_put_failure; 450869ae6ad2SNogah Frankel 450969ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 451069ae6ad2SNogah Frankel if (err == -ENODATA) 451169ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 451269ae6ad2SNogah Frankel else 451369ae6ad2SNogah Frankel nla_nest_end(skb, attr); 451469ae6ad2SNogah Frankel 451569ae6ad2SNogah Frankel if (err && err != -ENODATA) 451669ae6ad2SNogah Frankel goto nla_put_failure; 451769ae6ad2SNogah Frankel *idxattr = 0; 451869ae6ad2SNogah Frankel } 451969ae6ad2SNogah Frankel 4520aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4521aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4522aefb4d4aSRobert Shearman 4523aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4524aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4525aefb4d4aSRobert Shearman if (!attr) 4526aefb4d4aSRobert Shearman goto nla_put_failure; 4527aefb4d4aSRobert Shearman 45285fa85a09SFlorian Westphal rcu_read_lock(); 45295fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4530aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4531aefb4d4aSRobert Shearman struct nlattr *af; 4532aefb4d4aSRobert Shearman int err; 4533aefb4d4aSRobert Shearman 4534aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 45355fa85a09SFlorian Westphal if (!af) { 45365fa85a09SFlorian Westphal rcu_read_unlock(); 4537aefb4d4aSRobert Shearman goto nla_put_failure; 45385fa85a09SFlorian Westphal } 4539aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4540aefb4d4aSRobert Shearman 45415fa85a09SFlorian Westphal if (err == -ENODATA) { 4542aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 45435fa85a09SFlorian Westphal } else if (err < 0) { 45445fa85a09SFlorian Westphal rcu_read_unlock(); 4545aefb4d4aSRobert Shearman goto nla_put_failure; 45465fa85a09SFlorian Westphal } 4547aefb4d4aSRobert Shearman 4548aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4549aefb4d4aSRobert Shearman } 4550aefb4d4aSRobert Shearman } 45515fa85a09SFlorian Westphal rcu_read_unlock(); 4552aefb4d4aSRobert Shearman 4553aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4554aefb4d4aSRobert Shearman 4555aefb4d4aSRobert Shearman *idxattr = 0; 4556aefb4d4aSRobert Shearman } 4557aefb4d4aSRobert Shearman 455810c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 455910c9ead9SRoopa Prabhu 456010c9ead9SRoopa Prabhu return 0; 456110c9ead9SRoopa Prabhu 456210c9ead9SRoopa Prabhu nla_put_failure: 4563e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4564e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 456510c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4566e8872a25SNikolay Aleksandrov else 4567e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 456810c9ead9SRoopa Prabhu 456910c9ead9SRoopa Prabhu return -EMSGSIZE; 457010c9ead9SRoopa Prabhu } 457110c9ead9SRoopa Prabhu 457210c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 457310c9ead9SRoopa Prabhu u32 filter_mask) 457410c9ead9SRoopa Prabhu { 457510c9ead9SRoopa Prabhu size_t size = 0; 457610c9ead9SRoopa Prabhu 4577e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 457810c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 457910c9ead9SRoopa Prabhu 458097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 458197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 458280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 458397a47facSNikolay Aleksandrov 458497a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 458580e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 458680e73cc5SNikolay Aleksandrov attr)); 458797a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 458897a47facSNikolay Aleksandrov size += nla_total_size(0); 458997a47facSNikolay Aleksandrov } 459097a47facSNikolay Aleksandrov } 459197a47facSNikolay Aleksandrov 459280e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 459380e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 459480e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 459580e73cc5SNikolay Aleksandrov const struct net_device *master; 459680e73cc5SNikolay Aleksandrov 459780e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 459880e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 459980e73cc5SNikolay Aleksandrov if (master) 460080e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 460180e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 460280e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 460380e73cc5SNikolay Aleksandrov 460480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 460580e73cc5SNikolay Aleksandrov attr)); 460680e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 460780e73cc5SNikolay Aleksandrov size += nla_total_size(0); 460880e73cc5SNikolay Aleksandrov } 460980e73cc5SNikolay Aleksandrov } 461080e73cc5SNikolay Aleksandrov 461169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 461269ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 461369ae6ad2SNogah Frankel 4614aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4615aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4616aefb4d4aSRobert Shearman 4617aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4618aefb4d4aSRobert Shearman size += nla_total_size(0); 4619aefb4d4aSRobert Shearman 46205fa85a09SFlorian Westphal rcu_read_lock(); 46215fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4622aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4623aefb4d4aSRobert Shearman size += nla_total_size( 4624aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4625aefb4d4aSRobert Shearman 4626aefb4d4aSRobert Shearman /* for AF_* */ 4627aefb4d4aSRobert Shearman size += nla_total_size(0); 4628aefb4d4aSRobert Shearman } 4629aefb4d4aSRobert Shearman } 46305fa85a09SFlorian Westphal rcu_read_unlock(); 4631aefb4d4aSRobert Shearman } 4632aefb4d4aSRobert Shearman 463310c9ead9SRoopa Prabhu return size; 463410c9ead9SRoopa Prabhu } 463510c9ead9SRoopa Prabhu 4636c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4637c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 463810c9ead9SRoopa Prabhu { 463910c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 464010c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4641e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4642e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 464310c9ead9SRoopa Prabhu struct sk_buff *nskb; 464410c9ead9SRoopa Prabhu u32 filter_mask; 464510c9ead9SRoopa Prabhu int err; 464610c9ead9SRoopa Prabhu 46474775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 46484775cc1fSMathias Krause return -EINVAL; 46494775cc1fSMathias Krause 465010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 465110c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 465210c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 465310c9ead9SRoopa Prabhu else 465410c9ead9SRoopa Prabhu return -EINVAL; 465510c9ead9SRoopa Prabhu 465610c9ead9SRoopa Prabhu if (!dev) 465710c9ead9SRoopa Prabhu return -ENODEV; 465810c9ead9SRoopa Prabhu 465910c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 466010c9ead9SRoopa Prabhu if (!filter_mask) 466110c9ead9SRoopa Prabhu return -EINVAL; 466210c9ead9SRoopa Prabhu 466310c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 466410c9ead9SRoopa Prabhu if (!nskb) 466510c9ead9SRoopa Prabhu return -ENOBUFS; 466610c9ead9SRoopa Prabhu 466710c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 466810c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4669e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 467010c9ead9SRoopa Prabhu if (err < 0) { 467110c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 467210c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 467310c9ead9SRoopa Prabhu kfree_skb(nskb); 467410c9ead9SRoopa Prabhu } else { 467510c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 467610c9ead9SRoopa Prabhu } 467710c9ead9SRoopa Prabhu 467810c9ead9SRoopa Prabhu return err; 467910c9ead9SRoopa Prabhu } 468010c9ead9SRoopa Prabhu 468110c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 468210c9ead9SRoopa Prabhu { 4683*841891ecSDavid Ahern struct netlink_ext_ack *extack = cb->extack; 4684e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 468510c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 468610c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4687e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4688e8872a25SNikolay Aleksandrov struct hlist_head *head; 4689e8872a25SNikolay Aleksandrov struct net_device *dev; 469010c9ead9SRoopa Prabhu u32 filter_mask = 0; 4691e8872a25SNikolay Aleksandrov int idx = 0; 469210c9ead9SRoopa Prabhu 469310c9ead9SRoopa Prabhu s_h = cb->args[0]; 469410c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4695e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4696e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 469710c9ead9SRoopa Prabhu 469810c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 469910c9ead9SRoopa Prabhu 4700*841891ecSDavid Ahern if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) { 4701*841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 47024775cc1fSMathias Krause return -EINVAL; 4703*841891ecSDavid Ahern } 47044775cc1fSMathias Krause 470510c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 4706*841891ecSDavid Ahern 4707*841891ecSDavid Ahern /* only requests using NLM_F_DUMP_PROPER_HDR can pass data to 4708*841891ecSDavid Ahern * influence the dump. The legacy exception is filter_mask. 4709*841891ecSDavid Ahern */ 4710*841891ecSDavid Ahern if (cb->strict_check) { 4711*841891ecSDavid Ahern if (ifsm->pad1 || ifsm->pad2 || ifsm->ifindex) { 4712*841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 471310c9ead9SRoopa Prabhu return -EINVAL; 4714*841891ecSDavid Ahern } 4715*841891ecSDavid Ahern if (nlmsg_attrlen(cb->nlh, sizeof(*ifsm))) { 4716*841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 4717*841891ecSDavid Ahern return -EINVAL; 4718*841891ecSDavid Ahern } 4719*841891ecSDavid Ahern } 4720*841891ecSDavid Ahern 4721*841891ecSDavid Ahern filter_mask = ifsm->filter_mask; 4722*841891ecSDavid Ahern if (!filter_mask) { 4723*841891ecSDavid Ahern NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 4724*841891ecSDavid Ahern return -EINVAL; 4725*841891ecSDavid Ahern } 472610c9ead9SRoopa Prabhu 472710c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 472810c9ead9SRoopa Prabhu idx = 0; 472910c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 473010c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 473110c9ead9SRoopa Prabhu if (idx < s_idx) 473210c9ead9SRoopa Prabhu goto cont; 473310c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 473410c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 473510c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4736e8872a25SNikolay Aleksandrov flags, filter_mask, 4737e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 473810c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 473910c9ead9SRoopa Prabhu * we're in trouble 474010c9ead9SRoopa Prabhu */ 474110c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 474210c9ead9SRoopa Prabhu 474310c9ead9SRoopa Prabhu if (err < 0) 474410c9ead9SRoopa Prabhu goto out; 4745e8872a25SNikolay Aleksandrov s_prividx = 0; 4746e8872a25SNikolay Aleksandrov s_idxattr = 0; 474710c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 474810c9ead9SRoopa Prabhu cont: 474910c9ead9SRoopa Prabhu idx++; 475010c9ead9SRoopa Prabhu } 475110c9ead9SRoopa Prabhu } 475210c9ead9SRoopa Prabhu out: 4753e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4754e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 475510c9ead9SRoopa Prabhu cb->args[1] = idx; 475610c9ead9SRoopa Prabhu cb->args[0] = h; 475710c9ead9SRoopa Prabhu 475810c9ead9SRoopa Prabhu return skb->len; 475910c9ead9SRoopa Prabhu } 476010c9ead9SRoopa Prabhu 47611da177e4SLinus Torvalds /* Process one rtnetlink message. */ 47621da177e4SLinus Torvalds 47632d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 47642d4bc933SJohannes Berg struct netlink_ext_ack *extack) 47651da177e4SLinus Torvalds { 47663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4767addf9b90SFlorian Westphal struct rtnl_link *link; 4768e4202511SFlorian Westphal struct module *owner; 47696853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4770e2849863SThomas Graf rtnl_doit_func doit; 477162256f98SFlorian Westphal unsigned int flags; 4772617cfc75SAlexander Kuleshov int kind; 47731da177e4SLinus Torvalds int family; 47741da177e4SLinus Torvalds int type; 47751da177e4SLinus Torvalds 47761da177e4SLinus Torvalds type = nlh->nlmsg_type; 47771da177e4SLinus Torvalds if (type > RTM_MAX) 4778038890feSThomas Graf return -EOPNOTSUPP; 47791da177e4SLinus Torvalds 47801da177e4SLinus Torvalds type -= RTM_BASE; 47811da177e4SLinus Torvalds 47821da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4783573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 47841da177e4SLinus Torvalds return 0; 47851da177e4SLinus Torvalds 4786573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 47871da177e4SLinus Torvalds kind = type&3; 47881da177e4SLinus Torvalds 478990f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 47901d00a4ebSThomas Graf return -EPERM; 47911da177e4SLinus Torvalds 47926853dd48SFlorian Westphal rcu_read_lock(); 4793b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 479497c53cacSDenis V. Lunev struct sock *rtnl; 4795e2849863SThomas Graf rtnl_dumpit_func dumpit; 4796c7ac8679SGreg Rose u16 min_dump_alloc = 0; 47971da177e4SLinus Torvalds 4798addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4799addf9b90SFlorian Westphal if (!link || !link->dumpit) { 48006853dd48SFlorian Westphal family = PF_UNSPEC; 4801addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4802addf9b90SFlorian Westphal if (!link || !link->dumpit) 48036853dd48SFlorian Westphal goto err_unlock; 48046853dd48SFlorian Westphal } 4805e4202511SFlorian Westphal owner = link->owner; 4806addf9b90SFlorian Westphal dumpit = link->dumpit; 48076853dd48SFlorian Westphal 48085c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4809e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 48101da177e4SLinus Torvalds 4811e4202511SFlorian Westphal err = 0; 4812e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4813e4202511SFlorian Westphal if (!try_module_get(owner)) 4814e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4815e4202511SFlorian Westphal 48166853dd48SFlorian Westphal rcu_read_unlock(); 48176853dd48SFlorian Westphal 481897c53cacSDenis V. Lunev rtnl = net->rtnl; 4819e4202511SFlorian Westphal if (err == 0) { 482080d326faSPablo Neira Ayuso struct netlink_dump_control c = { 482180d326faSPablo Neira Ayuso .dump = dumpit, 482280d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4823e4202511SFlorian Westphal .module = owner, 482480d326faSPablo Neira Ayuso }; 482580d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4826e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4827e4202511SFlorian Westphal * module if dump is still in progress. 4828e4202511SFlorian Westphal */ 4829e4202511SFlorian Westphal module_put(owner); 483080d326faSPablo Neira Ayuso } 48312907c35fSEric Dumazet return err; 48321da177e4SLinus Torvalds } 48331da177e4SLinus Torvalds 4834addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4835addf9b90SFlorian Westphal if (!link || !link->doit) { 48368caa38b5SFlorian Westphal family = PF_UNSPEC; 4837addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4838addf9b90SFlorian Westphal if (!link || !link->doit) 4839addf9b90SFlorian Westphal goto out_unlock; 48408caa38b5SFlorian Westphal } 48418caa38b5SFlorian Westphal 4842e4202511SFlorian Westphal owner = link->owner; 4843e4202511SFlorian Westphal if (!try_module_get(owner)) { 4844e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4845e4202511SFlorian Westphal goto out_unlock; 4846e4202511SFlorian Westphal } 4847e4202511SFlorian Westphal 4848addf9b90SFlorian Westphal flags = link->flags; 484962256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4850addf9b90SFlorian Westphal doit = link->doit; 485162256f98SFlorian Westphal rcu_read_unlock(); 485262256f98SFlorian Westphal if (doit) 485362256f98SFlorian Westphal err = doit(skb, nlh, extack); 4854e4202511SFlorian Westphal module_put(owner); 485562256f98SFlorian Westphal return err; 485662256f98SFlorian Westphal } 48576853dd48SFlorian Westphal rcu_read_unlock(); 48586853dd48SFlorian Westphal 48590cc09020SFlorian Westphal rtnl_lock(); 4860addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4861addf9b90SFlorian Westphal if (link && link->doit) 4862addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 48630cc09020SFlorian Westphal rtnl_unlock(); 4864addf9b90SFlorian Westphal 4865e4202511SFlorian Westphal module_put(owner); 4866e4202511SFlorian Westphal 4867addf9b90SFlorian Westphal return err; 4868addf9b90SFlorian Westphal 4869addf9b90SFlorian Westphal out_unlock: 4870addf9b90SFlorian Westphal rcu_read_unlock(); 48710cc09020SFlorian Westphal return err; 48720cc09020SFlorian Westphal 48730cc09020SFlorian Westphal err_unlock: 48746853dd48SFlorian Westphal rcu_read_unlock(); 48750cc09020SFlorian Westphal return -EOPNOTSUPP; 48761da177e4SLinus Torvalds } 48771da177e4SLinus Torvalds 4878cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 48791da177e4SLinus Torvalds { 4880cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 48811da177e4SLinus Torvalds } 48821da177e4SLinus Torvalds 48835f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 48845f729eaaSJulien Gomes { 48855f729eaaSJulien Gomes switch (group) { 48865f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 48875f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 48885f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 48895f729eaaSJulien Gomes return -EPERM; 48905f729eaaSJulien Gomes break; 48915f729eaaSJulien Gomes } 48925f729eaaSJulien Gomes return 0; 48935f729eaaSJulien Gomes } 48945f729eaaSJulien Gomes 48951da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 48961da177e4SLinus Torvalds { 4897351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4898e9dc8653SEric W. Biederman 48991da177e4SLinus Torvalds switch (event) { 49005138e86fSVlad Yasevich case NETDEV_REBOOT: 49018a212589SXin Long case NETDEV_CHANGEMTU: 49023753654eSDavid Ahern case NETDEV_CHANGEADDR: 49035138e86fSVlad Yasevich case NETDEV_CHANGENAME: 49045138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 49055138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4906e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 49075138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4908dc709f37SXin Long case NETDEV_CHANGEUPPER: 49095138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 49105138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4911eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4912ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 49133d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 491438e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 49151da177e4SLinus Torvalds break; 49161da177e4SLinus Torvalds default: 49171da177e4SLinus Torvalds break; 49181da177e4SLinus Torvalds } 49191da177e4SLinus Torvalds return NOTIFY_DONE; 49201da177e4SLinus Torvalds } 49211da177e4SLinus Torvalds 49221da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 49231da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 49241da177e4SLinus Torvalds }; 49251da177e4SLinus Torvalds 492697c53cacSDenis V. Lunev 49272c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 492897c53cacSDenis V. Lunev { 492997c53cacSDenis V. Lunev struct sock *sk; 4930a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4931a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4932a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4933a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 49349785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 49355f729eaaSJulien Gomes .bind = rtnetlink_bind, 4936a31f2d17SPablo Neira Ayuso }; 4937a31f2d17SPablo Neira Ayuso 49389f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 493997c53cacSDenis V. Lunev if (!sk) 494097c53cacSDenis V. Lunev return -ENOMEM; 494197c53cacSDenis V. Lunev net->rtnl = sk; 494297c53cacSDenis V. Lunev return 0; 494397c53cacSDenis V. Lunev } 494497c53cacSDenis V. Lunev 49452c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 494697c53cacSDenis V. Lunev { 4947b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 494897c53cacSDenis V. Lunev net->rtnl = NULL; 494997c53cacSDenis V. Lunev } 495097c53cacSDenis V. Lunev 495197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 495297c53cacSDenis V. Lunev .init = rtnetlink_net_init, 495397c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 495497c53cacSDenis V. Lunev }; 495597c53cacSDenis V. Lunev 49561da177e4SLinus Torvalds void __init rtnetlink_init(void) 49571da177e4SLinus Torvalds { 495897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 49591da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 496097c53cacSDenis V. Lunev 49611da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4962340d17fcSThomas Graf 4963c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4964b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4965b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4966b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4967b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4968687ad8ccSThomas Graf 4969b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4970b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4971b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 497277162022SJohn Fastabend 4973b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4974b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4975b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4976e5a55a89SJohn Fastabend 4977b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4978b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4979b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 498010c9ead9SRoopa Prabhu 498110c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4982b97bac64SFlorian Westphal 0); 49831da177e4SLinus Torvalds } 4984