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 */ 96758038695SMartin KaFai Lau nla_total_size(4); /* XDP_PROG_ID */ 968d1fdd913SBrenden Blanco 969d1fdd913SBrenden Blanco return xdp_size; 970d1fdd913SBrenden Blanco } 971d1fdd913SBrenden Blanco 972115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 973115c9b81SGreg Rose u32 ext_filter_mask) 974339bf98fSThomas Graf { 975339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 976339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9770b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 978339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 979270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 980339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 98135c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 982339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 983339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 984339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 985339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 986339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 987339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 988339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9899a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 990edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 99176ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 99276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 9936919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 9946919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 995339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 99638f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 9972d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 998d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 999db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1000115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1001115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1002115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1003c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1004f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1005b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 100682f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 100788d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1008c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1009b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10103d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10116621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 101238e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 101303ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 101479e1ad14SJiri Benc + nla_total_size(4) /* IFLA_IF_NETNSID */ 1015b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1016b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 101779e1ad14SJiri Benc + 0; 1018339bf98fSThomas Graf } 1019339bf98fSThomas Graf 102057b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 102157b61080SScott Feldman { 102257b61080SScott Feldman struct nlattr *vf_ports; 102357b61080SScott Feldman struct nlattr *vf_port; 102457b61080SScott Feldman int vf; 102557b61080SScott Feldman int err; 102657b61080SScott Feldman 102757b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 102857b61080SScott Feldman if (!vf_ports) 102957b61080SScott Feldman return -EMSGSIZE; 103057b61080SScott Feldman 103157b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 103257b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10338ca94183SScott Feldman if (!vf_port) 10348ca94183SScott Feldman goto nla_put_failure; 1035a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1036a6574349SDavid S. Miller goto nla_put_failure; 103757b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10388ca94183SScott Feldman if (err == -EMSGSIZE) 10398ca94183SScott Feldman goto nla_put_failure; 104057b61080SScott Feldman if (err) { 104157b61080SScott Feldman nla_nest_cancel(skb, vf_port); 104257b61080SScott Feldman continue; 104357b61080SScott Feldman } 104457b61080SScott Feldman nla_nest_end(skb, vf_port); 104557b61080SScott Feldman } 104657b61080SScott Feldman 104757b61080SScott Feldman nla_nest_end(skb, vf_ports); 104857b61080SScott Feldman 104957b61080SScott Feldman return 0; 10508ca94183SScott Feldman 10518ca94183SScott Feldman nla_put_failure: 10528ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10538ca94183SScott Feldman return -EMSGSIZE; 105457b61080SScott Feldman } 105557b61080SScott Feldman 105657b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 105757b61080SScott Feldman { 105857b61080SScott Feldman struct nlattr *port_self; 105957b61080SScott Feldman int err; 106057b61080SScott Feldman 106157b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 106257b61080SScott Feldman if (!port_self) 106357b61080SScott Feldman return -EMSGSIZE; 106457b61080SScott Feldman 106557b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 106657b61080SScott Feldman if (err) { 106757b61080SScott Feldman nla_nest_cancel(skb, port_self); 10688ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 106957b61080SScott Feldman } 107057b61080SScott Feldman 107157b61080SScott Feldman nla_nest_end(skb, port_self); 107257b61080SScott Feldman 107357b61080SScott Feldman return 0; 107457b61080SScott Feldman } 107557b61080SScott Feldman 1076c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1077c53864fdSDavid Gibson u32 ext_filter_mask) 107857b61080SScott Feldman { 107957b61080SScott Feldman int err; 108057b61080SScott Feldman 1081c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1082c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 108357b61080SScott Feldman return 0; 108457b61080SScott Feldman 108557b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 108657b61080SScott Feldman if (err) 108757b61080SScott Feldman return err; 108857b61080SScott Feldman 108957b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 109057b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 109157b61080SScott Feldman if (err) 109257b61080SScott Feldman return err; 109357b61080SScott Feldman } 109457b61080SScott Feldman 109557b61080SScott Feldman return 0; 109657b61080SScott Feldman } 109757b61080SScott Feldman 109866cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 109966cae9edSJiri Pirko { 110066cae9edSJiri Pirko int err; 110102637fceSJiri Pirko struct netdev_phys_item_id ppid; 110266cae9edSJiri Pirko 110366cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 110466cae9edSJiri Pirko if (err) { 110566cae9edSJiri Pirko if (err == -EOPNOTSUPP) 110666cae9edSJiri Pirko return 0; 110766cae9edSJiri Pirko return err; 110866cae9edSJiri Pirko } 110966cae9edSJiri Pirko 111066cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 111166cae9edSJiri Pirko return -EMSGSIZE; 111266cae9edSJiri Pirko 111366cae9edSJiri Pirko return 0; 111466cae9edSJiri Pirko } 111566cae9edSJiri Pirko 1116db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1117db24a904SDavid Ahern { 1118db24a904SDavid Ahern char name[IFNAMSIZ]; 1119db24a904SDavid Ahern int err; 1120db24a904SDavid Ahern 1121db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1122db24a904SDavid Ahern if (err) { 1123db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1124db24a904SDavid Ahern return 0; 1125db24a904SDavid Ahern return err; 1126db24a904SDavid Ahern } 1127db24a904SDavid Ahern 112877ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1129db24a904SDavid Ahern return -EMSGSIZE; 1130db24a904SDavid Ahern 1131db24a904SDavid Ahern return 0; 1132db24a904SDavid Ahern } 1133db24a904SDavid Ahern 113482f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 113582f28412SJiri Pirko { 113682f28412SJiri Pirko int err; 1137f8e20a9fSScott Feldman struct switchdev_attr attr = { 11386ff64f6fSIdo Schimmel .orig_dev = dev, 11391f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1140f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1141f8e20a9fSScott Feldman }; 114282f28412SJiri Pirko 1143f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 114482f28412SJiri Pirko if (err) { 114582f28412SJiri Pirko if (err == -EOPNOTSUPP) 114682f28412SJiri Pirko return 0; 114782f28412SJiri Pirko return err; 114882f28412SJiri Pirko } 114982f28412SJiri Pirko 115042275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 115142275bd8SScott Feldman attr.u.ppid.id)) 115282f28412SJiri Pirko return -EMSGSIZE; 115382f28412SJiri Pirko 115482f28412SJiri Pirko return 0; 115582f28412SJiri Pirko } 115682f28412SJiri Pirko 1157b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1158b22b941bSHannes Frederic Sowa struct net_device *dev) 1159b22b941bSHannes Frederic Sowa { 1160550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1161b22b941bSHannes Frederic Sowa struct nlattr *attr; 1162b22b941bSHannes Frederic Sowa 116358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 116458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1165b22b941bSHannes Frederic Sowa if (!attr) 1166b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1167b22b941bSHannes Frederic Sowa 1168550bce59SRoopa Prabhu sp = nla_data(attr); 1169550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1170550bce59SRoopa Prabhu 1171550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1172550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1173550bce59SRoopa Prabhu if (!attr) 1174550bce59SRoopa Prabhu return -EMSGSIZE; 1175550bce59SRoopa Prabhu 1176550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1177b22b941bSHannes Frederic Sowa 1178b22b941bSHannes Frederic Sowa return 0; 1179b22b941bSHannes Frederic Sowa } 1180b22b941bSHannes Frederic Sowa 1181b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1182b22b941bSHannes Frederic Sowa struct net_device *dev, 1183b22b941bSHannes Frederic Sowa int vfs_num, 1184b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1185b22b941bSHannes Frederic Sowa { 1186b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 118779aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1188b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 118979aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1190b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1191b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1192b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1193b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1194b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1195b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1196b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1197b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1198b22b941bSHannes Frederic Sowa 11990eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12000eed9cf5SMintz, Yuval 1201b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1202b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1203b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1204b22b941bSHannes Frederic Sowa * didn't report anything. 1205b22b941bSHannes Frederic Sowa */ 1206b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1207b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1208b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1209b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1210b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1211b22b941bSHannes Frederic Sowa */ 1212b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 121379aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 121479aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1215b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1216b22b941bSHannes Frederic Sowa return 0; 1217b22b941bSHannes Frederic Sowa 1218775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1219775f4f05SDan Carpenter 1220b22b941bSHannes Frederic Sowa vf_mac.vf = 1221b22b941bSHannes Frederic Sowa vf_vlan.vf = 122279aab093SMoshe Shemesh vf_vlan_info.vf = 1223b22b941bSHannes Frederic Sowa vf_rate.vf = 1224b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1225b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1226b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1227b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1228b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1229b22b941bSHannes Frederic Sowa 1230b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1231b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1232b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 123379aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 123479aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 123579aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1236b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1237b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1238b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1239b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1240b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1241b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1242b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1243b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 124479aab093SMoshe Shemesh if (!vf) 124579aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1246b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1247b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1248b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1249b22b941bSHannes Frederic Sowa &vf_rate) || 1250b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1251b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1252b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1253b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1254b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1255b22b941bSHannes Frederic Sowa &vf_linkstate) || 1256b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1257b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1258b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1259b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1260b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 126179aab093SMoshe Shemesh goto nla_put_vf_failure; 126279aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 126379aab093SMoshe Shemesh if (!vfvlanlist) 126479aab093SMoshe Shemesh goto nla_put_vf_failure; 126579aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 126679aab093SMoshe Shemesh &vf_vlan_info)) { 126779aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 126879aab093SMoshe Shemesh goto nla_put_vf_failure; 126979aab093SMoshe Shemesh } 127079aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1271b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1272b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1273b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1274b22b941bSHannes Frederic Sowa &vf_stats); 1275b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 127679aab093SMoshe Shemesh if (!vfstats) 127779aab093SMoshe Shemesh goto nla_put_vf_failure; 1278343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1279343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1280343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1281343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1282343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1283343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1284343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1285343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1286343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1287343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1288343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1289c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1290c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1291c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1292c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1293c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 129479aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 129579aab093SMoshe Shemesh goto nla_put_vf_failure; 129679aab093SMoshe Shemesh } 1297b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1298b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1299b22b941bSHannes Frederic Sowa return 0; 130079aab093SMoshe Shemesh 130179aab093SMoshe Shemesh nla_put_vf_failure: 130279aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 130379aab093SMoshe Shemesh nla_put_vfinfo_failure: 130479aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 130579aab093SMoshe Shemesh return -EMSGSIZE; 1306b22b941bSHannes Frederic Sowa } 1307b22b941bSHannes Frederic Sowa 1308250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1309250fc3dfSFlorian Westphal struct net_device *dev, 1310250fc3dfSFlorian Westphal u32 ext_filter_mask) 1311250fc3dfSFlorian Westphal { 1312250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1313250fc3dfSFlorian Westphal int i, num_vfs; 1314250fc3dfSFlorian Westphal 1315250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1316250fc3dfSFlorian Westphal return 0; 1317250fc3dfSFlorian Westphal 1318250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1319250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1320250fc3dfSFlorian Westphal return -EMSGSIZE; 1321250fc3dfSFlorian Westphal 1322250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1323250fc3dfSFlorian Westphal return 0; 1324250fc3dfSFlorian Westphal 1325250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1326250fc3dfSFlorian Westphal if (!vfinfo) 1327250fc3dfSFlorian Westphal return -EMSGSIZE; 1328250fc3dfSFlorian Westphal 1329250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1330250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1331250fc3dfSFlorian Westphal return -EMSGSIZE; 1332250fc3dfSFlorian Westphal } 1333250fc3dfSFlorian Westphal 1334250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1335250fc3dfSFlorian Westphal return 0; 1336250fc3dfSFlorian Westphal } 1337250fc3dfSFlorian Westphal 1338b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1339b22b941bSHannes Frederic Sowa { 13405f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13415f8e4474SKangjie Lu 13425f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13435f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13445f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13455f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13465f8e4474SKangjie Lu map.irq = dev->irq; 13475f8e4474SKangjie Lu map.dma = dev->dma; 13485f8e4474SKangjie Lu map.port = dev->if_port; 13495f8e4474SKangjie Lu 1350270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1351b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1352b22b941bSHannes Frederic Sowa 1353b22b941bSHannes Frederic Sowa return 0; 1354b22b941bSHannes Frederic Sowa } 1355b22b941bSHannes Frederic Sowa 135658038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id) 1357d67b9cd2SDaniel Borkmann { 1358d67b9cd2SDaniel Borkmann const struct net_device_ops *ops = dev->netdev_ops; 135958038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1360118b4aa2SJakub Kicinski struct netdev_bpf xdp; 1361d67b9cd2SDaniel Borkmann 1362d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1363d67b9cd2SDaniel Borkmann 136458038695SMartin KaFai Lau *prog_id = 0; 136558038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 136658038695SMartin KaFai Lau if (generic_xdp_prog) { 136758038695SMartin KaFai Lau *prog_id = generic_xdp_prog->aux->id; 1368d67b9cd2SDaniel Borkmann return XDP_ATTACHED_SKB; 136958038695SMartin KaFai Lau } 1370f4e63525SJakub Kicinski if (!ops->ndo_bpf) 1371d67b9cd2SDaniel Borkmann return XDP_ATTACHED_NONE; 1372ce158e58SJakub Kicinski 1373118b4aa2SJakub Kicinski __dev_xdp_query(dev, ops->ndo_bpf, &xdp); 1374118b4aa2SJakub Kicinski *prog_id = xdp.prog_id; 1375118b4aa2SJakub Kicinski 1376118b4aa2SJakub Kicinski return xdp.prog_attached; 1377d67b9cd2SDaniel Borkmann } 1378d67b9cd2SDaniel Borkmann 1379d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1380d1fdd913SBrenden Blanco { 1381d1fdd913SBrenden Blanco struct nlattr *xdp; 138258038695SMartin KaFai Lau u32 prog_id; 1383d1fdd913SBrenden Blanco int err; 1384d1fdd913SBrenden Blanco 1385d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1386d1fdd913SBrenden Blanco if (!xdp) 1387d1fdd913SBrenden Blanco return -EMSGSIZE; 1388b5cdae32SDavid S. Miller 1389d67b9cd2SDaniel Borkmann err = nla_put_u8(skb, IFLA_XDP_ATTACHED, 139058038695SMartin KaFai Lau rtnl_xdp_attached_mode(dev, &prog_id)); 1391d1fdd913SBrenden Blanco if (err) 1392d1fdd913SBrenden Blanco goto err_cancel; 1393d1fdd913SBrenden Blanco 139458038695SMartin KaFai Lau if (prog_id) { 139558038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 139658038695SMartin KaFai Lau if (err) 139758038695SMartin KaFai Lau goto err_cancel; 139858038695SMartin KaFai Lau } 139958038695SMartin KaFai Lau 1400d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1401d1fdd913SBrenden Blanco return 0; 1402d1fdd913SBrenden Blanco 1403d1fdd913SBrenden Blanco err_cancel: 1404d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1405d1fdd913SBrenden Blanco return err; 1406d1fdd913SBrenden Blanco } 1407d1fdd913SBrenden Blanco 14083d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14093d3ea5afSVlad Yasevich { 14103d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14113d3ea5afSVlad Yasevich 14123d3ea5afSVlad Yasevich switch (event) { 14133d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14143d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14153d3ea5afSVlad Yasevich break; 14163d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14173d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14183d3ea5afSVlad Yasevich break; 14193d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14203d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14213d3ea5afSVlad Yasevich break; 14223d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14233d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14243d3ea5afSVlad Yasevich break; 14253d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14263d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14273d3ea5afSVlad Yasevich break; 14283d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14293d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14303d3ea5afSVlad Yasevich break; 14313d3ea5afSVlad Yasevich default: 14323d3ea5afSVlad Yasevich break; 14333d3ea5afSVlad Yasevich } 14343d3ea5afSVlad Yasevich 14353d3ea5afSVlad Yasevich return rtnl_event_type; 14363d3ea5afSVlad Yasevich } 14373d3ea5afSVlad Yasevich 143879110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 143979110a04SFlorian Westphal { 144079110a04SFlorian Westphal const struct net_device *upper_dev; 144179110a04SFlorian Westphal int ret = 0; 144279110a04SFlorian Westphal 144379110a04SFlorian Westphal rcu_read_lock(); 144479110a04SFlorian Westphal 144579110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 144679110a04SFlorian Westphal if (upper_dev) 144779110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 144879110a04SFlorian Westphal 144979110a04SFlorian Westphal rcu_read_unlock(); 145079110a04SFlorian Westphal return ret; 145179110a04SFlorian Westphal } 145279110a04SFlorian Westphal 145379110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 145479110a04SFlorian Westphal { 145579110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 145679110a04SFlorian Westphal 145779110a04SFlorian Westphal if (dev->ifindex == ifindex) 145879110a04SFlorian Westphal return 0; 145979110a04SFlorian Westphal 146079110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 146179110a04SFlorian Westphal } 146279110a04SFlorian Westphal 14636c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 14646c557001SFlorian Westphal struct net_device *dev) 14656c557001SFlorian Westphal { 14666c557001SFlorian Westphal char buf[IFALIASZ]; 14676c557001SFlorian Westphal int ret; 14686c557001SFlorian Westphal 14696c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 14706c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 14716c557001SFlorian Westphal } 14726c557001SFlorian Westphal 1473b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 147479e1ad14SJiri Benc const struct net_device *dev, 147579e1ad14SJiri Benc struct net *src_net) 1476b1e66b9aSFlorian Westphal { 1477b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1478b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1479b1e66b9aSFlorian Westphal 1480b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 148179e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1482b1e66b9aSFlorian Westphal 1483b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1484b1e66b9aSFlorian Westphal return -EMSGSIZE; 1485b1e66b9aSFlorian Westphal } 1486b1e66b9aSFlorian Westphal } 1487b1e66b9aSFlorian Westphal 1488b1e66b9aSFlorian Westphal return 0; 1489b1e66b9aSFlorian Westphal } 1490b1e66b9aSFlorian Westphal 1491070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1492070cbf5bSFlorian Westphal const struct net_device *dev, 1493070cbf5bSFlorian Westphal u32 ext_filter_mask) 1494070cbf5bSFlorian Westphal { 1495070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1496070cbf5bSFlorian Westphal struct nlattr *af_spec; 1497070cbf5bSFlorian Westphal 1498070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1499070cbf5bSFlorian Westphal if (!af_spec) 1500070cbf5bSFlorian Westphal return -EMSGSIZE; 1501070cbf5bSFlorian Westphal 15025fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1503070cbf5bSFlorian Westphal struct nlattr *af; 1504070cbf5bSFlorian Westphal int err; 1505070cbf5bSFlorian Westphal 1506070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1507070cbf5bSFlorian Westphal continue; 1508070cbf5bSFlorian Westphal 1509070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1510070cbf5bSFlorian Westphal if (!af) 1511070cbf5bSFlorian Westphal return -EMSGSIZE; 1512070cbf5bSFlorian Westphal 1513070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1514070cbf5bSFlorian Westphal /* 1515070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1516070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1517070cbf5bSFlorian Westphal * means we should trim the attribute header and 1518070cbf5bSFlorian Westphal * continue. 1519070cbf5bSFlorian Westphal */ 1520070cbf5bSFlorian Westphal if (err == -ENODATA) 1521070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1522070cbf5bSFlorian Westphal else if (err < 0) 1523070cbf5bSFlorian Westphal return -EMSGSIZE; 1524070cbf5bSFlorian Westphal 1525070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1526070cbf5bSFlorian Westphal } 1527070cbf5bSFlorian Westphal 1528070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1529070cbf5bSFlorian Westphal return 0; 1530070cbf5bSFlorian Westphal } 1531070cbf5bSFlorian Westphal 153279e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 153379e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1534575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15353d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 153638e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 153738e01b30SNicolas Dichtel int tgt_netnsid) 1538b60c5115SThomas Graf { 1539b60c5115SThomas Graf struct ifinfomsg *ifm; 15401da177e4SLinus Torvalds struct nlmsghdr *nlh; 15411da177e4SLinus Torvalds 15422907c35fSEric Dumazet ASSERT_RTNL(); 1543b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1544b60c5115SThomas Graf if (nlh == NULL) 154526932566SPatrick McHardy return -EMSGSIZE; 15461da177e4SLinus Torvalds 1547b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1548b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1549b60c5115SThomas Graf ifm->__ifi_pad = 0; 1550b60c5115SThomas Graf ifm->ifi_type = dev->type; 1551b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1552b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1553b60c5115SThomas Graf ifm->ifi_change = change; 15541da177e4SLinus Torvalds 155579e1ad14SJiri Benc if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 155679e1ad14SJiri Benc goto nla_put_failure; 155779e1ad14SJiri Benc 1558a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1559a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1560a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1561a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1562a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1563a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1564a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1565edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 156676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1567c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1568c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 15691d69c2b3SMark A. Greer #ifdef CONFIG_RPS 157076ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 15711d69c2b3SMark A. Greer #endif 157279110a04SFlorian Westphal nla_put_iflink(skb, dev) || 157379110a04SFlorian Westphal put_master_ifindex(skb, dev) || 15749a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1575a6574349SDavid S. Miller (dev->qdisc && 1576a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 15776c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 15782d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1579b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1580b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1581b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1582b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1583b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1584b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1585b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1586a6574349SDavid S. Miller goto nla_put_failure; 15870b815a1aSStephen Hemminger 15883d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 15893d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 15903d3ea5afSVlad Yasevich goto nla_put_failure; 15913d3ea5afSVlad Yasevich } 15923d3ea5afSVlad Yasevich 1593b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1594a6574349SDavid S. Miller goto nla_put_failure; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds if (dev->addr_len) { 1597a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1598a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1599a6574349SDavid S. Miller goto nla_put_failure; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 160266cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 160366cae9edSJiri Pirko goto nla_put_failure; 160466cae9edSJiri Pirko 1605db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1606db24a904SDavid Ahern goto nla_put_failure; 1607db24a904SDavid Ahern 160882f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 160982f28412SJiri Pirko goto nla_put_failure; 161082f28412SJiri Pirko 1611b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1612b60c5115SThomas Graf goto nla_put_failure; 1613b60c5115SThomas Graf 1614250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1615a6574349SDavid S. Miller goto nla_put_failure; 161657b61080SScott Feldman 1617c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 161857b61080SScott Feldman goto nla_put_failure; 161957b61080SScott Feldman 1620d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1621d1fdd913SBrenden Blanco goto nla_put_failure; 1622d1fdd913SBrenden Blanco 1623ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 162438f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 162538f7b870SPatrick McHardy goto nla_put_failure; 162638f7b870SPatrick McHardy } 162738f7b870SPatrick McHardy 162879e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1629d37512a2SNicolas Dichtel goto nla_put_failure; 1630d37512a2SNicolas Dichtel 16316621dd29SNicolas Dichtel if (new_nsid && 16326621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16336621dd29SNicolas Dichtel goto nla_put_failure; 163438e01b30SNicolas Dichtel if (new_ifindex && 163538e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 163638e01b30SNicolas Dichtel goto nla_put_failure; 163738e01b30SNicolas Dichtel 16386621dd29SNicolas Dichtel 16395fa85a09SFlorian Westphal rcu_read_lock(); 1640070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16415fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16425fa85a09SFlorian Westphal rcu_read_unlock(); 1643f8ff182cSThomas Graf 1644053c095aSJohannes Berg nlmsg_end(skb, nlh); 1645053c095aSJohannes Berg return 0; 1646b60c5115SThomas Graf 16475fa85a09SFlorian Westphal nla_put_failure_rcu: 16485fa85a09SFlorian Westphal rcu_read_unlock(); 1649b60c5115SThomas Graf nla_put_failure: 165026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 165126932566SPatrick McHardy return -EMSGSIZE; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 1654f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1655f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1656f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1657f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1658f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1659f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1660f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1661f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1662f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1663f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1664f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1665f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1666f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1667f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1668f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1669f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 16702459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 16712459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 16722459b4c6SNicolas Dichtel */ 16732459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1674f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1675f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1676f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1677f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1678f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1679f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1680f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1681f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 168246e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 168346e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 168402637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 16852d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 168682f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1687317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 168888d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1689d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 16903d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1691db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 169279e1ad14SJiri Benc [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1693b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1694b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 1695f7b12606SJiri Pirko }; 1696f7b12606SJiri Pirko 1697f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1698f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1699f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1700f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1701f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1702f7b12606SJiri Pirko }; 1703f7b12606SJiri Pirko 1704f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1705364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1706364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 170779aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1708364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1709364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1710364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1711364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 171201a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17133b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1714dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1715cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1716cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17173b766cd8SEran Ben Elisha }; 17183b766cd8SEran Ben Elisha 1719f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1720f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1721f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1722f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1723f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1724f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1725f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1726f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1727f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1728f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1729025331dfSDaniel Borkmann 1730025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1731025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1732025331dfSDaniel Borkmann * combination with structs. 1733025331dfSDaniel Borkmann */ 1734025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1735025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1736f7b12606SJiri Pirko }; 1737f7b12606SJiri Pirko 1738d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1739d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1740d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 174185de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 174258038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1743d1fdd913SBrenden Blanco }; 1744d1fdd913SBrenden Blanco 1745dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1746dc599f76SDavid Ahern { 1747dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1748dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1749dc599f76SDavid Ahern 1750fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1751fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1752dc599f76SDavid Ahern return NULL; 1753dc599f76SDavid Ahern 1754dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1755dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1756dc599f76SDavid Ahern 1757dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1758dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1759dc599f76SDavid Ahern } 1760dc599f76SDavid Ahern 1761dc599f76SDavid Ahern return ops; 1762dc599f76SDavid Ahern } 1763dc599f76SDavid Ahern 1764dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1765dc599f76SDavid Ahern { 1766dc599f76SDavid Ahern struct net_device *master; 1767dc599f76SDavid Ahern 1768dc599f76SDavid Ahern if (!master_idx) 1769dc599f76SDavid Ahern return false; 1770dc599f76SDavid Ahern 1771dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1772dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1773dc599f76SDavid Ahern return true; 1774dc599f76SDavid Ahern 1775dc599f76SDavid Ahern return false; 1776dc599f76SDavid Ahern } 1777dc599f76SDavid Ahern 1778dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1779dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1780dc599f76SDavid Ahern { 1781dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1782dc599f76SDavid Ahern return true; 1783dc599f76SDavid Ahern 1784dc599f76SDavid Ahern return false; 1785dc599f76SDavid Ahern } 1786dc599f76SDavid Ahern 1787dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1788dc599f76SDavid Ahern int master_idx, 1789dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1790dc599f76SDavid Ahern { 1791dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1792dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1793dc599f76SDavid Ahern return true; 1794dc599f76SDavid Ahern 1795dc599f76SDavid Ahern return false; 1796dc599f76SDavid Ahern } 1797dc599f76SDavid Ahern 1798f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid) 179979e1ad14SJiri Benc { 180079e1ad14SJiri Benc struct net *net; 180179e1ad14SJiri Benc 1802f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 180379e1ad14SJiri Benc if (!net) 180479e1ad14SJiri Benc return ERR_PTR(-EINVAL); 180579e1ad14SJiri Benc 180679e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 180779e1ad14SJiri Benc * the user namespace owning the target net ns. 180879e1ad14SJiri Benc */ 1809f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 181079e1ad14SJiri Benc put_net(net); 181179e1ad14SJiri Benc return ERR_PTR(-EACCES); 181279e1ad14SJiri Benc } 181379e1ad14SJiri Benc return net; 181479e1ad14SJiri Benc } 181579e1ad14SJiri Benc 1816b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 18171da177e4SLinus Torvalds { 18183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 181979e1ad14SJiri Benc struct net *tgt_net = net; 18207c28bd0bSEric Dumazet int h, s_h; 18217c28bd0bSEric Dumazet int idx = 0, s_idx; 18221da177e4SLinus Torvalds struct net_device *dev; 18237c28bd0bSEric Dumazet struct hlist_head *head; 1824115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1825115c9b81SGreg Rose u32 ext_filter_mask = 0; 1826dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1827dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1828dc599f76SDavid Ahern int master_idx = 0; 182979e1ad14SJiri Benc int netnsid = -1; 1830973462bbSDavid Gibson int err; 1831e5eca6d4SMichal Schmidt int hdrlen; 18321da177e4SLinus Torvalds 18337c28bd0bSEric Dumazet s_h = cb->args[0]; 18347c28bd0bSEric Dumazet s_idx = cb->args[1]; 18357c28bd0bSEric Dumazet 1836e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1837e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1838e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1839e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1840e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1841e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1842e5eca6d4SMichal Schmidt */ 1843e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1844e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1845e5eca6d4SMichal Schmidt 1846fceb6435SJohannes Berg if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1847fceb6435SJohannes Berg ifla_policy, NULL) >= 0) { 184879e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 184979e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1850f428fe4aSAndrei Vagin tgt_net = get_target_net(skb->sk, netnsid); 185179e1ad14SJiri Benc if (IS_ERR(tgt_net)) { 185279e1ad14SJiri Benc tgt_net = net; 185379e1ad14SJiri Benc netnsid = -1; 185479e1ad14SJiri Benc } 185579e1ad14SJiri Benc } 185679e1ad14SJiri Benc 1857115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1858115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1859dc599f76SDavid Ahern 1860dc599f76SDavid Ahern if (tb[IFLA_MASTER]) 1861dc599f76SDavid Ahern master_idx = nla_get_u32(tb[IFLA_MASTER]); 1862dc599f76SDavid Ahern 1863dc599f76SDavid Ahern if (tb[IFLA_LINKINFO]) 1864dc599f76SDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1865dc599f76SDavid Ahern 1866dc599f76SDavid Ahern if (master_idx || kind_ops) 1867dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1868a4b64fbeSEric Dumazet } 1869115c9b81SGreg Rose 18707c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 18717562f876SPavel Emelianov idx = 0; 187279e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1873cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1874dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 18753f0ae05dSZhang Shengju goto cont; 18761da177e4SLinus Torvalds if (idx < s_idx) 18777562f876SPavel Emelianov goto cont; 187879e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 187979e1ad14SJiri Benc RTM_NEWLINK, 188015e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 18817c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1882dc599f76SDavid Ahern flags, 188338e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 188479e1ad14SJiri Benc netnsid); 1885973462bbSDavid Gibson 1886f6c5775fSDavid Ahern if (err < 0) { 1887f6c5775fSDavid Ahern if (likely(skb->len)) 18887c28bd0bSEric Dumazet goto out; 18894e985adaSThomas Graf 1890f6c5775fSDavid Ahern goto out_err; 1891f6c5775fSDavid Ahern } 18927562f876SPavel Emelianov cont: 18937562f876SPavel Emelianov idx++; 18941da177e4SLinus Torvalds } 18957c28bd0bSEric Dumazet } 18967c28bd0bSEric Dumazet out: 1897f6c5775fSDavid Ahern err = skb->len; 1898f6c5775fSDavid Ahern out_err: 18997c28bd0bSEric Dumazet cb->args[1] = idx; 19007c28bd0bSEric Dumazet cb->args[0] = h; 1901d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 1902d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 190379e1ad14SJiri Benc if (netnsid >= 0) 190479e1ad14SJiri Benc put_net(tgt_net); 19051da177e4SLinus Torvalds 1906f6c5775fSDavid Ahern return err; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 1909fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1910fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 1911f7b12606SJiri Pirko { 1912fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1913f7b12606SJiri Pirko } 1914f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 191557b61080SScott Feldman 191681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 191781adee47SEric W. Biederman { 191881adee47SEric W. Biederman struct net *net; 191981adee47SEric W. Biederman /* Examine the link attributes and figure out which 192081adee47SEric W. Biederman * network namespace we are talking about. 192181adee47SEric W. Biederman */ 192281adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 192381adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1924f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1925f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 192681adee47SEric W. Biederman else 192781adee47SEric W. Biederman net = get_net(src_net); 192881adee47SEric W. Biederman return net; 192981adee47SEric W. Biederman } 193081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 193181adee47SEric W. Biederman 19327c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 19337c4f63baSChristian Brauner * examining the link attributes in the following order: 19347c4f63baSChristian Brauner * 19357c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 19367c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 19377c4f63baSChristian Brauner * 3. IFLA_IF_NETNSID 19387c4f63baSChristian Brauner */ 19397c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 19407c4f63baSChristian Brauner struct nlattr *tb[]) 19417c4f63baSChristian Brauner { 19427c4f63baSChristian Brauner struct net *net; 19437c4f63baSChristian Brauner 19447c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 19457c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 19467c4f63baSChristian Brauner 19477c4f63baSChristian Brauner if (!tb[IFLA_IF_NETNSID]) 19487c4f63baSChristian Brauner return get_net(src_net); 19497c4f63baSChristian Brauner 19507c4f63baSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 19517c4f63baSChristian Brauner if (!net) 19527c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 19537c4f63baSChristian Brauner 19547c4f63baSChristian Brauner return net; 19557c4f63baSChristian Brauner } 19567c4f63baSChristian Brauner 19577c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 19587c4f63baSChristian Brauner struct net *src_net, 19597c4f63baSChristian Brauner struct nlattr *tb[], int cap) 19607c4f63baSChristian Brauner { 19617c4f63baSChristian Brauner struct net *net; 19627c4f63baSChristian Brauner 19637c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 19647c4f63baSChristian Brauner if (IS_ERR(net)) 19657c4f63baSChristian Brauner return net; 19667c4f63baSChristian Brauner 19677c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 19687c4f63baSChristian Brauner put_net(net); 19697c4f63baSChristian Brauner return ERR_PTR(-EPERM); 19707c4f63baSChristian Brauner } 19717c4f63baSChristian Brauner 19727c4f63baSChristian Brauner return net; 19737c4f63baSChristian Brauner } 19747c4f63baSChristian Brauner 19754ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 19764ff66caeSChristian Brauner * potentially referring to different network namespaces. 19774ff66caeSChristian Brauner */ 19784ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 19794ff66caeSChristian Brauner struct netlink_ext_ack *extack, 19804ff66caeSChristian Brauner bool netns_id_only) 19814ff66caeSChristian Brauner { 19824ff66caeSChristian Brauner 19834ff66caeSChristian Brauner if (netns_id_only) { 19844ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 19854ff66caeSChristian Brauner return 0; 19864ff66caeSChristian Brauner 19874ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 19884ff66caeSChristian Brauner return -EOPNOTSUPP; 19894ff66caeSChristian Brauner } 19904ff66caeSChristian Brauner 19914ff66caeSChristian Brauner if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 19924ff66caeSChristian Brauner goto invalid_attr; 19934ff66caeSChristian Brauner 19944ff66caeSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 19954ff66caeSChristian Brauner goto invalid_attr; 19964ff66caeSChristian Brauner 19974ff66caeSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 19984ff66caeSChristian Brauner goto invalid_attr; 19994ff66caeSChristian Brauner 20004ff66caeSChristian Brauner return 0; 20014ff66caeSChristian Brauner 20024ff66caeSChristian Brauner invalid_attr: 20034ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 20044ff66caeSChristian Brauner return -EINVAL; 20054ff66caeSChristian Brauner } 20064ff66caeSChristian Brauner 20071840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 20081840bb13SThomas Graf { 20091840bb13SThomas Graf if (dev) { 20101840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 20111840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 20121840bb13SThomas Graf return -EINVAL; 20131840bb13SThomas Graf 20141840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 20151840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 20161840bb13SThomas Graf return -EINVAL; 20171840bb13SThomas Graf } 20181840bb13SThomas Graf 2019cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2020cf7afbfeSThomas Graf struct nlattr *af; 2021cf7afbfeSThomas Graf int rem, err; 2022cf7afbfeSThomas Graf 2023cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2024cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2025cf7afbfeSThomas Graf 20265fa85a09SFlorian Westphal rcu_read_lock(); 20275fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 20285fa85a09SFlorian Westphal if (!af_ops) { 20295fa85a09SFlorian Westphal rcu_read_unlock(); 2030cf7afbfeSThomas Graf return -EAFNOSUPPORT; 20315fa85a09SFlorian Westphal } 2032cf7afbfeSThomas Graf 20335fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 20345fa85a09SFlorian Westphal rcu_read_unlock(); 2035cf7afbfeSThomas Graf return -EOPNOTSUPP; 20365fa85a09SFlorian Westphal } 2037cf7afbfeSThomas Graf 2038cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 20396d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 20405fa85a09SFlorian Westphal if (err < 0) { 20415fa85a09SFlorian Westphal rcu_read_unlock(); 2042cf7afbfeSThomas Graf return err; 2043cf7afbfeSThomas Graf } 2044cf7afbfeSThomas Graf } 20455fa85a09SFlorian Westphal 20465fa85a09SFlorian Westphal rcu_read_unlock(); 20475fa85a09SFlorian Westphal } 2048cf7afbfeSThomas Graf } 2049cf7afbfeSThomas Graf 20501840bb13SThomas Graf return 0; 20511840bb13SThomas Graf } 20521840bb13SThomas Graf 2053cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2054cc8e27ccSEli Cohen int guid_type) 2055cc8e27ccSEli Cohen { 2056cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2057cc8e27ccSEli Cohen 2058cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2059cc8e27ccSEli Cohen } 2060cc8e27ccSEli Cohen 2061cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2062cc8e27ccSEli Cohen { 2063cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2064cc8e27ccSEli Cohen return -EOPNOTSUPP; 2065cc8e27ccSEli Cohen 2066cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2067cc8e27ccSEli Cohen } 2068cc8e27ccSEli Cohen 20694f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2070c02db8c6SChris Wright { 2071c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 20724f7d2cdfSDaniel Borkmann int err = -EINVAL; 2073c02db8c6SChris Wright 20744f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 20754f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 20764f7d2cdfSDaniel Borkmann 2077c02db8c6SChris Wright err = -EOPNOTSUPP; 2078c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2079c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2080c02db8c6SChris Wright ivm->mac); 20814f7d2cdfSDaniel Borkmann if (err < 0) 20824f7d2cdfSDaniel Borkmann return err; 2083c02db8c6SChris Wright } 20844f7d2cdfSDaniel Borkmann 20854f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 20864f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 20874f7d2cdfSDaniel Borkmann 2088c02db8c6SChris Wright err = -EOPNOTSUPP; 2089c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 20904f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 209179aab093SMoshe Shemesh ivv->qos, 209279aab093SMoshe Shemesh htons(ETH_P_8021Q)); 209379aab093SMoshe Shemesh if (err < 0) 209479aab093SMoshe Shemesh return err; 209579aab093SMoshe Shemesh } 209679aab093SMoshe Shemesh 209779aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 209879aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 209979aab093SMoshe Shemesh struct nlattr *attr; 210079aab093SMoshe Shemesh int rem, len = 0; 210179aab093SMoshe Shemesh 210279aab093SMoshe Shemesh err = -EOPNOTSUPP; 210379aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 210479aab093SMoshe Shemesh return err; 210579aab093SMoshe Shemesh 210679aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 210779aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 210879aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 210979aab093SMoshe Shemesh return -EINVAL; 211079aab093SMoshe Shemesh } 211179aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 211279aab093SMoshe Shemesh return -EOPNOTSUPP; 211379aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 211479aab093SMoshe Shemesh 211579aab093SMoshe Shemesh len++; 211679aab093SMoshe Shemesh } 2117fa34cd94SArnd Bergmann if (len == 0) 2118fa34cd94SArnd Bergmann return -EINVAL; 2119fa34cd94SArnd Bergmann 212079aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 212179aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 21224f7d2cdfSDaniel Borkmann if (err < 0) 21234f7d2cdfSDaniel Borkmann return err; 2124c02db8c6SChris Wright } 21254f7d2cdfSDaniel Borkmann 21264f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 21274f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2128ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 21294f7d2cdfSDaniel Borkmann 2130c02db8c6SChris Wright err = -EOPNOTSUPP; 2131ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 21324f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 21334f7d2cdfSDaniel Borkmann if (err < 0) 21344f7d2cdfSDaniel Borkmann return err; 21354f7d2cdfSDaniel Borkmann 2136ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2137ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2138ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2139ed616689SSucheta Chakraborty ivf.min_tx_rate, 2140c02db8c6SChris Wright ivt->rate); 21414f7d2cdfSDaniel Borkmann if (err < 0) 21424f7d2cdfSDaniel Borkmann return err; 2143c02db8c6SChris Wright } 21444f7d2cdfSDaniel Borkmann 21454f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 21464f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 21474f7d2cdfSDaniel Borkmann 2148ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2149ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2150ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2151ed616689SSucheta Chakraborty ivt->min_tx_rate, 2152ed616689SSucheta Chakraborty ivt->max_tx_rate); 21534f7d2cdfSDaniel Borkmann if (err < 0) 21544f7d2cdfSDaniel Borkmann return err; 2155ed616689SSucheta Chakraborty } 21564f7d2cdfSDaniel Borkmann 21574f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 21584f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 21594f7d2cdfSDaniel Borkmann 21605f8444a3SGreg Rose err = -EOPNOTSUPP; 21615f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 21625f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 21635f8444a3SGreg Rose ivs->setting); 21644f7d2cdfSDaniel Borkmann if (err < 0) 21654f7d2cdfSDaniel Borkmann return err; 21665f8444a3SGreg Rose } 21674f7d2cdfSDaniel Borkmann 21684f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 21694f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 21704f7d2cdfSDaniel Borkmann 21711d8faf48SRony Efraim err = -EOPNOTSUPP; 21721d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 21731d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 21741d8faf48SRony Efraim ivl->link_state); 21754f7d2cdfSDaniel Borkmann if (err < 0) 21764f7d2cdfSDaniel Borkmann return err; 21771d8faf48SRony Efraim } 21784f7d2cdfSDaniel Borkmann 21794f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 218001a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 218101a3d796SVlad Zolotarov 218201a3d796SVlad Zolotarov err = -EOPNOTSUPP; 21834f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 218401a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 21854f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 218601a3d796SVlad Zolotarov ivrssq_en->setting); 21874f7d2cdfSDaniel Borkmann if (err < 0) 21884f7d2cdfSDaniel Borkmann return err; 218901a3d796SVlad Zolotarov } 21904f7d2cdfSDaniel Borkmann 2191dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2192dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2193dd461d6aSHiroshi Shimamoto 2194dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2195dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2196dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2197dd461d6aSHiroshi Shimamoto if (err < 0) 2198dd461d6aSHiroshi Shimamoto return err; 2199dd461d6aSHiroshi Shimamoto } 2200dd461d6aSHiroshi Shimamoto 2201cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2202cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2203cc8e27ccSEli Cohen 2204cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2205cc8e27ccSEli Cohen return -EOPNOTSUPP; 2206cc8e27ccSEli Cohen 2207cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2208cc8e27ccSEli Cohen } 2209cc8e27ccSEli Cohen 2210cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2211cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2212cc8e27ccSEli Cohen 2213cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2214cc8e27ccSEli Cohen return -EOPNOTSUPP; 2215cc8e27ccSEli Cohen 2216cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2217cc8e27ccSEli Cohen } 2218cc8e27ccSEli Cohen 2219c02db8c6SChris Wright return err; 2220c02db8c6SChris Wright } 2221c02db8c6SChris Wright 222233eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 222333eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2224fbaec0eaSJiri Pirko { 2225898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2226fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2227fbaec0eaSJiri Pirko int err; 2228fbaec0eaSJiri Pirko 2229898e5061SJiri Pirko if (upper_dev) { 2230898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2231fbaec0eaSJiri Pirko return 0; 2232898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2233fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2234898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2235fbaec0eaSJiri Pirko if (err) 2236fbaec0eaSJiri Pirko return err; 2237fbaec0eaSJiri Pirko } else { 2238fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2239fbaec0eaSJiri Pirko } 2240fbaec0eaSJiri Pirko } 2241fbaec0eaSJiri Pirko 2242fbaec0eaSJiri Pirko if (ifindex) { 2243898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2244898e5061SJiri Pirko if (!upper_dev) 2245fbaec0eaSJiri Pirko return -EINVAL; 2246898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2247fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 224833eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2249fbaec0eaSJiri Pirko if (err) 2250fbaec0eaSJiri Pirko return err; 2251fbaec0eaSJiri Pirko } else { 2252fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2253fbaec0eaSJiri Pirko } 2254fbaec0eaSJiri Pirko } 2255fbaec0eaSJiri Pirko return 0; 2256fbaec0eaSJiri Pirko } 2257fbaec0eaSJiri Pirko 225890c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2259ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2260ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 226190f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 226290f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2263ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 226490c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 22650157f60cSPatrick McHardy { 2266d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 22670157f60cSPatrick McHardy int err; 22680157f60cSPatrick McHardy 2269644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2270644c7eebSEric Dumazet if (err < 0) 2271644c7eebSEric Dumazet return err; 2272644c7eebSEric Dumazet 22737c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 22747c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 22757c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2276d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2277d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2278d8a5ec67SEric W. Biederman goto errout; 2279d8a5ec67SEric W. Biederman } 22807c4f63baSChristian Brauner 2281d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2282d8a5ec67SEric W. Biederman put_net(net); 2283d8a5ec67SEric W. Biederman if (err) 2284d8a5ec67SEric W. Biederman goto errout; 228590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2286d8a5ec67SEric W. Biederman } 2287d8a5ec67SEric W. Biederman 22880157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 22890157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 22900157f60cSPatrick McHardy struct ifmap k_map; 22910157f60cSPatrick McHardy 2292d314774cSStephen Hemminger if (!ops->ndo_set_config) { 22930157f60cSPatrick McHardy err = -EOPNOTSUPP; 22940157f60cSPatrick McHardy goto errout; 22950157f60cSPatrick McHardy } 22960157f60cSPatrick McHardy 22970157f60cSPatrick McHardy if (!netif_device_present(dev)) { 22980157f60cSPatrick McHardy err = -ENODEV; 22990157f60cSPatrick McHardy goto errout; 23000157f60cSPatrick McHardy } 23010157f60cSPatrick McHardy 23020157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 23030157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 23040157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 23050157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 23060157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 23070157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 23080157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 23090157f60cSPatrick McHardy 2310d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 23110157f60cSPatrick McHardy if (err < 0) 23120157f60cSPatrick McHardy goto errout; 23130157f60cSPatrick McHardy 2314ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23150157f60cSPatrick McHardy } 23160157f60cSPatrick McHardy 23170157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 23180157f60cSPatrick McHardy struct sockaddr *sa; 23190157f60cSPatrick McHardy int len; 23200157f60cSPatrick McHardy 2321153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2322153711f9SWANG Cong sizeof(*sa)); 23230157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 23240157f60cSPatrick McHardy if (!sa) { 23250157f60cSPatrick McHardy err = -ENOMEM; 23260157f60cSPatrick McHardy goto errout; 23270157f60cSPatrick McHardy } 23280157f60cSPatrick McHardy sa->sa_family = dev->type; 23290157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 23300157f60cSPatrick McHardy dev->addr_len); 2331e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 23320157f60cSPatrick McHardy kfree(sa); 23330157f60cSPatrick McHardy if (err) 23340157f60cSPatrick McHardy goto errout; 233590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23360157f60cSPatrick McHardy } 23370157f60cSPatrick McHardy 23380157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 23390157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 23400157f60cSPatrick McHardy if (err < 0) 23410157f60cSPatrick McHardy goto errout; 234290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23430157f60cSPatrick McHardy } 23440157f60cSPatrick McHardy 2345cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2346cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2347ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2348cbda10faSVlad Dogaru } 2349cbda10faSVlad Dogaru 23500157f60cSPatrick McHardy /* 23510157f60cSPatrick McHardy * Interface selected by interface index but interface 23520157f60cSPatrick McHardy * name provided implies that a name change has been 23530157f60cSPatrick McHardy * requested. 23540157f60cSPatrick McHardy */ 23550157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 23560157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 23570157f60cSPatrick McHardy if (err < 0) 23580157f60cSPatrick McHardy goto errout; 235990c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23600157f60cSPatrick McHardy } 23610157f60cSPatrick McHardy 23620b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 23630b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 23640b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 23650b815a1aSStephen Hemminger if (err < 0) 23660b815a1aSStephen Hemminger goto errout; 2367ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23680b815a1aSStephen Hemminger } 23690b815a1aSStephen Hemminger 23700157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 23710157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2372e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 23730157f60cSPatrick McHardy } 23740157f60cSPatrick McHardy 23750157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 23763729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 23775f9021cfSJohannes Berg if (err < 0) 23785f9021cfSJohannes Berg goto errout; 23790157f60cSPatrick McHardy } 23800157f60cSPatrick McHardy 2381fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 238233eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2383fbaec0eaSJiri Pirko if (err) 2384fbaec0eaSJiri Pirko goto errout; 238590c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2386fbaec0eaSJiri Pirko } 2387fbaec0eaSJiri Pirko 23889a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 23899a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 23909a57247fSJiri Pirko if (err) 23919a57247fSJiri Pirko goto errout; 239290c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23939a57247fSJiri Pirko } 23949a57247fSJiri Pirko 23955d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 23960cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 23975d1180fcSNicolas Dichtel 23986a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 23996a643ddbSCong Wang if (err) 240008294a26SJason Wang goto errout; 24012d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 240208294a26SJason Wang } 24030157f60cSPatrick McHardy 240446e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 240546e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 240646e6b992SStephen Hemminger 240746e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 240846e6b992SStephen Hemminger err = -EINVAL; 240946e6b992SStephen Hemminger goto errout; 241046e6b992SStephen Hemminger } 241146e6b992SStephen Hemminger 241246e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 241346e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 241446e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 241546e6b992SStephen Hemminger } 241646e6b992SStephen Hemminger } 241746e6b992SStephen Hemminger 241846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 241946e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 242046e6b992SStephen Hemminger 242146e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 242246e6b992SStephen Hemminger err = -EINVAL; 242346e6b992SStephen Hemminger goto errout; 242446e6b992SStephen Hemminger } 242546e6b992SStephen Hemminger 242646e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 242746e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 242846e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 242946e6b992SStephen Hemminger } 243046e6b992SStephen Hemminger } 243146e6b992SStephen Hemminger 24320157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 243393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 24340157f60cSPatrick McHardy 24350157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 24361889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 24371889b0e7SNicolas Dichtel 24380157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 24391889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2440ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24411889b0e7SNicolas Dichtel dev->link_mode = value; 244293b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 24430157f60cSPatrick McHardy } 24440157f60cSPatrick McHardy 2445c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 24464f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2447c02db8c6SChris Wright struct nlattr *attr; 2448c02db8c6SChris Wright int rem; 24494f7d2cdfSDaniel Borkmann 2450c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 24514f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 24524f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2453253683bbSDavid Howells err = -EINVAL; 2454c02db8c6SChris Wright goto errout; 2455253683bbSDavid Howells } 24564f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2457fceb6435SJohannes Berg ifla_vf_policy, NULL); 24584f7d2cdfSDaniel Borkmann if (err < 0) 24594f7d2cdfSDaniel Borkmann goto errout; 24604f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2461ebc08a6fSWilliams, Mitch A if (err < 0) 2462ebc08a6fSWilliams, Mitch A goto errout; 2463ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2464ebc08a6fSWilliams, Mitch A } 2465ebc08a6fSWilliams, Mitch A } 24660157f60cSPatrick McHardy err = 0; 24670157f60cSPatrick McHardy 246857b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 246957b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 247057b61080SScott Feldman struct nlattr *attr; 247157b61080SScott Feldman int vf; 247257b61080SScott Feldman int rem; 247357b61080SScott Feldman 247457b61080SScott Feldman err = -EOPNOTSUPP; 247557b61080SScott Feldman if (!ops->ndo_set_vf_port) 247657b61080SScott Feldman goto errout; 247757b61080SScott Feldman 247857b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2479035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2480035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2481035d210fSDaniel Borkmann err = -EINVAL; 2482035d210fSDaniel Borkmann goto errout; 2483035d210fSDaniel Borkmann } 2484035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2485fceb6435SJohannes Berg ifla_port_policy, NULL); 248657b61080SScott Feldman if (err < 0) 248757b61080SScott Feldman goto errout; 248857b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 248957b61080SScott Feldman err = -EOPNOTSUPP; 249057b61080SScott Feldman goto errout; 249157b61080SScott Feldman } 249257b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 249357b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 249457b61080SScott Feldman if (err < 0) 249557b61080SScott Feldman goto errout; 2496ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 249757b61080SScott Feldman } 249857b61080SScott Feldman } 249957b61080SScott Feldman err = 0; 250057b61080SScott Feldman 250157b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 250257b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 250357b61080SScott Feldman 250457b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2505fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2506fceb6435SJohannes Berg NULL); 250757b61080SScott Feldman if (err < 0) 250857b61080SScott Feldman goto errout; 250957b61080SScott Feldman 251057b61080SScott Feldman err = -EOPNOTSUPP; 251157b61080SScott Feldman if (ops->ndo_set_vf_port) 251257b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 251357b61080SScott Feldman if (err < 0) 251457b61080SScott Feldman goto errout; 2515ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 251657b61080SScott Feldman } 2517f8ff182cSThomas Graf 2518f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2519f8ff182cSThomas Graf struct nlattr *af; 2520f8ff182cSThomas Graf int rem; 2521f8ff182cSThomas Graf 2522f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2523f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2524f8ff182cSThomas Graf 25255fa85a09SFlorian Westphal rcu_read_lock(); 25265fa85a09SFlorian Westphal 2527058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2528f8ff182cSThomas Graf 2529cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 25305fa85a09SFlorian Westphal if (err < 0) { 25315fa85a09SFlorian Westphal rcu_read_unlock(); 2532f8ff182cSThomas Graf goto errout; 25335fa85a09SFlorian Westphal } 2534f8ff182cSThomas Graf 25355fa85a09SFlorian Westphal rcu_read_unlock(); 2536ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2537f8ff182cSThomas Graf } 2538f8ff182cSThomas Graf } 253957b61080SScott Feldman err = 0; 254057b61080SScott Feldman 254188d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 254288d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 254388d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 254488d6378bSAnuradha Karuppiah if (err) 254588d6378bSAnuradha Karuppiah goto errout; 254688d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 254788d6378bSAnuradha Karuppiah } 254888d6378bSAnuradha Karuppiah 2549d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2550d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 255185de8576SDaniel Borkmann u32 xdp_flags = 0; 2552d1fdd913SBrenden Blanco 2553d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2554fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2555d1fdd913SBrenden Blanco if (err < 0) 2556d1fdd913SBrenden Blanco goto errout; 2557d1fdd913SBrenden Blanco 255858038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2559262d8625SBrenden Blanco err = -EINVAL; 2560262d8625SBrenden Blanco goto errout; 2561262d8625SBrenden Blanco } 256285de8576SDaniel Borkmann 256385de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 256485de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 256585de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 256685de8576SDaniel Borkmann err = -EINVAL; 256785de8576SDaniel Borkmann goto errout; 256885de8576SDaniel Borkmann } 2569ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 25700489df9aSDaniel Borkmann err = -EINVAL; 25710489df9aSDaniel Borkmann goto errout; 25720489df9aSDaniel Borkmann } 257385de8576SDaniel Borkmann } 257485de8576SDaniel Borkmann 2575d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2576ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 257785de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 257885de8576SDaniel Borkmann xdp_flags); 2579d1fdd913SBrenden Blanco if (err) 2580d1fdd913SBrenden Blanco goto errout; 2581d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2582d1fdd913SBrenden Blanco } 2583d1fdd913SBrenden Blanco } 2584d1fdd913SBrenden Blanco 25850157f60cSPatrick McHardy errout: 2586ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 258764ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2588ba998906SNicolas Dichtel netdev_state_change(dev); 2589ba998906SNicolas Dichtel 2590ba998906SNicolas Dichtel if (err < 0) 2591e87cc472SJoe 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", 2592e87cc472SJoe Perches dev->name); 2593ba998906SNicolas Dichtel } 25940157f60cSPatrick McHardy 25950157f60cSPatrick McHardy return err; 25960157f60cSPatrick McHardy } 25970157f60cSPatrick McHardy 2598c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2599c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2600da5e0494SThomas Graf { 26013b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2602da5e0494SThomas Graf struct ifinfomsg *ifm; 2603da5e0494SThomas Graf struct net_device *dev; 26040157f60cSPatrick McHardy int err; 2605da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 26061da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 26071da177e4SLinus Torvalds 2608c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2609c21ef3e3SDavid Ahern extack); 2610da5e0494SThomas Graf if (err < 0) 2611da5e0494SThomas Graf goto errout; 26121da177e4SLinus Torvalds 26134ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 26144ff66caeSChristian Brauner if (err < 0) 26154ff66caeSChristian Brauner goto errout; 26164ff66caeSChristian Brauner 26175176f91eSThomas Graf if (tb[IFLA_IFNAME]) 26185176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 261978e5b891SPatrick McHardy else 262078e5b891SPatrick McHardy ifname[0] = '\0'; 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds err = -EINVAL; 2623da5e0494SThomas Graf ifm = nlmsg_data(nlh); 262451055be8SPatrick McHardy if (ifm->ifi_index > 0) 2625a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2626da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2627a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2628da5e0494SThomas Graf else 2629da5e0494SThomas Graf goto errout; 26301da177e4SLinus Torvalds 2631da5e0494SThomas Graf if (dev == NULL) { 2632da5e0494SThomas Graf err = -ENODEV; 2633da5e0494SThomas Graf goto errout; 2634da5e0494SThomas Graf } 26351da177e4SLinus Torvalds 2636ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2637da5e0494SThomas Graf errout: 26381da177e4SLinus Torvalds return err; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds 264166400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 264266400d54SWANG Cong { 264366400d54SWANG Cong struct net_device *dev, *aux; 264466400d54SWANG Cong LIST_HEAD(list_kill); 264566400d54SWANG Cong bool found = false; 264666400d54SWANG Cong 264766400d54SWANG Cong if (!group) 264866400d54SWANG Cong return -EPERM; 264966400d54SWANG Cong 265066400d54SWANG Cong for_each_netdev(net, dev) { 265166400d54SWANG Cong if (dev->group == group) { 265266400d54SWANG Cong const struct rtnl_link_ops *ops; 265366400d54SWANG Cong 265466400d54SWANG Cong found = true; 265566400d54SWANG Cong ops = dev->rtnl_link_ops; 265666400d54SWANG Cong if (!ops || !ops->dellink) 265766400d54SWANG Cong return -EOPNOTSUPP; 265866400d54SWANG Cong } 265966400d54SWANG Cong } 266066400d54SWANG Cong 266166400d54SWANG Cong if (!found) 266266400d54SWANG Cong return -ENODEV; 266366400d54SWANG Cong 266466400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 266566400d54SWANG Cong if (dev->group == group) { 266666400d54SWANG Cong const struct rtnl_link_ops *ops; 266766400d54SWANG Cong 266866400d54SWANG Cong ops = dev->rtnl_link_ops; 266966400d54SWANG Cong ops->dellink(dev, &list_kill); 267066400d54SWANG Cong } 267166400d54SWANG Cong } 267266400d54SWANG Cong unregister_netdevice_many(&list_kill); 267366400d54SWANG Cong 267466400d54SWANG Cong return 0; 267566400d54SWANG Cong } 267666400d54SWANG Cong 2677614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2678614732eaSThomas Graf { 2679614732eaSThomas Graf const struct rtnl_link_ops *ops; 2680614732eaSThomas Graf LIST_HEAD(list_kill); 2681614732eaSThomas Graf 2682614732eaSThomas Graf ops = dev->rtnl_link_ops; 2683614732eaSThomas Graf if (!ops || !ops->dellink) 2684614732eaSThomas Graf return -EOPNOTSUPP; 2685614732eaSThomas Graf 2686614732eaSThomas Graf ops->dellink(dev, &list_kill); 2687614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2688614732eaSThomas Graf 2689614732eaSThomas Graf return 0; 2690614732eaSThomas Graf } 2691614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2692614732eaSThomas Graf 2693c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2694c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 269538f7b870SPatrick McHardy { 26963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2697b61ad68aSChristian Brauner struct net *tgt_net = net; 2698b61ad68aSChristian Brauner struct net_device *dev = NULL; 269938f7b870SPatrick McHardy struct ifinfomsg *ifm; 270038f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 270138f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 270238f7b870SPatrick McHardy int err; 2703b61ad68aSChristian Brauner int netnsid = -1; 270438f7b870SPatrick McHardy 2705c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 270638f7b870SPatrick McHardy if (err < 0) 270738f7b870SPatrick McHardy return err; 270838f7b870SPatrick McHardy 27094ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 27104ff66caeSChristian Brauner if (err < 0) 27114ff66caeSChristian Brauner return err; 27124ff66caeSChristian Brauner 271338f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 271438f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 271538f7b870SPatrick McHardy 2716b61ad68aSChristian Brauner if (tb[IFLA_IF_NETNSID]) { 2717b61ad68aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2718b61ad68aSChristian Brauner tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2719b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2720b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2721b61ad68aSChristian Brauner } 2722b61ad68aSChristian Brauner 2723b61ad68aSChristian Brauner err = -EINVAL; 272438f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 272538f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2726b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 272738f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2728b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 272966400d54SWANG Cong else if (tb[IFLA_GROUP]) 2730b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 273138f7b870SPatrick McHardy else 2732b61ad68aSChristian Brauner goto out; 273338f7b870SPatrick McHardy 2734b61ad68aSChristian Brauner if (!dev) { 2735b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2736b61ad68aSChristian Brauner err = -ENODEV; 273738f7b870SPatrick McHardy 2738b61ad68aSChristian Brauner goto out; 2739b61ad68aSChristian Brauner } 2740b61ad68aSChristian Brauner 2741b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2742b61ad68aSChristian Brauner 2743b61ad68aSChristian Brauner out: 2744b61ad68aSChristian Brauner if (netnsid >= 0) 2745b61ad68aSChristian Brauner put_net(tgt_net); 2746b61ad68aSChristian Brauner 2747b61ad68aSChristian Brauner return err; 274838f7b870SPatrick McHardy } 274938f7b870SPatrick McHardy 27503729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 27513729d502SPatrick McHardy { 27523729d502SPatrick McHardy unsigned int old_flags; 27533729d502SPatrick McHardy int err; 27543729d502SPatrick McHardy 27553729d502SPatrick McHardy old_flags = dev->flags; 27563729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 27573729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 27583729d502SPatrick McHardy if (err < 0) 27593729d502SPatrick McHardy return err; 27603729d502SPatrick McHardy } 27613729d502SPatrick McHardy 2762*8d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 2763*8d356b89SRoopa Prabhu __dev_notify_flags(dev, old_flags, 0U); 2764*8d356b89SRoopa Prabhu } else { 27653729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2766a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 2767*8d356b89SRoopa Prabhu } 27683729d502SPatrick McHardy return 0; 27693729d502SPatrick McHardy } 27703729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 27713729d502SPatrick McHardy 2772c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 277378ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 27745517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2775e7199288SPavel Emelianov { 2776e7199288SPavel Emelianov struct net_device *dev; 2777d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2778d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2779e7199288SPavel Emelianov 278076ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 278176ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 278276ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2783d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 278476ff5cc9SJiri Pirko 278576ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 278676ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 278776ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2788d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2789efacb309Sstephen hemminger 27905517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2791c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2792e7199288SPavel Emelianov if (!dev) 2793d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2794e7199288SPavel Emelianov 279581adee47SEric W. Biederman dev_net_set(dev, net); 279681adee47SEric W. Biederman dev->rtnl_link_ops = ops; 27973729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 279881adee47SEric W. Biederman 2799e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2800e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 28012afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2802e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2803e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 28042afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 28052afb9b53SJiri Pirko } 2806e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2807e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2808e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2809e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2810e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2811e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 281293b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2813e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2814e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2815ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2816ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 281746e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 281846e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 281946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2820a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2821e7199288SPavel Emelianov 2822e7199288SPavel Emelianov return dev; 2823e7199288SPavel Emelianov } 2824e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2825e7199288SPavel Emelianov 282690f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 282790f62cf3SEric W. Biederman struct net *net, int group, 2828e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2829ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2830e7ed828fSVlad Dogaru struct nlattr **tb) 2831e7ed828fSVlad Dogaru { 2832d079535dSWANG Cong struct net_device *dev, *aux; 2833e7ed828fSVlad Dogaru int err; 2834e7ed828fSVlad Dogaru 2835d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2836e7ed828fSVlad Dogaru if (dev->group == group) { 2837ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2838e7ed828fSVlad Dogaru if (err < 0) 2839e7ed828fSVlad Dogaru return err; 2840e7ed828fSVlad Dogaru } 2841e7ed828fSVlad Dogaru } 2842e7ed828fSVlad Dogaru 2843e7ed828fSVlad Dogaru return 0; 2844e7ed828fSVlad Dogaru } 2845e7ed828fSVlad Dogaru 2846c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2847c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 284838f7b870SPatrick McHardy { 28493b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 285038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2851ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 285238f7b870SPatrick McHardy struct net_device *dev; 2853ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 285438f7b870SPatrick McHardy struct ifinfomsg *ifm; 285538f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 285638f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 285738f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 285838f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 28595517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 286038f7b870SPatrick McHardy int err; 286138f7b870SPatrick McHardy 286295a5afcaSJohannes Berg #ifdef CONFIG_MODULES 286338f7b870SPatrick McHardy replay: 28648072f085SThomas Graf #endif 2865c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 286638f7b870SPatrick McHardy if (err < 0) 286738f7b870SPatrick McHardy return err; 286838f7b870SPatrick McHardy 28694ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 28704ff66caeSChristian Brauner if (err < 0) 28714ff66caeSChristian Brauner return err; 28724ff66caeSChristian Brauner 287338f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 287438f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 287538f7b870SPatrick McHardy else 287638f7b870SPatrick McHardy ifname[0] = '\0'; 287738f7b870SPatrick McHardy 287838f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 287938f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2880881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 2881e7ed828fSVlad Dogaru else { 2882e7ed828fSVlad Dogaru if (ifname[0]) 2883881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 288438f7b870SPatrick McHardy else 288538f7b870SPatrick McHardy dev = NULL; 2886e7ed828fSVlad Dogaru } 288738f7b870SPatrick McHardy 2888ba7d49b1SJiri Pirko if (dev) { 2889ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 2890ba7d49b1SJiri Pirko if (master_dev) 2891ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 2892ba7d49b1SJiri Pirko } 2893ba7d49b1SJiri Pirko 2894e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2895e0d087afSEric Dumazet if (err < 0) 28961840bb13SThomas Graf return err; 28971840bb13SThomas Graf 289838f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 289938f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2900fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 2901fceb6435SJohannes Berg NULL); 290238f7b870SPatrick McHardy if (err < 0) 290338f7b870SPatrick McHardy return err; 290438f7b870SPatrick McHardy } else 290538f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 290638f7b870SPatrick McHardy 290738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 290838f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 290938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 291038f7b870SPatrick McHardy } else { 291138f7b870SPatrick McHardy kind[0] = '\0'; 291238f7b870SPatrick McHardy ops = NULL; 291338f7b870SPatrick McHardy } 291438f7b870SPatrick McHardy 291538f7b870SPatrick McHardy if (1) { 2916ccf8dbcdSKees Cook struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2917ccf8dbcdSKees Cook struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2918ba7d49b1SJiri Pirko struct nlattr **data = NULL; 2919ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 2920317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 292138f7b870SPatrick McHardy 292238f7b870SPatrick McHardy if (ops) { 2923ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 2924ccf8dbcdSKees Cook return -EINVAL; 2925ccf8dbcdSKees Cook 292638f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 292738f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 292838f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 2929fceb6435SJohannes Berg ops->policy, NULL); 293038f7b870SPatrick McHardy if (err < 0) 293138f7b870SPatrick McHardy return err; 293238f7b870SPatrick McHardy data = attr; 293338f7b870SPatrick McHardy } 293438f7b870SPatrick McHardy if (ops->validate) { 2935a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 293638f7b870SPatrick McHardy if (err < 0) 293738f7b870SPatrick McHardy return err; 293838f7b870SPatrick McHardy } 293938f7b870SPatrick McHardy } 294038f7b870SPatrick McHardy 2941ba7d49b1SJiri Pirko if (m_ops) { 2942af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2943ccf8dbcdSKees Cook return -EINVAL; 2944ccf8dbcdSKees Cook 2945ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 2946ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 2947ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 2948ba7d49b1SJiri Pirko m_ops->slave_maxtype, 2949ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 2950fceb6435SJohannes Berg m_ops->slave_policy, 2951fceb6435SJohannes Berg NULL); 2952ba7d49b1SJiri Pirko if (err < 0) 2953ba7d49b1SJiri Pirko return err; 2954ba7d49b1SJiri Pirko slave_data = slave_attr; 2955ba7d49b1SJiri Pirko } 2956ba7d49b1SJiri Pirko } 2957ba7d49b1SJiri Pirko 295838f7b870SPatrick McHardy if (dev) { 295990c325e3SNicolas Dichtel int status = 0; 296038f7b870SPatrick McHardy 296138f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 296238f7b870SPatrick McHardy return -EEXIST; 296338f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 296438f7b870SPatrick McHardy return -EOPNOTSUPP; 296538f7b870SPatrick McHardy 296638f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 296738f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 296838f7b870SPatrick McHardy !ops->changelink) 296938f7b870SPatrick McHardy return -EOPNOTSUPP; 297038f7b870SPatrick McHardy 2971ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 297238f7b870SPatrick McHardy if (err < 0) 297338f7b870SPatrick McHardy return err; 2974ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 297538f7b870SPatrick McHardy } 297638f7b870SPatrick McHardy 2977ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2978ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 2979ba7d49b1SJiri Pirko return -EOPNOTSUPP; 2980ba7d49b1SJiri Pirko 2981ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 298217dd0ec4SMatthias Schiffer tb, slave_data, 298317dd0ec4SMatthias Schiffer extack); 2984ba7d49b1SJiri Pirko if (err < 0) 2985ba7d49b1SJiri Pirko return err; 2986ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2987ba7d49b1SJiri Pirko } 2988ba7d49b1SJiri Pirko 2989ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 2990ddf9f970SJakub Kicinski status); 299138f7b870SPatrick McHardy } 299238f7b870SPatrick McHardy 2993ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2994ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 299590f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 2996ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 2997ddf9f970SJakub Kicinski ifm, extack, tb); 299838f7b870SPatrick McHardy return -ENODEV; 2999ffa934f1SPatrick McHardy } 300038f7b870SPatrick McHardy 3001160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 300238f7b870SPatrick McHardy return -EOPNOTSUPP; 300338f7b870SPatrick McHardy 300438f7b870SPatrick McHardy if (!ops) { 300595a5afcaSJohannes Berg #ifdef CONFIG_MODULES 300638f7b870SPatrick McHardy if (kind[0]) { 300738f7b870SPatrick McHardy __rtnl_unlock(); 300838f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 300938f7b870SPatrick McHardy rtnl_lock(); 301038f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 301138f7b870SPatrick McHardy if (ops) 301238f7b870SPatrick McHardy goto replay; 301338f7b870SPatrick McHardy } 301438f7b870SPatrick McHardy #endif 301538f7b870SPatrick McHardy return -EOPNOTSUPP; 301638f7b870SPatrick McHardy } 301738f7b870SPatrick McHardy 3018b0ab2fabSJiri Pirko if (!ops->setup) 3019b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3020b0ab2fabSJiri Pirko 30215517750fSTom Gundersen if (!ifname[0]) { 302238f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 30235517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 30245517750fSTom Gundersen } 302538f7b870SPatrick McHardy 30265bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 302713ad1774SEric W. Biederman if (IS_ERR(dest_net)) 302813ad1774SEric W. Biederman return PTR_ERR(dest_net); 302913ad1774SEric W. Biederman 3030317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3031317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3032317f4810SNicolas Dichtel 3033317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3034317f4810SNicolas Dichtel if (!link_net) { 3035317f4810SNicolas Dichtel err = -EINVAL; 3036317f4810SNicolas Dichtel goto out; 3037317f4810SNicolas Dichtel } 303806615bedSEric W. Biederman err = -EPERM; 303906615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 304006615bedSEric W. Biederman goto out; 3041317f4810SNicolas Dichtel } 3042317f4810SNicolas Dichtel 3043317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3044317f4810SNicolas Dichtel name_assign_type, ops, tb); 30459c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3046e7199288SPavel Emelianov err = PTR_ERR(dev); 30479c7dafbfSPavel Emelyanov goto out; 30489c7dafbfSPavel Emelyanov } 30499c7dafbfSPavel Emelyanov 30509c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 30519c7dafbfSPavel Emelyanov 30520e0eee24SCong Wang if (ops->newlink) { 30537a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 30547a3f4a18SMatthias Schiffer extack); 30550e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3056e51fb152SCong Wang * and unregister it on failure after registration 3057e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 30580e0eee24SCong Wang */ 3059e51fb152SCong Wang if (err < 0) { 3060e51fb152SCong Wang /* If device is not registered at all, free it now */ 3061e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3062e51fb152SCong Wang free_netdev(dev); 30630e0eee24SCong Wang goto out; 3064e51fb152SCong Wang } 30650e0eee24SCong Wang } else { 30662d85cba2SPatrick McHardy err = register_netdevice(dev); 3067fce9b9beSDan Carpenter if (err < 0) { 306838f7b870SPatrick McHardy free_netdev(dev); 30693729d502SPatrick McHardy goto out; 3070fce9b9beSDan Carpenter } 30710e0eee24SCong Wang } 30723729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 307343638900SDavid S. Miller if (err < 0) 307443638900SDavid S. Miller goto out_unregister; 307543638900SDavid S. Miller if (link_net) { 307643638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 307743638900SDavid S. Miller if (err < 0) 307843638900SDavid S. Miller goto out_unregister; 307943638900SDavid S. Miller } 3080160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 308133eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 308233eaf2a6SDavid Ahern extack); 3083160ca014STheuns Verwoerd if (err) 3084160ca014STheuns Verwoerd goto out_unregister; 3085160ca014STheuns Verwoerd } 308643638900SDavid S. Miller out: 308743638900SDavid S. Miller if (link_net) 308843638900SDavid S. Miller put_net(link_net); 308943638900SDavid S. Miller put_net(dest_net); 309043638900SDavid S. Miller return err; 309143638900SDavid S. Miller out_unregister: 30927afb8886SWANG Cong if (ops->newlink) { 30937afb8886SWANG Cong LIST_HEAD(list_kill); 30947afb8886SWANG Cong 30957afb8886SWANG Cong ops->dellink(dev, &list_kill); 30967afb8886SWANG Cong unregister_netdevice_many(&list_kill); 30977afb8886SWANG Cong } else { 30983729d502SPatrick McHardy unregister_netdevice(dev); 30997afb8886SWANG Cong } 3100317f4810SNicolas Dichtel goto out; 3101317f4810SNicolas Dichtel } 310238f7b870SPatrick McHardy } 310338f7b870SPatrick McHardy 3104c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3105c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3106711e2c33SJean Tourrilhes { 31073b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 310879e1ad14SJiri Benc struct net *tgt_net = net; 3109b60c5115SThomas Graf struct ifinfomsg *ifm; 3110a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3111b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3112b60c5115SThomas Graf struct net_device *dev = NULL; 3113b60c5115SThomas Graf struct sk_buff *nskb; 311479e1ad14SJiri Benc int netnsid = -1; 3115339bf98fSThomas Graf int err; 3116115c9b81SGreg Rose u32 ext_filter_mask = 0; 3117711e2c33SJean Tourrilhes 3118c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3119b60c5115SThomas Graf if (err < 0) 31209918f230SEric Sesterhenn return err; 3121b60c5115SThomas Graf 31224ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31234ff66caeSChristian Brauner if (err < 0) 31244ff66caeSChristian Brauner return err; 31254ff66caeSChristian Brauner 312679e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 312779e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3128f428fe4aSAndrei Vagin tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 312979e1ad14SJiri Benc if (IS_ERR(tgt_net)) 313079e1ad14SJiri Benc return PTR_ERR(tgt_net); 313179e1ad14SJiri Benc } 313279e1ad14SJiri Benc 3133a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3134a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3135a3d12891SEric Dumazet 3136115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3137115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3138115c9b81SGreg Rose 313979e1ad14SJiri Benc err = -EINVAL; 3140b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3141a3d12891SEric Dumazet if (ifm->ifi_index > 0) 314279e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3143a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 314479e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3145a3d12891SEric Dumazet else 314679e1ad14SJiri Benc goto out; 3147b60c5115SThomas Graf 314879e1ad14SJiri Benc err = -ENODEV; 3149a3d12891SEric Dumazet if (dev == NULL) 315079e1ad14SJiri Benc goto out; 3151a3d12891SEric Dumazet 315279e1ad14SJiri Benc err = -ENOBUFS; 3153115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3154a3d12891SEric Dumazet if (nskb == NULL) 315579e1ad14SJiri Benc goto out; 3156711e2c33SJean Tourrilhes 315779e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 315879e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 315979e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 316038e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 316126932566SPatrick McHardy if (err < 0) { 316226932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 316326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 316426932566SPatrick McHardy kfree_skb(nskb); 3165a3d12891SEric Dumazet } else 316615e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 316779e1ad14SJiri Benc out: 316879e1ad14SJiri Benc if (netnsid >= 0) 316979e1ad14SJiri Benc put_net(tgt_net); 3170b60c5115SThomas Graf 3171711e2c33SJean Tourrilhes return err; 3172711e2c33SJean Tourrilhes } 3173711e2c33SJean Tourrilhes 3174115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3175c7ac8679SGreg Rose { 3176115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3177115c9b81SGreg Rose struct net_device *dev; 3178115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3179115c9b81SGreg Rose u32 ext_filter_mask = 0; 3180115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3181e5eca6d4SMichal Schmidt int hdrlen; 3182115c9b81SGreg Rose 3183e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3184e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3185e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3186e5eca6d4SMichal Schmidt 3187fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3188115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3189115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3190a4b64fbeSEric Dumazet } 3191115c9b81SGreg Rose 3192115c9b81SGreg Rose if (!ext_filter_mask) 3193115c9b81SGreg Rose return NLMSG_GOODSIZE; 3194115c9b81SGreg Rose /* 3195115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3196115c9b81SGreg Rose * buffer size based upon the filter mask. 3197115c9b81SGreg Rose */ 31986853dd48SFlorian Westphal rcu_read_lock(); 31996853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3200115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3201115c9b81SGreg Rose if_nlmsg_size(dev, 3202115c9b81SGreg Rose ext_filter_mask)); 3203115c9b81SGreg Rose } 32046853dd48SFlorian Westphal rcu_read_unlock(); 3205115c9b81SGreg Rose 320693af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3207c7ac8679SGreg Rose } 3208c7ac8679SGreg Rose 320942bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 32101da177e4SLinus Torvalds { 32111da177e4SLinus Torvalds int idx; 32121da177e4SLinus Torvalds int s_idx = cb->family; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds if (s_idx == 0) 32151da177e4SLinus Torvalds s_idx = 1; 32166853dd48SFlorian Westphal 321725239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3218addf9b90SFlorian Westphal struct rtnl_link **tab; 32191da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 3220addf9b90SFlorian Westphal struct rtnl_link *link; 32216853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 32226853dd48SFlorian Westphal 32231da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 32241da177e4SLinus Torvalds continue; 32256853dd48SFlorian Westphal 3226addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 32271da177e4SLinus Torvalds continue; 32286853dd48SFlorian Westphal 3229addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3230addf9b90SFlorian Westphal if (!tab) 3231addf9b90SFlorian Westphal continue; 3232addf9b90SFlorian Westphal 3233addf9b90SFlorian Westphal link = tab[type]; 3234addf9b90SFlorian Westphal if (!link) 3235addf9b90SFlorian Westphal continue; 3236addf9b90SFlorian Westphal 3237addf9b90SFlorian Westphal dumpit = link->dumpit; 32386853dd48SFlorian Westphal if (!dumpit) 32396853dd48SFlorian Westphal continue; 32406853dd48SFlorian Westphal 32410465277fSNicolas Dichtel if (idx > s_idx) { 32421da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 32430465277fSNicolas Dichtel cb->prev_seq = 0; 32440465277fSNicolas Dichtel cb->seq = 0; 32450465277fSNicolas Dichtel } 32466853dd48SFlorian Westphal if (dumpit(skb, cb)) 32471da177e4SLinus Torvalds break; 32481da177e4SLinus Torvalds } 32491da177e4SLinus Torvalds cb->family = idx; 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds return skb->len; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 3254395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 32553d3ea5afSVlad Yasevich unsigned int change, 325638e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 325738e01b30SNicolas Dichtel int new_ifindex) 32581da177e4SLinus Torvalds { 3259c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 32601da177e4SLinus Torvalds struct sk_buff *skb; 32610ec6d3f4SThomas Graf int err = -ENOBUFS; 3262c7ac8679SGreg Rose size_t if_info_size; 32631da177e4SLinus Torvalds 32647f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 32650ec6d3f4SThomas Graf if (skb == NULL) 32660ec6d3f4SThomas Graf goto errout; 32671da177e4SLinus Torvalds 326879e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 326979e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 327038e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 327126932566SPatrick McHardy if (err < 0) { 327226932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 327326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 327426932566SPatrick McHardy kfree_skb(skb); 327526932566SPatrick McHardy goto errout; 327626932566SPatrick McHardy } 3277395eea6cSMahesh Bandewar return skb; 32780ec6d3f4SThomas Graf errout: 32790ec6d3f4SThomas Graf if (err < 0) 32804b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3281395eea6cSMahesh Bandewar return NULL; 3282395eea6cSMahesh Bandewar } 3283395eea6cSMahesh Bandewar 3284395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3285395eea6cSMahesh Bandewar { 3286395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3287395eea6cSMahesh Bandewar 3288395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3289395eea6cSMahesh Bandewar } 3290395eea6cSMahesh Bandewar 32913d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 32923d3ea5afSVlad Yasevich unsigned int change, u32 event, 329338e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3294395eea6cSMahesh Bandewar { 3295395eea6cSMahesh Bandewar struct sk_buff *skb; 3296395eea6cSMahesh Bandewar 3297ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3298ed2a80abSNicolas Dichtel return; 3299ed2a80abSNicolas Dichtel 330038e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 330138e01b30SNicolas Dichtel new_ifindex); 3302395eea6cSMahesh Bandewar if (skb) 3303395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 33041da177e4SLinus Torvalds } 33053d3ea5afSVlad Yasevich 33063d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 33073d3ea5afSVlad Yasevich gfp_t flags) 33083d3ea5afSVlad Yasevich { 330938e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 331038e01b30SNicolas Dichtel NULL, 0); 33113d3ea5afSVlad Yasevich } 33121da177e4SLinus Torvalds 33136621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 331438e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 33156621dd29SNicolas Dichtel { 33166621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 331738e01b30SNicolas Dichtel new_nsid, new_ifindex); 33186621dd29SNicolas Dichtel } 33196621dd29SNicolas Dichtel 3320d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3321d83b0603SJohn Fastabend struct net_device *dev, 33221e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 33231c104a6bSNicolas Dichtel int type, unsigned int flags, 3324b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3325d83b0603SJohn Fastabend { 3326d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3327d83b0603SJohn Fastabend struct ndmsg *ndm; 3328d83b0603SJohn Fastabend 33291c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3330d83b0603SJohn Fastabend if (!nlh) 3331d83b0603SJohn Fastabend return -EMSGSIZE; 3332d83b0603SJohn Fastabend 3333d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3334d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3335d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3336d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3337d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3338d83b0603SJohn Fastabend ndm->ndm_type = 0; 3339d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3340b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3341d83b0603SJohn Fastabend 3342d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3343d83b0603SJohn Fastabend goto nla_put_failure; 33441e53d5bbSHubert Sokolowski if (vid) 33451e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 33461e53d5bbSHubert Sokolowski goto nla_put_failure; 3347d83b0603SJohn Fastabend 3348053c095aSJohannes Berg nlmsg_end(skb, nlh); 3349053c095aSJohannes Berg return 0; 3350d83b0603SJohn Fastabend 3351d83b0603SJohn Fastabend nla_put_failure: 3352d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3353d83b0603SJohn Fastabend return -EMSGSIZE; 3354d83b0603SJohn Fastabend } 3355d83b0603SJohn Fastabend 33563ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 33573ff661c3SJohn Fastabend { 3358f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3359f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3360f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3361f82ef3e1SSabrina Dubroca 0; 33623ff661c3SJohn Fastabend } 33633ff661c3SJohn Fastabend 3364b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3365b3379041SHubert Sokolowski u16 ndm_state) 33663ff661c3SJohn Fastabend { 33673ff661c3SJohn Fastabend struct net *net = dev_net(dev); 33683ff661c3SJohn Fastabend struct sk_buff *skb; 33693ff661c3SJohn Fastabend int err = -ENOBUFS; 33703ff661c3SJohn Fastabend 33713ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 33723ff661c3SJohn Fastabend if (!skb) 33733ff661c3SJohn Fastabend goto errout; 33743ff661c3SJohn Fastabend 33751e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3376b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 33773ff661c3SJohn Fastabend if (err < 0) { 33783ff661c3SJohn Fastabend kfree_skb(skb); 33793ff661c3SJohn Fastabend goto errout; 33803ff661c3SJohn Fastabend } 33813ff661c3SJohn Fastabend 33823ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 33833ff661c3SJohn Fastabend return; 33843ff661c3SJohn Fastabend errout: 33853ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 33863ff661c3SJohn Fastabend } 33873ff661c3SJohn Fastabend 3388090096bfSVlad Yasevich /** 3389090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3390090096bfSVlad Yasevich */ 3391090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3392090096bfSVlad Yasevich struct nlattr *tb[], 3393090096bfSVlad Yasevich struct net_device *dev, 3394f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3395090096bfSVlad Yasevich u16 flags) 3396090096bfSVlad Yasevich { 3397090096bfSVlad Yasevich int err = -EINVAL; 3398090096bfSVlad Yasevich 3399090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3400090096bfSVlad Yasevich * implement its own handler for this. 3401090096bfSVlad Yasevich */ 3402090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3403090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3404090096bfSVlad Yasevich return err; 3405090096bfSVlad Yasevich } 3406090096bfSVlad Yasevich 340765891feaSOr Gerlitz if (vid) { 340865891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 340965891feaSOr Gerlitz return err; 341065891feaSOr Gerlitz } 341165891feaSOr Gerlitz 3412090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3413090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3414090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3415090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3416090096bfSVlad Yasevich 3417090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3418090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3419090096bfSVlad Yasevich err = 0; 3420090096bfSVlad Yasevich 3421090096bfSVlad Yasevich return err; 3422090096bfSVlad Yasevich } 3423090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3424090096bfSVlad Yasevich 3425b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3426b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3427f6f6424bSJiri Pirko { 3428f6f6424bSJiri Pirko u16 vid = 0; 3429f6f6424bSJiri Pirko 3430f6f6424bSJiri Pirko if (vlan_attr) { 3431f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3432b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3433f6f6424bSJiri Pirko return -EINVAL; 3434f6f6424bSJiri Pirko } 3435f6f6424bSJiri Pirko 3436f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3437f6f6424bSJiri Pirko 3438f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3439b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3440f6f6424bSJiri Pirko return -EINVAL; 3441f6f6424bSJiri Pirko } 3442f6f6424bSJiri Pirko } 3443f6f6424bSJiri Pirko *p_vid = vid; 3444f6f6424bSJiri Pirko return 0; 3445f6f6424bSJiri Pirko } 3446f6f6424bSJiri Pirko 3447c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3448c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 344977162022SJohn Fastabend { 345077162022SJohn Fastabend struct net *net = sock_net(skb->sk); 345177162022SJohn Fastabend struct ndmsg *ndm; 345277162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 345377162022SJohn Fastabend struct net_device *dev; 345477162022SJohn Fastabend u8 *addr; 3455f6f6424bSJiri Pirko u16 vid; 345677162022SJohn Fastabend int err; 345777162022SJohn Fastabend 3458c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 345977162022SJohn Fastabend if (err < 0) 346077162022SJohn Fastabend return err; 346177162022SJohn Fastabend 346277162022SJohn Fastabend ndm = nlmsg_data(nlh); 346377162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3464b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 346577162022SJohn Fastabend return -EINVAL; 346677162022SJohn Fastabend } 346777162022SJohn Fastabend 346877162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 346977162022SJohn Fastabend if (dev == NULL) { 3470b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 347177162022SJohn Fastabend return -ENODEV; 347277162022SJohn Fastabend } 347377162022SJohn Fastabend 347477162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3475b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 347677162022SJohn Fastabend return -EINVAL; 347777162022SJohn Fastabend } 347877162022SJohn Fastabend 347977162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 348077162022SJohn Fastabend 3481b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3482f6f6424bSJiri Pirko if (err) 3483f6f6424bSJiri Pirko return err; 3484f6f6424bSJiri Pirko 348577162022SJohn Fastabend err = -EOPNOTSUPP; 348677162022SJohn Fastabend 348777162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 348877162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 348977162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3490898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3491898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3492898e5061SJiri Pirko 3493f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3494f6f6424bSJiri Pirko nlh->nlmsg_flags); 349577162022SJohn Fastabend if (err) 349677162022SJohn Fastabend goto out; 349777162022SJohn Fastabend else 349877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 349977162022SJohn Fastabend } 350077162022SJohn Fastabend 350177162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3502090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3503090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3504090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3505f6f6424bSJiri Pirko vid, 3506090096bfSVlad Yasevich nlh->nlmsg_flags); 3507090096bfSVlad Yasevich else 3508f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 350977162022SJohn Fastabend nlh->nlmsg_flags); 351077162022SJohn Fastabend 35113ff661c3SJohn Fastabend if (!err) { 3512b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3513b3379041SHubert Sokolowski ndm->ndm_state); 351477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 351577162022SJohn Fastabend } 35163ff661c3SJohn Fastabend } 351777162022SJohn Fastabend out: 351877162022SJohn Fastabend return err; 351977162022SJohn Fastabend } 352077162022SJohn Fastabend 3521090096bfSVlad Yasevich /** 3522090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3523090096bfSVlad Yasevich */ 3524090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3525090096bfSVlad Yasevich struct nlattr *tb[], 3526090096bfSVlad Yasevich struct net_device *dev, 3527f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3528090096bfSVlad Yasevich { 3529c8a89c4aSAlexander Duyck int err = -EINVAL; 3530090096bfSVlad Yasevich 3531090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3532090096bfSVlad Yasevich * implement its own handler for this. 3533090096bfSVlad Yasevich */ 353464535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3535090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3536c8a89c4aSAlexander Duyck return err; 3537090096bfSVlad Yasevich } 3538090096bfSVlad Yasevich 3539090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3540090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3541090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3542090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3543090096bfSVlad Yasevich 3544090096bfSVlad Yasevich return err; 3545090096bfSVlad Yasevich } 3546090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3547090096bfSVlad Yasevich 3548c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3549c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 355077162022SJohn Fastabend { 355177162022SJohn Fastabend struct net *net = sock_net(skb->sk); 355277162022SJohn Fastabend struct ndmsg *ndm; 35531690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 355477162022SJohn Fastabend struct net_device *dev; 355577162022SJohn Fastabend int err = -EINVAL; 355677162022SJohn Fastabend __u8 *addr; 3557f6f6424bSJiri Pirko u16 vid; 355877162022SJohn Fastabend 355990f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 35601690be63SVlad Yasevich return -EPERM; 35611690be63SVlad Yasevich 3562c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 35631690be63SVlad Yasevich if (err < 0) 35641690be63SVlad Yasevich return err; 356577162022SJohn Fastabend 356677162022SJohn Fastabend ndm = nlmsg_data(nlh); 356777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3568b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 356977162022SJohn Fastabend return -EINVAL; 357077162022SJohn Fastabend } 357177162022SJohn Fastabend 357277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 357377162022SJohn Fastabend if (dev == NULL) { 3574b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 357577162022SJohn Fastabend return -ENODEV; 357677162022SJohn Fastabend } 357777162022SJohn Fastabend 35781690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3579b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 358077162022SJohn Fastabend return -EINVAL; 358177162022SJohn Fastabend } 358277162022SJohn Fastabend 35831690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 35841690be63SVlad Yasevich 3585b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3586f6f6424bSJiri Pirko if (err) 3587f6f6424bSJiri Pirko return err; 3588f6f6424bSJiri Pirko 358977162022SJohn Fastabend err = -EOPNOTSUPP; 359077162022SJohn Fastabend 359177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 359277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 359377162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3594898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3595898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 359677162022SJohn Fastabend 3597898e5061SJiri Pirko if (ops->ndo_fdb_del) 3598f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 359977162022SJohn Fastabend 360077162022SJohn Fastabend if (err) 360177162022SJohn Fastabend goto out; 360277162022SJohn Fastabend else 360377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 360477162022SJohn Fastabend } 360577162022SJohn Fastabend 360677162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3607090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3608090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3609f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3610f6f6424bSJiri Pirko vid); 3611090096bfSVlad Yasevich else 3612f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 361377162022SJohn Fastabend 36143ff661c3SJohn Fastabend if (!err) { 3615b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3616b3379041SHubert Sokolowski ndm->ndm_state); 361777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 361877162022SJohn Fastabend } 36193ff661c3SJohn Fastabend } 362077162022SJohn Fastabend out: 362177162022SJohn Fastabend return err; 362277162022SJohn Fastabend } 362377162022SJohn Fastabend 3624d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3625d83b0603SJohn Fastabend struct netlink_callback *cb, 3626d83b0603SJohn Fastabend struct net_device *dev, 3627d83b0603SJohn Fastabend int *idx, 3628d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3629d83b0603SJohn Fastabend { 3630d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3631d83b0603SJohn Fastabend int err; 363215e47304SEric W. Biederman u32 portid, seq; 3633d83b0603SJohn Fastabend 363415e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3635d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3636d83b0603SJohn Fastabend 3637d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3638d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3639d83b0603SJohn Fastabend goto skip; 3640d83b0603SJohn Fastabend 36411e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3642a7a558feSJohn Fastabend portid, seq, 36431c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3644b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3645d83b0603SJohn Fastabend if (err < 0) 3646d83b0603SJohn Fastabend return err; 3647d83b0603SJohn Fastabend skip: 3648d83b0603SJohn Fastabend *idx += 1; 3649d83b0603SJohn Fastabend } 3650d83b0603SJohn Fastabend return 0; 3651d83b0603SJohn Fastabend } 3652d83b0603SJohn Fastabend 3653d83b0603SJohn Fastabend /** 36542c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3655d83b0603SJohn Fastabend * @nlh: netlink message header 3656d83b0603SJohn Fastabend * @dev: netdevice 3657d83b0603SJohn Fastabend * 3658d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 365991f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3660d83b0603SJohn Fastabend */ 3661d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3662d83b0603SJohn Fastabend struct netlink_callback *cb, 3663d83b0603SJohn Fastabend struct net_device *dev, 36645d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3665d297653dSRoopa Prabhu int *idx) 3666d83b0603SJohn Fastabend { 3667d83b0603SJohn Fastabend int err; 3668d83b0603SJohn Fastabend 3669d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3670d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3671d83b0603SJohn Fastabend if (err) 3672d83b0603SJohn Fastabend goto out; 36732934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3674d83b0603SJohn Fastabend out: 3675d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3676d297653dSRoopa Prabhu return err; 3677d83b0603SJohn Fastabend } 3678d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3679d83b0603SJohn Fastabend 368077162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 368177162022SJohn Fastabend { 368277162022SJohn Fastabend struct net_device *dev; 36835e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 36845e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 36855e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 36865e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 36875e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 36885e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3689d297653dSRoopa Prabhu struct hlist_head *head; 36905e6d2435SJamal Hadi Salim int brport_idx = 0; 36915e6d2435SJamal Hadi Salim int br_idx = 0; 3692d297653dSRoopa Prabhu int h, s_h; 3693d297653dSRoopa Prabhu int idx = 0, s_idx; 3694d297653dSRoopa Prabhu int err = 0; 3695d297653dSRoopa Prabhu int fidx = 0; 369677162022SJohn Fastabend 36970ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 36980ff50e83SAlexander Potapenko IFLA_MAX, ifla_policy, NULL); 36990ff50e83SAlexander Potapenko if (err < 0) { 37000ff50e83SAlexander Potapenko return -EINVAL; 37010ff50e83SAlexander Potapenko } else if (err == 0) { 37025e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 37035e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 37045e6d2435SJamal Hadi Salim } 370577162022SJohn Fastabend 37065e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 37075e6d2435SJamal Hadi Salim 37085e6d2435SJamal Hadi Salim if (br_idx) { 37095e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 37105e6d2435SJamal Hadi Salim if (!br_dev) 37115e6d2435SJamal Hadi Salim return -ENODEV; 37125e6d2435SJamal Hadi Salim 3713898e5061SJiri Pirko ops = br_dev->netdev_ops; 37145e6d2435SJamal Hadi Salim } 37155e6d2435SJamal Hadi Salim 3716d297653dSRoopa Prabhu s_h = cb->args[0]; 3717d297653dSRoopa Prabhu s_idx = cb->args[1]; 3718d297653dSRoopa Prabhu 3719d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3720d297653dSRoopa Prabhu idx = 0; 3721d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3722d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3723d297653dSRoopa Prabhu 37245e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 37255e6d2435SJamal Hadi Salim continue; 37265e6d2435SJamal Hadi Salim 37275e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 37285e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 37295e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 37305e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 37315e6d2435SJamal Hadi Salim } 37325e6d2435SJamal Hadi Salim } else { 37335e6d2435SJamal Hadi Salim if (dev != br_dev && 37345e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 37355e6d2435SJamal Hadi Salim continue; 37365e6d2435SJamal Hadi Salim 37375e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 37385e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 37395e6d2435SJamal Hadi Salim continue; 37405e6d2435SJamal Hadi Salim cops = ops; 37415e6d2435SJamal Hadi Salim } 37425e6d2435SJamal Hadi Salim 3743d297653dSRoopa Prabhu if (idx < s_idx) 3744d297653dSRoopa Prabhu goto cont; 3745d297653dSRoopa Prabhu 37465e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3747d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3748d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3749d297653dSRoopa Prabhu br_dev, dev, 3750d297653dSRoopa Prabhu &fidx); 3751d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3752d297653dSRoopa Prabhu goto out; 375377162022SJohn Fastabend } 3754d297653dSRoopa Prabhu } 375577162022SJohn Fastabend 37565e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3757d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3758d297653dSRoopa Prabhu dev, NULL, 3759d297653dSRoopa Prabhu &fidx); 37606cb69742SHubert Sokolowski else 3761d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3762d297653dSRoopa Prabhu &fidx); 3763d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3764d297653dSRoopa Prabhu goto out; 37655e6d2435SJamal Hadi Salim 37665e6d2435SJamal Hadi Salim cops = NULL; 3767d297653dSRoopa Prabhu 3768d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3769d297653dSRoopa Prabhu cb->args[2] = 0; 3770d297653dSRoopa Prabhu fidx = 0; 3771d297653dSRoopa Prabhu cont: 3772d297653dSRoopa Prabhu idx++; 3773d297653dSRoopa Prabhu } 377477162022SJohn Fastabend } 377577162022SJohn Fastabend 3776d297653dSRoopa Prabhu out: 3777d297653dSRoopa Prabhu cb->args[0] = h; 3778d297653dSRoopa Prabhu cb->args[1] = idx; 3779d297653dSRoopa Prabhu cb->args[2] = fidx; 3780d297653dSRoopa Prabhu 378177162022SJohn Fastabend return skb->len; 378277162022SJohn Fastabend } 378377162022SJohn Fastabend 37842c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 37852c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 37862c3c031cSScott Feldman { 37872c3c031cSScott Feldman if (mask & flag) 37882c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 37892c3c031cSScott Feldman return 0; 37902c3c031cSScott Feldman } 37912c3c031cSScott Feldman 3792815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 37932c3c031cSScott Feldman struct net_device *dev, u16 mode, 37947d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 37957d4f8d87SScott Feldman u32 filter_mask, 37967d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 37977d4f8d87SScott Feldman struct net_device *dev, 37987d4f8d87SScott Feldman u32 filter_mask)) 3799815cccbfSJohn Fastabend { 3800815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3801815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3802815cccbfSJohn Fastabend struct nlattr *br_afspec; 38032c3c031cSScott Feldman struct nlattr *protinfo; 3804815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3805898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 38067d4f8d87SScott Feldman int err = 0; 3807815cccbfSJohn Fastabend 380846c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3809815cccbfSJohn Fastabend if (nlh == NULL) 3810815cccbfSJohn Fastabend return -EMSGSIZE; 3811815cccbfSJohn Fastabend 3812815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3813815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3814815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3815815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3816815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3817815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3818815cccbfSJohn Fastabend ifm->ifi_change = 0; 3819815cccbfSJohn Fastabend 3820815cccbfSJohn Fastabend 3821815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3822815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3823815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3824898e5061SJiri Pirko (br_dev && 3825898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3826815cccbfSJohn Fastabend (dev->addr_len && 3827815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3828a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3829a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3830815cccbfSJohn Fastabend goto nla_put_failure; 3831815cccbfSJohn Fastabend 3832815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3833815cccbfSJohn Fastabend if (!br_afspec) 3834815cccbfSJohn Fastabend goto nla_put_failure; 3835815cccbfSJohn Fastabend 38361d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3837815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3838815cccbfSJohn Fastabend goto nla_put_failure; 3839815cccbfSJohn Fastabend } 38401d460b98SRoopa Prabhu 38411d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 38421d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 38431d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 38441d460b98SRoopa Prabhu goto nla_put_failure; 38451d460b98SRoopa Prabhu } 38461d460b98SRoopa Prabhu } 38477d4f8d87SScott Feldman if (vlan_fill) { 38487d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 38497d4f8d87SScott Feldman if (err) { 38507d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 38517d4f8d87SScott Feldman goto nla_put_failure; 38527d4f8d87SScott Feldman } 38537d4f8d87SScott Feldman } 3854815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3855815cccbfSJohn Fastabend 38562c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 38572c3c031cSScott Feldman if (!protinfo) 38582c3c031cSScott Feldman goto nla_put_failure; 38592c3c031cSScott Feldman 38602c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 38612c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 38622c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38632c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 38642c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38652c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 38662c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 38672c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38682c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 38692c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38702c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 38712c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38722c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 38732c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38742c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 38752c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38762c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 38772c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 38782c3c031cSScott Feldman goto nla_put_failure; 38792c3c031cSScott Feldman } 38802c3c031cSScott Feldman 38812c3c031cSScott Feldman nla_nest_end(skb, protinfo); 38822c3c031cSScott Feldman 3883053c095aSJohannes Berg nlmsg_end(skb, nlh); 3884053c095aSJohannes Berg return 0; 3885815cccbfSJohn Fastabend nla_put_failure: 3886815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 38877d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 3888815cccbfSJohn Fastabend } 38897d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3890815cccbfSJohn Fastabend 3891e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3892e5a55a89SJohn Fastabend { 3893e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3894e5a55a89SJohn Fastabend struct net_device *dev; 3895e5a55a89SJohn Fastabend int idx = 0; 3896e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 3897e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 38986cbdceebSVlad Yasevich u32 filter_mask = 0; 3899d64f69b0SRoopa Prabhu int err; 39006cbdceebSVlad Yasevich 3901aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3902aa68c20fSThomas Graf struct nlattr *extfilt; 3903aa68c20fSThomas Graf 39043e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 39056cbdceebSVlad Yasevich IFLA_EXT_MASK); 3906aa68c20fSThomas Graf if (extfilt) { 3907aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 3908aa68c20fSThomas Graf return -EINVAL; 3909aa68c20fSThomas Graf 39106cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 3911aa68c20fSThomas Graf } 3912aa68c20fSThomas Graf } 3913e5a55a89SJohn Fastabend 3914e5a55a89SJohn Fastabend rcu_read_lock(); 3915e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 3916e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 3917898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3918e5a55a89SJohn Fastabend 3919898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3920d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3921d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 3922d64f69b0SRoopa Prabhu skb, portid, seq, dev, 3923d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 3924f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3925f6c5775fSDavid Ahern if (likely(skb->len)) 3926e5a55a89SJohn Fastabend break; 3927f6c5775fSDavid Ahern 3928f6c5775fSDavid Ahern goto out_err; 3929f6c5775fSDavid Ahern } 3930d64f69b0SRoopa Prabhu } 3931e5a55a89SJohn Fastabend idx++; 3932e5a55a89SJohn Fastabend } 3933e5a55a89SJohn Fastabend 3934e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 3935d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3936d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 3937d64f69b0SRoopa Prabhu seq, dev, 393846c264daSNicolas Dichtel filter_mask, 3939d64f69b0SRoopa Prabhu NLM_F_MULTI); 3940f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3941f6c5775fSDavid Ahern if (likely(skb->len)) 3942e5a55a89SJohn Fastabend break; 3943f6c5775fSDavid Ahern 3944f6c5775fSDavid Ahern goto out_err; 3945f6c5775fSDavid Ahern } 3946d64f69b0SRoopa Prabhu } 3947e5a55a89SJohn Fastabend idx++; 3948e5a55a89SJohn Fastabend } 3949e5a55a89SJohn Fastabend } 3950f6c5775fSDavid Ahern err = skb->len; 3951f6c5775fSDavid Ahern out_err: 3952e5a55a89SJohn Fastabend rcu_read_unlock(); 3953e5a55a89SJohn Fastabend cb->args[0] = idx; 3954e5a55a89SJohn Fastabend 3955f6c5775fSDavid Ahern return err; 3956e5a55a89SJohn Fastabend } 3957e5a55a89SJohn Fastabend 39582469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 39592469ffd7SJohn Fastabend { 39602469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 39612469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 39622469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 39632469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 39642469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 39652469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 39662469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 39672469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 39682469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 39692469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 39702469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 39712469ffd7SJohn Fastabend } 39722469ffd7SJohn Fastabend 397302dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 39742469ffd7SJohn Fastabend { 39752469ffd7SJohn Fastabend struct net *net = dev_net(dev); 39762469ffd7SJohn Fastabend struct sk_buff *skb; 39772469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 39782469ffd7SJohn Fastabend 397902dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 398002dba438SRoopa Prabhu return 0; 398102dba438SRoopa Prabhu 39822469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 39832469ffd7SJohn Fastabend if (!skb) { 39842469ffd7SJohn Fastabend err = -ENOMEM; 39852469ffd7SJohn Fastabend goto errout; 39862469ffd7SJohn Fastabend } 39872469ffd7SJohn Fastabend 398846c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 3989c38e01b8SJohn Fastabend if (err < 0) 3990c38e01b8SJohn Fastabend goto errout; 39912469ffd7SJohn Fastabend 399259ccaaaaSRoopa Prabhu if (!skb->len) 399359ccaaaaSRoopa Prabhu goto errout; 399459ccaaaaSRoopa Prabhu 39952469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 39962469ffd7SJohn Fastabend return 0; 39972469ffd7SJohn Fastabend errout: 39982469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 39992469ffd7SJohn Fastabend kfree_skb(skb); 400059ccaaaaSRoopa Prabhu if (err) 40012469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 40022469ffd7SJohn Fastabend return err; 40032469ffd7SJohn Fastabend } 40042469ffd7SJohn Fastabend 4005c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4006c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4007e5a55a89SJohn Fastabend { 4008e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4009e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4010e5a55a89SJohn Fastabend struct net_device *dev; 40112469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 40122469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 40134de8b413SRosen, Rami u16 flags = 0; 4014c38e01b8SJohn Fastabend bool have_flags = false; 4015e5a55a89SJohn Fastabend 4016e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4017e5a55a89SJohn Fastabend return -EINVAL; 4018e5a55a89SJohn Fastabend 4019e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4020e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4021e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4022e5a55a89SJohn Fastabend 4023e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4024e5a55a89SJohn Fastabend if (!dev) { 4025b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4026e5a55a89SJohn Fastabend return -ENODEV; 4027e5a55a89SJohn Fastabend } 4028e5a55a89SJohn Fastabend 40292469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 40302469ffd7SJohn Fastabend if (br_spec) { 40312469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 40322469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40336e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40346e8d1c55SThomas Graf return -EINVAL; 40356e8d1c55SThomas Graf 4036c38e01b8SJohn Fastabend have_flags = true; 40372469ffd7SJohn Fastabend flags = nla_get_u16(attr); 40382469ffd7SJohn Fastabend break; 40392469ffd7SJohn Fastabend } 40402469ffd7SJohn Fastabend } 40412469ffd7SJohn Fastabend } 40422469ffd7SJohn Fastabend 40432469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4044898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4045898e5061SJiri Pirko 4046898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 40472469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4048e5a55a89SJohn Fastabend goto out; 4049e5a55a89SJohn Fastabend } 4050e5a55a89SJohn Fastabend 4051add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 40522469ffd7SJohn Fastabend if (err) 40532469ffd7SJohn Fastabend goto out; 40542469ffd7SJohn Fastabend 40552469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 40562469ffd7SJohn Fastabend } 40572469ffd7SJohn Fastabend 40582469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 40592469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 40602469ffd7SJohn Fastabend err = -EOPNOTSUPP; 40612469ffd7SJohn Fastabend else 4062add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4063add511b3SRoopa Prabhu flags); 406402dba438SRoopa Prabhu if (!err) { 40652469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 406602dba438SRoopa Prabhu 406702dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 406802dba438SRoopa Prabhu * change 406902dba438SRoopa Prabhu */ 407002dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 407102dba438SRoopa Prabhu } 40722469ffd7SJohn Fastabend } 40732469ffd7SJohn Fastabend 4074c38e01b8SJohn Fastabend if (have_flags) 40752469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4076e5a55a89SJohn Fastabend out: 4077e5a55a89SJohn Fastabend return err; 4078e5a55a89SJohn Fastabend } 4079e5a55a89SJohn Fastabend 4080c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4081c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4082407af329SVlad Yasevich { 4083407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4084407af329SVlad Yasevich struct ifinfomsg *ifm; 4085407af329SVlad Yasevich struct net_device *dev; 4086407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4087407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 40884de8b413SRosen, Rami u16 flags = 0; 4089407af329SVlad Yasevich bool have_flags = false; 4090407af329SVlad Yasevich 4091407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4092407af329SVlad Yasevich return -EINVAL; 4093407af329SVlad Yasevich 4094407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4095407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4096407af329SVlad Yasevich return -EPFNOSUPPORT; 4097407af329SVlad Yasevich 4098407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4099407af329SVlad Yasevich if (!dev) { 4100b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4101407af329SVlad Yasevich return -ENODEV; 4102407af329SVlad Yasevich } 4103407af329SVlad Yasevich 4104407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4105407af329SVlad Yasevich if (br_spec) { 4106407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4107407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 41086e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 41096e8d1c55SThomas Graf return -EINVAL; 41106e8d1c55SThomas Graf 4111407af329SVlad Yasevich have_flags = true; 4112407af329SVlad Yasevich flags = nla_get_u16(attr); 4113407af329SVlad Yasevich break; 4114407af329SVlad Yasevich } 4115407af329SVlad Yasevich } 4116407af329SVlad Yasevich } 4117407af329SVlad Yasevich 4118407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4119407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4120407af329SVlad Yasevich 4121407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4122407af329SVlad Yasevich err = -EOPNOTSUPP; 4123407af329SVlad Yasevich goto out; 4124407af329SVlad Yasevich } 4125407af329SVlad Yasevich 4126add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4127407af329SVlad Yasevich if (err) 4128407af329SVlad Yasevich goto out; 4129407af329SVlad Yasevich 4130407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4131407af329SVlad Yasevich } 4132407af329SVlad Yasevich 4133407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4134407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4135407af329SVlad Yasevich err = -EOPNOTSUPP; 4136407af329SVlad Yasevich else 4137add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4138add511b3SRoopa Prabhu flags); 4139407af329SVlad Yasevich 414002dba438SRoopa Prabhu if (!err) { 4141407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 414202dba438SRoopa Prabhu 414302dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 414402dba438SRoopa Prabhu * change 414502dba438SRoopa Prabhu */ 414602dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 414702dba438SRoopa Prabhu } 4148407af329SVlad Yasevich } 4149407af329SVlad Yasevich 4150407af329SVlad Yasevich if (have_flags) 4151407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4152407af329SVlad Yasevich out: 4153407af329SVlad Yasevich return err; 4154407af329SVlad Yasevich } 4155407af329SVlad Yasevich 4156e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4157e8872a25SNikolay Aleksandrov { 4158e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4159e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4160e8872a25SNikolay Aleksandrov } 4161e8872a25SNikolay Aleksandrov 416269ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 416369ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 416469ae6ad2SNogah Frankel { 416569ae6ad2SNogah Frankel switch (attr_id) { 416669ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 416769ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 416869ae6ad2SNogah Frankel } 416969ae6ad2SNogah Frankel 417069ae6ad2SNogah Frankel return 0; 417169ae6ad2SNogah Frankel } 417269ae6ad2SNogah Frankel 417369ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 417469ae6ad2SNogah Frankel int *prividx) 417569ae6ad2SNogah Frankel { 417669ae6ad2SNogah Frankel struct nlattr *attr = NULL; 417769ae6ad2SNogah Frankel int attr_id, size; 417869ae6ad2SNogah Frankel void *attr_data; 417969ae6ad2SNogah Frankel int err; 418069ae6ad2SNogah Frankel 418169ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 418269ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 418369ae6ad2SNogah Frankel return -ENODATA; 418469ae6ad2SNogah Frankel 418569ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 418669ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 418769ae6ad2SNogah Frankel if (attr_id < *prividx) 418869ae6ad2SNogah Frankel continue; 418969ae6ad2SNogah Frankel 419069ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 419169ae6ad2SNogah Frankel if (!size) 419269ae6ad2SNogah Frankel continue; 419369ae6ad2SNogah Frankel 41943df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 419569ae6ad2SNogah Frankel continue; 419669ae6ad2SNogah Frankel 419769ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 419869ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 419969ae6ad2SNogah Frankel if (!attr) 420069ae6ad2SNogah Frankel goto nla_put_failure; 420169ae6ad2SNogah Frankel 420269ae6ad2SNogah Frankel attr_data = nla_data(attr); 420369ae6ad2SNogah Frankel memset(attr_data, 0, size); 420469ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 420569ae6ad2SNogah Frankel attr_data); 420669ae6ad2SNogah Frankel if (err) 420769ae6ad2SNogah Frankel goto get_offload_stats_failure; 420869ae6ad2SNogah Frankel } 420969ae6ad2SNogah Frankel 421069ae6ad2SNogah Frankel if (!attr) 421169ae6ad2SNogah Frankel return -ENODATA; 421269ae6ad2SNogah Frankel 421369ae6ad2SNogah Frankel *prividx = 0; 421469ae6ad2SNogah Frankel return 0; 421569ae6ad2SNogah Frankel 421669ae6ad2SNogah Frankel nla_put_failure: 421769ae6ad2SNogah Frankel err = -EMSGSIZE; 421869ae6ad2SNogah Frankel get_offload_stats_failure: 421969ae6ad2SNogah Frankel *prividx = attr_id; 422069ae6ad2SNogah Frankel return err; 422169ae6ad2SNogah Frankel } 422269ae6ad2SNogah Frankel 422369ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 422469ae6ad2SNogah Frankel { 422569ae6ad2SNogah Frankel int nla_size = 0; 422669ae6ad2SNogah Frankel int attr_id; 422769ae6ad2SNogah Frankel int size; 422869ae6ad2SNogah Frankel 422969ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 423069ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 423169ae6ad2SNogah Frankel return 0; 423269ae6ad2SNogah Frankel 423369ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 423469ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 42353df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 423669ae6ad2SNogah Frankel continue; 423769ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 423869ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 423969ae6ad2SNogah Frankel } 424069ae6ad2SNogah Frankel 424169ae6ad2SNogah Frankel if (nla_size != 0) 424269ae6ad2SNogah Frankel nla_size += nla_total_size(0); 424369ae6ad2SNogah Frankel 424469ae6ad2SNogah Frankel return nla_size; 424569ae6ad2SNogah Frankel } 424669ae6ad2SNogah Frankel 424710c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 424810c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4249e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4250e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 425110c9ead9SRoopa Prabhu { 425210c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 425310c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 425410c9ead9SRoopa Prabhu struct nlattr *attr; 4255e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 425669ae6ad2SNogah Frankel int err; 425710c9ead9SRoopa Prabhu 425810c9ead9SRoopa Prabhu ASSERT_RTNL(); 425910c9ead9SRoopa Prabhu 426010c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 426110c9ead9SRoopa Prabhu if (!nlh) 426210c9ead9SRoopa Prabhu return -EMSGSIZE; 426310c9ead9SRoopa Prabhu 426410c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4265ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4266ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4267ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 426810c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 426910c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 427010c9ead9SRoopa Prabhu 4271e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 427210c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 427310c9ead9SRoopa Prabhu 427458414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 427558414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 427658414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 427710c9ead9SRoopa Prabhu if (!attr) 427810c9ead9SRoopa Prabhu goto nla_put_failure; 427910c9ead9SRoopa Prabhu 428010c9ead9SRoopa Prabhu sp = nla_data(attr); 428110c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 428210c9ead9SRoopa Prabhu } 428310c9ead9SRoopa Prabhu 428497a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 428597a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 428697a47facSNikolay Aleksandrov 428797a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 428897a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 428997a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 429097a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 429197a47facSNikolay Aleksandrov if (!attr) 429297a47facSNikolay Aleksandrov goto nla_put_failure; 429397a47facSNikolay Aleksandrov 429480e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 429580e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 429680e73cc5SNikolay Aleksandrov if (err) 429780e73cc5SNikolay Aleksandrov goto nla_put_failure; 429880e73cc5SNikolay Aleksandrov *idxattr = 0; 429980e73cc5SNikolay Aleksandrov } 430080e73cc5SNikolay Aleksandrov } 430180e73cc5SNikolay Aleksandrov 430280e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 430380e73cc5SNikolay Aleksandrov *idxattr)) { 430480e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 430580e73cc5SNikolay Aleksandrov const struct net_device *master; 430680e73cc5SNikolay Aleksandrov 430780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 430880e73cc5SNikolay Aleksandrov if (master) 430980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 431080e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 431180e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 431280e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 431380e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 431480e73cc5SNikolay Aleksandrov if (!attr) 431580e73cc5SNikolay Aleksandrov goto nla_put_failure; 431680e73cc5SNikolay Aleksandrov 431780e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 431897a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 431997a47facSNikolay Aleksandrov if (err) 432097a47facSNikolay Aleksandrov goto nla_put_failure; 432197a47facSNikolay Aleksandrov *idxattr = 0; 432297a47facSNikolay Aleksandrov } 432397a47facSNikolay Aleksandrov } 432497a47facSNikolay Aleksandrov 432569ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 432669ae6ad2SNogah Frankel *idxattr)) { 432769ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 432869ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 432969ae6ad2SNogah Frankel if (!attr) 433069ae6ad2SNogah Frankel goto nla_put_failure; 433169ae6ad2SNogah Frankel 433269ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 433369ae6ad2SNogah Frankel if (err == -ENODATA) 433469ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 433569ae6ad2SNogah Frankel else 433669ae6ad2SNogah Frankel nla_nest_end(skb, attr); 433769ae6ad2SNogah Frankel 433869ae6ad2SNogah Frankel if (err && err != -ENODATA) 433969ae6ad2SNogah Frankel goto nla_put_failure; 434069ae6ad2SNogah Frankel *idxattr = 0; 434169ae6ad2SNogah Frankel } 434269ae6ad2SNogah Frankel 4343aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4344aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4345aefb4d4aSRobert Shearman 4346aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4347aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4348aefb4d4aSRobert Shearman if (!attr) 4349aefb4d4aSRobert Shearman goto nla_put_failure; 4350aefb4d4aSRobert Shearman 43515fa85a09SFlorian Westphal rcu_read_lock(); 43525fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4353aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4354aefb4d4aSRobert Shearman struct nlattr *af; 4355aefb4d4aSRobert Shearman int err; 4356aefb4d4aSRobert Shearman 4357aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 43585fa85a09SFlorian Westphal if (!af) { 43595fa85a09SFlorian Westphal rcu_read_unlock(); 4360aefb4d4aSRobert Shearman goto nla_put_failure; 43615fa85a09SFlorian Westphal } 4362aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4363aefb4d4aSRobert Shearman 43645fa85a09SFlorian Westphal if (err == -ENODATA) { 4365aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 43665fa85a09SFlorian Westphal } else if (err < 0) { 43675fa85a09SFlorian Westphal rcu_read_unlock(); 4368aefb4d4aSRobert Shearman goto nla_put_failure; 43695fa85a09SFlorian Westphal } 4370aefb4d4aSRobert Shearman 4371aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4372aefb4d4aSRobert Shearman } 4373aefb4d4aSRobert Shearman } 43745fa85a09SFlorian Westphal rcu_read_unlock(); 4375aefb4d4aSRobert Shearman 4376aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4377aefb4d4aSRobert Shearman 4378aefb4d4aSRobert Shearman *idxattr = 0; 4379aefb4d4aSRobert Shearman } 4380aefb4d4aSRobert Shearman 438110c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 438210c9ead9SRoopa Prabhu 438310c9ead9SRoopa Prabhu return 0; 438410c9ead9SRoopa Prabhu 438510c9ead9SRoopa Prabhu nla_put_failure: 4386e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4387e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 438810c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4389e8872a25SNikolay Aleksandrov else 4390e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 439110c9ead9SRoopa Prabhu 439210c9ead9SRoopa Prabhu return -EMSGSIZE; 439310c9ead9SRoopa Prabhu } 439410c9ead9SRoopa Prabhu 439510c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 439610c9ead9SRoopa Prabhu u32 filter_mask) 439710c9ead9SRoopa Prabhu { 439810c9ead9SRoopa Prabhu size_t size = 0; 439910c9ead9SRoopa Prabhu 4400e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 440110c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 440210c9ead9SRoopa Prabhu 440397a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 440497a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 440580e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 440697a47facSNikolay Aleksandrov 440797a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 440880e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 440980e73cc5SNikolay Aleksandrov attr)); 441097a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 441197a47facSNikolay Aleksandrov size += nla_total_size(0); 441297a47facSNikolay Aleksandrov } 441397a47facSNikolay Aleksandrov } 441497a47facSNikolay Aleksandrov 441580e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 441680e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 441780e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 441880e73cc5SNikolay Aleksandrov const struct net_device *master; 441980e73cc5SNikolay Aleksandrov 442080e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 442180e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 442280e73cc5SNikolay Aleksandrov if (master) 442380e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 442480e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 442580e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 442680e73cc5SNikolay Aleksandrov 442780e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 442880e73cc5SNikolay Aleksandrov attr)); 442980e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 443080e73cc5SNikolay Aleksandrov size += nla_total_size(0); 443180e73cc5SNikolay Aleksandrov } 443280e73cc5SNikolay Aleksandrov } 443380e73cc5SNikolay Aleksandrov 443469ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 443569ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 443669ae6ad2SNogah Frankel 4437aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4438aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4439aefb4d4aSRobert Shearman 4440aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4441aefb4d4aSRobert Shearman size += nla_total_size(0); 4442aefb4d4aSRobert Shearman 44435fa85a09SFlorian Westphal rcu_read_lock(); 44445fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4445aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4446aefb4d4aSRobert Shearman size += nla_total_size( 4447aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4448aefb4d4aSRobert Shearman 4449aefb4d4aSRobert Shearman /* for AF_* */ 4450aefb4d4aSRobert Shearman size += nla_total_size(0); 4451aefb4d4aSRobert Shearman } 4452aefb4d4aSRobert Shearman } 44535fa85a09SFlorian Westphal rcu_read_unlock(); 4454aefb4d4aSRobert Shearman } 4455aefb4d4aSRobert Shearman 445610c9ead9SRoopa Prabhu return size; 445710c9ead9SRoopa Prabhu } 445810c9ead9SRoopa Prabhu 4459c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4460c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 446110c9ead9SRoopa Prabhu { 446210c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 446310c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4464e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4465e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 446610c9ead9SRoopa Prabhu struct sk_buff *nskb; 446710c9ead9SRoopa Prabhu u32 filter_mask; 446810c9ead9SRoopa Prabhu int err; 446910c9ead9SRoopa Prabhu 44704775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 44714775cc1fSMathias Krause return -EINVAL; 44724775cc1fSMathias Krause 447310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 447410c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 447510c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 447610c9ead9SRoopa Prabhu else 447710c9ead9SRoopa Prabhu return -EINVAL; 447810c9ead9SRoopa Prabhu 447910c9ead9SRoopa Prabhu if (!dev) 448010c9ead9SRoopa Prabhu return -ENODEV; 448110c9ead9SRoopa Prabhu 448210c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 448310c9ead9SRoopa Prabhu if (!filter_mask) 448410c9ead9SRoopa Prabhu return -EINVAL; 448510c9ead9SRoopa Prabhu 448610c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 448710c9ead9SRoopa Prabhu if (!nskb) 448810c9ead9SRoopa Prabhu return -ENOBUFS; 448910c9ead9SRoopa Prabhu 449010c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 449110c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4492e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 449310c9ead9SRoopa Prabhu if (err < 0) { 449410c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 449510c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 449610c9ead9SRoopa Prabhu kfree_skb(nskb); 449710c9ead9SRoopa Prabhu } else { 449810c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 449910c9ead9SRoopa Prabhu } 450010c9ead9SRoopa Prabhu 450110c9ead9SRoopa Prabhu return err; 450210c9ead9SRoopa Prabhu } 450310c9ead9SRoopa Prabhu 450410c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 450510c9ead9SRoopa Prabhu { 4506e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 450710c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 450810c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4509e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4510e8872a25SNikolay Aleksandrov struct hlist_head *head; 4511e8872a25SNikolay Aleksandrov struct net_device *dev; 451210c9ead9SRoopa Prabhu u32 filter_mask = 0; 4513e8872a25SNikolay Aleksandrov int idx = 0; 451410c9ead9SRoopa Prabhu 451510c9ead9SRoopa Prabhu s_h = cb->args[0]; 451610c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4517e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4518e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 451910c9ead9SRoopa Prabhu 452010c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 452110c9ead9SRoopa Prabhu 45224775cc1fSMathias Krause if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 45234775cc1fSMathias Krause return -EINVAL; 45244775cc1fSMathias Krause 452510c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 452610c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 452710c9ead9SRoopa Prabhu if (!filter_mask) 452810c9ead9SRoopa Prabhu return -EINVAL; 452910c9ead9SRoopa Prabhu 453010c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 453110c9ead9SRoopa Prabhu idx = 0; 453210c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 453310c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 453410c9ead9SRoopa Prabhu if (idx < s_idx) 453510c9ead9SRoopa Prabhu goto cont; 453610c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 453710c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 453810c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4539e8872a25SNikolay Aleksandrov flags, filter_mask, 4540e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 454110c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 454210c9ead9SRoopa Prabhu * we're in trouble 454310c9ead9SRoopa Prabhu */ 454410c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 454510c9ead9SRoopa Prabhu 454610c9ead9SRoopa Prabhu if (err < 0) 454710c9ead9SRoopa Prabhu goto out; 4548e8872a25SNikolay Aleksandrov s_prividx = 0; 4549e8872a25SNikolay Aleksandrov s_idxattr = 0; 455010c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 455110c9ead9SRoopa Prabhu cont: 455210c9ead9SRoopa Prabhu idx++; 455310c9ead9SRoopa Prabhu } 455410c9ead9SRoopa Prabhu } 455510c9ead9SRoopa Prabhu out: 4556e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4557e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 455810c9ead9SRoopa Prabhu cb->args[1] = idx; 455910c9ead9SRoopa Prabhu cb->args[0] = h; 456010c9ead9SRoopa Prabhu 456110c9ead9SRoopa Prabhu return skb->len; 456210c9ead9SRoopa Prabhu } 456310c9ead9SRoopa Prabhu 45641da177e4SLinus Torvalds /* Process one rtnetlink message. */ 45651da177e4SLinus Torvalds 45662d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 45672d4bc933SJohannes Berg struct netlink_ext_ack *extack) 45681da177e4SLinus Torvalds { 45693b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4570addf9b90SFlorian Westphal struct rtnl_link *link; 4571e4202511SFlorian Westphal struct module *owner; 45726853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4573e2849863SThomas Graf rtnl_doit_func doit; 457462256f98SFlorian Westphal unsigned int flags; 4575617cfc75SAlexander Kuleshov int kind; 45761da177e4SLinus Torvalds int family; 45771da177e4SLinus Torvalds int type; 45781da177e4SLinus Torvalds 45791da177e4SLinus Torvalds type = nlh->nlmsg_type; 45801da177e4SLinus Torvalds if (type > RTM_MAX) 4581038890feSThomas Graf return -EOPNOTSUPP; 45821da177e4SLinus Torvalds 45831da177e4SLinus Torvalds type -= RTM_BASE; 45841da177e4SLinus Torvalds 45851da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4586573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 45871da177e4SLinus Torvalds return 0; 45881da177e4SLinus Torvalds 4589573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 45901da177e4SLinus Torvalds kind = type&3; 45911da177e4SLinus Torvalds 459290f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 45931d00a4ebSThomas Graf return -EPERM; 45941da177e4SLinus Torvalds 45956853dd48SFlorian Westphal rcu_read_lock(); 4596b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 459797c53cacSDenis V. Lunev struct sock *rtnl; 4598e2849863SThomas Graf rtnl_dumpit_func dumpit; 4599c7ac8679SGreg Rose u16 min_dump_alloc = 0; 46001da177e4SLinus Torvalds 4601addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4602addf9b90SFlorian Westphal if (!link || !link->dumpit) { 46036853dd48SFlorian Westphal family = PF_UNSPEC; 4604addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4605addf9b90SFlorian Westphal if (!link || !link->dumpit) 46066853dd48SFlorian Westphal goto err_unlock; 46076853dd48SFlorian Westphal } 4608e4202511SFlorian Westphal owner = link->owner; 4609addf9b90SFlorian Westphal dumpit = link->dumpit; 46106853dd48SFlorian Westphal 46115c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4612e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 46131da177e4SLinus Torvalds 4614e4202511SFlorian Westphal err = 0; 4615e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4616e4202511SFlorian Westphal if (!try_module_get(owner)) 4617e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4618e4202511SFlorian Westphal 46196853dd48SFlorian Westphal rcu_read_unlock(); 46206853dd48SFlorian Westphal 462197c53cacSDenis V. Lunev rtnl = net->rtnl; 4622e4202511SFlorian Westphal if (err == 0) { 462380d326faSPablo Neira Ayuso struct netlink_dump_control c = { 462480d326faSPablo Neira Ayuso .dump = dumpit, 462580d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4626e4202511SFlorian Westphal .module = owner, 462780d326faSPablo Neira Ayuso }; 462880d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4629e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4630e4202511SFlorian Westphal * module if dump is still in progress. 4631e4202511SFlorian Westphal */ 4632e4202511SFlorian Westphal module_put(owner); 463380d326faSPablo Neira Ayuso } 46342907c35fSEric Dumazet return err; 46351da177e4SLinus Torvalds } 46361da177e4SLinus Torvalds 4637addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4638addf9b90SFlorian Westphal if (!link || !link->doit) { 46398caa38b5SFlorian Westphal family = PF_UNSPEC; 4640addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4641addf9b90SFlorian Westphal if (!link || !link->doit) 4642addf9b90SFlorian Westphal goto out_unlock; 46438caa38b5SFlorian Westphal } 46448caa38b5SFlorian Westphal 4645e4202511SFlorian Westphal owner = link->owner; 4646e4202511SFlorian Westphal if (!try_module_get(owner)) { 4647e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4648e4202511SFlorian Westphal goto out_unlock; 4649e4202511SFlorian Westphal } 4650e4202511SFlorian Westphal 4651addf9b90SFlorian Westphal flags = link->flags; 465262256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4653addf9b90SFlorian Westphal doit = link->doit; 465462256f98SFlorian Westphal rcu_read_unlock(); 465562256f98SFlorian Westphal if (doit) 465662256f98SFlorian Westphal err = doit(skb, nlh, extack); 4657e4202511SFlorian Westphal module_put(owner); 465862256f98SFlorian Westphal return err; 465962256f98SFlorian Westphal } 46606853dd48SFlorian Westphal rcu_read_unlock(); 46616853dd48SFlorian Westphal 46620cc09020SFlorian Westphal rtnl_lock(); 4663addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4664addf9b90SFlorian Westphal if (link && link->doit) 4665addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 46660cc09020SFlorian Westphal rtnl_unlock(); 4667addf9b90SFlorian Westphal 4668e4202511SFlorian Westphal module_put(owner); 4669e4202511SFlorian Westphal 4670addf9b90SFlorian Westphal return err; 4671addf9b90SFlorian Westphal 4672addf9b90SFlorian Westphal out_unlock: 4673addf9b90SFlorian Westphal rcu_read_unlock(); 46740cc09020SFlorian Westphal return err; 46750cc09020SFlorian Westphal 46760cc09020SFlorian Westphal err_unlock: 46776853dd48SFlorian Westphal rcu_read_unlock(); 46780cc09020SFlorian Westphal return -EOPNOTSUPP; 46791da177e4SLinus Torvalds } 46801da177e4SLinus Torvalds 4681cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 46821da177e4SLinus Torvalds { 4683cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 46841da177e4SLinus Torvalds } 46851da177e4SLinus Torvalds 46865f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 46875f729eaaSJulien Gomes { 46885f729eaaSJulien Gomes switch (group) { 46895f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 46905f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 46915f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 46925f729eaaSJulien Gomes return -EPERM; 46935f729eaaSJulien Gomes break; 46945f729eaaSJulien Gomes } 46955f729eaaSJulien Gomes return 0; 46965f729eaaSJulien Gomes } 46975f729eaaSJulien Gomes 46981da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 46991da177e4SLinus Torvalds { 4700351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4701e9dc8653SEric W. Biederman 47021da177e4SLinus Torvalds switch (event) { 47035138e86fSVlad Yasevich case NETDEV_REBOOT: 47048a212589SXin Long case NETDEV_CHANGEMTU: 47053753654eSDavid Ahern case NETDEV_CHANGEADDR: 47065138e86fSVlad Yasevich case NETDEV_CHANGENAME: 47075138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 47085138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4709e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 47105138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4711dc709f37SXin Long case NETDEV_CHANGEUPPER: 47125138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 47135138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4714eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4715ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 47163d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 471738e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 47181da177e4SLinus Torvalds break; 47191da177e4SLinus Torvalds default: 47201da177e4SLinus Torvalds break; 47211da177e4SLinus Torvalds } 47221da177e4SLinus Torvalds return NOTIFY_DONE; 47231da177e4SLinus Torvalds } 47241da177e4SLinus Torvalds 47251da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 47261da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 47271da177e4SLinus Torvalds }; 47281da177e4SLinus Torvalds 472997c53cacSDenis V. Lunev 47302c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 473197c53cacSDenis V. Lunev { 473297c53cacSDenis V. Lunev struct sock *sk; 4733a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4734a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4735a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4736a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 47379785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 47385f729eaaSJulien Gomes .bind = rtnetlink_bind, 4739a31f2d17SPablo Neira Ayuso }; 4740a31f2d17SPablo Neira Ayuso 47419f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 474297c53cacSDenis V. Lunev if (!sk) 474397c53cacSDenis V. Lunev return -ENOMEM; 474497c53cacSDenis V. Lunev net->rtnl = sk; 474597c53cacSDenis V. Lunev return 0; 474697c53cacSDenis V. Lunev } 474797c53cacSDenis V. Lunev 47482c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 474997c53cacSDenis V. Lunev { 4750b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 475197c53cacSDenis V. Lunev net->rtnl = NULL; 475297c53cacSDenis V. Lunev } 475397c53cacSDenis V. Lunev 475497c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 475597c53cacSDenis V. Lunev .init = rtnetlink_net_init, 475697c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 475797c53cacSDenis V. Lunev }; 475897c53cacSDenis V. Lunev 47591da177e4SLinus Torvalds void __init rtnetlink_init(void) 47601da177e4SLinus Torvalds { 476197c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 47621da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 476397c53cacSDenis V. Lunev 47641da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4765340d17fcSThomas Graf 4766c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4767b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4768b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4769b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4770b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4771687ad8ccSThomas Graf 4772b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4773b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4774b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 477577162022SJohn Fastabend 4776b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4777b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4778b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4779e5a55a89SJohn Fastabend 4780b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4781b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4782b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 478310c9ead9SRoopa Prabhu 478410c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4785b97bac64SFlorian Westphal 0); 47861da177e4SLinus Torvalds } 4787