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 133a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1340cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 135a898def2SPaul E. McKenney { 136a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 137a898def2SPaul E. McKenney } 138a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 139a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 140a898def2SPaul E. McKenney 141b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 142e2849863SThomas Graf 143e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 144e2849863SThomas Graf { 145e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 146e2849863SThomas Graf 147e2849863SThomas Graf /* 148e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 149e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 150e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 151e2849863SThomas Graf */ 152e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 153e2849863SThomas Graf 154e2849863SThomas Graf return msgindex; 155e2849863SThomas Graf } 156e2849863SThomas Graf 157addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 158addf9b90SFlorian Westphal { 159addf9b90SFlorian Westphal struct rtnl_link **tab; 160addf9b90SFlorian Westphal 161addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 162addf9b90SFlorian Westphal protocol = PF_UNSPEC; 163addf9b90SFlorian Westphal 164addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 165addf9b90SFlorian Westphal if (!tab) 166addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 167addf9b90SFlorian Westphal 168addf9b90SFlorian Westphal return tab[msgtype]; 169addf9b90SFlorian Westphal } 170addf9b90SFlorian Westphal 171e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 172e4202511SFlorian Westphal int protocol, int msgtype, 173c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 174b97bac64SFlorian Westphal unsigned int flags) 175e2849863SThomas Graf { 176b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 177b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 178e2849863SThomas Graf int msgindex; 179addf9b90SFlorian Westphal int ret = -ENOBUFS; 180e2849863SThomas Graf 18125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 182e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 183e2849863SThomas Graf 184addf9b90SFlorian Westphal rtnl_lock(); 185addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 186e2849863SThomas Graf if (tab == NULL) { 187addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 188addf9b90SFlorian Westphal if (!tab) 189addf9b90SFlorian Westphal goto unlock; 190e2849863SThomas Graf 191addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1926853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 193e2849863SThomas Graf } 194e2849863SThomas Graf 195addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 196addf9b90SFlorian Westphal if (old) { 197addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 198addf9b90SFlorian Westphal if (!link) 199addf9b90SFlorian Westphal goto unlock; 200addf9b90SFlorian Westphal } else { 201addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 202addf9b90SFlorian Westphal if (!link) 203addf9b90SFlorian Westphal goto unlock; 204addf9b90SFlorian Westphal } 205e2849863SThomas Graf 206e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 207e4202511SFlorian Westphal link->owner = owner; 208e4202511SFlorian Westphal 209addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 210addf9b90SFlorian Westphal if (doit) 211addf9b90SFlorian Westphal link->doit = doit; 212addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 213addf9b90SFlorian Westphal if (dumpit) 214addf9b90SFlorian Westphal link->dumpit = dumpit; 215addf9b90SFlorian Westphal 216addf9b90SFlorian Westphal link->flags |= flags; 217addf9b90SFlorian Westphal 218addf9b90SFlorian Westphal /* publish protocol:msgtype */ 219addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 220addf9b90SFlorian Westphal ret = 0; 221addf9b90SFlorian Westphal if (old) 222addf9b90SFlorian Westphal kfree_rcu(old, rcu); 223addf9b90SFlorian Westphal unlock: 224addf9b90SFlorian Westphal rtnl_unlock(); 225addf9b90SFlorian Westphal return ret; 226e2849863SThomas Graf } 227e4202511SFlorian Westphal 228e4202511SFlorian Westphal /** 229e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 230e4202511SFlorian Westphal * 231e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 232e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 233e4202511SFlorian Westphal * @msgtype: rtnetlink message type 234e4202511SFlorian Westphal * @doit: Function pointer called for each request message 235e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 236e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 237e4202511SFlorian Westphal * 238e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 239e4202511SFlorian Westphal */ 240e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 241e4202511SFlorian Westphal int protocol, int msgtype, 242e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 243e4202511SFlorian Westphal unsigned int flags) 244e4202511SFlorian Westphal { 245e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 246e4202511SFlorian Westphal doit, dumpit, flags); 247e4202511SFlorian Westphal } 248e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 249e4202511SFlorian Westphal 250e4202511SFlorian Westphal /** 25116feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 252e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 253e4202511SFlorian Westphal * @msgtype: rtnetlink message type 254e4202511SFlorian Westphal * @doit: Function pointer called for each request message 255e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 256e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 257e4202511SFlorian Westphal * 258e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 259e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 260e4202511SFlorian Westphal * specified protocol family and message type is received. 261e4202511SFlorian Westphal * 262e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 263e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 264e4202511SFlorian Westphal * family exists. 265e2849863SThomas Graf */ 266e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 267c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 268b97bac64SFlorian Westphal unsigned int flags) 269e2849863SThomas Graf { 27016feebcfSFlorian Westphal int err; 27116feebcfSFlorian Westphal 27216feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27316feebcfSFlorian Westphal flags); 27416feebcfSFlorian Westphal if (err) 27516feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 27616feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 277e2849863SThomas Graf } 278e2849863SThomas Graf 279e2849863SThomas Graf /** 280e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 281e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 282e2849863SThomas Graf * @msgtype: rtnetlink message type 283e2849863SThomas Graf * 284e2849863SThomas Graf * Returns 0 on success or a negative error code. 285e2849863SThomas Graf */ 286e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 287e2849863SThomas Graf { 288addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 289e2849863SThomas Graf int msgindex; 290e2849863SThomas Graf 29125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 292e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 293e2849863SThomas Graf 2946853dd48SFlorian Westphal rtnl_lock(); 295addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 296addf9b90SFlorian Westphal if (!tab) { 2976853dd48SFlorian Westphal rtnl_unlock(); 298e2849863SThomas Graf return -ENOENT; 2996853dd48SFlorian Westphal } 300e2849863SThomas Graf 301addf9b90SFlorian Westphal link = tab[msgindex]; 302addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 3036853dd48SFlorian Westphal rtnl_unlock(); 304e2849863SThomas Graf 305addf9b90SFlorian Westphal kfree_rcu(link, rcu); 306addf9b90SFlorian Westphal 307e2849863SThomas Graf return 0; 308e2849863SThomas Graf } 309e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 310e2849863SThomas Graf 311e2849863SThomas Graf /** 312e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 313e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 314e2849863SThomas Graf * 315e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 316e2849863SThomas Graf * of a certain protocol family. 317e2849863SThomas Graf */ 318e2849863SThomas Graf void rtnl_unregister_all(int protocol) 319e2849863SThomas Graf { 320addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 321addf9b90SFlorian Westphal int msgindex; 322019a3169SFlorian Westphal 32325239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 324e2849863SThomas Graf 325019a3169SFlorian Westphal rtnl_lock(); 326addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 3276853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 328addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 329addf9b90SFlorian Westphal link = tab[msgindex]; 330addf9b90SFlorian Westphal if (!link) 331addf9b90SFlorian Westphal continue; 332addf9b90SFlorian Westphal 333addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 334addf9b90SFlorian Westphal kfree_rcu(link, rcu); 335addf9b90SFlorian Westphal } 336019a3169SFlorian Westphal rtnl_unlock(); 337019a3169SFlorian Westphal 3386853dd48SFlorian Westphal synchronize_net(); 3396853dd48SFlorian Westphal 340addf9b90SFlorian Westphal kfree(tab); 341e2849863SThomas Graf } 342e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3431da177e4SLinus Torvalds 34438f7b870SPatrick McHardy static LIST_HEAD(link_ops); 34538f7b870SPatrick McHardy 346c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 347c63044f0SEric Dumazet { 348c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 349c63044f0SEric Dumazet 350c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 351c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 352c63044f0SEric Dumazet return ops; 353c63044f0SEric Dumazet } 354c63044f0SEric Dumazet return NULL; 355c63044f0SEric Dumazet } 356c63044f0SEric Dumazet 35738f7b870SPatrick McHardy /** 35838f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 35938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 36038f7b870SPatrick McHardy * 36138f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 36238f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 36338f7b870SPatrick McHardy * must be called before registering the devices. 36438f7b870SPatrick McHardy * 36538f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 36638f7b870SPatrick McHardy */ 36738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 36838f7b870SPatrick McHardy { 369c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 370c63044f0SEric Dumazet return -EEXIST; 371c63044f0SEric Dumazet 372b0ab2fabSJiri Pirko /* The check for setup is here because if ops 373b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 374b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 375b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 376b0ab2fabSJiri Pirko */ 377b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 37823289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3792d85cba2SPatrick McHardy 38038f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 38138f7b870SPatrick McHardy return 0; 38238f7b870SPatrick McHardy } 38338f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 38438f7b870SPatrick McHardy 38538f7b870SPatrick McHardy /** 38638f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 38738f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 38838f7b870SPatrick McHardy * 38938f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 39038f7b870SPatrick McHardy */ 39138f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 39238f7b870SPatrick McHardy { 39338f7b870SPatrick McHardy int err; 39438f7b870SPatrick McHardy 395ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 396ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 397ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 398ccf8dbcdSKees Cook return -EINVAL; 399ccf8dbcdSKees Cook 40038f7b870SPatrick McHardy rtnl_lock(); 40138f7b870SPatrick McHardy err = __rtnl_link_register(ops); 40238f7b870SPatrick McHardy rtnl_unlock(); 40338f7b870SPatrick McHardy return err; 40438f7b870SPatrick McHardy } 40538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 40638f7b870SPatrick McHardy 407669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 408669f87baSPavel Emelyanov { 409669f87baSPavel Emelyanov struct net_device *dev; 41023289a37SEric Dumazet LIST_HEAD(list_kill); 41123289a37SEric Dumazet 412669f87baSPavel Emelyanov for_each_netdev(net, dev) { 41323289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 41423289a37SEric Dumazet ops->dellink(dev, &list_kill); 415669f87baSPavel Emelyanov } 41623289a37SEric Dumazet unregister_netdevice_many(&list_kill); 417669f87baSPavel Emelyanov } 418669f87baSPavel Emelyanov 41938f7b870SPatrick McHardy /** 42038f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 42138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 42238f7b870SPatrick McHardy * 423554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 424554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 425554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 42638f7b870SPatrick McHardy */ 42738f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 42838f7b870SPatrick McHardy { 429881d966bSEric W. Biederman struct net *net; 4302d85cba2SPatrick McHardy 431881d966bSEric W. Biederman for_each_net(net) { 432669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 433881d966bSEric W. Biederman } 43438f7b870SPatrick McHardy list_del(&ops->list); 43538f7b870SPatrick McHardy } 43638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 43738f7b870SPatrick McHardy 438200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 439200b916fSCong Wang * devices unregistering in any network namespace. 440200b916fSCong Wang */ 441200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 442200b916fSCong Wang { 443200b916fSCong Wang struct net *net; 444200b916fSCong Wang bool unregistering; 445ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 446200b916fSCong Wang 447ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 448200b916fSCong Wang for (;;) { 449200b916fSCong Wang unregistering = false; 450200b916fSCong Wang rtnl_lock(); 451f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 452f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 453f0b07bb1SKirill Tkhai */ 454200b916fSCong Wang for_each_net(net) { 455200b916fSCong Wang if (net->dev_unreg_count > 0) { 456200b916fSCong Wang unregistering = true; 457200b916fSCong Wang break; 458200b916fSCong Wang } 459200b916fSCong Wang } 460200b916fSCong Wang if (!unregistering) 461200b916fSCong Wang break; 462200b916fSCong Wang __rtnl_unlock(); 463ff960a73SPeter Zijlstra 464ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 465200b916fSCong Wang } 466ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 467200b916fSCong Wang } 468200b916fSCong Wang 46938f7b870SPatrick McHardy /** 47038f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 47138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47238f7b870SPatrick McHardy */ 47338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 47438f7b870SPatrick McHardy { 4758518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4764420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 477200b916fSCong Wang rtnl_lock_unregistering_all(); 47838f7b870SPatrick McHardy __rtnl_link_unregister(ops); 47938f7b870SPatrick McHardy rtnl_unlock(); 4804420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 48138f7b870SPatrick McHardy } 48238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 48338f7b870SPatrick McHardy 484ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 485ba7d49b1SJiri Pirko { 486ba7d49b1SJiri Pirko struct net_device *master_dev; 487ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4888515ae38SFlorian Westphal size_t size = 0; 489ba7d49b1SJiri Pirko 4908515ae38SFlorian Westphal rcu_read_lock(); 4918515ae38SFlorian Westphal 4928515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 493ba7d49b1SJiri Pirko if (!master_dev) 4948515ae38SFlorian Westphal goto out; 4958515ae38SFlorian Westphal 496ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 4976049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 4988515ae38SFlorian Westphal goto out; 499ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5008515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 501ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5028515ae38SFlorian Westphal 5038515ae38SFlorian Westphal out: 5048515ae38SFlorian Westphal rcu_read_unlock(); 5058515ae38SFlorian Westphal return size; 506ba7d49b1SJiri Pirko } 507ba7d49b1SJiri Pirko 50838f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 50938f7b870SPatrick McHardy { 51038f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 51138f7b870SPatrick McHardy size_t size; 51238f7b870SPatrick McHardy 51338f7b870SPatrick McHardy if (!ops) 51438f7b870SPatrick McHardy return 0; 51538f7b870SPatrick McHardy 516369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 517369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 51838f7b870SPatrick McHardy 51938f7b870SPatrick McHardy if (ops->get_size) 52038f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 521369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 52238f7b870SPatrick McHardy ops->get_size(dev); 52338f7b870SPatrick McHardy 52438f7b870SPatrick McHardy if (ops->get_xstats_size) 525369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 526369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 52738f7b870SPatrick McHardy 528ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 529ba7d49b1SJiri Pirko 53038f7b870SPatrick McHardy return size; 53138f7b870SPatrick McHardy } 53238f7b870SPatrick McHardy 533f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 534f8ff182cSThomas Graf 535f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 536f8ff182cSThomas Graf { 537f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 538f8ff182cSThomas Graf 5395fa85a09SFlorian Westphal list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 540f8ff182cSThomas Graf if (ops->family == family) 541f8ff182cSThomas Graf return ops; 542f8ff182cSThomas Graf } 543f8ff182cSThomas Graf 544f8ff182cSThomas Graf return NULL; 545f8ff182cSThomas Graf } 546f8ff182cSThomas Graf 547f8ff182cSThomas Graf /** 548f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 549f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 550f8ff182cSThomas Graf * 551f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 552f8ff182cSThomas Graf */ 5533678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 554f8ff182cSThomas Graf { 555f8ff182cSThomas Graf rtnl_lock(); 5565fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 557f8ff182cSThomas Graf rtnl_unlock(); 558f8ff182cSThomas Graf } 559f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 560f8ff182cSThomas Graf 561f8ff182cSThomas Graf /** 562f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 563f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 564f8ff182cSThomas Graf */ 565f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 566f8ff182cSThomas Graf { 567f8ff182cSThomas Graf rtnl_lock(); 5685fa85a09SFlorian Westphal list_del_rcu(&ops->list); 569f8ff182cSThomas Graf rtnl_unlock(); 5705fa85a09SFlorian Westphal 5715fa85a09SFlorian Westphal synchronize_rcu(); 572f8ff182cSThomas Graf } 573f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 574f8ff182cSThomas Graf 575b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 576b1974ed0SArad, Ronen u32 ext_filter_mask) 577f8ff182cSThomas Graf { 578f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 579f8ff182cSThomas Graf size_t size; 580f8ff182cSThomas Graf 581f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 582f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 583f8ff182cSThomas Graf 5845fa85a09SFlorian Westphal rcu_read_lock(); 5855fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 586f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 587f8ff182cSThomas Graf /* AF_* + nested data */ 588f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 589b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 590f8ff182cSThomas Graf } 591f8ff182cSThomas Graf } 5925fa85a09SFlorian Westphal rcu_read_unlock(); 593f8ff182cSThomas Graf 594f8ff182cSThomas Graf return size; 595f8ff182cSThomas Graf } 596f8ff182cSThomas Graf 597ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 598ba7d49b1SJiri Pirko { 599ba7d49b1SJiri Pirko struct net_device *master_dev; 6004c82a95eSFlorian Westphal bool ret = false; 601ba7d49b1SJiri Pirko 6024c82a95eSFlorian Westphal rcu_read_lock(); 6034c82a95eSFlorian Westphal 6044c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 605813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6064c82a95eSFlorian Westphal ret = true; 6074c82a95eSFlorian Westphal rcu_read_unlock(); 6084c82a95eSFlorian Westphal return ret; 609ba7d49b1SJiri Pirko } 610ba7d49b1SJiri Pirko 611ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 612ba7d49b1SJiri Pirko const struct net_device *dev) 613ba7d49b1SJiri Pirko { 614ba7d49b1SJiri Pirko struct net_device *master_dev; 615ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 616ba7d49b1SJiri Pirko struct nlattr *slave_data; 617ba7d49b1SJiri Pirko int err; 618ba7d49b1SJiri Pirko 619ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 620ba7d49b1SJiri Pirko if (!master_dev) 621ba7d49b1SJiri Pirko return 0; 622ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 623ba7d49b1SJiri Pirko if (!ops) 624ba7d49b1SJiri Pirko return 0; 625ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 626ba7d49b1SJiri Pirko return -EMSGSIZE; 627ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 628ba7d49b1SJiri Pirko slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 629ba7d49b1SJiri Pirko if (!slave_data) 630ba7d49b1SJiri Pirko return -EMSGSIZE; 631ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 632ba7d49b1SJiri Pirko if (err < 0) 633ba7d49b1SJiri Pirko goto err_cancel_slave_data; 634ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 635ba7d49b1SJiri Pirko } 636ba7d49b1SJiri Pirko return 0; 637ba7d49b1SJiri Pirko 638ba7d49b1SJiri Pirko err_cancel_slave_data: 639ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 640ba7d49b1SJiri Pirko return err; 641ba7d49b1SJiri Pirko } 642ba7d49b1SJiri Pirko 643ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 644ba7d49b1SJiri Pirko const struct net_device *dev) 64538f7b870SPatrick McHardy { 64638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 647ba7d49b1SJiri Pirko struct nlattr *data; 648ba7d49b1SJiri Pirko int err; 649ba7d49b1SJiri Pirko 650ba7d49b1SJiri Pirko if (!ops) 651ba7d49b1SJiri Pirko return 0; 652ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 653ba7d49b1SJiri Pirko return -EMSGSIZE; 654ba7d49b1SJiri Pirko if (ops->fill_xstats) { 655ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 656ba7d49b1SJiri Pirko if (err < 0) 657ba7d49b1SJiri Pirko return err; 658ba7d49b1SJiri Pirko } 659ba7d49b1SJiri Pirko if (ops->fill_info) { 660ba7d49b1SJiri Pirko data = nla_nest_start(skb, IFLA_INFO_DATA); 661ba7d49b1SJiri Pirko if (data == NULL) 662ba7d49b1SJiri Pirko return -EMSGSIZE; 663ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 664ba7d49b1SJiri Pirko if (err < 0) 665ba7d49b1SJiri Pirko goto err_cancel_data; 666ba7d49b1SJiri Pirko nla_nest_end(skb, data); 667ba7d49b1SJiri Pirko } 668ba7d49b1SJiri Pirko return 0; 669ba7d49b1SJiri Pirko 670ba7d49b1SJiri Pirko err_cancel_data: 671ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 672ba7d49b1SJiri Pirko return err; 673ba7d49b1SJiri Pirko } 674ba7d49b1SJiri Pirko 675ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 676ba7d49b1SJiri Pirko { 677ba7d49b1SJiri Pirko struct nlattr *linkinfo; 67838f7b870SPatrick McHardy int err = -EMSGSIZE; 67938f7b870SPatrick McHardy 68038f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 68138f7b870SPatrick McHardy if (linkinfo == NULL) 68238f7b870SPatrick McHardy goto out; 68338f7b870SPatrick McHardy 684ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 68538f7b870SPatrick McHardy if (err < 0) 68638f7b870SPatrick McHardy goto err_cancel_link; 687ba7d49b1SJiri Pirko 688ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 68938f7b870SPatrick McHardy if (err < 0) 690ba7d49b1SJiri Pirko goto err_cancel_link; 69138f7b870SPatrick McHardy 69238f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 69338f7b870SPatrick McHardy return 0; 69438f7b870SPatrick McHardy 69538f7b870SPatrick McHardy err_cancel_link: 69638f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 69738f7b870SPatrick McHardy out: 69838f7b870SPatrick McHardy return err; 69938f7b870SPatrick McHardy } 70038f7b870SPatrick McHardy 70195c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7021da177e4SLinus Torvalds { 70397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7041da177e4SLinus Torvalds int err = 0; 7051da177e4SLinus Torvalds 706ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 7071da177e4SLinus Torvalds if (echo) 70863354797SReshetova, Elena refcount_inc(&skb->users); 7091da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 7101da177e4SLinus Torvalds if (echo) 7111da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 7121da177e4SLinus Torvalds return err; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 71597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7162942e900SThomas Graf { 71797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 71897c53cacSDenis V. Lunev 7192942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7202942e900SThomas Graf } 721e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7222942e900SThomas Graf 7231ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 72497676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 72597676b6bSThomas Graf { 72697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72797676b6bSThomas Graf int report = 0; 72897676b6bSThomas Graf 72997676b6bSThomas Graf if (nlh) 73097676b6bSThomas Graf report = nlmsg_report(nlh); 73197676b6bSThomas Graf 7321ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 73397676b6bSThomas Graf } 734e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 73597676b6bSThomas Graf 73697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 73797676b6bSThomas Graf { 73897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73997c53cacSDenis V. Lunev 74097676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 74197676b6bSThomas Graf } 742e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 74397676b6bSThomas Graf 7441da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7451da177e4SLinus Torvalds { 7462d7202bfSThomas Graf struct nlattr *mx; 7472d7202bfSThomas Graf int i, valid = 0; 7481da177e4SLinus Torvalds 7492d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 7502d7202bfSThomas Graf if (mx == NULL) 7512d7202bfSThomas Graf return -ENOBUFS; 7522d7202bfSThomas Graf 7531da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7542d7202bfSThomas Graf if (metrics[i]) { 755ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 756ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 757ea697639SDaniel Borkmann 758ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 759ea697639SDaniel Borkmann if (!name) 760ea697639SDaniel Borkmann continue; 761ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 762ea697639SDaniel Borkmann goto nla_put_failure; 763c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 764c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 765c3a8d947SDaniel Borkmann 766f8edcd12SPhil Sutter if (!user_features) 767f8edcd12SPhil Sutter continue; 768c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 769c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 770c3a8d947SDaniel Borkmann goto nla_put_failure; 771ea697639SDaniel Borkmann } else { 772a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 773a6574349SDavid S. Miller goto nla_put_failure; 7741da177e4SLinus Torvalds } 775ea697639SDaniel Borkmann valid++; 776ea697639SDaniel Borkmann } 7772d7202bfSThomas Graf } 7781da177e4SLinus Torvalds 779a57d27fcSDavid S. Miller if (!valid) { 780a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 781a57d27fcSDavid S. Miller return 0; 782a57d27fcSDavid S. Miller } 7832d7202bfSThomas Graf 7842d7202bfSThomas Graf return nla_nest_end(skb, mx); 7852d7202bfSThomas Graf 7862d7202bfSThomas Graf nla_put_failure: 787bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 788bc3ed28cSThomas Graf return -EMSGSIZE; 7891da177e4SLinus Torvalds } 790e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 7911da177e4SLinus Torvalds 792e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 79387a50699SDavid S. Miller long expires, u32 error) 794e3703b3dSThomas Graf { 795e3703b3dSThomas Graf struct rta_cacheinfo ci = { 796e3703b3dSThomas Graf .rta_error = error, 797e3703b3dSThomas Graf .rta_id = id, 798e3703b3dSThomas Graf }; 799e3703b3dSThomas Graf 8003940746dSDavid Ahern if (dst) { 8013940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8023940746dSDavid Ahern ci.rta_used = dst->__use; 8033940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8043940746dSDavid Ahern } 8058253947eSLi Wei if (expires) { 8068253947eSLi Wei unsigned long clock; 807e3703b3dSThomas Graf 8088253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8098253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8108253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8118253947eSLi Wei } 812e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 813e3703b3dSThomas Graf } 814e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8151da177e4SLinus Torvalds 81693b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 817b00055aaSStefan Rompf { 818b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 819b00055aaSStefan Rompf 820b00055aaSStefan Rompf switch (transition) { 821b00055aaSStefan Rompf case IF_OPER_UP: 822b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 823b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 824b00055aaSStefan Rompf !netif_dormant(dev)) 825b00055aaSStefan Rompf operstate = IF_OPER_UP; 826b00055aaSStefan Rompf break; 827b00055aaSStefan Rompf 828b00055aaSStefan Rompf case IF_OPER_DORMANT: 829b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 830b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 831b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 832b00055aaSStefan Rompf break; 8333ff50b79SStephen Hemminger } 834b00055aaSStefan Rompf 835b00055aaSStefan Rompf if (dev->operstate != operstate) { 836b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 837b00055aaSStefan Rompf dev->operstate = operstate; 838b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 839b00055aaSStefan Rompf netdev_state_change(dev); 84093b2d4a2SDavid S. Miller } 841b00055aaSStefan Rompf } 842b00055aaSStefan Rompf 843b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 844b1beb681SJiri Benc { 845b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 846b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 847b1beb681SJiri Benc } 848b1beb681SJiri Benc 8493729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8503729d502SPatrick McHardy const struct ifinfomsg *ifm) 8513729d502SPatrick McHardy { 8523729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8533729d502SPatrick McHardy 8543729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8553729d502SPatrick McHardy if (ifm->ifi_change) 8563729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 857b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8583729d502SPatrick McHardy 8593729d502SPatrick McHardy return flags; 8603729d502SPatrick McHardy } 8613729d502SPatrick McHardy 862b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 863be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8641da177e4SLinus Torvalds { 865b60c5115SThomas Graf a->rx_packets = b->rx_packets; 866b60c5115SThomas Graf a->tx_packets = b->tx_packets; 867b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 868b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 869b60c5115SThomas Graf a->rx_errors = b->rx_errors; 870b60c5115SThomas Graf a->tx_errors = b->tx_errors; 871b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 872b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 873b60c5115SThomas Graf 874b60c5115SThomas Graf a->multicast = b->multicast; 875b60c5115SThomas Graf a->collisions = b->collisions; 876b60c5115SThomas Graf 877b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 878b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 879b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 880b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 881b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 882b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 883b60c5115SThomas Graf 884b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 885b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 886b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 887b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 888b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 889b60c5115SThomas Graf 890b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 891b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 8926e7333d3SJarod Wilson 8936e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 89410708f37SJan Engelhardt } 89510708f37SJan Engelhardt 896c02db8c6SChris Wright /* All VF info */ 897115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 898115c9b81SGreg Rose u32 ext_filter_mask) 899ebc08a6fSWilliams, Mitch A { 9009af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 901c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9027e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 903045de01aSScott Feldman size += num_vfs * 9047e75f74aSSabrina Dubroca (nla_total_size(0) + 9057e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 9067e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9077e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 90879aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 90979aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 910ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9117e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 912945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 91301a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9143b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9157e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9163b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 917343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9183b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 919343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9203b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 921343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9223b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 923343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9243b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 925343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9263b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 927343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 928c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 929c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 930c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 931c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 932dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 933c02db8c6SChris Wright return size; 934c02db8c6SChris Wright } else 935ebc08a6fSWilliams, Mitch A return 0; 936ebc08a6fSWilliams, Mitch A } 937ebc08a6fSWilliams, Mitch A 938c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 939c53864fdSDavid Gibson u32 ext_filter_mask) 94057b61080SScott Feldman { 94157b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 94257b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 94357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 94457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 94557b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 94657b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 94757b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 94857b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 94957b61080SScott Feldman + port_size; 95057b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 95157b61080SScott Feldman + port_size; 95257b61080SScott Feldman 953c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 954c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 95557b61080SScott Feldman return 0; 95657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 95757b61080SScott Feldman return port_self_size + vf_ports_size + 95857b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 95957b61080SScott Feldman else 96057b61080SScott Feldman return port_self_size; 96157b61080SScott Feldman } 96257b61080SScott Feldman 963b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 964d1fdd913SBrenden Blanco { 965b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 96658038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 967a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9684f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 969d1fdd913SBrenden Blanco 970d1fdd913SBrenden Blanco return xdp_size; 971d1fdd913SBrenden Blanco } 972d1fdd913SBrenden Blanco 973115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 974115c9b81SGreg Rose u32 ext_filter_mask) 975339bf98fSThomas Graf { 976339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 977339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9780b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 979339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 980270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 981339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 98235c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 983339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 984339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 985339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 986339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 987339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 988339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 989339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9909a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 991edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 99276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 99376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 9946919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 9956919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 996339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 99738f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 9982d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 999d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1000db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1001115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1002115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1003115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1004c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1005f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1006b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 100782f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 100888d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1009c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1010b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10113d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10126621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 101338e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 101403ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 101579e1ad14SJiri Benc + nla_total_size(4) /* IFLA_IF_NETNSID */ 1016b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1017b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10183e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10193e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 102079e1ad14SJiri Benc + 0; 1021339bf98fSThomas Graf } 1022339bf98fSThomas Graf 102357b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 102457b61080SScott Feldman { 102557b61080SScott Feldman struct nlattr *vf_ports; 102657b61080SScott Feldman struct nlattr *vf_port; 102757b61080SScott Feldman int vf; 102857b61080SScott Feldman int err; 102957b61080SScott Feldman 103057b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 103157b61080SScott Feldman if (!vf_ports) 103257b61080SScott Feldman return -EMSGSIZE; 103357b61080SScott Feldman 103457b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 103557b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10368ca94183SScott Feldman if (!vf_port) 10378ca94183SScott Feldman goto nla_put_failure; 1038a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1039a6574349SDavid S. Miller goto nla_put_failure; 104057b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10418ca94183SScott Feldman if (err == -EMSGSIZE) 10428ca94183SScott Feldman goto nla_put_failure; 104357b61080SScott Feldman if (err) { 104457b61080SScott Feldman nla_nest_cancel(skb, vf_port); 104557b61080SScott Feldman continue; 104657b61080SScott Feldman } 104757b61080SScott Feldman nla_nest_end(skb, vf_port); 104857b61080SScott Feldman } 104957b61080SScott Feldman 105057b61080SScott Feldman nla_nest_end(skb, vf_ports); 105157b61080SScott Feldman 105257b61080SScott Feldman return 0; 10538ca94183SScott Feldman 10548ca94183SScott Feldman nla_put_failure: 10558ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10568ca94183SScott Feldman return -EMSGSIZE; 105757b61080SScott Feldman } 105857b61080SScott Feldman 105957b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 106057b61080SScott Feldman { 106157b61080SScott Feldman struct nlattr *port_self; 106257b61080SScott Feldman int err; 106357b61080SScott Feldman 106457b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 106557b61080SScott Feldman if (!port_self) 106657b61080SScott Feldman return -EMSGSIZE; 106757b61080SScott Feldman 106857b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 106957b61080SScott Feldman if (err) { 107057b61080SScott Feldman nla_nest_cancel(skb, port_self); 10718ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 107257b61080SScott Feldman } 107357b61080SScott Feldman 107457b61080SScott Feldman nla_nest_end(skb, port_self); 107557b61080SScott Feldman 107657b61080SScott Feldman return 0; 107757b61080SScott Feldman } 107857b61080SScott Feldman 1079c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1080c53864fdSDavid Gibson u32 ext_filter_mask) 108157b61080SScott Feldman { 108257b61080SScott Feldman int err; 108357b61080SScott Feldman 1084c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1085c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 108657b61080SScott Feldman return 0; 108757b61080SScott Feldman 108857b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 108957b61080SScott Feldman if (err) 109057b61080SScott Feldman return err; 109157b61080SScott Feldman 109257b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 109357b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 109457b61080SScott Feldman if (err) 109557b61080SScott Feldman return err; 109657b61080SScott Feldman } 109757b61080SScott Feldman 109857b61080SScott Feldman return 0; 109957b61080SScott Feldman } 110057b61080SScott Feldman 110166cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 110266cae9edSJiri Pirko { 110366cae9edSJiri Pirko int err; 110402637fceSJiri Pirko struct netdev_phys_item_id ppid; 110566cae9edSJiri Pirko 110666cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 110766cae9edSJiri Pirko if (err) { 110866cae9edSJiri Pirko if (err == -EOPNOTSUPP) 110966cae9edSJiri Pirko return 0; 111066cae9edSJiri Pirko return err; 111166cae9edSJiri Pirko } 111266cae9edSJiri Pirko 111366cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 111466cae9edSJiri Pirko return -EMSGSIZE; 111566cae9edSJiri Pirko 111666cae9edSJiri Pirko return 0; 111766cae9edSJiri Pirko } 111866cae9edSJiri Pirko 1119db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1120db24a904SDavid Ahern { 1121db24a904SDavid Ahern char name[IFNAMSIZ]; 1122db24a904SDavid Ahern int err; 1123db24a904SDavid Ahern 1124db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1125db24a904SDavid Ahern if (err) { 1126db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1127db24a904SDavid Ahern return 0; 1128db24a904SDavid Ahern return err; 1129db24a904SDavid Ahern } 1130db24a904SDavid Ahern 113177ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1132db24a904SDavid Ahern return -EMSGSIZE; 1133db24a904SDavid Ahern 1134db24a904SDavid Ahern return 0; 1135db24a904SDavid Ahern } 1136db24a904SDavid Ahern 113782f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 113882f28412SJiri Pirko { 113982f28412SJiri Pirko int err; 1140f8e20a9fSScott Feldman struct switchdev_attr attr = { 11416ff64f6fSIdo Schimmel .orig_dev = dev, 11421f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1143f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1144f8e20a9fSScott Feldman }; 114582f28412SJiri Pirko 1146f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 114782f28412SJiri Pirko if (err) { 114882f28412SJiri Pirko if (err == -EOPNOTSUPP) 114982f28412SJiri Pirko return 0; 115082f28412SJiri Pirko return err; 115182f28412SJiri Pirko } 115282f28412SJiri Pirko 115342275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 115442275bd8SScott Feldman attr.u.ppid.id)) 115582f28412SJiri Pirko return -EMSGSIZE; 115682f28412SJiri Pirko 115782f28412SJiri Pirko return 0; 115882f28412SJiri Pirko } 115982f28412SJiri Pirko 1160b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1161b22b941bSHannes Frederic Sowa struct net_device *dev) 1162b22b941bSHannes Frederic Sowa { 1163550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1164b22b941bSHannes Frederic Sowa struct nlattr *attr; 1165b22b941bSHannes Frederic Sowa 116658414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 116758414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1168b22b941bSHannes Frederic Sowa if (!attr) 1169b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1170b22b941bSHannes Frederic Sowa 1171550bce59SRoopa Prabhu sp = nla_data(attr); 1172550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1173550bce59SRoopa Prabhu 1174550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1175550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1176550bce59SRoopa Prabhu if (!attr) 1177550bce59SRoopa Prabhu return -EMSGSIZE; 1178550bce59SRoopa Prabhu 1179550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1180b22b941bSHannes Frederic Sowa 1181b22b941bSHannes Frederic Sowa return 0; 1182b22b941bSHannes Frederic Sowa } 1183b22b941bSHannes Frederic Sowa 1184b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1185b22b941bSHannes Frederic Sowa struct net_device *dev, 1186b22b941bSHannes Frederic Sowa int vfs_num, 1187b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1188b22b941bSHannes Frederic Sowa { 1189b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 119079aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1191b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 119279aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1193b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1194b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1195b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1196b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1197b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1198b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1199b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1200b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1201b22b941bSHannes Frederic Sowa 12020eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12030eed9cf5SMintz, Yuval 1204b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1205b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1206b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1207b22b941bSHannes Frederic Sowa * didn't report anything. 1208b22b941bSHannes Frederic Sowa */ 1209b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1210b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1211b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1212b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1213b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1214b22b941bSHannes Frederic Sowa */ 1215b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 121679aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 121779aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1218b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1219b22b941bSHannes Frederic Sowa return 0; 1220b22b941bSHannes Frederic Sowa 1221775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1222775f4f05SDan Carpenter 1223b22b941bSHannes Frederic Sowa vf_mac.vf = 1224b22b941bSHannes Frederic Sowa vf_vlan.vf = 122579aab093SMoshe Shemesh vf_vlan_info.vf = 1226b22b941bSHannes Frederic Sowa vf_rate.vf = 1227b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1228b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1229b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1230b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1231b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1232b22b941bSHannes Frederic Sowa 1233b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1234b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1235b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 123679aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 123779aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 123879aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1239b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1240b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1241b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1242b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1243b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1244b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1245b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1246b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 124779aab093SMoshe Shemesh if (!vf) 124879aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1249b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1250b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1251b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1252b22b941bSHannes Frederic Sowa &vf_rate) || 1253b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1254b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1255b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1256b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1257b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1258b22b941bSHannes Frederic Sowa &vf_linkstate) || 1259b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1260b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1261b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1262b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1263b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 126479aab093SMoshe Shemesh goto nla_put_vf_failure; 126579aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 126679aab093SMoshe Shemesh if (!vfvlanlist) 126779aab093SMoshe Shemesh goto nla_put_vf_failure; 126879aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 126979aab093SMoshe Shemesh &vf_vlan_info)) { 127079aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 127179aab093SMoshe Shemesh goto nla_put_vf_failure; 127279aab093SMoshe Shemesh } 127379aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1274b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1275b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1276b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1277b22b941bSHannes Frederic Sowa &vf_stats); 1278b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 127979aab093SMoshe Shemesh if (!vfstats) 128079aab093SMoshe Shemesh goto nla_put_vf_failure; 1281343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1282343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1283343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1284343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1285343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1286343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1287343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1288343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1289343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1290343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1291343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1292c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1293c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1294c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1295c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1296c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 129779aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 129879aab093SMoshe Shemesh goto nla_put_vf_failure; 129979aab093SMoshe Shemesh } 1300b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1301b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1302b22b941bSHannes Frederic Sowa return 0; 130379aab093SMoshe Shemesh 130479aab093SMoshe Shemesh nla_put_vf_failure: 130579aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 130679aab093SMoshe Shemesh nla_put_vfinfo_failure: 130779aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 130879aab093SMoshe Shemesh return -EMSGSIZE; 1309b22b941bSHannes Frederic Sowa } 1310b22b941bSHannes Frederic Sowa 1311250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1312250fc3dfSFlorian Westphal struct net_device *dev, 1313250fc3dfSFlorian Westphal u32 ext_filter_mask) 1314250fc3dfSFlorian Westphal { 1315250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1316250fc3dfSFlorian Westphal int i, num_vfs; 1317250fc3dfSFlorian Westphal 1318250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1319250fc3dfSFlorian Westphal return 0; 1320250fc3dfSFlorian Westphal 1321250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1322250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1323250fc3dfSFlorian Westphal return -EMSGSIZE; 1324250fc3dfSFlorian Westphal 1325250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1326250fc3dfSFlorian Westphal return 0; 1327250fc3dfSFlorian Westphal 1328250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1329250fc3dfSFlorian Westphal if (!vfinfo) 1330250fc3dfSFlorian Westphal return -EMSGSIZE; 1331250fc3dfSFlorian Westphal 1332250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1333250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1334250fc3dfSFlorian Westphal return -EMSGSIZE; 1335250fc3dfSFlorian Westphal } 1336250fc3dfSFlorian Westphal 1337250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1338250fc3dfSFlorian Westphal return 0; 1339250fc3dfSFlorian Westphal } 1340250fc3dfSFlorian Westphal 1341b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1342b22b941bSHannes Frederic Sowa { 13435f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13445f8e4474SKangjie Lu 13455f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13465f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13475f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13485f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13495f8e4474SKangjie Lu map.irq = dev->irq; 13505f8e4474SKangjie Lu map.dma = dev->dma; 13515f8e4474SKangjie Lu map.port = dev->if_port; 13525f8e4474SKangjie Lu 1353270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1354b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1355b22b941bSHannes Frederic Sowa 1356b22b941bSHannes Frederic Sowa return 0; 1357b22b941bSHannes Frederic Sowa } 1358b22b941bSHannes Frederic Sowa 1359a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1360d67b9cd2SDaniel Borkmann { 136158038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1362d67b9cd2SDaniel Borkmann 1363d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1364d67b9cd2SDaniel Borkmann 136558038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1366a25717d2SJakub Kicinski if (!generic_xdp_prog) 1367a25717d2SJakub Kicinski return 0; 1368a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 136958038695SMartin KaFai Lau } 1370ce158e58SJakub Kicinski 1371a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1372a25717d2SJakub Kicinski { 1373a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1374a25717d2SJakub Kicinski } 1375118b4aa2SJakub Kicinski 1376a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1377a25717d2SJakub Kicinski { 1378a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1379a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1380a25717d2SJakub Kicinski } 1381a25717d2SJakub Kicinski 1382a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1383a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1384a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1385a25717d2SJakub Kicinski { 1386a25717d2SJakub Kicinski u32 curr_id; 1387a25717d2SJakub Kicinski int err; 1388a25717d2SJakub Kicinski 1389a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1390a25717d2SJakub Kicinski if (!curr_id) 1391a25717d2SJakub Kicinski return 0; 1392a25717d2SJakub Kicinski 1393a25717d2SJakub Kicinski *prog_id = curr_id; 1394a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1395a25717d2SJakub Kicinski if (err) 1396a25717d2SJakub Kicinski return err; 1397a25717d2SJakub Kicinski 1398a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1399a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1400a25717d2SJakub Kicinski else 1401a25717d2SJakub Kicinski *mode = tgt_mode; 1402a25717d2SJakub Kicinski 1403a25717d2SJakub Kicinski return 0; 1404d67b9cd2SDaniel Borkmann } 1405d67b9cd2SDaniel Borkmann 1406d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1407d1fdd913SBrenden Blanco { 1408d1fdd913SBrenden Blanco struct nlattr *xdp; 140958038695SMartin KaFai Lau u32 prog_id; 1410d1fdd913SBrenden Blanco int err; 14114f91da26SJakub Kicinski u8 mode; 1412d1fdd913SBrenden Blanco 1413d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1414d1fdd913SBrenden Blanco if (!xdp) 1415d1fdd913SBrenden Blanco return -EMSGSIZE; 1416b5cdae32SDavid S. Miller 1417a25717d2SJakub Kicinski prog_id = 0; 1418a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1419202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1420202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1421202aabe8SJakub Kicinski if (err) 1422a25717d2SJakub Kicinski goto err_cancel; 1423202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1424202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1425202aabe8SJakub Kicinski if (err) 1426a25717d2SJakub Kicinski goto err_cancel; 1427202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1428202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1429202aabe8SJakub Kicinski if (err) 1430a25717d2SJakub Kicinski goto err_cancel; 1431a25717d2SJakub Kicinski 14324f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1433d1fdd913SBrenden Blanco if (err) 1434d1fdd913SBrenden Blanco goto err_cancel; 1435d1fdd913SBrenden Blanco 1436a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 143758038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 143858038695SMartin KaFai Lau if (err) 143958038695SMartin KaFai Lau goto err_cancel; 144058038695SMartin KaFai Lau } 144158038695SMartin KaFai Lau 1442d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1443d1fdd913SBrenden Blanco return 0; 1444d1fdd913SBrenden Blanco 1445d1fdd913SBrenden Blanco err_cancel: 1446d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1447d1fdd913SBrenden Blanco return err; 1448d1fdd913SBrenden Blanco } 1449d1fdd913SBrenden Blanco 14503d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14513d3ea5afSVlad Yasevich { 14523d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14533d3ea5afSVlad Yasevich 14543d3ea5afSVlad Yasevich switch (event) { 14553d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14563d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14573d3ea5afSVlad Yasevich break; 14583d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14593d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14603d3ea5afSVlad Yasevich break; 14613d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14623d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14633d3ea5afSVlad Yasevich break; 14643d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14653d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14663d3ea5afSVlad Yasevich break; 14673d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14683d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14693d3ea5afSVlad Yasevich break; 14703d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14713d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14723d3ea5afSVlad Yasevich break; 14733d3ea5afSVlad Yasevich default: 14743d3ea5afSVlad Yasevich break; 14753d3ea5afSVlad Yasevich } 14763d3ea5afSVlad Yasevich 14773d3ea5afSVlad Yasevich return rtnl_event_type; 14783d3ea5afSVlad Yasevich } 14793d3ea5afSVlad Yasevich 148079110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 148179110a04SFlorian Westphal { 148279110a04SFlorian Westphal const struct net_device *upper_dev; 148379110a04SFlorian Westphal int ret = 0; 148479110a04SFlorian Westphal 148579110a04SFlorian Westphal rcu_read_lock(); 148679110a04SFlorian Westphal 148779110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 148879110a04SFlorian Westphal if (upper_dev) 148979110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 149079110a04SFlorian Westphal 149179110a04SFlorian Westphal rcu_read_unlock(); 149279110a04SFlorian Westphal return ret; 149379110a04SFlorian Westphal } 149479110a04SFlorian Westphal 149579110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 149679110a04SFlorian Westphal { 149779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 149879110a04SFlorian Westphal 149979110a04SFlorian Westphal if (dev->ifindex == ifindex) 150079110a04SFlorian Westphal return 0; 150179110a04SFlorian Westphal 150279110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 150379110a04SFlorian Westphal } 150479110a04SFlorian Westphal 15056c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15066c557001SFlorian Westphal struct net_device *dev) 15076c557001SFlorian Westphal { 15086c557001SFlorian Westphal char buf[IFALIASZ]; 15096c557001SFlorian Westphal int ret; 15106c557001SFlorian Westphal 15116c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15126c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15136c557001SFlorian Westphal } 15146c557001SFlorian Westphal 1515b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 151679e1ad14SJiri Benc const struct net_device *dev, 151779e1ad14SJiri Benc struct net *src_net) 1518b1e66b9aSFlorian Westphal { 1519b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1520b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1521b1e66b9aSFlorian Westphal 1522b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 152379e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1524b1e66b9aSFlorian Westphal 1525b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1526b1e66b9aSFlorian Westphal return -EMSGSIZE; 1527b1e66b9aSFlorian Westphal } 1528b1e66b9aSFlorian Westphal } 1529b1e66b9aSFlorian Westphal 1530b1e66b9aSFlorian Westphal return 0; 1531b1e66b9aSFlorian Westphal } 1532b1e66b9aSFlorian Westphal 1533070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1534070cbf5bSFlorian Westphal const struct net_device *dev, 1535070cbf5bSFlorian Westphal u32 ext_filter_mask) 1536070cbf5bSFlorian Westphal { 1537070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1538070cbf5bSFlorian Westphal struct nlattr *af_spec; 1539070cbf5bSFlorian Westphal 1540070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1541070cbf5bSFlorian Westphal if (!af_spec) 1542070cbf5bSFlorian Westphal return -EMSGSIZE; 1543070cbf5bSFlorian Westphal 15445fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1545070cbf5bSFlorian Westphal struct nlattr *af; 1546070cbf5bSFlorian Westphal int err; 1547070cbf5bSFlorian Westphal 1548070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1549070cbf5bSFlorian Westphal continue; 1550070cbf5bSFlorian Westphal 1551070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1552070cbf5bSFlorian Westphal if (!af) 1553070cbf5bSFlorian Westphal return -EMSGSIZE; 1554070cbf5bSFlorian Westphal 1555070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1556070cbf5bSFlorian Westphal /* 1557070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1558070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1559070cbf5bSFlorian Westphal * means we should trim the attribute header and 1560070cbf5bSFlorian Westphal * continue. 1561070cbf5bSFlorian Westphal */ 1562070cbf5bSFlorian Westphal if (err == -ENODATA) 1563070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1564070cbf5bSFlorian Westphal else if (err < 0) 1565070cbf5bSFlorian Westphal return -EMSGSIZE; 1566070cbf5bSFlorian Westphal 1567070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1568070cbf5bSFlorian Westphal } 1569070cbf5bSFlorian Westphal 1570070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1571070cbf5bSFlorian Westphal return 0; 1572070cbf5bSFlorian Westphal } 1573070cbf5bSFlorian Westphal 157479e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 157579e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1576575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15773d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 157838e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 157938e01b30SNicolas Dichtel int tgt_netnsid) 1580b60c5115SThomas Graf { 1581b60c5115SThomas Graf struct ifinfomsg *ifm; 15821da177e4SLinus Torvalds struct nlmsghdr *nlh; 15831da177e4SLinus Torvalds 15842907c35fSEric Dumazet ASSERT_RTNL(); 1585b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1586b60c5115SThomas Graf if (nlh == NULL) 158726932566SPatrick McHardy return -EMSGSIZE; 15881da177e4SLinus Torvalds 1589b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1590b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1591b60c5115SThomas Graf ifm->__ifi_pad = 0; 1592b60c5115SThomas Graf ifm->ifi_type = dev->type; 1593b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1594b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1595b60c5115SThomas Graf ifm->ifi_change = change; 15961da177e4SLinus Torvalds 159779e1ad14SJiri Benc if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 159879e1ad14SJiri Benc goto nla_put_failure; 159979e1ad14SJiri Benc 1600a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1601a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1602a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1603a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1604a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1605a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 16063e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 16073e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1608a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1609edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 161076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1611c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1612c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 16131d69c2b3SMark A. Greer #ifdef CONFIG_RPS 161476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 16151d69c2b3SMark A. Greer #endif 161679110a04SFlorian Westphal nla_put_iflink(skb, dev) || 161779110a04SFlorian Westphal put_master_ifindex(skb, dev) || 16189a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1619a6574349SDavid S. Miller (dev->qdisc && 1620a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 16216c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 16222d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1623b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1624b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1625b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1626b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1627b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1628b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1629b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1630a6574349SDavid S. Miller goto nla_put_failure; 16310b815a1aSStephen Hemminger 16323d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 16333d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 16343d3ea5afSVlad Yasevich goto nla_put_failure; 16353d3ea5afSVlad Yasevich } 16363d3ea5afSVlad Yasevich 1637b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1638a6574349SDavid S. Miller goto nla_put_failure; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds if (dev->addr_len) { 1641a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1642a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1643a6574349SDavid S. Miller goto nla_put_failure; 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 164666cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 164766cae9edSJiri Pirko goto nla_put_failure; 164866cae9edSJiri Pirko 1649db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1650db24a904SDavid Ahern goto nla_put_failure; 1651db24a904SDavid Ahern 165282f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 165382f28412SJiri Pirko goto nla_put_failure; 165482f28412SJiri Pirko 1655b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1656b60c5115SThomas Graf goto nla_put_failure; 1657b60c5115SThomas Graf 1658250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1659a6574349SDavid S. Miller goto nla_put_failure; 166057b61080SScott Feldman 1661c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 166257b61080SScott Feldman goto nla_put_failure; 166357b61080SScott Feldman 1664d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1665d1fdd913SBrenden Blanco goto nla_put_failure; 1666d1fdd913SBrenden Blanco 1667ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 166838f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 166938f7b870SPatrick McHardy goto nla_put_failure; 167038f7b870SPatrick McHardy } 167138f7b870SPatrick McHardy 167279e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1673d37512a2SNicolas Dichtel goto nla_put_failure; 1674d37512a2SNicolas Dichtel 16756621dd29SNicolas Dichtel if (new_nsid && 16766621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16776621dd29SNicolas Dichtel goto nla_put_failure; 167838e01b30SNicolas Dichtel if (new_ifindex && 167938e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 168038e01b30SNicolas Dichtel goto nla_put_failure; 168138e01b30SNicolas Dichtel 16826621dd29SNicolas Dichtel 16835fa85a09SFlorian Westphal rcu_read_lock(); 1684070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16855fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16865fa85a09SFlorian Westphal rcu_read_unlock(); 1687f8ff182cSThomas Graf 1688053c095aSJohannes Berg nlmsg_end(skb, nlh); 1689053c095aSJohannes Berg return 0; 1690b60c5115SThomas Graf 16915fa85a09SFlorian Westphal nla_put_failure_rcu: 16925fa85a09SFlorian Westphal rcu_read_unlock(); 1693b60c5115SThomas Graf nla_put_failure: 169426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 169526932566SPatrick McHardy return -EMSGSIZE; 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 1698f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1699f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1700f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1701f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1702f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1703f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1704f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1705f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1706f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1707f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1708f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1709f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1710f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1711f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1712f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1713f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 17142459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 17152459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 17162459b4c6SNicolas Dichtel */ 17172459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1718f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1719f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1720f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1721f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1722f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1723f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1724f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1725f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 172646e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 172746e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 172802637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 17292d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 173082f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1731317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 173288d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1733d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 17343d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1735db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 173679e1ad14SJiri Benc [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1737b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1738b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 17393e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 17403e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 1741f7b12606SJiri Pirko }; 1742f7b12606SJiri Pirko 1743f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1744f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1745f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1746f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1747f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1748f7b12606SJiri Pirko }; 1749f7b12606SJiri Pirko 1750f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1751364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1752364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 175379aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1754364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1755364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1756364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1757364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 175801a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17593b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1760dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1761cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1762cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17633b766cd8SEran Ben Elisha }; 17643b766cd8SEran Ben Elisha 1765f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1766f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1767f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1768f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1769f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1770f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1771f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1772f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1773f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1774f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1775025331dfSDaniel Borkmann 1776025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1777025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1778025331dfSDaniel Borkmann * combination with structs. 1779025331dfSDaniel Borkmann */ 1780025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1781025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1782f7b12606SJiri Pirko }; 1783f7b12606SJiri Pirko 1784d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1785d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1786d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 178785de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 178858038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1789d1fdd913SBrenden Blanco }; 1790d1fdd913SBrenden Blanco 1791dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1792dc599f76SDavid Ahern { 1793dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1794dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1795dc599f76SDavid Ahern 1796fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1797fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1798dc599f76SDavid Ahern return NULL; 1799dc599f76SDavid Ahern 1800dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1801dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1802dc599f76SDavid Ahern 1803dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1804dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1805dc599f76SDavid Ahern } 1806dc599f76SDavid Ahern 1807dc599f76SDavid Ahern return ops; 1808dc599f76SDavid Ahern } 1809dc599f76SDavid Ahern 1810dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1811dc599f76SDavid Ahern { 1812dc599f76SDavid Ahern struct net_device *master; 1813dc599f76SDavid Ahern 1814dc599f76SDavid Ahern if (!master_idx) 1815dc599f76SDavid Ahern return false; 1816dc599f76SDavid Ahern 1817dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1818dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1819dc599f76SDavid Ahern return true; 1820dc599f76SDavid Ahern 1821dc599f76SDavid Ahern return false; 1822dc599f76SDavid Ahern } 1823dc599f76SDavid Ahern 1824dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1825dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1826dc599f76SDavid Ahern { 1827dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1828dc599f76SDavid Ahern return true; 1829dc599f76SDavid Ahern 1830dc599f76SDavid Ahern return false; 1831dc599f76SDavid Ahern } 1832dc599f76SDavid Ahern 1833dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1834dc599f76SDavid Ahern int master_idx, 1835dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1836dc599f76SDavid Ahern { 1837dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1838dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1839dc599f76SDavid Ahern return true; 1840dc599f76SDavid Ahern 1841dc599f76SDavid Ahern return false; 1842dc599f76SDavid Ahern } 1843dc599f76SDavid Ahern 1844f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid) 184579e1ad14SJiri Benc { 184679e1ad14SJiri Benc struct net *net; 184779e1ad14SJiri Benc 1848f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 184979e1ad14SJiri Benc if (!net) 185079e1ad14SJiri Benc return ERR_PTR(-EINVAL); 185179e1ad14SJiri Benc 185279e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 185379e1ad14SJiri Benc * the user namespace owning the target net ns. 185479e1ad14SJiri Benc */ 1855f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 185679e1ad14SJiri Benc put_net(net); 185779e1ad14SJiri Benc return ERR_PTR(-EACCES); 185879e1ad14SJiri Benc } 185979e1ad14SJiri Benc return net; 186079e1ad14SJiri Benc } 186179e1ad14SJiri Benc 1862b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 18631da177e4SLinus Torvalds { 18643b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 186579e1ad14SJiri Benc struct net *tgt_net = net; 18667c28bd0bSEric Dumazet int h, s_h; 18677c28bd0bSEric Dumazet int idx = 0, s_idx; 18681da177e4SLinus Torvalds struct net_device *dev; 18697c28bd0bSEric Dumazet struct hlist_head *head; 1870115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1871115c9b81SGreg Rose u32 ext_filter_mask = 0; 1872dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1873dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1874dc599f76SDavid Ahern int master_idx = 0; 187579e1ad14SJiri Benc int netnsid = -1; 1876973462bbSDavid Gibson int err; 1877e5eca6d4SMichal Schmidt int hdrlen; 18781da177e4SLinus Torvalds 18797c28bd0bSEric Dumazet s_h = cb->args[0]; 18807c28bd0bSEric Dumazet s_idx = cb->args[1]; 18817c28bd0bSEric Dumazet 1882e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1883e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1884e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1885e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1886e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1887e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1888e5eca6d4SMichal Schmidt */ 1889e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1890e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1891e5eca6d4SMichal Schmidt 1892fceb6435SJohannes Berg if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1893fceb6435SJohannes Berg ifla_policy, NULL) >= 0) { 189479e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 189579e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1896f428fe4aSAndrei Vagin tgt_net = get_target_net(skb->sk, netnsid); 189779e1ad14SJiri Benc if (IS_ERR(tgt_net)) { 189879e1ad14SJiri Benc tgt_net = net; 189979e1ad14SJiri Benc netnsid = -1; 190079e1ad14SJiri Benc } 190179e1ad14SJiri Benc } 190279e1ad14SJiri Benc 1903115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1904115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1905dc599f76SDavid Ahern 1906dc599f76SDavid Ahern if (tb[IFLA_MASTER]) 1907dc599f76SDavid Ahern master_idx = nla_get_u32(tb[IFLA_MASTER]); 1908dc599f76SDavid Ahern 1909dc599f76SDavid Ahern if (tb[IFLA_LINKINFO]) 1910dc599f76SDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1911dc599f76SDavid Ahern 1912dc599f76SDavid Ahern if (master_idx || kind_ops) 1913dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1914a4b64fbeSEric Dumazet } 1915115c9b81SGreg Rose 19167c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 19177562f876SPavel Emelianov idx = 0; 191879e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1919cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1920dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 19213f0ae05dSZhang Shengju goto cont; 19221da177e4SLinus Torvalds if (idx < s_idx) 19237562f876SPavel Emelianov goto cont; 192479e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 192579e1ad14SJiri Benc RTM_NEWLINK, 192615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 19277c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1928dc599f76SDavid Ahern flags, 192938e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 193079e1ad14SJiri Benc netnsid); 1931973462bbSDavid Gibson 1932f6c5775fSDavid Ahern if (err < 0) { 1933f6c5775fSDavid Ahern if (likely(skb->len)) 19347c28bd0bSEric Dumazet goto out; 19354e985adaSThomas Graf 1936f6c5775fSDavid Ahern goto out_err; 1937f6c5775fSDavid Ahern } 19387562f876SPavel Emelianov cont: 19397562f876SPavel Emelianov idx++; 19401da177e4SLinus Torvalds } 19417c28bd0bSEric Dumazet } 19427c28bd0bSEric Dumazet out: 1943f6c5775fSDavid Ahern err = skb->len; 1944f6c5775fSDavid Ahern out_err: 19457c28bd0bSEric Dumazet cb->args[1] = idx; 19467c28bd0bSEric Dumazet cb->args[0] = h; 1947d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 1948d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 194979e1ad14SJiri Benc if (netnsid >= 0) 195079e1ad14SJiri Benc put_net(tgt_net); 19511da177e4SLinus Torvalds 1952f6c5775fSDavid Ahern return err; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds 1955fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1956fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 1957f7b12606SJiri Pirko { 1958fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1959f7b12606SJiri Pirko } 1960f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 196157b61080SScott Feldman 196281adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 196381adee47SEric W. Biederman { 196481adee47SEric W. Biederman struct net *net; 196581adee47SEric W. Biederman /* Examine the link attributes and figure out which 196681adee47SEric W. Biederman * network namespace we are talking about. 196781adee47SEric W. Biederman */ 196881adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 196981adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1970f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1971f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 197281adee47SEric W. Biederman else 197381adee47SEric W. Biederman net = get_net(src_net); 197481adee47SEric W. Biederman return net; 197581adee47SEric W. Biederman } 197681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 197781adee47SEric W. Biederman 19787c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 19797c4f63baSChristian Brauner * examining the link attributes in the following order: 19807c4f63baSChristian Brauner * 19817c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 19827c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 19837c4f63baSChristian Brauner * 3. IFLA_IF_NETNSID 19847c4f63baSChristian Brauner */ 19857c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 19867c4f63baSChristian Brauner struct nlattr *tb[]) 19877c4f63baSChristian Brauner { 19887c4f63baSChristian Brauner struct net *net; 19897c4f63baSChristian Brauner 19907c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 19917c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 19927c4f63baSChristian Brauner 19937c4f63baSChristian Brauner if (!tb[IFLA_IF_NETNSID]) 19947c4f63baSChristian Brauner return get_net(src_net); 19957c4f63baSChristian Brauner 19967c4f63baSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 19977c4f63baSChristian Brauner if (!net) 19987c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 19997c4f63baSChristian Brauner 20007c4f63baSChristian Brauner return net; 20017c4f63baSChristian Brauner } 20027c4f63baSChristian Brauner 20037c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 20047c4f63baSChristian Brauner struct net *src_net, 20057c4f63baSChristian Brauner struct nlattr *tb[], int cap) 20067c4f63baSChristian Brauner { 20077c4f63baSChristian Brauner struct net *net; 20087c4f63baSChristian Brauner 20097c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 20107c4f63baSChristian Brauner if (IS_ERR(net)) 20117c4f63baSChristian Brauner return net; 20127c4f63baSChristian Brauner 20137c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 20147c4f63baSChristian Brauner put_net(net); 20157c4f63baSChristian Brauner return ERR_PTR(-EPERM); 20167c4f63baSChristian Brauner } 20177c4f63baSChristian Brauner 20187c4f63baSChristian Brauner return net; 20197c4f63baSChristian Brauner } 20207c4f63baSChristian Brauner 20214ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 20224ff66caeSChristian Brauner * potentially referring to different network namespaces. 20234ff66caeSChristian Brauner */ 20244ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 20254ff66caeSChristian Brauner struct netlink_ext_ack *extack, 20264ff66caeSChristian Brauner bool netns_id_only) 20274ff66caeSChristian Brauner { 20284ff66caeSChristian Brauner 20294ff66caeSChristian Brauner if (netns_id_only) { 20304ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 20314ff66caeSChristian Brauner return 0; 20324ff66caeSChristian Brauner 20334ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 20344ff66caeSChristian Brauner return -EOPNOTSUPP; 20354ff66caeSChristian Brauner } 20364ff66caeSChristian Brauner 20374ff66caeSChristian Brauner if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 20384ff66caeSChristian Brauner goto invalid_attr; 20394ff66caeSChristian Brauner 20404ff66caeSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 20414ff66caeSChristian Brauner goto invalid_attr; 20424ff66caeSChristian Brauner 20434ff66caeSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 20444ff66caeSChristian Brauner goto invalid_attr; 20454ff66caeSChristian Brauner 20464ff66caeSChristian Brauner return 0; 20474ff66caeSChristian Brauner 20484ff66caeSChristian Brauner invalid_attr: 20494ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 20504ff66caeSChristian Brauner return -EINVAL; 20514ff66caeSChristian Brauner } 20524ff66caeSChristian Brauner 20531840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 20541840bb13SThomas Graf { 20551840bb13SThomas Graf if (dev) { 20561840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 20571840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 20581840bb13SThomas Graf return -EINVAL; 20591840bb13SThomas Graf 20601840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 20611840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 20621840bb13SThomas Graf return -EINVAL; 20631840bb13SThomas Graf } 20641840bb13SThomas Graf 2065cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2066cf7afbfeSThomas Graf struct nlattr *af; 2067cf7afbfeSThomas Graf int rem, err; 2068cf7afbfeSThomas Graf 2069cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2070cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2071cf7afbfeSThomas Graf 20725fa85a09SFlorian Westphal rcu_read_lock(); 20735fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 20745fa85a09SFlorian Westphal if (!af_ops) { 20755fa85a09SFlorian Westphal rcu_read_unlock(); 2076cf7afbfeSThomas Graf return -EAFNOSUPPORT; 20775fa85a09SFlorian Westphal } 2078cf7afbfeSThomas Graf 20795fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 20805fa85a09SFlorian Westphal rcu_read_unlock(); 2081cf7afbfeSThomas Graf return -EOPNOTSUPP; 20825fa85a09SFlorian Westphal } 2083cf7afbfeSThomas Graf 2084cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 20856d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 20865fa85a09SFlorian Westphal if (err < 0) { 20875fa85a09SFlorian Westphal rcu_read_unlock(); 2088cf7afbfeSThomas Graf return err; 2089cf7afbfeSThomas Graf } 2090cf7afbfeSThomas Graf } 20915fa85a09SFlorian Westphal 20925fa85a09SFlorian Westphal rcu_read_unlock(); 20935fa85a09SFlorian Westphal } 2094cf7afbfeSThomas Graf } 2095cf7afbfeSThomas Graf 20961840bb13SThomas Graf return 0; 20971840bb13SThomas Graf } 20981840bb13SThomas Graf 2099cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2100cc8e27ccSEli Cohen int guid_type) 2101cc8e27ccSEli Cohen { 2102cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2103cc8e27ccSEli Cohen 2104cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2105cc8e27ccSEli Cohen } 2106cc8e27ccSEli Cohen 2107cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2108cc8e27ccSEli Cohen { 2109cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2110cc8e27ccSEli Cohen return -EOPNOTSUPP; 2111cc8e27ccSEli Cohen 2112cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2113cc8e27ccSEli Cohen } 2114cc8e27ccSEli Cohen 21154f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2116c02db8c6SChris Wright { 2117c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 21184f7d2cdfSDaniel Borkmann int err = -EINVAL; 2119c02db8c6SChris Wright 21204f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 21214f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 21224f7d2cdfSDaniel Borkmann 2123c02db8c6SChris Wright err = -EOPNOTSUPP; 2124c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2125c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2126c02db8c6SChris Wright ivm->mac); 21274f7d2cdfSDaniel Borkmann if (err < 0) 21284f7d2cdfSDaniel Borkmann return err; 2129c02db8c6SChris Wright } 21304f7d2cdfSDaniel Borkmann 21314f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 21324f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 21334f7d2cdfSDaniel Borkmann 2134c02db8c6SChris Wright err = -EOPNOTSUPP; 2135c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 21364f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 213779aab093SMoshe Shemesh ivv->qos, 213879aab093SMoshe Shemesh htons(ETH_P_8021Q)); 213979aab093SMoshe Shemesh if (err < 0) 214079aab093SMoshe Shemesh return err; 214179aab093SMoshe Shemesh } 214279aab093SMoshe Shemesh 214379aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 214479aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 214579aab093SMoshe Shemesh struct nlattr *attr; 214679aab093SMoshe Shemesh int rem, len = 0; 214779aab093SMoshe Shemesh 214879aab093SMoshe Shemesh err = -EOPNOTSUPP; 214979aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 215079aab093SMoshe Shemesh return err; 215179aab093SMoshe Shemesh 215279aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 215379aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 215479aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 215579aab093SMoshe Shemesh return -EINVAL; 215679aab093SMoshe Shemesh } 215779aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 215879aab093SMoshe Shemesh return -EOPNOTSUPP; 215979aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 216079aab093SMoshe Shemesh 216179aab093SMoshe Shemesh len++; 216279aab093SMoshe Shemesh } 2163fa34cd94SArnd Bergmann if (len == 0) 2164fa34cd94SArnd Bergmann return -EINVAL; 2165fa34cd94SArnd Bergmann 216679aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 216779aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 21684f7d2cdfSDaniel Borkmann if (err < 0) 21694f7d2cdfSDaniel Borkmann return err; 2170c02db8c6SChris Wright } 21714f7d2cdfSDaniel Borkmann 21724f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 21734f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2174ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 21754f7d2cdfSDaniel Borkmann 2176c02db8c6SChris Wright err = -EOPNOTSUPP; 2177ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 21784f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 21794f7d2cdfSDaniel Borkmann if (err < 0) 21804f7d2cdfSDaniel Borkmann return err; 21814f7d2cdfSDaniel Borkmann 2182ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2183ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2184ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2185ed616689SSucheta Chakraborty ivf.min_tx_rate, 2186c02db8c6SChris Wright ivt->rate); 21874f7d2cdfSDaniel Borkmann if (err < 0) 21884f7d2cdfSDaniel Borkmann return err; 2189c02db8c6SChris Wright } 21904f7d2cdfSDaniel Borkmann 21914f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 21924f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 21934f7d2cdfSDaniel Borkmann 2194ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2195ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2196ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2197ed616689SSucheta Chakraborty ivt->min_tx_rate, 2198ed616689SSucheta Chakraborty ivt->max_tx_rate); 21994f7d2cdfSDaniel Borkmann if (err < 0) 22004f7d2cdfSDaniel Borkmann return err; 2201ed616689SSucheta Chakraborty } 22024f7d2cdfSDaniel Borkmann 22034f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 22044f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 22054f7d2cdfSDaniel Borkmann 22065f8444a3SGreg Rose err = -EOPNOTSUPP; 22075f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 22085f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 22095f8444a3SGreg Rose ivs->setting); 22104f7d2cdfSDaniel Borkmann if (err < 0) 22114f7d2cdfSDaniel Borkmann return err; 22125f8444a3SGreg Rose } 22134f7d2cdfSDaniel Borkmann 22144f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 22154f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 22164f7d2cdfSDaniel Borkmann 22171d8faf48SRony Efraim err = -EOPNOTSUPP; 22181d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 22191d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 22201d8faf48SRony Efraim ivl->link_state); 22214f7d2cdfSDaniel Borkmann if (err < 0) 22224f7d2cdfSDaniel Borkmann return err; 22231d8faf48SRony Efraim } 22244f7d2cdfSDaniel Borkmann 22254f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 222601a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 222701a3d796SVlad Zolotarov 222801a3d796SVlad Zolotarov err = -EOPNOTSUPP; 22294f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 223001a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 22314f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 223201a3d796SVlad Zolotarov ivrssq_en->setting); 22334f7d2cdfSDaniel Borkmann if (err < 0) 22344f7d2cdfSDaniel Borkmann return err; 223501a3d796SVlad Zolotarov } 22364f7d2cdfSDaniel Borkmann 2237dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2238dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2239dd461d6aSHiroshi Shimamoto 2240dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2241dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2242dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2243dd461d6aSHiroshi Shimamoto if (err < 0) 2244dd461d6aSHiroshi Shimamoto return err; 2245dd461d6aSHiroshi Shimamoto } 2246dd461d6aSHiroshi Shimamoto 2247cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2248cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2249cc8e27ccSEli Cohen 2250cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2251cc8e27ccSEli Cohen return -EOPNOTSUPP; 2252cc8e27ccSEli Cohen 2253cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2254cc8e27ccSEli Cohen } 2255cc8e27ccSEli Cohen 2256cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2257cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2258cc8e27ccSEli Cohen 2259cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2260cc8e27ccSEli Cohen return -EOPNOTSUPP; 2261cc8e27ccSEli Cohen 2262cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2263cc8e27ccSEli Cohen } 2264cc8e27ccSEli Cohen 2265c02db8c6SChris Wright return err; 2266c02db8c6SChris Wright } 2267c02db8c6SChris Wright 226833eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 226933eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2270fbaec0eaSJiri Pirko { 2271898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2272fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2273fbaec0eaSJiri Pirko int err; 2274fbaec0eaSJiri Pirko 2275898e5061SJiri Pirko if (upper_dev) { 2276898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2277fbaec0eaSJiri Pirko return 0; 2278898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2279fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2280898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2281fbaec0eaSJiri Pirko if (err) 2282fbaec0eaSJiri Pirko return err; 2283fbaec0eaSJiri Pirko } else { 2284fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2285fbaec0eaSJiri Pirko } 2286fbaec0eaSJiri Pirko } 2287fbaec0eaSJiri Pirko 2288fbaec0eaSJiri Pirko if (ifindex) { 2289898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2290898e5061SJiri Pirko if (!upper_dev) 2291fbaec0eaSJiri Pirko return -EINVAL; 2292898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2293fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 229433eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2295fbaec0eaSJiri Pirko if (err) 2296fbaec0eaSJiri Pirko return err; 2297fbaec0eaSJiri Pirko } else { 2298fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2299fbaec0eaSJiri Pirko } 2300fbaec0eaSJiri Pirko } 2301fbaec0eaSJiri Pirko return 0; 2302fbaec0eaSJiri Pirko } 2303fbaec0eaSJiri Pirko 230490c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2305ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2306ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 230790f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 230890f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2309ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 231090c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 23110157f60cSPatrick McHardy { 2312d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 23130157f60cSPatrick McHardy int err; 23140157f60cSPatrick McHardy 2315644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2316644c7eebSEric Dumazet if (err < 0) 2317644c7eebSEric Dumazet return err; 2318644c7eebSEric Dumazet 23197c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 23207c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 23217c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2322d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2323d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2324d8a5ec67SEric W. Biederman goto errout; 2325d8a5ec67SEric W. Biederman } 23267c4f63baSChristian Brauner 2327d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2328d8a5ec67SEric W. Biederman put_net(net); 2329d8a5ec67SEric W. Biederman if (err) 2330d8a5ec67SEric W. Biederman goto errout; 233190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2332d8a5ec67SEric W. Biederman } 2333d8a5ec67SEric W. Biederman 23340157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 23350157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 23360157f60cSPatrick McHardy struct ifmap k_map; 23370157f60cSPatrick McHardy 2338d314774cSStephen Hemminger if (!ops->ndo_set_config) { 23390157f60cSPatrick McHardy err = -EOPNOTSUPP; 23400157f60cSPatrick McHardy goto errout; 23410157f60cSPatrick McHardy } 23420157f60cSPatrick McHardy 23430157f60cSPatrick McHardy if (!netif_device_present(dev)) { 23440157f60cSPatrick McHardy err = -ENODEV; 23450157f60cSPatrick McHardy goto errout; 23460157f60cSPatrick McHardy } 23470157f60cSPatrick McHardy 23480157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 23490157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 23500157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 23510157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 23520157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 23530157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 23540157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 23550157f60cSPatrick McHardy 2356d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 23570157f60cSPatrick McHardy if (err < 0) 23580157f60cSPatrick McHardy goto errout; 23590157f60cSPatrick McHardy 2360ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23610157f60cSPatrick McHardy } 23620157f60cSPatrick McHardy 23630157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 23640157f60cSPatrick McHardy struct sockaddr *sa; 23650157f60cSPatrick McHardy int len; 23660157f60cSPatrick McHardy 2367153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2368153711f9SWANG Cong sizeof(*sa)); 23690157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 23700157f60cSPatrick McHardy if (!sa) { 23710157f60cSPatrick McHardy err = -ENOMEM; 23720157f60cSPatrick McHardy goto errout; 23730157f60cSPatrick McHardy } 23740157f60cSPatrick McHardy sa->sa_family = dev->type; 23750157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 23760157f60cSPatrick McHardy dev->addr_len); 2377e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 23780157f60cSPatrick McHardy kfree(sa); 23790157f60cSPatrick McHardy if (err) 23800157f60cSPatrick McHardy goto errout; 238190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23820157f60cSPatrick McHardy } 23830157f60cSPatrick McHardy 23840157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 2385*7a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 23860157f60cSPatrick McHardy if (err < 0) 23870157f60cSPatrick McHardy goto errout; 238890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23890157f60cSPatrick McHardy } 23900157f60cSPatrick McHardy 2391cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2392cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2393ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2394cbda10faSVlad Dogaru } 2395cbda10faSVlad Dogaru 23960157f60cSPatrick McHardy /* 23970157f60cSPatrick McHardy * Interface selected by interface index but interface 23980157f60cSPatrick McHardy * name provided implies that a name change has been 23990157f60cSPatrick McHardy * requested. 24000157f60cSPatrick McHardy */ 24010157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 24020157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 24030157f60cSPatrick McHardy if (err < 0) 24040157f60cSPatrick McHardy goto errout; 240590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24060157f60cSPatrick McHardy } 24070157f60cSPatrick McHardy 24080b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 24090b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 24100b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 24110b815a1aSStephen Hemminger if (err < 0) 24120b815a1aSStephen Hemminger goto errout; 2413ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24140b815a1aSStephen Hemminger } 24150b815a1aSStephen Hemminger 24160157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 24170157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2418e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 24190157f60cSPatrick McHardy } 24200157f60cSPatrick McHardy 24210157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 24223729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 24235f9021cfSJohannes Berg if (err < 0) 24245f9021cfSJohannes Berg goto errout; 24250157f60cSPatrick McHardy } 24260157f60cSPatrick McHardy 2427fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 242833eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2429fbaec0eaSJiri Pirko if (err) 2430fbaec0eaSJiri Pirko goto errout; 243190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2432fbaec0eaSJiri Pirko } 2433fbaec0eaSJiri Pirko 24349a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 24359a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 24369a57247fSJiri Pirko if (err) 24379a57247fSJiri Pirko goto errout; 243890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24399a57247fSJiri Pirko } 24409a57247fSJiri Pirko 24415d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 24420cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 24435d1180fcSNicolas Dichtel 24446a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 24456a643ddbSCong Wang if (err) 244608294a26SJason Wang goto errout; 24472d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 244808294a26SJason Wang } 24490157f60cSPatrick McHardy 245046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 245146e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 245246e6b992SStephen Hemminger 245346e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 245446e6b992SStephen Hemminger err = -EINVAL; 245546e6b992SStephen Hemminger goto errout; 245646e6b992SStephen Hemminger } 245746e6b992SStephen Hemminger 245846e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 245946e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 246046e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 246146e6b992SStephen Hemminger } 246246e6b992SStephen Hemminger } 246346e6b992SStephen Hemminger 246446e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 246546e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 246646e6b992SStephen Hemminger 246746e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 246846e6b992SStephen Hemminger err = -EINVAL; 246946e6b992SStephen Hemminger goto errout; 247046e6b992SStephen Hemminger } 247146e6b992SStephen Hemminger 247246e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 247346e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 247446e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 247546e6b992SStephen Hemminger } 247646e6b992SStephen Hemminger } 247746e6b992SStephen Hemminger 24780157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 247993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 24800157f60cSPatrick McHardy 24810157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 24821889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 24831889b0e7SNicolas Dichtel 24840157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 24851889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2486ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24871889b0e7SNicolas Dichtel dev->link_mode = value; 248893b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 24890157f60cSPatrick McHardy } 24900157f60cSPatrick McHardy 2491c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 24924f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2493c02db8c6SChris Wright struct nlattr *attr; 2494c02db8c6SChris Wright int rem; 24954f7d2cdfSDaniel Borkmann 2496c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 24974f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 24984f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2499253683bbSDavid Howells err = -EINVAL; 2500c02db8c6SChris Wright goto errout; 2501253683bbSDavid Howells } 25024f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2503fceb6435SJohannes Berg ifla_vf_policy, NULL); 25044f7d2cdfSDaniel Borkmann if (err < 0) 25054f7d2cdfSDaniel Borkmann goto errout; 25064f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2507ebc08a6fSWilliams, Mitch A if (err < 0) 2508ebc08a6fSWilliams, Mitch A goto errout; 2509ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2510ebc08a6fSWilliams, Mitch A } 2511ebc08a6fSWilliams, Mitch A } 25120157f60cSPatrick McHardy err = 0; 25130157f60cSPatrick McHardy 251457b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 251557b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 251657b61080SScott Feldman struct nlattr *attr; 251757b61080SScott Feldman int vf; 251857b61080SScott Feldman int rem; 251957b61080SScott Feldman 252057b61080SScott Feldman err = -EOPNOTSUPP; 252157b61080SScott Feldman if (!ops->ndo_set_vf_port) 252257b61080SScott Feldman goto errout; 252357b61080SScott Feldman 252457b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2525035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2526035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2527035d210fSDaniel Borkmann err = -EINVAL; 2528035d210fSDaniel Borkmann goto errout; 2529035d210fSDaniel Borkmann } 2530035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2531fceb6435SJohannes Berg ifla_port_policy, NULL); 253257b61080SScott Feldman if (err < 0) 253357b61080SScott Feldman goto errout; 253457b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 253557b61080SScott Feldman err = -EOPNOTSUPP; 253657b61080SScott Feldman goto errout; 253757b61080SScott Feldman } 253857b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 253957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 254057b61080SScott Feldman if (err < 0) 254157b61080SScott Feldman goto errout; 2542ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 254357b61080SScott Feldman } 254457b61080SScott Feldman } 254557b61080SScott Feldman err = 0; 254657b61080SScott Feldman 254757b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 254857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 254957b61080SScott Feldman 255057b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2551fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2552fceb6435SJohannes Berg NULL); 255357b61080SScott Feldman if (err < 0) 255457b61080SScott Feldman goto errout; 255557b61080SScott Feldman 255657b61080SScott Feldman err = -EOPNOTSUPP; 255757b61080SScott Feldman if (ops->ndo_set_vf_port) 255857b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 255957b61080SScott Feldman if (err < 0) 256057b61080SScott Feldman goto errout; 2561ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 256257b61080SScott Feldman } 2563f8ff182cSThomas Graf 2564f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2565f8ff182cSThomas Graf struct nlattr *af; 2566f8ff182cSThomas Graf int rem; 2567f8ff182cSThomas Graf 2568f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2569f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2570f8ff182cSThomas Graf 25715fa85a09SFlorian Westphal rcu_read_lock(); 25725fa85a09SFlorian Westphal 2573058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2574f8ff182cSThomas Graf 2575cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 25765fa85a09SFlorian Westphal if (err < 0) { 25775fa85a09SFlorian Westphal rcu_read_unlock(); 2578f8ff182cSThomas Graf goto errout; 25795fa85a09SFlorian Westphal } 2580f8ff182cSThomas Graf 25815fa85a09SFlorian Westphal rcu_read_unlock(); 2582ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2583f8ff182cSThomas Graf } 2584f8ff182cSThomas Graf } 258557b61080SScott Feldman err = 0; 258657b61080SScott Feldman 258788d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 258888d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 258988d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 259088d6378bSAnuradha Karuppiah if (err) 259188d6378bSAnuradha Karuppiah goto errout; 259288d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 259388d6378bSAnuradha Karuppiah } 259488d6378bSAnuradha Karuppiah 2595d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2596d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 259785de8576SDaniel Borkmann u32 xdp_flags = 0; 2598d1fdd913SBrenden Blanco 2599d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2600fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2601d1fdd913SBrenden Blanco if (err < 0) 2602d1fdd913SBrenden Blanco goto errout; 2603d1fdd913SBrenden Blanco 260458038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2605262d8625SBrenden Blanco err = -EINVAL; 2606262d8625SBrenden Blanco goto errout; 2607262d8625SBrenden Blanco } 260885de8576SDaniel Borkmann 260985de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 261085de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 261185de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 261285de8576SDaniel Borkmann err = -EINVAL; 261385de8576SDaniel Borkmann goto errout; 261485de8576SDaniel Borkmann } 2615ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 26160489df9aSDaniel Borkmann err = -EINVAL; 26170489df9aSDaniel Borkmann goto errout; 26180489df9aSDaniel Borkmann } 261985de8576SDaniel Borkmann } 262085de8576SDaniel Borkmann 2621d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2622ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 262385de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 262485de8576SDaniel Borkmann xdp_flags); 2625d1fdd913SBrenden Blanco if (err) 2626d1fdd913SBrenden Blanco goto errout; 2627d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2628d1fdd913SBrenden Blanco } 2629d1fdd913SBrenden Blanco } 2630d1fdd913SBrenden Blanco 26310157f60cSPatrick McHardy errout: 2632ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 263364ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2634ba998906SNicolas Dichtel netdev_state_change(dev); 2635ba998906SNicolas Dichtel 2636ba998906SNicolas Dichtel if (err < 0) 2637e87cc472SJoe 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", 2638e87cc472SJoe Perches dev->name); 2639ba998906SNicolas Dichtel } 26400157f60cSPatrick McHardy 26410157f60cSPatrick McHardy return err; 26420157f60cSPatrick McHardy } 26430157f60cSPatrick McHardy 2644c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2645c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2646da5e0494SThomas Graf { 26473b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2648da5e0494SThomas Graf struct ifinfomsg *ifm; 2649da5e0494SThomas Graf struct net_device *dev; 26500157f60cSPatrick McHardy int err; 2651da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 26521da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 26531da177e4SLinus Torvalds 2654c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2655c21ef3e3SDavid Ahern extack); 2656da5e0494SThomas Graf if (err < 0) 2657da5e0494SThomas Graf goto errout; 26581da177e4SLinus Torvalds 26594ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 26604ff66caeSChristian Brauner if (err < 0) 26614ff66caeSChristian Brauner goto errout; 26624ff66caeSChristian Brauner 26635176f91eSThomas Graf if (tb[IFLA_IFNAME]) 26645176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 266578e5b891SPatrick McHardy else 266678e5b891SPatrick McHardy ifname[0] = '\0'; 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds err = -EINVAL; 2669da5e0494SThomas Graf ifm = nlmsg_data(nlh); 267051055be8SPatrick McHardy if (ifm->ifi_index > 0) 2671a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2672da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2673a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2674da5e0494SThomas Graf else 2675da5e0494SThomas Graf goto errout; 26761da177e4SLinus Torvalds 2677da5e0494SThomas Graf if (dev == NULL) { 2678da5e0494SThomas Graf err = -ENODEV; 2679da5e0494SThomas Graf goto errout; 2680da5e0494SThomas Graf } 26811da177e4SLinus Torvalds 2682ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2683da5e0494SThomas Graf errout: 26841da177e4SLinus Torvalds return err; 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 268766400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 268866400d54SWANG Cong { 268966400d54SWANG Cong struct net_device *dev, *aux; 269066400d54SWANG Cong LIST_HEAD(list_kill); 269166400d54SWANG Cong bool found = false; 269266400d54SWANG Cong 269366400d54SWANG Cong if (!group) 269466400d54SWANG Cong return -EPERM; 269566400d54SWANG Cong 269666400d54SWANG Cong for_each_netdev(net, dev) { 269766400d54SWANG Cong if (dev->group == group) { 269866400d54SWANG Cong const struct rtnl_link_ops *ops; 269966400d54SWANG Cong 270066400d54SWANG Cong found = true; 270166400d54SWANG Cong ops = dev->rtnl_link_ops; 270266400d54SWANG Cong if (!ops || !ops->dellink) 270366400d54SWANG Cong return -EOPNOTSUPP; 270466400d54SWANG Cong } 270566400d54SWANG Cong } 270666400d54SWANG Cong 270766400d54SWANG Cong if (!found) 270866400d54SWANG Cong return -ENODEV; 270966400d54SWANG Cong 271066400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 271166400d54SWANG Cong if (dev->group == group) { 271266400d54SWANG Cong const struct rtnl_link_ops *ops; 271366400d54SWANG Cong 271466400d54SWANG Cong ops = dev->rtnl_link_ops; 271566400d54SWANG Cong ops->dellink(dev, &list_kill); 271666400d54SWANG Cong } 271766400d54SWANG Cong } 271866400d54SWANG Cong unregister_netdevice_many(&list_kill); 271966400d54SWANG Cong 272066400d54SWANG Cong return 0; 272166400d54SWANG Cong } 272266400d54SWANG Cong 2723614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2724614732eaSThomas Graf { 2725614732eaSThomas Graf const struct rtnl_link_ops *ops; 2726614732eaSThomas Graf LIST_HEAD(list_kill); 2727614732eaSThomas Graf 2728614732eaSThomas Graf ops = dev->rtnl_link_ops; 2729614732eaSThomas Graf if (!ops || !ops->dellink) 2730614732eaSThomas Graf return -EOPNOTSUPP; 2731614732eaSThomas Graf 2732614732eaSThomas Graf ops->dellink(dev, &list_kill); 2733614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2734614732eaSThomas Graf 2735614732eaSThomas Graf return 0; 2736614732eaSThomas Graf } 2737614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2738614732eaSThomas Graf 2739c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2740c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 274138f7b870SPatrick McHardy { 27423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2743b61ad68aSChristian Brauner struct net *tgt_net = net; 2744b61ad68aSChristian Brauner struct net_device *dev = NULL; 274538f7b870SPatrick McHardy struct ifinfomsg *ifm; 274638f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 274738f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 274838f7b870SPatrick McHardy int err; 2749b61ad68aSChristian Brauner int netnsid = -1; 275038f7b870SPatrick McHardy 2751c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 275238f7b870SPatrick McHardy if (err < 0) 275338f7b870SPatrick McHardy return err; 275438f7b870SPatrick McHardy 27554ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 27564ff66caeSChristian Brauner if (err < 0) 27574ff66caeSChristian Brauner return err; 27584ff66caeSChristian Brauner 275938f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 276038f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 276138f7b870SPatrick McHardy 2762b61ad68aSChristian Brauner if (tb[IFLA_IF_NETNSID]) { 2763b61ad68aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2764b61ad68aSChristian Brauner tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2765b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2766b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2767b61ad68aSChristian Brauner } 2768b61ad68aSChristian Brauner 2769b61ad68aSChristian Brauner err = -EINVAL; 277038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 277138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2772b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 277338f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2774b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 277566400d54SWANG Cong else if (tb[IFLA_GROUP]) 2776b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 277738f7b870SPatrick McHardy else 2778b61ad68aSChristian Brauner goto out; 277938f7b870SPatrick McHardy 2780b61ad68aSChristian Brauner if (!dev) { 2781b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2782b61ad68aSChristian Brauner err = -ENODEV; 278338f7b870SPatrick McHardy 2784b61ad68aSChristian Brauner goto out; 2785b61ad68aSChristian Brauner } 2786b61ad68aSChristian Brauner 2787b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2788b61ad68aSChristian Brauner 2789b61ad68aSChristian Brauner out: 2790b61ad68aSChristian Brauner if (netnsid >= 0) 2791b61ad68aSChristian Brauner put_net(tgt_net); 2792b61ad68aSChristian Brauner 2793b61ad68aSChristian Brauner return err; 279438f7b870SPatrick McHardy } 279538f7b870SPatrick McHardy 27963729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 27973729d502SPatrick McHardy { 27983729d502SPatrick McHardy unsigned int old_flags; 27993729d502SPatrick McHardy int err; 28003729d502SPatrick McHardy 28013729d502SPatrick McHardy old_flags = dev->flags; 28023729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 28033729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 28043729d502SPatrick McHardy if (err < 0) 28053729d502SPatrick McHardy return err; 28063729d502SPatrick McHardy } 28073729d502SPatrick McHardy 28088d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 28098d356b89SRoopa Prabhu __dev_notify_flags(dev, old_flags, 0U); 28108d356b89SRoopa Prabhu } else { 28113729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2812a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 28138d356b89SRoopa Prabhu } 28143729d502SPatrick McHardy return 0; 28153729d502SPatrick McHardy } 28163729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 28173729d502SPatrick McHardy 2818c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 281978ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 28205517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2821e7199288SPavel Emelianov { 2822e7199288SPavel Emelianov struct net_device *dev; 2823d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2824d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2825e7199288SPavel Emelianov 282676ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 282776ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 282876ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2829d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 283076ff5cc9SJiri Pirko 283176ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 283276ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 283376ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2834d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2835efacb309Sstephen hemminger 28365517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2837c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2838e7199288SPavel Emelianov if (!dev) 2839d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2840e7199288SPavel Emelianov 284181adee47SEric W. Biederman dev_net_set(dev, net); 284281adee47SEric W. Biederman dev->rtnl_link_ops = ops; 28433729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 284481adee47SEric W. Biederman 2845e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2846e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 28472afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2848e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2849e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 28502afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 28512afb9b53SJiri Pirko } 2852e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2853e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2854e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2855e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2856e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2857e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 285893b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2859e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2860e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2861ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2862ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 286346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 286446e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 286546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2866a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2867e7199288SPavel Emelianov 2868e7199288SPavel Emelianov return dev; 2869e7199288SPavel Emelianov } 2870e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2871e7199288SPavel Emelianov 287290f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 287390f62cf3SEric W. Biederman struct net *net, int group, 2874e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2875ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2876e7ed828fSVlad Dogaru struct nlattr **tb) 2877e7ed828fSVlad Dogaru { 2878d079535dSWANG Cong struct net_device *dev, *aux; 2879e7ed828fSVlad Dogaru int err; 2880e7ed828fSVlad Dogaru 2881d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2882e7ed828fSVlad Dogaru if (dev->group == group) { 2883ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2884e7ed828fSVlad Dogaru if (err < 0) 2885e7ed828fSVlad Dogaru return err; 2886e7ed828fSVlad Dogaru } 2887e7ed828fSVlad Dogaru } 2888e7ed828fSVlad Dogaru 2889e7ed828fSVlad Dogaru return 0; 2890e7ed828fSVlad Dogaru } 2891e7ed828fSVlad Dogaru 2892c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2893c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 289438f7b870SPatrick McHardy { 28953b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 289638f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2897ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 289838f7b870SPatrick McHardy struct net_device *dev; 2899ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 290038f7b870SPatrick McHardy struct ifinfomsg *ifm; 290138f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 290238f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 290338f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 290438f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 29055517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 290638f7b870SPatrick McHardy int err; 290738f7b870SPatrick McHardy 290895a5afcaSJohannes Berg #ifdef CONFIG_MODULES 290938f7b870SPatrick McHardy replay: 29108072f085SThomas Graf #endif 2911c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 291238f7b870SPatrick McHardy if (err < 0) 291338f7b870SPatrick McHardy return err; 291438f7b870SPatrick McHardy 29154ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 29164ff66caeSChristian Brauner if (err < 0) 29174ff66caeSChristian Brauner return err; 29184ff66caeSChristian Brauner 291938f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 292038f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 292138f7b870SPatrick McHardy else 292238f7b870SPatrick McHardy ifname[0] = '\0'; 292338f7b870SPatrick McHardy 292438f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 292538f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2926881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 2927e7ed828fSVlad Dogaru else { 2928e7ed828fSVlad Dogaru if (ifname[0]) 2929881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 293038f7b870SPatrick McHardy else 293138f7b870SPatrick McHardy dev = NULL; 2932e7ed828fSVlad Dogaru } 293338f7b870SPatrick McHardy 2934ba7d49b1SJiri Pirko if (dev) { 2935ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 2936ba7d49b1SJiri Pirko if (master_dev) 2937ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 2938ba7d49b1SJiri Pirko } 2939ba7d49b1SJiri Pirko 2940e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2941e0d087afSEric Dumazet if (err < 0) 29421840bb13SThomas Graf return err; 29431840bb13SThomas Graf 294438f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 294538f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2946fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 2947fceb6435SJohannes Berg NULL); 294838f7b870SPatrick McHardy if (err < 0) 294938f7b870SPatrick McHardy return err; 295038f7b870SPatrick McHardy } else 295138f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 295238f7b870SPatrick McHardy 295338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 295438f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 295538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 295638f7b870SPatrick McHardy } else { 295738f7b870SPatrick McHardy kind[0] = '\0'; 295838f7b870SPatrick McHardy ops = NULL; 295938f7b870SPatrick McHardy } 296038f7b870SPatrick McHardy 296138f7b870SPatrick McHardy if (1) { 2962ccf8dbcdSKees Cook struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2963ccf8dbcdSKees Cook struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2964ba7d49b1SJiri Pirko struct nlattr **data = NULL; 2965ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 2966317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 296738f7b870SPatrick McHardy 296838f7b870SPatrick McHardy if (ops) { 2969ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 2970ccf8dbcdSKees Cook return -EINVAL; 2971ccf8dbcdSKees Cook 297238f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 297338f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 297438f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 2975fceb6435SJohannes Berg ops->policy, NULL); 297638f7b870SPatrick McHardy if (err < 0) 297738f7b870SPatrick McHardy return err; 297838f7b870SPatrick McHardy data = attr; 297938f7b870SPatrick McHardy } 298038f7b870SPatrick McHardy if (ops->validate) { 2981a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 298238f7b870SPatrick McHardy if (err < 0) 298338f7b870SPatrick McHardy return err; 298438f7b870SPatrick McHardy } 298538f7b870SPatrick McHardy } 298638f7b870SPatrick McHardy 2987ba7d49b1SJiri Pirko if (m_ops) { 2988af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2989ccf8dbcdSKees Cook return -EINVAL; 2990ccf8dbcdSKees Cook 2991ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 2992ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 2993ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 2994ba7d49b1SJiri Pirko m_ops->slave_maxtype, 2995ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 2996fceb6435SJohannes Berg m_ops->slave_policy, 2997fceb6435SJohannes Berg NULL); 2998ba7d49b1SJiri Pirko if (err < 0) 2999ba7d49b1SJiri Pirko return err; 3000ba7d49b1SJiri Pirko slave_data = slave_attr; 3001ba7d49b1SJiri Pirko } 3002ba7d49b1SJiri Pirko } 3003ba7d49b1SJiri Pirko 300438f7b870SPatrick McHardy if (dev) { 300590c325e3SNicolas Dichtel int status = 0; 300638f7b870SPatrick McHardy 300738f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 300838f7b870SPatrick McHardy return -EEXIST; 300938f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 301038f7b870SPatrick McHardy return -EOPNOTSUPP; 301138f7b870SPatrick McHardy 301238f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 301338f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 301438f7b870SPatrick McHardy !ops->changelink) 301538f7b870SPatrick McHardy return -EOPNOTSUPP; 301638f7b870SPatrick McHardy 3017ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 301838f7b870SPatrick McHardy if (err < 0) 301938f7b870SPatrick McHardy return err; 3020ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 302138f7b870SPatrick McHardy } 302238f7b870SPatrick McHardy 3023ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3024ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3025ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3026ba7d49b1SJiri Pirko 3027ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 302817dd0ec4SMatthias Schiffer tb, slave_data, 302917dd0ec4SMatthias Schiffer extack); 3030ba7d49b1SJiri Pirko if (err < 0) 3031ba7d49b1SJiri Pirko return err; 3032ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3033ba7d49b1SJiri Pirko } 3034ba7d49b1SJiri Pirko 3035ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 3036ddf9f970SJakub Kicinski status); 303738f7b870SPatrick McHardy } 303838f7b870SPatrick McHardy 3039ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3040ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 304190f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3042ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3043ddf9f970SJakub Kicinski ifm, extack, tb); 304438f7b870SPatrick McHardy return -ENODEV; 3045ffa934f1SPatrick McHardy } 304638f7b870SPatrick McHardy 3047160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 304838f7b870SPatrick McHardy return -EOPNOTSUPP; 304938f7b870SPatrick McHardy 305038f7b870SPatrick McHardy if (!ops) { 305195a5afcaSJohannes Berg #ifdef CONFIG_MODULES 305238f7b870SPatrick McHardy if (kind[0]) { 305338f7b870SPatrick McHardy __rtnl_unlock(); 305438f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 305538f7b870SPatrick McHardy rtnl_lock(); 305638f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 305738f7b870SPatrick McHardy if (ops) 305838f7b870SPatrick McHardy goto replay; 305938f7b870SPatrick McHardy } 306038f7b870SPatrick McHardy #endif 306138f7b870SPatrick McHardy return -EOPNOTSUPP; 306238f7b870SPatrick McHardy } 306338f7b870SPatrick McHardy 3064b0ab2fabSJiri Pirko if (!ops->setup) 3065b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3066b0ab2fabSJiri Pirko 30675517750fSTom Gundersen if (!ifname[0]) { 306838f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 30695517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 30705517750fSTom Gundersen } 307138f7b870SPatrick McHardy 30725bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 307313ad1774SEric W. Biederman if (IS_ERR(dest_net)) 307413ad1774SEric W. Biederman return PTR_ERR(dest_net); 307513ad1774SEric W. Biederman 3076317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3077317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3078317f4810SNicolas Dichtel 3079317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3080317f4810SNicolas Dichtel if (!link_net) { 3081317f4810SNicolas Dichtel err = -EINVAL; 3082317f4810SNicolas Dichtel goto out; 3083317f4810SNicolas Dichtel } 308406615bedSEric W. Biederman err = -EPERM; 308506615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 308606615bedSEric W. Biederman goto out; 3087317f4810SNicolas Dichtel } 3088317f4810SNicolas Dichtel 3089317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3090317f4810SNicolas Dichtel name_assign_type, ops, tb); 30919c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3092e7199288SPavel Emelianov err = PTR_ERR(dev); 30939c7dafbfSPavel Emelyanov goto out; 30949c7dafbfSPavel Emelyanov } 30959c7dafbfSPavel Emelyanov 30969c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 30979c7dafbfSPavel Emelyanov 30980e0eee24SCong Wang if (ops->newlink) { 30997a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 31007a3f4a18SMatthias Schiffer extack); 31010e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3102e51fb152SCong Wang * and unregister it on failure after registration 3103e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 31040e0eee24SCong Wang */ 3105e51fb152SCong Wang if (err < 0) { 3106e51fb152SCong Wang /* If device is not registered at all, free it now */ 3107e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3108e51fb152SCong Wang free_netdev(dev); 31090e0eee24SCong Wang goto out; 3110e51fb152SCong Wang } 31110e0eee24SCong Wang } else { 31122d85cba2SPatrick McHardy err = register_netdevice(dev); 3113fce9b9beSDan Carpenter if (err < 0) { 311438f7b870SPatrick McHardy free_netdev(dev); 31153729d502SPatrick McHardy goto out; 3116fce9b9beSDan Carpenter } 31170e0eee24SCong Wang } 31183729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 311943638900SDavid S. Miller if (err < 0) 312043638900SDavid S. Miller goto out_unregister; 312143638900SDavid S. Miller if (link_net) { 312243638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 312343638900SDavid S. Miller if (err < 0) 312443638900SDavid S. Miller goto out_unregister; 312543638900SDavid S. Miller } 3126160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 312733eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 312833eaf2a6SDavid Ahern extack); 3129160ca014STheuns Verwoerd if (err) 3130160ca014STheuns Verwoerd goto out_unregister; 3131160ca014STheuns Verwoerd } 313243638900SDavid S. Miller out: 313343638900SDavid S. Miller if (link_net) 313443638900SDavid S. Miller put_net(link_net); 313543638900SDavid S. Miller put_net(dest_net); 313643638900SDavid S. Miller return err; 313743638900SDavid S. Miller out_unregister: 31387afb8886SWANG Cong if (ops->newlink) { 31397afb8886SWANG Cong LIST_HEAD(list_kill); 31407afb8886SWANG Cong 31417afb8886SWANG Cong ops->dellink(dev, &list_kill); 31427afb8886SWANG Cong unregister_netdevice_many(&list_kill); 31437afb8886SWANG Cong } else { 31443729d502SPatrick McHardy unregister_netdevice(dev); 31457afb8886SWANG Cong } 3146317f4810SNicolas Dichtel goto out; 3147317f4810SNicolas Dichtel } 314838f7b870SPatrick McHardy } 314938f7b870SPatrick McHardy 3150c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3151c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3152711e2c33SJean Tourrilhes { 31533b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 315479e1ad14SJiri Benc struct net *tgt_net = net; 3155b60c5115SThomas Graf struct ifinfomsg *ifm; 3156a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3157b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3158b60c5115SThomas Graf struct net_device *dev = NULL; 3159b60c5115SThomas Graf struct sk_buff *nskb; 316079e1ad14SJiri Benc int netnsid = -1; 3161339bf98fSThomas Graf int err; 3162115c9b81SGreg Rose u32 ext_filter_mask = 0; 3163711e2c33SJean Tourrilhes 3164c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3165b60c5115SThomas Graf if (err < 0) 31669918f230SEric Sesterhenn return err; 3167b60c5115SThomas Graf 31684ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31694ff66caeSChristian Brauner if (err < 0) 31704ff66caeSChristian Brauner return err; 31714ff66caeSChristian Brauner 317279e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 317379e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3174f428fe4aSAndrei Vagin tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 317579e1ad14SJiri Benc if (IS_ERR(tgt_net)) 317679e1ad14SJiri Benc return PTR_ERR(tgt_net); 317779e1ad14SJiri Benc } 317879e1ad14SJiri Benc 3179a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3180a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3181a3d12891SEric Dumazet 3182115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3183115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3184115c9b81SGreg Rose 318579e1ad14SJiri Benc err = -EINVAL; 3186b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3187a3d12891SEric Dumazet if (ifm->ifi_index > 0) 318879e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3189a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 319079e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3191a3d12891SEric Dumazet else 319279e1ad14SJiri Benc goto out; 3193b60c5115SThomas Graf 319479e1ad14SJiri Benc err = -ENODEV; 3195a3d12891SEric Dumazet if (dev == NULL) 319679e1ad14SJiri Benc goto out; 3197a3d12891SEric Dumazet 319879e1ad14SJiri Benc err = -ENOBUFS; 3199115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3200a3d12891SEric Dumazet if (nskb == NULL) 320179e1ad14SJiri Benc goto out; 3202711e2c33SJean Tourrilhes 320379e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 320479e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 320579e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 320638e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 320726932566SPatrick McHardy if (err < 0) { 320826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 320926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 321026932566SPatrick McHardy kfree_skb(nskb); 3211a3d12891SEric Dumazet } else 321215e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 321379e1ad14SJiri Benc out: 321479e1ad14SJiri Benc if (netnsid >= 0) 321579e1ad14SJiri Benc put_net(tgt_net); 3216b60c5115SThomas Graf 3217711e2c33SJean Tourrilhes return err; 3218711e2c33SJean Tourrilhes } 3219711e2c33SJean Tourrilhes 3220115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3221c7ac8679SGreg Rose { 3222115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3223115c9b81SGreg Rose struct net_device *dev; 3224115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3225115c9b81SGreg Rose u32 ext_filter_mask = 0; 3226115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3227e5eca6d4SMichal Schmidt int hdrlen; 3228115c9b81SGreg Rose 3229e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3230e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3231e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3232e5eca6d4SMichal Schmidt 3233fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3234115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3235115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3236a4b64fbeSEric Dumazet } 3237115c9b81SGreg Rose 3238115c9b81SGreg Rose if (!ext_filter_mask) 3239115c9b81SGreg Rose return NLMSG_GOODSIZE; 3240115c9b81SGreg Rose /* 3241115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3242115c9b81SGreg Rose * buffer size based upon the filter mask. 3243115c9b81SGreg Rose */ 32446853dd48SFlorian Westphal rcu_read_lock(); 32456853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3246115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3247115c9b81SGreg Rose if_nlmsg_size(dev, 3248115c9b81SGreg Rose ext_filter_mask)); 3249115c9b81SGreg Rose } 32506853dd48SFlorian Westphal rcu_read_unlock(); 3251115c9b81SGreg Rose 325293af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3253c7ac8679SGreg Rose } 3254c7ac8679SGreg Rose 325542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 32561da177e4SLinus Torvalds { 32571da177e4SLinus Torvalds int idx; 32581da177e4SLinus Torvalds int s_idx = cb->family; 32591da177e4SLinus Torvalds 32601da177e4SLinus Torvalds if (s_idx == 0) 32611da177e4SLinus Torvalds s_idx = 1; 32626853dd48SFlorian Westphal 326325239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3264addf9b90SFlorian Westphal struct rtnl_link **tab; 32651da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 3266addf9b90SFlorian Westphal struct rtnl_link *link; 32676853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 32686853dd48SFlorian Westphal 32691da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 32701da177e4SLinus Torvalds continue; 32716853dd48SFlorian Westphal 3272addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 32731da177e4SLinus Torvalds continue; 32746853dd48SFlorian Westphal 3275addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3276addf9b90SFlorian Westphal if (!tab) 3277addf9b90SFlorian Westphal continue; 3278addf9b90SFlorian Westphal 3279addf9b90SFlorian Westphal link = tab[type]; 3280addf9b90SFlorian Westphal if (!link) 3281addf9b90SFlorian Westphal continue; 3282addf9b90SFlorian Westphal 3283addf9b90SFlorian Westphal dumpit = link->dumpit; 32846853dd48SFlorian Westphal if (!dumpit) 32856853dd48SFlorian Westphal continue; 32866853dd48SFlorian Westphal 32870465277fSNicolas Dichtel if (idx > s_idx) { 32881da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 32890465277fSNicolas Dichtel cb->prev_seq = 0; 32900465277fSNicolas Dichtel cb->seq = 0; 32910465277fSNicolas Dichtel } 32926853dd48SFlorian Westphal if (dumpit(skb, cb)) 32931da177e4SLinus Torvalds break; 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds cb->family = idx; 32961da177e4SLinus Torvalds 32971da177e4SLinus Torvalds return skb->len; 32981da177e4SLinus Torvalds } 32991da177e4SLinus Torvalds 3300395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 33013d3ea5afSVlad Yasevich unsigned int change, 330238e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 330338e01b30SNicolas Dichtel int new_ifindex) 33041da177e4SLinus Torvalds { 3305c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 33061da177e4SLinus Torvalds struct sk_buff *skb; 33070ec6d3f4SThomas Graf int err = -ENOBUFS; 3308c7ac8679SGreg Rose size_t if_info_size; 33091da177e4SLinus Torvalds 33107f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 33110ec6d3f4SThomas Graf if (skb == NULL) 33120ec6d3f4SThomas Graf goto errout; 33131da177e4SLinus Torvalds 331479e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 331579e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 331638e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 331726932566SPatrick McHardy if (err < 0) { 331826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 331926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 332026932566SPatrick McHardy kfree_skb(skb); 332126932566SPatrick McHardy goto errout; 332226932566SPatrick McHardy } 3323395eea6cSMahesh Bandewar return skb; 33240ec6d3f4SThomas Graf errout: 33250ec6d3f4SThomas Graf if (err < 0) 33264b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3327395eea6cSMahesh Bandewar return NULL; 3328395eea6cSMahesh Bandewar } 3329395eea6cSMahesh Bandewar 3330395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3331395eea6cSMahesh Bandewar { 3332395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3333395eea6cSMahesh Bandewar 3334395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3335395eea6cSMahesh Bandewar } 3336395eea6cSMahesh Bandewar 33373d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 33383d3ea5afSVlad Yasevich unsigned int change, u32 event, 333938e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3340395eea6cSMahesh Bandewar { 3341395eea6cSMahesh Bandewar struct sk_buff *skb; 3342395eea6cSMahesh Bandewar 3343ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3344ed2a80abSNicolas Dichtel return; 3345ed2a80abSNicolas Dichtel 334638e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 334738e01b30SNicolas Dichtel new_ifindex); 3348395eea6cSMahesh Bandewar if (skb) 3349395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 33501da177e4SLinus Torvalds } 33513d3ea5afSVlad Yasevich 33523d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 33533d3ea5afSVlad Yasevich gfp_t flags) 33543d3ea5afSVlad Yasevich { 335538e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 335638e01b30SNicolas Dichtel NULL, 0); 33573d3ea5afSVlad Yasevich } 33581da177e4SLinus Torvalds 33596621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 336038e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 33616621dd29SNicolas Dichtel { 33626621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 336338e01b30SNicolas Dichtel new_nsid, new_ifindex); 33646621dd29SNicolas Dichtel } 33656621dd29SNicolas Dichtel 3366d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3367d83b0603SJohn Fastabend struct net_device *dev, 33681e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 33691c104a6bSNicolas Dichtel int type, unsigned int flags, 3370b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3371d83b0603SJohn Fastabend { 3372d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3373d83b0603SJohn Fastabend struct ndmsg *ndm; 3374d83b0603SJohn Fastabend 33751c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3376d83b0603SJohn Fastabend if (!nlh) 3377d83b0603SJohn Fastabend return -EMSGSIZE; 3378d83b0603SJohn Fastabend 3379d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3380d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3381d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3382d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3383d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3384d83b0603SJohn Fastabend ndm->ndm_type = 0; 3385d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3386b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3387d83b0603SJohn Fastabend 3388d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3389d83b0603SJohn Fastabend goto nla_put_failure; 33901e53d5bbSHubert Sokolowski if (vid) 33911e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 33921e53d5bbSHubert Sokolowski goto nla_put_failure; 3393d83b0603SJohn Fastabend 3394053c095aSJohannes Berg nlmsg_end(skb, nlh); 3395053c095aSJohannes Berg return 0; 3396d83b0603SJohn Fastabend 3397d83b0603SJohn Fastabend nla_put_failure: 3398d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3399d83b0603SJohn Fastabend return -EMSGSIZE; 3400d83b0603SJohn Fastabend } 3401d83b0603SJohn Fastabend 34023ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 34033ff661c3SJohn Fastabend { 3404f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3405f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3406f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3407f82ef3e1SSabrina Dubroca 0; 34083ff661c3SJohn Fastabend } 34093ff661c3SJohn Fastabend 3410b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3411b3379041SHubert Sokolowski u16 ndm_state) 34123ff661c3SJohn Fastabend { 34133ff661c3SJohn Fastabend struct net *net = dev_net(dev); 34143ff661c3SJohn Fastabend struct sk_buff *skb; 34153ff661c3SJohn Fastabend int err = -ENOBUFS; 34163ff661c3SJohn Fastabend 34173ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 34183ff661c3SJohn Fastabend if (!skb) 34193ff661c3SJohn Fastabend goto errout; 34203ff661c3SJohn Fastabend 34211e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3422b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 34233ff661c3SJohn Fastabend if (err < 0) { 34243ff661c3SJohn Fastabend kfree_skb(skb); 34253ff661c3SJohn Fastabend goto errout; 34263ff661c3SJohn Fastabend } 34273ff661c3SJohn Fastabend 34283ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 34293ff661c3SJohn Fastabend return; 34303ff661c3SJohn Fastabend errout: 34313ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 34323ff661c3SJohn Fastabend } 34333ff661c3SJohn Fastabend 3434090096bfSVlad Yasevich /** 3435090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3436090096bfSVlad Yasevich */ 3437090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3438090096bfSVlad Yasevich struct nlattr *tb[], 3439090096bfSVlad Yasevich struct net_device *dev, 3440f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3441090096bfSVlad Yasevich u16 flags) 3442090096bfSVlad Yasevich { 3443090096bfSVlad Yasevich int err = -EINVAL; 3444090096bfSVlad Yasevich 3445090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3446090096bfSVlad Yasevich * implement its own handler for this. 3447090096bfSVlad Yasevich */ 3448090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3449090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3450090096bfSVlad Yasevich return err; 3451090096bfSVlad Yasevich } 3452090096bfSVlad Yasevich 345365891feaSOr Gerlitz if (vid) { 345465891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 345565891feaSOr Gerlitz return err; 345665891feaSOr Gerlitz } 345765891feaSOr Gerlitz 3458090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3459090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3460090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3461090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3462090096bfSVlad Yasevich 3463090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3464090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3465090096bfSVlad Yasevich err = 0; 3466090096bfSVlad Yasevich 3467090096bfSVlad Yasevich return err; 3468090096bfSVlad Yasevich } 3469090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3470090096bfSVlad Yasevich 3471b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3472b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3473f6f6424bSJiri Pirko { 3474f6f6424bSJiri Pirko u16 vid = 0; 3475f6f6424bSJiri Pirko 3476f6f6424bSJiri Pirko if (vlan_attr) { 3477f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3478b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3479f6f6424bSJiri Pirko return -EINVAL; 3480f6f6424bSJiri Pirko } 3481f6f6424bSJiri Pirko 3482f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3483f6f6424bSJiri Pirko 3484f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3485b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3486f6f6424bSJiri Pirko return -EINVAL; 3487f6f6424bSJiri Pirko } 3488f6f6424bSJiri Pirko } 3489f6f6424bSJiri Pirko *p_vid = vid; 3490f6f6424bSJiri Pirko return 0; 3491f6f6424bSJiri Pirko } 3492f6f6424bSJiri Pirko 3493c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3494c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 349577162022SJohn Fastabend { 349677162022SJohn Fastabend struct net *net = sock_net(skb->sk); 349777162022SJohn Fastabend struct ndmsg *ndm; 349877162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 349977162022SJohn Fastabend struct net_device *dev; 350077162022SJohn Fastabend u8 *addr; 3501f6f6424bSJiri Pirko u16 vid; 350277162022SJohn Fastabend int err; 350377162022SJohn Fastabend 3504c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 350577162022SJohn Fastabend if (err < 0) 350677162022SJohn Fastabend return err; 350777162022SJohn Fastabend 350877162022SJohn Fastabend ndm = nlmsg_data(nlh); 350977162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3510b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 351177162022SJohn Fastabend return -EINVAL; 351277162022SJohn Fastabend } 351377162022SJohn Fastabend 351477162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 351577162022SJohn Fastabend if (dev == NULL) { 3516b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 351777162022SJohn Fastabend return -ENODEV; 351877162022SJohn Fastabend } 351977162022SJohn Fastabend 352077162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3521b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 352277162022SJohn Fastabend return -EINVAL; 352377162022SJohn Fastabend } 352477162022SJohn Fastabend 352577162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 352677162022SJohn Fastabend 3527b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3528f6f6424bSJiri Pirko if (err) 3529f6f6424bSJiri Pirko return err; 3530f6f6424bSJiri Pirko 353177162022SJohn Fastabend err = -EOPNOTSUPP; 353277162022SJohn Fastabend 353377162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 353477162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 353577162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3536898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3537898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3538898e5061SJiri Pirko 3539f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3540f6f6424bSJiri Pirko nlh->nlmsg_flags); 354177162022SJohn Fastabend if (err) 354277162022SJohn Fastabend goto out; 354377162022SJohn Fastabend else 354477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 354577162022SJohn Fastabend } 354677162022SJohn Fastabend 354777162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3548090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3549090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3550090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3551f6f6424bSJiri Pirko vid, 3552090096bfSVlad Yasevich nlh->nlmsg_flags); 3553090096bfSVlad Yasevich else 3554f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 355577162022SJohn Fastabend nlh->nlmsg_flags); 355677162022SJohn Fastabend 35573ff661c3SJohn Fastabend if (!err) { 3558b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3559b3379041SHubert Sokolowski ndm->ndm_state); 356077162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 356177162022SJohn Fastabend } 35623ff661c3SJohn Fastabend } 356377162022SJohn Fastabend out: 356477162022SJohn Fastabend return err; 356577162022SJohn Fastabend } 356677162022SJohn Fastabend 3567090096bfSVlad Yasevich /** 3568090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3569090096bfSVlad Yasevich */ 3570090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3571090096bfSVlad Yasevich struct nlattr *tb[], 3572090096bfSVlad Yasevich struct net_device *dev, 3573f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3574090096bfSVlad Yasevich { 3575c8a89c4aSAlexander Duyck int err = -EINVAL; 3576090096bfSVlad Yasevich 3577090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3578090096bfSVlad Yasevich * implement its own handler for this. 3579090096bfSVlad Yasevich */ 358064535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3581090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3582c8a89c4aSAlexander Duyck return err; 3583090096bfSVlad Yasevich } 3584090096bfSVlad Yasevich 3585090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3586090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3587090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3588090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3589090096bfSVlad Yasevich 3590090096bfSVlad Yasevich return err; 3591090096bfSVlad Yasevich } 3592090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3593090096bfSVlad Yasevich 3594c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3595c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 359677162022SJohn Fastabend { 359777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 359877162022SJohn Fastabend struct ndmsg *ndm; 35991690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 360077162022SJohn Fastabend struct net_device *dev; 360177162022SJohn Fastabend int err = -EINVAL; 360277162022SJohn Fastabend __u8 *addr; 3603f6f6424bSJiri Pirko u16 vid; 360477162022SJohn Fastabend 360590f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 36061690be63SVlad Yasevich return -EPERM; 36071690be63SVlad Yasevich 3608c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 36091690be63SVlad Yasevich if (err < 0) 36101690be63SVlad Yasevich return err; 361177162022SJohn Fastabend 361277162022SJohn Fastabend ndm = nlmsg_data(nlh); 361377162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3614b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 361577162022SJohn Fastabend return -EINVAL; 361677162022SJohn Fastabend } 361777162022SJohn Fastabend 361877162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 361977162022SJohn Fastabend if (dev == NULL) { 3620b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 362177162022SJohn Fastabend return -ENODEV; 362277162022SJohn Fastabend } 362377162022SJohn Fastabend 36241690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3625b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 362677162022SJohn Fastabend return -EINVAL; 362777162022SJohn Fastabend } 362877162022SJohn Fastabend 36291690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 36301690be63SVlad Yasevich 3631b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3632f6f6424bSJiri Pirko if (err) 3633f6f6424bSJiri Pirko return err; 3634f6f6424bSJiri Pirko 363577162022SJohn Fastabend err = -EOPNOTSUPP; 363677162022SJohn Fastabend 363777162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 363877162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 363977162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3640898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3641898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 364277162022SJohn Fastabend 3643898e5061SJiri Pirko if (ops->ndo_fdb_del) 3644f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 364577162022SJohn Fastabend 364677162022SJohn Fastabend if (err) 364777162022SJohn Fastabend goto out; 364877162022SJohn Fastabend else 364977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 365077162022SJohn Fastabend } 365177162022SJohn Fastabend 365277162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3653090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3654090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3655f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3656f6f6424bSJiri Pirko vid); 3657090096bfSVlad Yasevich else 3658f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 365977162022SJohn Fastabend 36603ff661c3SJohn Fastabend if (!err) { 3661b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3662b3379041SHubert Sokolowski ndm->ndm_state); 366377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 366477162022SJohn Fastabend } 36653ff661c3SJohn Fastabend } 366677162022SJohn Fastabend out: 366777162022SJohn Fastabend return err; 366877162022SJohn Fastabend } 366977162022SJohn Fastabend 3670d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3671d83b0603SJohn Fastabend struct netlink_callback *cb, 3672d83b0603SJohn Fastabend struct net_device *dev, 3673d83b0603SJohn Fastabend int *idx, 3674d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3675d83b0603SJohn Fastabend { 3676d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3677d83b0603SJohn Fastabend int err; 367815e47304SEric W. Biederman u32 portid, seq; 3679d83b0603SJohn Fastabend 368015e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3681d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3682d83b0603SJohn Fastabend 3683d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3684d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3685d83b0603SJohn Fastabend goto skip; 3686d83b0603SJohn Fastabend 36871e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3688a7a558feSJohn Fastabend portid, seq, 36891c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3690b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3691d83b0603SJohn Fastabend if (err < 0) 3692d83b0603SJohn Fastabend return err; 3693d83b0603SJohn Fastabend skip: 3694d83b0603SJohn Fastabend *idx += 1; 3695d83b0603SJohn Fastabend } 3696d83b0603SJohn Fastabend return 0; 3697d83b0603SJohn Fastabend } 3698d83b0603SJohn Fastabend 3699d83b0603SJohn Fastabend /** 37002c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3701d83b0603SJohn Fastabend * @nlh: netlink message header 3702d83b0603SJohn Fastabend * @dev: netdevice 3703d83b0603SJohn Fastabend * 3704d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 370591f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3706d83b0603SJohn Fastabend */ 3707d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3708d83b0603SJohn Fastabend struct netlink_callback *cb, 3709d83b0603SJohn Fastabend struct net_device *dev, 37105d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3711d297653dSRoopa Prabhu int *idx) 3712d83b0603SJohn Fastabend { 3713d83b0603SJohn Fastabend int err; 3714d83b0603SJohn Fastabend 3715d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3716d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3717d83b0603SJohn Fastabend if (err) 3718d83b0603SJohn Fastabend goto out; 37192934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3720d83b0603SJohn Fastabend out: 3721d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3722d297653dSRoopa Prabhu return err; 3723d83b0603SJohn Fastabend } 3724d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3725d83b0603SJohn Fastabend 372677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 372777162022SJohn Fastabend { 372877162022SJohn Fastabend struct net_device *dev; 37295e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 37305e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 37315e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 37325e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 37335e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 37345e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3735d297653dSRoopa Prabhu struct hlist_head *head; 37365e6d2435SJamal Hadi Salim int brport_idx = 0; 37375e6d2435SJamal Hadi Salim int br_idx = 0; 3738d297653dSRoopa Prabhu int h, s_h; 3739d297653dSRoopa Prabhu int idx = 0, s_idx; 3740d297653dSRoopa Prabhu int err = 0; 3741d297653dSRoopa Prabhu int fidx = 0; 374277162022SJohn Fastabend 37430ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 37440ff50e83SAlexander Potapenko IFLA_MAX, ifla_policy, NULL); 37450ff50e83SAlexander Potapenko if (err < 0) { 37460ff50e83SAlexander Potapenko return -EINVAL; 37470ff50e83SAlexander Potapenko } else if (err == 0) { 37485e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 37495e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 37505e6d2435SJamal Hadi Salim } 375177162022SJohn Fastabend 37525e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 37535e6d2435SJamal Hadi Salim 37545e6d2435SJamal Hadi Salim if (br_idx) { 37555e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 37565e6d2435SJamal Hadi Salim if (!br_dev) 37575e6d2435SJamal Hadi Salim return -ENODEV; 37585e6d2435SJamal Hadi Salim 3759898e5061SJiri Pirko ops = br_dev->netdev_ops; 37605e6d2435SJamal Hadi Salim } 37615e6d2435SJamal Hadi Salim 3762d297653dSRoopa Prabhu s_h = cb->args[0]; 3763d297653dSRoopa Prabhu s_idx = cb->args[1]; 3764d297653dSRoopa Prabhu 3765d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3766d297653dSRoopa Prabhu idx = 0; 3767d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3768d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3769d297653dSRoopa Prabhu 37705e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 37715e6d2435SJamal Hadi Salim continue; 37725e6d2435SJamal Hadi Salim 37735e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 37745e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 37755e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 37765e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 37775e6d2435SJamal Hadi Salim } 37785e6d2435SJamal Hadi Salim } else { 37795e6d2435SJamal Hadi Salim if (dev != br_dev && 37805e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 37815e6d2435SJamal Hadi Salim continue; 37825e6d2435SJamal Hadi Salim 37835e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 37845e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 37855e6d2435SJamal Hadi Salim continue; 37865e6d2435SJamal Hadi Salim cops = ops; 37875e6d2435SJamal Hadi Salim } 37885e6d2435SJamal Hadi Salim 3789d297653dSRoopa Prabhu if (idx < s_idx) 3790d297653dSRoopa Prabhu goto cont; 3791d297653dSRoopa Prabhu 37925e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3793d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3794d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3795d297653dSRoopa Prabhu br_dev, dev, 3796d297653dSRoopa Prabhu &fidx); 3797d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3798d297653dSRoopa Prabhu goto out; 379977162022SJohn Fastabend } 3800d297653dSRoopa Prabhu } 380177162022SJohn Fastabend 38025e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3803d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3804d297653dSRoopa Prabhu dev, NULL, 3805d297653dSRoopa Prabhu &fidx); 38066cb69742SHubert Sokolowski else 3807d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3808d297653dSRoopa Prabhu &fidx); 3809d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3810d297653dSRoopa Prabhu goto out; 38115e6d2435SJamal Hadi Salim 38125e6d2435SJamal Hadi Salim cops = NULL; 3813d297653dSRoopa Prabhu 3814d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3815d297653dSRoopa Prabhu cb->args[2] = 0; 3816d297653dSRoopa Prabhu fidx = 0; 3817d297653dSRoopa Prabhu cont: 3818d297653dSRoopa Prabhu idx++; 3819d297653dSRoopa Prabhu } 382077162022SJohn Fastabend } 382177162022SJohn Fastabend 3822d297653dSRoopa Prabhu out: 3823d297653dSRoopa Prabhu cb->args[0] = h; 3824d297653dSRoopa Prabhu cb->args[1] = idx; 3825d297653dSRoopa Prabhu cb->args[2] = fidx; 3826d297653dSRoopa Prabhu 382777162022SJohn Fastabend return skb->len; 382877162022SJohn Fastabend } 382977162022SJohn Fastabend 38302c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 38312c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 38322c3c031cSScott Feldman { 38332c3c031cSScott Feldman if (mask & flag) 38342c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 38352c3c031cSScott Feldman return 0; 38362c3c031cSScott Feldman } 38372c3c031cSScott Feldman 3838815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 38392c3c031cSScott Feldman struct net_device *dev, u16 mode, 38407d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 38417d4f8d87SScott Feldman u32 filter_mask, 38427d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 38437d4f8d87SScott Feldman struct net_device *dev, 38447d4f8d87SScott Feldman u32 filter_mask)) 3845815cccbfSJohn Fastabend { 3846815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3847815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3848815cccbfSJohn Fastabend struct nlattr *br_afspec; 38492c3c031cSScott Feldman struct nlattr *protinfo; 3850815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3851898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 38527d4f8d87SScott Feldman int err = 0; 3853815cccbfSJohn Fastabend 385446c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3855815cccbfSJohn Fastabend if (nlh == NULL) 3856815cccbfSJohn Fastabend return -EMSGSIZE; 3857815cccbfSJohn Fastabend 3858815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3859815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3860815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3861815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3862815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3863815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3864815cccbfSJohn Fastabend ifm->ifi_change = 0; 3865815cccbfSJohn Fastabend 3866815cccbfSJohn Fastabend 3867815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3868815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3869815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3870898e5061SJiri Pirko (br_dev && 3871898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3872815cccbfSJohn Fastabend (dev->addr_len && 3873815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3874a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3875a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3876815cccbfSJohn Fastabend goto nla_put_failure; 3877815cccbfSJohn Fastabend 3878815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3879815cccbfSJohn Fastabend if (!br_afspec) 3880815cccbfSJohn Fastabend goto nla_put_failure; 3881815cccbfSJohn Fastabend 38821d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3883815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3884815cccbfSJohn Fastabend goto nla_put_failure; 3885815cccbfSJohn Fastabend } 38861d460b98SRoopa Prabhu 38871d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 38881d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 38891d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 38901d460b98SRoopa Prabhu goto nla_put_failure; 38911d460b98SRoopa Prabhu } 38921d460b98SRoopa Prabhu } 38937d4f8d87SScott Feldman if (vlan_fill) { 38947d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 38957d4f8d87SScott Feldman if (err) { 38967d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 38977d4f8d87SScott Feldman goto nla_put_failure; 38987d4f8d87SScott Feldman } 38997d4f8d87SScott Feldman } 3900815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3901815cccbfSJohn Fastabend 39022c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 39032c3c031cSScott Feldman if (!protinfo) 39042c3c031cSScott Feldman goto nla_put_failure; 39052c3c031cSScott Feldman 39062c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 39072c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 39082c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39092c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 39102c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39112c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 39122c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 39132c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39142c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 39152c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39162c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 39172c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39182c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 39192c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39202c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 39212c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39222c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 39232c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 39242c3c031cSScott Feldman goto nla_put_failure; 39252c3c031cSScott Feldman } 39262c3c031cSScott Feldman 39272c3c031cSScott Feldman nla_nest_end(skb, protinfo); 39282c3c031cSScott Feldman 3929053c095aSJohannes Berg nlmsg_end(skb, nlh); 3930053c095aSJohannes Berg return 0; 3931815cccbfSJohn Fastabend nla_put_failure: 3932815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 39337d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 3934815cccbfSJohn Fastabend } 39357d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3936815cccbfSJohn Fastabend 3937e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3938e5a55a89SJohn Fastabend { 3939e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3940e5a55a89SJohn Fastabend struct net_device *dev; 3941e5a55a89SJohn Fastabend int idx = 0; 3942e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 3943e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 39446cbdceebSVlad Yasevich u32 filter_mask = 0; 3945d64f69b0SRoopa Prabhu int err; 39466cbdceebSVlad Yasevich 3947aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3948aa68c20fSThomas Graf struct nlattr *extfilt; 3949aa68c20fSThomas Graf 39503e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 39516cbdceebSVlad Yasevich IFLA_EXT_MASK); 3952aa68c20fSThomas Graf if (extfilt) { 3953aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 3954aa68c20fSThomas Graf return -EINVAL; 3955aa68c20fSThomas Graf 39566cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 3957aa68c20fSThomas Graf } 3958aa68c20fSThomas Graf } 3959e5a55a89SJohn Fastabend 3960e5a55a89SJohn Fastabend rcu_read_lock(); 3961e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 3962e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 3963898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3964e5a55a89SJohn Fastabend 3965898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3966d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3967d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 3968d64f69b0SRoopa Prabhu skb, portid, seq, dev, 3969d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 3970f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3971f6c5775fSDavid Ahern if (likely(skb->len)) 3972e5a55a89SJohn Fastabend break; 3973f6c5775fSDavid Ahern 3974f6c5775fSDavid Ahern goto out_err; 3975f6c5775fSDavid Ahern } 3976d64f69b0SRoopa Prabhu } 3977e5a55a89SJohn Fastabend idx++; 3978e5a55a89SJohn Fastabend } 3979e5a55a89SJohn Fastabend 3980e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 3981d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3982d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 3983d64f69b0SRoopa Prabhu seq, dev, 398446c264daSNicolas Dichtel filter_mask, 3985d64f69b0SRoopa Prabhu NLM_F_MULTI); 3986f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3987f6c5775fSDavid Ahern if (likely(skb->len)) 3988e5a55a89SJohn Fastabend break; 3989f6c5775fSDavid Ahern 3990f6c5775fSDavid Ahern goto out_err; 3991f6c5775fSDavid Ahern } 3992d64f69b0SRoopa Prabhu } 3993e5a55a89SJohn Fastabend idx++; 3994e5a55a89SJohn Fastabend } 3995e5a55a89SJohn Fastabend } 3996f6c5775fSDavid Ahern err = skb->len; 3997f6c5775fSDavid Ahern out_err: 3998e5a55a89SJohn Fastabend rcu_read_unlock(); 3999e5a55a89SJohn Fastabend cb->args[0] = idx; 4000e5a55a89SJohn Fastabend 4001f6c5775fSDavid Ahern return err; 4002e5a55a89SJohn Fastabend } 4003e5a55a89SJohn Fastabend 40042469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 40052469ffd7SJohn Fastabend { 40062469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 40072469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 40082469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 40092469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 40102469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 40112469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 40122469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 40132469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 40142469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 40152469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 40162469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 40172469ffd7SJohn Fastabend } 40182469ffd7SJohn Fastabend 401902dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 40202469ffd7SJohn Fastabend { 40212469ffd7SJohn Fastabend struct net *net = dev_net(dev); 40222469ffd7SJohn Fastabend struct sk_buff *skb; 40232469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 40242469ffd7SJohn Fastabend 402502dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 402602dba438SRoopa Prabhu return 0; 402702dba438SRoopa Prabhu 40282469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 40292469ffd7SJohn Fastabend if (!skb) { 40302469ffd7SJohn Fastabend err = -ENOMEM; 40312469ffd7SJohn Fastabend goto errout; 40322469ffd7SJohn Fastabend } 40332469ffd7SJohn Fastabend 403446c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4035c38e01b8SJohn Fastabend if (err < 0) 4036c38e01b8SJohn Fastabend goto errout; 40372469ffd7SJohn Fastabend 403859ccaaaaSRoopa Prabhu if (!skb->len) 403959ccaaaaSRoopa Prabhu goto errout; 404059ccaaaaSRoopa Prabhu 40412469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 40422469ffd7SJohn Fastabend return 0; 40432469ffd7SJohn Fastabend errout: 40442469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 40452469ffd7SJohn Fastabend kfree_skb(skb); 404659ccaaaaSRoopa Prabhu if (err) 40472469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 40482469ffd7SJohn Fastabend return err; 40492469ffd7SJohn Fastabend } 40502469ffd7SJohn Fastabend 4051c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4052c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4053e5a55a89SJohn Fastabend { 4054e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4055e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4056e5a55a89SJohn Fastabend struct net_device *dev; 40572469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 40582469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 40594de8b413SRosen, Rami u16 flags = 0; 4060c38e01b8SJohn Fastabend bool have_flags = false; 4061e5a55a89SJohn Fastabend 4062e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4063e5a55a89SJohn Fastabend return -EINVAL; 4064e5a55a89SJohn Fastabend 4065e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4066e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4067e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4068e5a55a89SJohn Fastabend 4069e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4070e5a55a89SJohn Fastabend if (!dev) { 4071b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4072e5a55a89SJohn Fastabend return -ENODEV; 4073e5a55a89SJohn Fastabend } 4074e5a55a89SJohn Fastabend 40752469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 40762469ffd7SJohn Fastabend if (br_spec) { 40772469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 40782469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40796e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40806e8d1c55SThomas Graf return -EINVAL; 40816e8d1c55SThomas Graf 4082c38e01b8SJohn Fastabend have_flags = true; 40832469ffd7SJohn Fastabend flags = nla_get_u16(attr); 40842469ffd7SJohn Fastabend break; 40852469ffd7SJohn Fastabend } 40862469ffd7SJohn Fastabend } 40872469ffd7SJohn Fastabend } 40882469ffd7SJohn Fastabend 40892469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4090898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4091898e5061SJiri Pirko 4092898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 40932469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4094e5a55a89SJohn Fastabend goto out; 4095e5a55a89SJohn Fastabend } 4096e5a55a89SJohn Fastabend 4097add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 40982469ffd7SJohn Fastabend if (err) 40992469ffd7SJohn Fastabend goto out; 41002469ffd7SJohn Fastabend 41012469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 41022469ffd7SJohn Fastabend } 41032469ffd7SJohn Fastabend 41042469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 41052469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 41062469ffd7SJohn Fastabend err = -EOPNOTSUPP; 41072469ffd7SJohn Fastabend else 4108add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4109add511b3SRoopa Prabhu flags); 411002dba438SRoopa Prabhu if (!err) { 41112469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 411202dba438SRoopa Prabhu 411302dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 411402dba438SRoopa Prabhu * change 411502dba438SRoopa Prabhu */ 411602dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 411702dba438SRoopa Prabhu } 41182469ffd7SJohn Fastabend } 41192469ffd7SJohn Fastabend 4120c38e01b8SJohn Fastabend if (have_flags) 41212469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4122e5a55a89SJohn Fastabend out: 4123e5a55a89SJohn Fastabend return err; 4124e5a55a89SJohn Fastabend } 4125e5a55a89SJohn Fastabend 4126c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4127c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4128407af329SVlad Yasevich { 4129407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4130407af329SVlad Yasevich struct ifinfomsg *ifm; 4131407af329SVlad Yasevich struct net_device *dev; 4132407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4133407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 41344de8b413SRosen, Rami u16 flags = 0; 4135407af329SVlad Yasevich bool have_flags = false; 4136407af329SVlad Yasevich 4137407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4138407af329SVlad Yasevich return -EINVAL; 4139407af329SVlad Yasevich 4140407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4141407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4142407af329SVlad Yasevich return -EPFNOSUPPORT; 4143407af329SVlad Yasevich 4144407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4145407af329SVlad Yasevich if (!dev) { 4146b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4147407af329SVlad Yasevich return -ENODEV; 4148407af329SVlad Yasevich } 4149407af329SVlad Yasevich 4150407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4151407af329SVlad Yasevich if (br_spec) { 4152407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4153407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 41546e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 41556e8d1c55SThomas Graf return -EINVAL; 41566e8d1c55SThomas Graf 4157407af329SVlad Yasevich have_flags = true; 4158407af329SVlad Yasevich flags = nla_get_u16(attr); 4159407af329SVlad Yasevich break; 4160407af329SVlad Yasevich } 4161407af329SVlad Yasevich } 4162407af329SVlad Yasevich } 4163407af329SVlad Yasevich 4164407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4165407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4166407af329SVlad Yasevich 4167407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4168407af329SVlad Yasevich err = -EOPNOTSUPP; 4169407af329SVlad Yasevich goto out; 4170407af329SVlad Yasevich } 4171407af329SVlad Yasevich 4172add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4173407af329SVlad Yasevich if (err) 4174407af329SVlad Yasevich goto out; 4175407af329SVlad Yasevich 4176407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4177407af329SVlad Yasevich } 4178407af329SVlad Yasevich 4179407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4180407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4181407af329SVlad Yasevich err = -EOPNOTSUPP; 4182407af329SVlad Yasevich else 4183add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4184add511b3SRoopa Prabhu flags); 4185407af329SVlad Yasevich 418602dba438SRoopa Prabhu if (!err) { 4187407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 418802dba438SRoopa Prabhu 418902dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 419002dba438SRoopa Prabhu * change 419102dba438SRoopa Prabhu */ 419202dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 419302dba438SRoopa Prabhu } 4194407af329SVlad Yasevich } 4195407af329SVlad Yasevich 4196407af329SVlad Yasevich if (have_flags) 4197407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4198407af329SVlad Yasevich out: 4199407af329SVlad Yasevich return err; 4200407af329SVlad Yasevich } 4201407af329SVlad Yasevich 4202e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4203e8872a25SNikolay Aleksandrov { 4204e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4205e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4206e8872a25SNikolay Aleksandrov } 4207e8872a25SNikolay Aleksandrov 420869ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 420969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 421069ae6ad2SNogah Frankel { 421169ae6ad2SNogah Frankel switch (attr_id) { 421269ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 421369ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 421469ae6ad2SNogah Frankel } 421569ae6ad2SNogah Frankel 421669ae6ad2SNogah Frankel return 0; 421769ae6ad2SNogah Frankel } 421869ae6ad2SNogah Frankel 421969ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 422069ae6ad2SNogah Frankel int *prividx) 422169ae6ad2SNogah Frankel { 422269ae6ad2SNogah Frankel struct nlattr *attr = NULL; 422369ae6ad2SNogah Frankel int attr_id, size; 422469ae6ad2SNogah Frankel void *attr_data; 422569ae6ad2SNogah Frankel int err; 422669ae6ad2SNogah Frankel 422769ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 422869ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 422969ae6ad2SNogah Frankel return -ENODATA; 423069ae6ad2SNogah Frankel 423169ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 423269ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 423369ae6ad2SNogah Frankel if (attr_id < *prividx) 423469ae6ad2SNogah Frankel continue; 423569ae6ad2SNogah Frankel 423669ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 423769ae6ad2SNogah Frankel if (!size) 423869ae6ad2SNogah Frankel continue; 423969ae6ad2SNogah Frankel 42403df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 424169ae6ad2SNogah Frankel continue; 424269ae6ad2SNogah Frankel 424369ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 424469ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 424569ae6ad2SNogah Frankel if (!attr) 424669ae6ad2SNogah Frankel goto nla_put_failure; 424769ae6ad2SNogah Frankel 424869ae6ad2SNogah Frankel attr_data = nla_data(attr); 424969ae6ad2SNogah Frankel memset(attr_data, 0, size); 425069ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 425169ae6ad2SNogah Frankel attr_data); 425269ae6ad2SNogah Frankel if (err) 425369ae6ad2SNogah Frankel goto get_offload_stats_failure; 425469ae6ad2SNogah Frankel } 425569ae6ad2SNogah Frankel 425669ae6ad2SNogah Frankel if (!attr) 425769ae6ad2SNogah Frankel return -ENODATA; 425869ae6ad2SNogah Frankel 425969ae6ad2SNogah Frankel *prividx = 0; 426069ae6ad2SNogah Frankel return 0; 426169ae6ad2SNogah Frankel 426269ae6ad2SNogah Frankel nla_put_failure: 426369ae6ad2SNogah Frankel err = -EMSGSIZE; 426469ae6ad2SNogah Frankel get_offload_stats_failure: 426569ae6ad2SNogah Frankel *prividx = attr_id; 426669ae6ad2SNogah Frankel return err; 426769ae6ad2SNogah Frankel } 426869ae6ad2SNogah Frankel 426969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 427069ae6ad2SNogah Frankel { 427169ae6ad2SNogah Frankel int nla_size = 0; 427269ae6ad2SNogah Frankel int attr_id; 427369ae6ad2SNogah Frankel int size; 427469ae6ad2SNogah Frankel 427569ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 427669ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 427769ae6ad2SNogah Frankel return 0; 427869ae6ad2SNogah Frankel 427969ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 428069ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 42813df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 428269ae6ad2SNogah Frankel continue; 428369ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 428469ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 428569ae6ad2SNogah Frankel } 428669ae6ad2SNogah Frankel 428769ae6ad2SNogah Frankel if (nla_size != 0) 428869ae6ad2SNogah Frankel nla_size += nla_total_size(0); 428969ae6ad2SNogah Frankel 429069ae6ad2SNogah Frankel return nla_size; 429169ae6ad2SNogah Frankel } 429269ae6ad2SNogah Frankel 429310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 429410c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4295e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4296e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 429710c9ead9SRoopa Prabhu { 429810c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 429910c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 430010c9ead9SRoopa Prabhu struct nlattr *attr; 4301e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 430269ae6ad2SNogah Frankel int err; 430310c9ead9SRoopa Prabhu 430410c9ead9SRoopa Prabhu ASSERT_RTNL(); 430510c9ead9SRoopa Prabhu 430610c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 430710c9ead9SRoopa Prabhu if (!nlh) 430810c9ead9SRoopa Prabhu return -EMSGSIZE; 430910c9ead9SRoopa Prabhu 431010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4311ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4312ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4313ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 431410c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 431510c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 431610c9ead9SRoopa Prabhu 4317e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 431810c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 431910c9ead9SRoopa Prabhu 432058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 432158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 432258414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 432310c9ead9SRoopa Prabhu if (!attr) 432410c9ead9SRoopa Prabhu goto nla_put_failure; 432510c9ead9SRoopa Prabhu 432610c9ead9SRoopa Prabhu sp = nla_data(attr); 432710c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 432810c9ead9SRoopa Prabhu } 432910c9ead9SRoopa Prabhu 433097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 433197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 433297a47facSNikolay Aleksandrov 433397a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 433497a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 433597a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 433697a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 433797a47facSNikolay Aleksandrov if (!attr) 433897a47facSNikolay Aleksandrov goto nla_put_failure; 433997a47facSNikolay Aleksandrov 434080e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 434180e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 434280e73cc5SNikolay Aleksandrov if (err) 434380e73cc5SNikolay Aleksandrov goto nla_put_failure; 434480e73cc5SNikolay Aleksandrov *idxattr = 0; 434580e73cc5SNikolay Aleksandrov } 434680e73cc5SNikolay Aleksandrov } 434780e73cc5SNikolay Aleksandrov 434880e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 434980e73cc5SNikolay Aleksandrov *idxattr)) { 435080e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 435180e73cc5SNikolay Aleksandrov const struct net_device *master; 435280e73cc5SNikolay Aleksandrov 435380e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 435480e73cc5SNikolay Aleksandrov if (master) 435580e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 435680e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 435780e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 435880e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 435980e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 436080e73cc5SNikolay Aleksandrov if (!attr) 436180e73cc5SNikolay Aleksandrov goto nla_put_failure; 436280e73cc5SNikolay Aleksandrov 436380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 436497a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 436597a47facSNikolay Aleksandrov if (err) 436697a47facSNikolay Aleksandrov goto nla_put_failure; 436797a47facSNikolay Aleksandrov *idxattr = 0; 436897a47facSNikolay Aleksandrov } 436997a47facSNikolay Aleksandrov } 437097a47facSNikolay Aleksandrov 437169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 437269ae6ad2SNogah Frankel *idxattr)) { 437369ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 437469ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 437569ae6ad2SNogah Frankel if (!attr) 437669ae6ad2SNogah Frankel goto nla_put_failure; 437769ae6ad2SNogah Frankel 437869ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 437969ae6ad2SNogah Frankel if (err == -ENODATA) 438069ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 438169ae6ad2SNogah Frankel else 438269ae6ad2SNogah Frankel nla_nest_end(skb, attr); 438369ae6ad2SNogah Frankel 438469ae6ad2SNogah Frankel if (err && err != -ENODATA) 438569ae6ad2SNogah Frankel goto nla_put_failure; 438669ae6ad2SNogah Frankel *idxattr = 0; 438769ae6ad2SNogah Frankel } 438869ae6ad2SNogah Frankel 4389aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4390aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4391aefb4d4aSRobert Shearman 4392aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4393aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4394aefb4d4aSRobert Shearman if (!attr) 4395aefb4d4aSRobert Shearman goto nla_put_failure; 4396aefb4d4aSRobert Shearman 43975fa85a09SFlorian Westphal rcu_read_lock(); 43985fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4399aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4400aefb4d4aSRobert Shearman struct nlattr *af; 4401aefb4d4aSRobert Shearman int err; 4402aefb4d4aSRobert Shearman 4403aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 44045fa85a09SFlorian Westphal if (!af) { 44055fa85a09SFlorian Westphal rcu_read_unlock(); 4406aefb4d4aSRobert Shearman goto nla_put_failure; 44075fa85a09SFlorian Westphal } 4408aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4409aefb4d4aSRobert Shearman 44105fa85a09SFlorian Westphal if (err == -ENODATA) { 4411aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 44125fa85a09SFlorian Westphal } else if (err < 0) { 44135fa85a09SFlorian Westphal rcu_read_unlock(); 4414aefb4d4aSRobert Shearman goto nla_put_failure; 44155fa85a09SFlorian Westphal } 4416aefb4d4aSRobert Shearman 4417aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4418aefb4d4aSRobert Shearman } 4419aefb4d4aSRobert Shearman } 44205fa85a09SFlorian Westphal rcu_read_unlock(); 4421aefb4d4aSRobert Shearman 4422aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4423aefb4d4aSRobert Shearman 4424aefb4d4aSRobert Shearman *idxattr = 0; 4425aefb4d4aSRobert Shearman } 4426aefb4d4aSRobert Shearman 442710c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 442810c9ead9SRoopa Prabhu 442910c9ead9SRoopa Prabhu return 0; 443010c9ead9SRoopa Prabhu 443110c9ead9SRoopa Prabhu nla_put_failure: 4432e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4433e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 443410c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4435e8872a25SNikolay Aleksandrov else 4436e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 443710c9ead9SRoopa Prabhu 443810c9ead9SRoopa Prabhu return -EMSGSIZE; 443910c9ead9SRoopa Prabhu } 444010c9ead9SRoopa Prabhu 444110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 444210c9ead9SRoopa Prabhu u32 filter_mask) 444310c9ead9SRoopa Prabhu { 444410c9ead9SRoopa Prabhu size_t size = 0; 444510c9ead9SRoopa Prabhu 4446e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 444710c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 444810c9ead9SRoopa Prabhu 444997a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 445097a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 445180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 445297a47facSNikolay Aleksandrov 445397a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 445480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 445580e73cc5SNikolay Aleksandrov attr)); 445697a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 445797a47facSNikolay Aleksandrov size += nla_total_size(0); 445897a47facSNikolay Aleksandrov } 445997a47facSNikolay Aleksandrov } 446097a47facSNikolay Aleksandrov 446180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 446280e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 446380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 446480e73cc5SNikolay Aleksandrov const struct net_device *master; 446580e73cc5SNikolay Aleksandrov 446680e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 446780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 446880e73cc5SNikolay Aleksandrov if (master) 446980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 447080e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 447180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 447280e73cc5SNikolay Aleksandrov 447380e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 447480e73cc5SNikolay Aleksandrov attr)); 447580e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 447680e73cc5SNikolay Aleksandrov size += nla_total_size(0); 447780e73cc5SNikolay Aleksandrov } 447880e73cc5SNikolay Aleksandrov } 447980e73cc5SNikolay Aleksandrov 448069ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 448169ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 448269ae6ad2SNogah Frankel 4483aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4484aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4485aefb4d4aSRobert Shearman 4486aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4487aefb4d4aSRobert Shearman size += nla_total_size(0); 4488aefb4d4aSRobert Shearman 44895fa85a09SFlorian Westphal rcu_read_lock(); 44905fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4491aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4492aefb4d4aSRobert Shearman size += nla_total_size( 4493aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4494aefb4d4aSRobert Shearman 4495aefb4d4aSRobert Shearman /* for AF_* */ 4496aefb4d4aSRobert Shearman size += nla_total_size(0); 4497aefb4d4aSRobert Shearman } 4498aefb4d4aSRobert Shearman } 44995fa85a09SFlorian Westphal rcu_read_unlock(); 4500aefb4d4aSRobert Shearman } 4501aefb4d4aSRobert Shearman 450210c9ead9SRoopa Prabhu return size; 450310c9ead9SRoopa Prabhu } 450410c9ead9SRoopa Prabhu 4505c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4506c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 450710c9ead9SRoopa Prabhu { 450810c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 450910c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4510e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4511e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 451210c9ead9SRoopa Prabhu struct sk_buff *nskb; 451310c9ead9SRoopa Prabhu u32 filter_mask; 451410c9ead9SRoopa Prabhu int err; 451510c9ead9SRoopa Prabhu 45164775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 45174775cc1fSMathias Krause return -EINVAL; 45184775cc1fSMathias Krause 451910c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 452010c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 452110c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 452210c9ead9SRoopa Prabhu else 452310c9ead9SRoopa Prabhu return -EINVAL; 452410c9ead9SRoopa Prabhu 452510c9ead9SRoopa Prabhu if (!dev) 452610c9ead9SRoopa Prabhu return -ENODEV; 452710c9ead9SRoopa Prabhu 452810c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 452910c9ead9SRoopa Prabhu if (!filter_mask) 453010c9ead9SRoopa Prabhu return -EINVAL; 453110c9ead9SRoopa Prabhu 453210c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 453310c9ead9SRoopa Prabhu if (!nskb) 453410c9ead9SRoopa Prabhu return -ENOBUFS; 453510c9ead9SRoopa Prabhu 453610c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 453710c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4538e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 453910c9ead9SRoopa Prabhu if (err < 0) { 454010c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 454110c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 454210c9ead9SRoopa Prabhu kfree_skb(nskb); 454310c9ead9SRoopa Prabhu } else { 454410c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 454510c9ead9SRoopa Prabhu } 454610c9ead9SRoopa Prabhu 454710c9ead9SRoopa Prabhu return err; 454810c9ead9SRoopa Prabhu } 454910c9ead9SRoopa Prabhu 455010c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 455110c9ead9SRoopa Prabhu { 4552e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 455310c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 455410c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4555e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4556e8872a25SNikolay Aleksandrov struct hlist_head *head; 4557e8872a25SNikolay Aleksandrov struct net_device *dev; 455810c9ead9SRoopa Prabhu u32 filter_mask = 0; 4559e8872a25SNikolay Aleksandrov int idx = 0; 456010c9ead9SRoopa Prabhu 456110c9ead9SRoopa Prabhu s_h = cb->args[0]; 456210c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4563e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4564e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 456510c9ead9SRoopa Prabhu 456610c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 456710c9ead9SRoopa Prabhu 45684775cc1fSMathias Krause if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 45694775cc1fSMathias Krause return -EINVAL; 45704775cc1fSMathias Krause 457110c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 457210c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 457310c9ead9SRoopa Prabhu if (!filter_mask) 457410c9ead9SRoopa Prabhu return -EINVAL; 457510c9ead9SRoopa Prabhu 457610c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 457710c9ead9SRoopa Prabhu idx = 0; 457810c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 457910c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 458010c9ead9SRoopa Prabhu if (idx < s_idx) 458110c9ead9SRoopa Prabhu goto cont; 458210c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 458310c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 458410c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4585e8872a25SNikolay Aleksandrov flags, filter_mask, 4586e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 458710c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 458810c9ead9SRoopa Prabhu * we're in trouble 458910c9ead9SRoopa Prabhu */ 459010c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 459110c9ead9SRoopa Prabhu 459210c9ead9SRoopa Prabhu if (err < 0) 459310c9ead9SRoopa Prabhu goto out; 4594e8872a25SNikolay Aleksandrov s_prividx = 0; 4595e8872a25SNikolay Aleksandrov s_idxattr = 0; 459610c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 459710c9ead9SRoopa Prabhu cont: 459810c9ead9SRoopa Prabhu idx++; 459910c9ead9SRoopa Prabhu } 460010c9ead9SRoopa Prabhu } 460110c9ead9SRoopa Prabhu out: 4602e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4603e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 460410c9ead9SRoopa Prabhu cb->args[1] = idx; 460510c9ead9SRoopa Prabhu cb->args[0] = h; 460610c9ead9SRoopa Prabhu 460710c9ead9SRoopa Prabhu return skb->len; 460810c9ead9SRoopa Prabhu } 460910c9ead9SRoopa Prabhu 46101da177e4SLinus Torvalds /* Process one rtnetlink message. */ 46111da177e4SLinus Torvalds 46122d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 46132d4bc933SJohannes Berg struct netlink_ext_ack *extack) 46141da177e4SLinus Torvalds { 46153b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4616addf9b90SFlorian Westphal struct rtnl_link *link; 4617e4202511SFlorian Westphal struct module *owner; 46186853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4619e2849863SThomas Graf rtnl_doit_func doit; 462062256f98SFlorian Westphal unsigned int flags; 4621617cfc75SAlexander Kuleshov int kind; 46221da177e4SLinus Torvalds int family; 46231da177e4SLinus Torvalds int type; 46241da177e4SLinus Torvalds 46251da177e4SLinus Torvalds type = nlh->nlmsg_type; 46261da177e4SLinus Torvalds if (type > RTM_MAX) 4627038890feSThomas Graf return -EOPNOTSUPP; 46281da177e4SLinus Torvalds 46291da177e4SLinus Torvalds type -= RTM_BASE; 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4632573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 46331da177e4SLinus Torvalds return 0; 46341da177e4SLinus Torvalds 4635573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 46361da177e4SLinus Torvalds kind = type&3; 46371da177e4SLinus Torvalds 463890f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 46391d00a4ebSThomas Graf return -EPERM; 46401da177e4SLinus Torvalds 46416853dd48SFlorian Westphal rcu_read_lock(); 4642b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 464397c53cacSDenis V. Lunev struct sock *rtnl; 4644e2849863SThomas Graf rtnl_dumpit_func dumpit; 4645c7ac8679SGreg Rose u16 min_dump_alloc = 0; 46461da177e4SLinus Torvalds 4647addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4648addf9b90SFlorian Westphal if (!link || !link->dumpit) { 46496853dd48SFlorian Westphal family = PF_UNSPEC; 4650addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4651addf9b90SFlorian Westphal if (!link || !link->dumpit) 46526853dd48SFlorian Westphal goto err_unlock; 46536853dd48SFlorian Westphal } 4654e4202511SFlorian Westphal owner = link->owner; 4655addf9b90SFlorian Westphal dumpit = link->dumpit; 46566853dd48SFlorian Westphal 46575c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4658e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 46591da177e4SLinus Torvalds 4660e4202511SFlorian Westphal err = 0; 4661e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4662e4202511SFlorian Westphal if (!try_module_get(owner)) 4663e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4664e4202511SFlorian Westphal 46656853dd48SFlorian Westphal rcu_read_unlock(); 46666853dd48SFlorian Westphal 466797c53cacSDenis V. Lunev rtnl = net->rtnl; 4668e4202511SFlorian Westphal if (err == 0) { 466980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 467080d326faSPablo Neira Ayuso .dump = dumpit, 467180d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4672e4202511SFlorian Westphal .module = owner, 467380d326faSPablo Neira Ayuso }; 467480d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4675e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4676e4202511SFlorian Westphal * module if dump is still in progress. 4677e4202511SFlorian Westphal */ 4678e4202511SFlorian Westphal module_put(owner); 467980d326faSPablo Neira Ayuso } 46802907c35fSEric Dumazet return err; 46811da177e4SLinus Torvalds } 46821da177e4SLinus Torvalds 4683addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4684addf9b90SFlorian Westphal if (!link || !link->doit) { 46858caa38b5SFlorian Westphal family = PF_UNSPEC; 4686addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4687addf9b90SFlorian Westphal if (!link || !link->doit) 4688addf9b90SFlorian Westphal goto out_unlock; 46898caa38b5SFlorian Westphal } 46908caa38b5SFlorian Westphal 4691e4202511SFlorian Westphal owner = link->owner; 4692e4202511SFlorian Westphal if (!try_module_get(owner)) { 4693e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4694e4202511SFlorian Westphal goto out_unlock; 4695e4202511SFlorian Westphal } 4696e4202511SFlorian Westphal 4697addf9b90SFlorian Westphal flags = link->flags; 469862256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4699addf9b90SFlorian Westphal doit = link->doit; 470062256f98SFlorian Westphal rcu_read_unlock(); 470162256f98SFlorian Westphal if (doit) 470262256f98SFlorian Westphal err = doit(skb, nlh, extack); 4703e4202511SFlorian Westphal module_put(owner); 470462256f98SFlorian Westphal return err; 470562256f98SFlorian Westphal } 47066853dd48SFlorian Westphal rcu_read_unlock(); 47076853dd48SFlorian Westphal 47080cc09020SFlorian Westphal rtnl_lock(); 4709addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4710addf9b90SFlorian Westphal if (link && link->doit) 4711addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 47120cc09020SFlorian Westphal rtnl_unlock(); 4713addf9b90SFlorian Westphal 4714e4202511SFlorian Westphal module_put(owner); 4715e4202511SFlorian Westphal 4716addf9b90SFlorian Westphal return err; 4717addf9b90SFlorian Westphal 4718addf9b90SFlorian Westphal out_unlock: 4719addf9b90SFlorian Westphal rcu_read_unlock(); 47200cc09020SFlorian Westphal return err; 47210cc09020SFlorian Westphal 47220cc09020SFlorian Westphal err_unlock: 47236853dd48SFlorian Westphal rcu_read_unlock(); 47240cc09020SFlorian Westphal return -EOPNOTSUPP; 47251da177e4SLinus Torvalds } 47261da177e4SLinus Torvalds 4727cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 47281da177e4SLinus Torvalds { 4729cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 47301da177e4SLinus Torvalds } 47311da177e4SLinus Torvalds 47325f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 47335f729eaaSJulien Gomes { 47345f729eaaSJulien Gomes switch (group) { 47355f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 47365f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 47375f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 47385f729eaaSJulien Gomes return -EPERM; 47395f729eaaSJulien Gomes break; 47405f729eaaSJulien Gomes } 47415f729eaaSJulien Gomes return 0; 47425f729eaaSJulien Gomes } 47435f729eaaSJulien Gomes 47441da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 47451da177e4SLinus Torvalds { 4746351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4747e9dc8653SEric W. Biederman 47481da177e4SLinus Torvalds switch (event) { 47495138e86fSVlad Yasevich case NETDEV_REBOOT: 47508a212589SXin Long case NETDEV_CHANGEMTU: 47513753654eSDavid Ahern case NETDEV_CHANGEADDR: 47525138e86fSVlad Yasevich case NETDEV_CHANGENAME: 47535138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 47545138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4755e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 47565138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4757dc709f37SXin Long case NETDEV_CHANGEUPPER: 47585138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 47595138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4760eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4761ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 47623d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 476338e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 47641da177e4SLinus Torvalds break; 47651da177e4SLinus Torvalds default: 47661da177e4SLinus Torvalds break; 47671da177e4SLinus Torvalds } 47681da177e4SLinus Torvalds return NOTIFY_DONE; 47691da177e4SLinus Torvalds } 47701da177e4SLinus Torvalds 47711da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 47721da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 47731da177e4SLinus Torvalds }; 47741da177e4SLinus Torvalds 477597c53cacSDenis V. Lunev 47762c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 477797c53cacSDenis V. Lunev { 477897c53cacSDenis V. Lunev struct sock *sk; 4779a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4780a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4781a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4782a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 47839785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 47845f729eaaSJulien Gomes .bind = rtnetlink_bind, 4785a31f2d17SPablo Neira Ayuso }; 4786a31f2d17SPablo Neira Ayuso 47879f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 478897c53cacSDenis V. Lunev if (!sk) 478997c53cacSDenis V. Lunev return -ENOMEM; 479097c53cacSDenis V. Lunev net->rtnl = sk; 479197c53cacSDenis V. Lunev return 0; 479297c53cacSDenis V. Lunev } 479397c53cacSDenis V. Lunev 47942c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 479597c53cacSDenis V. Lunev { 4796b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 479797c53cacSDenis V. Lunev net->rtnl = NULL; 479897c53cacSDenis V. Lunev } 479997c53cacSDenis V. Lunev 480097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 480197c53cacSDenis V. Lunev .init = rtnetlink_net_init, 480297c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 480397c53cacSDenis V. Lunev }; 480497c53cacSDenis V. Lunev 48051da177e4SLinus Torvalds void __init rtnetlink_init(void) 48061da177e4SLinus Torvalds { 480797c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 48081da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 480997c53cacSDenis V. Lunev 48101da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4811340d17fcSThomas Graf 4812c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4813b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4814b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4815b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4816b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4817687ad8ccSThomas Graf 4818b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4819b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4820b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 482177162022SJohn Fastabend 4822b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4823b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4824b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4825e5a55a89SJohn Fastabend 4826b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4827b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4828b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 482910c9ead9SRoopa Prabhu 483010c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4831b97bac64SFlorian Westphal 0); 48321da177e4SLinus Torvalds } 4833