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 */ 967*4f91da26SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID */ 968*4f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 969d1fdd913SBrenden Blanco 970d1fdd913SBrenden Blanco return xdp_size; 971d1fdd913SBrenden Blanco } 972d1fdd913SBrenden Blanco 973115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 974115c9b81SGreg Rose u32 ext_filter_mask) 975339bf98fSThomas Graf { 976339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 977339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9780b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 979339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 980270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 981339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 98235c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 983339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 984339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 985339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 986339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 987339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 988339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 989339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9909a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 991edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 99276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 99376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 9946919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 9956919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 996339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 99738f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 9982d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 999d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1000db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1001115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1002115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1003115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1004c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1005f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1006b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 100782f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 100888d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1009c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1010b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10113d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10126621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 101338e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 101403ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 101579e1ad14SJiri Benc + nla_total_size(4) /* IFLA_IF_NETNSID */ 1016b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1017b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 101879e1ad14SJiri Benc + 0; 1019339bf98fSThomas Graf } 1020339bf98fSThomas Graf 102157b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 102257b61080SScott Feldman { 102357b61080SScott Feldman struct nlattr *vf_ports; 102457b61080SScott Feldman struct nlattr *vf_port; 102557b61080SScott Feldman int vf; 102657b61080SScott Feldman int err; 102757b61080SScott Feldman 102857b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 102957b61080SScott Feldman if (!vf_ports) 103057b61080SScott Feldman return -EMSGSIZE; 103157b61080SScott Feldman 103257b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 103357b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10348ca94183SScott Feldman if (!vf_port) 10358ca94183SScott Feldman goto nla_put_failure; 1036a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1037a6574349SDavid S. Miller goto nla_put_failure; 103857b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10398ca94183SScott Feldman if (err == -EMSGSIZE) 10408ca94183SScott Feldman goto nla_put_failure; 104157b61080SScott Feldman if (err) { 104257b61080SScott Feldman nla_nest_cancel(skb, vf_port); 104357b61080SScott Feldman continue; 104457b61080SScott Feldman } 104557b61080SScott Feldman nla_nest_end(skb, vf_port); 104657b61080SScott Feldman } 104757b61080SScott Feldman 104857b61080SScott Feldman nla_nest_end(skb, vf_ports); 104957b61080SScott Feldman 105057b61080SScott Feldman return 0; 10518ca94183SScott Feldman 10528ca94183SScott Feldman nla_put_failure: 10538ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10548ca94183SScott Feldman return -EMSGSIZE; 105557b61080SScott Feldman } 105657b61080SScott Feldman 105757b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 105857b61080SScott Feldman { 105957b61080SScott Feldman struct nlattr *port_self; 106057b61080SScott Feldman int err; 106157b61080SScott Feldman 106257b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 106357b61080SScott Feldman if (!port_self) 106457b61080SScott Feldman return -EMSGSIZE; 106557b61080SScott Feldman 106657b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 106757b61080SScott Feldman if (err) { 106857b61080SScott Feldman nla_nest_cancel(skb, port_self); 10698ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 107057b61080SScott Feldman } 107157b61080SScott Feldman 107257b61080SScott Feldman nla_nest_end(skb, port_self); 107357b61080SScott Feldman 107457b61080SScott Feldman return 0; 107557b61080SScott Feldman } 107657b61080SScott Feldman 1077c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1078c53864fdSDavid Gibson u32 ext_filter_mask) 107957b61080SScott Feldman { 108057b61080SScott Feldman int err; 108157b61080SScott Feldman 1082c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1083c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 108457b61080SScott Feldman return 0; 108557b61080SScott Feldman 108657b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 108757b61080SScott Feldman if (err) 108857b61080SScott Feldman return err; 108957b61080SScott Feldman 109057b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 109157b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 109257b61080SScott Feldman if (err) 109357b61080SScott Feldman return err; 109457b61080SScott Feldman } 109557b61080SScott Feldman 109657b61080SScott Feldman return 0; 109757b61080SScott Feldman } 109857b61080SScott Feldman 109966cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 110066cae9edSJiri Pirko { 110166cae9edSJiri Pirko int err; 110202637fceSJiri Pirko struct netdev_phys_item_id ppid; 110366cae9edSJiri Pirko 110466cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 110566cae9edSJiri Pirko if (err) { 110666cae9edSJiri Pirko if (err == -EOPNOTSUPP) 110766cae9edSJiri Pirko return 0; 110866cae9edSJiri Pirko return err; 110966cae9edSJiri Pirko } 111066cae9edSJiri Pirko 111166cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 111266cae9edSJiri Pirko return -EMSGSIZE; 111366cae9edSJiri Pirko 111466cae9edSJiri Pirko return 0; 111566cae9edSJiri Pirko } 111666cae9edSJiri Pirko 1117db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1118db24a904SDavid Ahern { 1119db24a904SDavid Ahern char name[IFNAMSIZ]; 1120db24a904SDavid Ahern int err; 1121db24a904SDavid Ahern 1122db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1123db24a904SDavid Ahern if (err) { 1124db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1125db24a904SDavid Ahern return 0; 1126db24a904SDavid Ahern return err; 1127db24a904SDavid Ahern } 1128db24a904SDavid Ahern 112977ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1130db24a904SDavid Ahern return -EMSGSIZE; 1131db24a904SDavid Ahern 1132db24a904SDavid Ahern return 0; 1133db24a904SDavid Ahern } 1134db24a904SDavid Ahern 113582f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 113682f28412SJiri Pirko { 113782f28412SJiri Pirko int err; 1138f8e20a9fSScott Feldman struct switchdev_attr attr = { 11396ff64f6fSIdo Schimmel .orig_dev = dev, 11401f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1141f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1142f8e20a9fSScott Feldman }; 114382f28412SJiri Pirko 1144f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 114582f28412SJiri Pirko if (err) { 114682f28412SJiri Pirko if (err == -EOPNOTSUPP) 114782f28412SJiri Pirko return 0; 114882f28412SJiri Pirko return err; 114982f28412SJiri Pirko } 115082f28412SJiri Pirko 115142275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 115242275bd8SScott Feldman attr.u.ppid.id)) 115382f28412SJiri Pirko return -EMSGSIZE; 115482f28412SJiri Pirko 115582f28412SJiri Pirko return 0; 115682f28412SJiri Pirko } 115782f28412SJiri Pirko 1158b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1159b22b941bSHannes Frederic Sowa struct net_device *dev) 1160b22b941bSHannes Frederic Sowa { 1161550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1162b22b941bSHannes Frederic Sowa struct nlattr *attr; 1163b22b941bSHannes Frederic Sowa 116458414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 116558414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1166b22b941bSHannes Frederic Sowa if (!attr) 1167b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1168b22b941bSHannes Frederic Sowa 1169550bce59SRoopa Prabhu sp = nla_data(attr); 1170550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1171550bce59SRoopa Prabhu 1172550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1173550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1174550bce59SRoopa Prabhu if (!attr) 1175550bce59SRoopa Prabhu return -EMSGSIZE; 1176550bce59SRoopa Prabhu 1177550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1178b22b941bSHannes Frederic Sowa 1179b22b941bSHannes Frederic Sowa return 0; 1180b22b941bSHannes Frederic Sowa } 1181b22b941bSHannes Frederic Sowa 1182b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1183b22b941bSHannes Frederic Sowa struct net_device *dev, 1184b22b941bSHannes Frederic Sowa int vfs_num, 1185b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1186b22b941bSHannes Frederic Sowa { 1187b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 118879aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1189b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 119079aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1191b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1192b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1193b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1194b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1195b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1196b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1197b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1198b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1199b22b941bSHannes Frederic Sowa 12000eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12010eed9cf5SMintz, Yuval 1202b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1203b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1204b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1205b22b941bSHannes Frederic Sowa * didn't report anything. 1206b22b941bSHannes Frederic Sowa */ 1207b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1208b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1209b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1210b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1211b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1212b22b941bSHannes Frederic Sowa */ 1213b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 121479aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 121579aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1216b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1217b22b941bSHannes Frederic Sowa return 0; 1218b22b941bSHannes Frederic Sowa 1219775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1220775f4f05SDan Carpenter 1221b22b941bSHannes Frederic Sowa vf_mac.vf = 1222b22b941bSHannes Frederic Sowa vf_vlan.vf = 122379aab093SMoshe Shemesh vf_vlan_info.vf = 1224b22b941bSHannes Frederic Sowa vf_rate.vf = 1225b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1226b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1227b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1228b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1229b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1230b22b941bSHannes Frederic Sowa 1231b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1232b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1233b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 123479aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 123579aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 123679aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1237b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1238b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1239b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1240b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1241b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1242b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1243b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1244b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 124579aab093SMoshe Shemesh if (!vf) 124679aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1247b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1248b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1249b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1250b22b941bSHannes Frederic Sowa &vf_rate) || 1251b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1252b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1253b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1254b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1255b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1256b22b941bSHannes Frederic Sowa &vf_linkstate) || 1257b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1258b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1259b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1260b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1261b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 126279aab093SMoshe Shemesh goto nla_put_vf_failure; 126379aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 126479aab093SMoshe Shemesh if (!vfvlanlist) 126579aab093SMoshe Shemesh goto nla_put_vf_failure; 126679aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 126779aab093SMoshe Shemesh &vf_vlan_info)) { 126879aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 126979aab093SMoshe Shemesh goto nla_put_vf_failure; 127079aab093SMoshe Shemesh } 127179aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1272b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1273b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1274b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1275b22b941bSHannes Frederic Sowa &vf_stats); 1276b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 127779aab093SMoshe Shemesh if (!vfstats) 127879aab093SMoshe Shemesh goto nla_put_vf_failure; 1279343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1280343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1281343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1282343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1283343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1284343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1285343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1286343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1287343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1288343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1289343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1290c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1291c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1292c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1293c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1294c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 129579aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 129679aab093SMoshe Shemesh goto nla_put_vf_failure; 129779aab093SMoshe Shemesh } 1298b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1299b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1300b22b941bSHannes Frederic Sowa return 0; 130179aab093SMoshe Shemesh 130279aab093SMoshe Shemesh nla_put_vf_failure: 130379aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 130479aab093SMoshe Shemesh nla_put_vfinfo_failure: 130579aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 130679aab093SMoshe Shemesh return -EMSGSIZE; 1307b22b941bSHannes Frederic Sowa } 1308b22b941bSHannes Frederic Sowa 1309250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1310250fc3dfSFlorian Westphal struct net_device *dev, 1311250fc3dfSFlorian Westphal u32 ext_filter_mask) 1312250fc3dfSFlorian Westphal { 1313250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1314250fc3dfSFlorian Westphal int i, num_vfs; 1315250fc3dfSFlorian Westphal 1316250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1317250fc3dfSFlorian Westphal return 0; 1318250fc3dfSFlorian Westphal 1319250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1320250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1321250fc3dfSFlorian Westphal return -EMSGSIZE; 1322250fc3dfSFlorian Westphal 1323250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1324250fc3dfSFlorian Westphal return 0; 1325250fc3dfSFlorian Westphal 1326250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1327250fc3dfSFlorian Westphal if (!vfinfo) 1328250fc3dfSFlorian Westphal return -EMSGSIZE; 1329250fc3dfSFlorian Westphal 1330250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1331250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1332250fc3dfSFlorian Westphal return -EMSGSIZE; 1333250fc3dfSFlorian Westphal } 1334250fc3dfSFlorian Westphal 1335250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1336250fc3dfSFlorian Westphal return 0; 1337250fc3dfSFlorian Westphal } 1338250fc3dfSFlorian Westphal 1339b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1340b22b941bSHannes Frederic Sowa { 13415f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13425f8e4474SKangjie Lu 13435f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13445f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13455f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13465f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13475f8e4474SKangjie Lu map.irq = dev->irq; 13485f8e4474SKangjie Lu map.dma = dev->dma; 13495f8e4474SKangjie Lu map.port = dev->if_port; 13505f8e4474SKangjie Lu 1351270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1352b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1353b22b941bSHannes Frederic Sowa 1354b22b941bSHannes Frederic Sowa return 0; 1355b22b941bSHannes Frederic Sowa } 1356b22b941bSHannes Frederic Sowa 135758038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id) 1358d67b9cd2SDaniel Borkmann { 1359d67b9cd2SDaniel Borkmann const struct net_device_ops *ops = dev->netdev_ops; 136058038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1361118b4aa2SJakub Kicinski struct netdev_bpf xdp; 1362d67b9cd2SDaniel Borkmann 1363d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1364d67b9cd2SDaniel Borkmann 136558038695SMartin KaFai Lau *prog_id = 0; 136658038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 136758038695SMartin KaFai Lau if (generic_xdp_prog) { 136858038695SMartin KaFai Lau *prog_id = generic_xdp_prog->aux->id; 1369d67b9cd2SDaniel Borkmann return XDP_ATTACHED_SKB; 137058038695SMartin KaFai Lau } 1371f4e63525SJakub Kicinski if (!ops->ndo_bpf) 1372d67b9cd2SDaniel Borkmann return XDP_ATTACHED_NONE; 1373ce158e58SJakub Kicinski 1374118b4aa2SJakub Kicinski __dev_xdp_query(dev, ops->ndo_bpf, &xdp); 1375118b4aa2SJakub Kicinski *prog_id = xdp.prog_id; 1376118b4aa2SJakub Kicinski 1377118b4aa2SJakub Kicinski return xdp.prog_attached; 1378d67b9cd2SDaniel Borkmann } 1379d67b9cd2SDaniel Borkmann 1380d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1381d1fdd913SBrenden Blanco { 1382*4f91da26SJakub Kicinski u32 prog_attr, prog_id; 1383d1fdd913SBrenden Blanco struct nlattr *xdp; 1384d1fdd913SBrenden Blanco int err; 1385*4f91da26SJakub Kicinski u8 mode; 1386d1fdd913SBrenden Blanco 1387d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1388d1fdd913SBrenden Blanco if (!xdp) 1389d1fdd913SBrenden Blanco return -EMSGSIZE; 1390b5cdae32SDavid S. Miller 1391*4f91da26SJakub Kicinski mode = rtnl_xdp_attached_mode(dev, &prog_id); 1392*4f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1393d1fdd913SBrenden Blanco if (err) 1394d1fdd913SBrenden Blanco goto err_cancel; 1395d1fdd913SBrenden Blanco 139658038695SMartin KaFai Lau if (prog_id) { 139758038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 139858038695SMartin KaFai Lau if (err) 139958038695SMartin KaFai Lau goto err_cancel; 1400*4f91da26SJakub Kicinski 1401*4f91da26SJakub Kicinski switch (mode) { 1402*4f91da26SJakub Kicinski case XDP_ATTACHED_DRV: 1403*4f91da26SJakub Kicinski prog_attr = IFLA_XDP_DRV_PROG_ID; 1404*4f91da26SJakub Kicinski break; 1405*4f91da26SJakub Kicinski case XDP_ATTACHED_SKB: 1406*4f91da26SJakub Kicinski prog_attr = IFLA_XDP_SKB_PROG_ID; 1407*4f91da26SJakub Kicinski break; 1408*4f91da26SJakub Kicinski case XDP_ATTACHED_HW: 1409*4f91da26SJakub Kicinski prog_attr = IFLA_XDP_HW_PROG_ID; 1410*4f91da26SJakub Kicinski break; 1411*4f91da26SJakub Kicinski case XDP_ATTACHED_NONE: 1412*4f91da26SJakub Kicinski default: 1413*4f91da26SJakub Kicinski err = -EINVAL; 1414*4f91da26SJakub Kicinski goto err_cancel; 1415*4f91da26SJakub Kicinski } 1416*4f91da26SJakub Kicinski 1417*4f91da26SJakub Kicinski err = nla_put_u32(skb, prog_attr, prog_id); 1418*4f91da26SJakub Kicinski if (err) 1419*4f91da26SJakub Kicinski goto err_cancel; 142058038695SMartin KaFai Lau } 142158038695SMartin KaFai Lau 1422d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1423d1fdd913SBrenden Blanco return 0; 1424d1fdd913SBrenden Blanco 1425d1fdd913SBrenden Blanco err_cancel: 1426d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1427d1fdd913SBrenden Blanco return err; 1428d1fdd913SBrenden Blanco } 1429d1fdd913SBrenden Blanco 14303d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14313d3ea5afSVlad Yasevich { 14323d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14333d3ea5afSVlad Yasevich 14343d3ea5afSVlad Yasevich switch (event) { 14353d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14363d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14373d3ea5afSVlad Yasevich break; 14383d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14393d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14403d3ea5afSVlad Yasevich break; 14413d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14423d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14433d3ea5afSVlad Yasevich break; 14443d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14453d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14463d3ea5afSVlad Yasevich break; 14473d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14483d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14493d3ea5afSVlad Yasevich break; 14503d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14513d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14523d3ea5afSVlad Yasevich break; 14533d3ea5afSVlad Yasevich default: 14543d3ea5afSVlad Yasevich break; 14553d3ea5afSVlad Yasevich } 14563d3ea5afSVlad Yasevich 14573d3ea5afSVlad Yasevich return rtnl_event_type; 14583d3ea5afSVlad Yasevich } 14593d3ea5afSVlad Yasevich 146079110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 146179110a04SFlorian Westphal { 146279110a04SFlorian Westphal const struct net_device *upper_dev; 146379110a04SFlorian Westphal int ret = 0; 146479110a04SFlorian Westphal 146579110a04SFlorian Westphal rcu_read_lock(); 146679110a04SFlorian Westphal 146779110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 146879110a04SFlorian Westphal if (upper_dev) 146979110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 147079110a04SFlorian Westphal 147179110a04SFlorian Westphal rcu_read_unlock(); 147279110a04SFlorian Westphal return ret; 147379110a04SFlorian Westphal } 147479110a04SFlorian Westphal 147579110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 147679110a04SFlorian Westphal { 147779110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 147879110a04SFlorian Westphal 147979110a04SFlorian Westphal if (dev->ifindex == ifindex) 148079110a04SFlorian Westphal return 0; 148179110a04SFlorian Westphal 148279110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 148379110a04SFlorian Westphal } 148479110a04SFlorian Westphal 14856c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 14866c557001SFlorian Westphal struct net_device *dev) 14876c557001SFlorian Westphal { 14886c557001SFlorian Westphal char buf[IFALIASZ]; 14896c557001SFlorian Westphal int ret; 14906c557001SFlorian Westphal 14916c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 14926c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 14936c557001SFlorian Westphal } 14946c557001SFlorian Westphal 1495b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 149679e1ad14SJiri Benc const struct net_device *dev, 149779e1ad14SJiri Benc struct net *src_net) 1498b1e66b9aSFlorian Westphal { 1499b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1500b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1501b1e66b9aSFlorian Westphal 1502b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 150379e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1504b1e66b9aSFlorian Westphal 1505b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1506b1e66b9aSFlorian Westphal return -EMSGSIZE; 1507b1e66b9aSFlorian Westphal } 1508b1e66b9aSFlorian Westphal } 1509b1e66b9aSFlorian Westphal 1510b1e66b9aSFlorian Westphal return 0; 1511b1e66b9aSFlorian Westphal } 1512b1e66b9aSFlorian Westphal 1513070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1514070cbf5bSFlorian Westphal const struct net_device *dev, 1515070cbf5bSFlorian Westphal u32 ext_filter_mask) 1516070cbf5bSFlorian Westphal { 1517070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1518070cbf5bSFlorian Westphal struct nlattr *af_spec; 1519070cbf5bSFlorian Westphal 1520070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1521070cbf5bSFlorian Westphal if (!af_spec) 1522070cbf5bSFlorian Westphal return -EMSGSIZE; 1523070cbf5bSFlorian Westphal 15245fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1525070cbf5bSFlorian Westphal struct nlattr *af; 1526070cbf5bSFlorian Westphal int err; 1527070cbf5bSFlorian Westphal 1528070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1529070cbf5bSFlorian Westphal continue; 1530070cbf5bSFlorian Westphal 1531070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1532070cbf5bSFlorian Westphal if (!af) 1533070cbf5bSFlorian Westphal return -EMSGSIZE; 1534070cbf5bSFlorian Westphal 1535070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1536070cbf5bSFlorian Westphal /* 1537070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1538070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1539070cbf5bSFlorian Westphal * means we should trim the attribute header and 1540070cbf5bSFlorian Westphal * continue. 1541070cbf5bSFlorian Westphal */ 1542070cbf5bSFlorian Westphal if (err == -ENODATA) 1543070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1544070cbf5bSFlorian Westphal else if (err < 0) 1545070cbf5bSFlorian Westphal return -EMSGSIZE; 1546070cbf5bSFlorian Westphal 1547070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1548070cbf5bSFlorian Westphal } 1549070cbf5bSFlorian Westphal 1550070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1551070cbf5bSFlorian Westphal return 0; 1552070cbf5bSFlorian Westphal } 1553070cbf5bSFlorian Westphal 155479e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 155579e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1556575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15573d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 155838e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 155938e01b30SNicolas Dichtel int tgt_netnsid) 1560b60c5115SThomas Graf { 1561b60c5115SThomas Graf struct ifinfomsg *ifm; 15621da177e4SLinus Torvalds struct nlmsghdr *nlh; 15631da177e4SLinus Torvalds 15642907c35fSEric Dumazet ASSERT_RTNL(); 1565b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1566b60c5115SThomas Graf if (nlh == NULL) 156726932566SPatrick McHardy return -EMSGSIZE; 15681da177e4SLinus Torvalds 1569b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1570b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1571b60c5115SThomas Graf ifm->__ifi_pad = 0; 1572b60c5115SThomas Graf ifm->ifi_type = dev->type; 1573b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1574b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1575b60c5115SThomas Graf ifm->ifi_change = change; 15761da177e4SLinus Torvalds 157779e1ad14SJiri Benc if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 157879e1ad14SJiri Benc goto nla_put_failure; 157979e1ad14SJiri Benc 1580a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1581a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1582a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1583a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1584a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1585a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1586a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1587edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 158876ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1589c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1590c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 15911d69c2b3SMark A. Greer #ifdef CONFIG_RPS 159276ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 15931d69c2b3SMark A. Greer #endif 159479110a04SFlorian Westphal nla_put_iflink(skb, dev) || 159579110a04SFlorian Westphal put_master_ifindex(skb, dev) || 15969a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1597a6574349SDavid S. Miller (dev->qdisc && 1598a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 15996c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 16002d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1601b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1602b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1603b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1604b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1605b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1606b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1607b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1608a6574349SDavid S. Miller goto nla_put_failure; 16090b815a1aSStephen Hemminger 16103d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 16113d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 16123d3ea5afSVlad Yasevich goto nla_put_failure; 16133d3ea5afSVlad Yasevich } 16143d3ea5afSVlad Yasevich 1615b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1616a6574349SDavid S. Miller goto nla_put_failure; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds if (dev->addr_len) { 1619a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1620a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1621a6574349SDavid S. Miller goto nla_put_failure; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 162466cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 162566cae9edSJiri Pirko goto nla_put_failure; 162666cae9edSJiri Pirko 1627db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1628db24a904SDavid Ahern goto nla_put_failure; 1629db24a904SDavid Ahern 163082f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 163182f28412SJiri Pirko goto nla_put_failure; 163282f28412SJiri Pirko 1633b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1634b60c5115SThomas Graf goto nla_put_failure; 1635b60c5115SThomas Graf 1636250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1637a6574349SDavid S. Miller goto nla_put_failure; 163857b61080SScott Feldman 1639c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 164057b61080SScott Feldman goto nla_put_failure; 164157b61080SScott Feldman 1642d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1643d1fdd913SBrenden Blanco goto nla_put_failure; 1644d1fdd913SBrenden Blanco 1645ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 164638f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 164738f7b870SPatrick McHardy goto nla_put_failure; 164838f7b870SPatrick McHardy } 164938f7b870SPatrick McHardy 165079e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1651d37512a2SNicolas Dichtel goto nla_put_failure; 1652d37512a2SNicolas Dichtel 16536621dd29SNicolas Dichtel if (new_nsid && 16546621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16556621dd29SNicolas Dichtel goto nla_put_failure; 165638e01b30SNicolas Dichtel if (new_ifindex && 165738e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 165838e01b30SNicolas Dichtel goto nla_put_failure; 165938e01b30SNicolas Dichtel 16606621dd29SNicolas Dichtel 16615fa85a09SFlorian Westphal rcu_read_lock(); 1662070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16635fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16645fa85a09SFlorian Westphal rcu_read_unlock(); 1665f8ff182cSThomas Graf 1666053c095aSJohannes Berg nlmsg_end(skb, nlh); 1667053c095aSJohannes Berg return 0; 1668b60c5115SThomas Graf 16695fa85a09SFlorian Westphal nla_put_failure_rcu: 16705fa85a09SFlorian Westphal rcu_read_unlock(); 1671b60c5115SThomas Graf nla_put_failure: 167226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 167326932566SPatrick McHardy return -EMSGSIZE; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 1676f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1677f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1678f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1679f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1680f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1681f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1682f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1683f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1684f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1685f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1686f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1687f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1688f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1689f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1690f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1691f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 16922459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 16932459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 16942459b4c6SNicolas Dichtel */ 16952459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1696f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1697f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1698f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1699f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1700f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1701f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1702f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1703f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 170446e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 170546e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 170602637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 17072d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 170882f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1709317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 171088d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1711d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 17123d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1713db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 171479e1ad14SJiri Benc [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1715b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1716b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 1717f7b12606SJiri Pirko }; 1718f7b12606SJiri Pirko 1719f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1720f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1721f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1722f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1723f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1724f7b12606SJiri Pirko }; 1725f7b12606SJiri Pirko 1726f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1727364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1728364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 172979aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1730364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1731364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1732364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1733364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 173401a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17353b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1736dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1737cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1738cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17393b766cd8SEran Ben Elisha }; 17403b766cd8SEran Ben Elisha 1741f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1742f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1743f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1744f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1745f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1746f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1747f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1748f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1749f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1750f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1751025331dfSDaniel Borkmann 1752025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1753025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1754025331dfSDaniel Borkmann * combination with structs. 1755025331dfSDaniel Borkmann */ 1756025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1757025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1758f7b12606SJiri Pirko }; 1759f7b12606SJiri Pirko 1760d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1761d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1762d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 176385de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 176458038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1765d1fdd913SBrenden Blanco }; 1766d1fdd913SBrenden Blanco 1767dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1768dc599f76SDavid Ahern { 1769dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1770dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1771dc599f76SDavid Ahern 1772fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1773fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1774dc599f76SDavid Ahern return NULL; 1775dc599f76SDavid Ahern 1776dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1777dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1778dc599f76SDavid Ahern 1779dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1780dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1781dc599f76SDavid Ahern } 1782dc599f76SDavid Ahern 1783dc599f76SDavid Ahern return ops; 1784dc599f76SDavid Ahern } 1785dc599f76SDavid Ahern 1786dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1787dc599f76SDavid Ahern { 1788dc599f76SDavid Ahern struct net_device *master; 1789dc599f76SDavid Ahern 1790dc599f76SDavid Ahern if (!master_idx) 1791dc599f76SDavid Ahern return false; 1792dc599f76SDavid Ahern 1793dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1794dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1795dc599f76SDavid Ahern return true; 1796dc599f76SDavid Ahern 1797dc599f76SDavid Ahern return false; 1798dc599f76SDavid Ahern } 1799dc599f76SDavid Ahern 1800dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1801dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1802dc599f76SDavid Ahern { 1803dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1804dc599f76SDavid Ahern return true; 1805dc599f76SDavid Ahern 1806dc599f76SDavid Ahern return false; 1807dc599f76SDavid Ahern } 1808dc599f76SDavid Ahern 1809dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1810dc599f76SDavid Ahern int master_idx, 1811dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1812dc599f76SDavid Ahern { 1813dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1814dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1815dc599f76SDavid Ahern return true; 1816dc599f76SDavid Ahern 1817dc599f76SDavid Ahern return false; 1818dc599f76SDavid Ahern } 1819dc599f76SDavid Ahern 1820f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid) 182179e1ad14SJiri Benc { 182279e1ad14SJiri Benc struct net *net; 182379e1ad14SJiri Benc 1824f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 182579e1ad14SJiri Benc if (!net) 182679e1ad14SJiri Benc return ERR_PTR(-EINVAL); 182779e1ad14SJiri Benc 182879e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 182979e1ad14SJiri Benc * the user namespace owning the target net ns. 183079e1ad14SJiri Benc */ 1831f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 183279e1ad14SJiri Benc put_net(net); 183379e1ad14SJiri Benc return ERR_PTR(-EACCES); 183479e1ad14SJiri Benc } 183579e1ad14SJiri Benc return net; 183679e1ad14SJiri Benc } 183779e1ad14SJiri Benc 1838b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 18391da177e4SLinus Torvalds { 18403b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 184179e1ad14SJiri Benc struct net *tgt_net = net; 18427c28bd0bSEric Dumazet int h, s_h; 18437c28bd0bSEric Dumazet int idx = 0, s_idx; 18441da177e4SLinus Torvalds struct net_device *dev; 18457c28bd0bSEric Dumazet struct hlist_head *head; 1846115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1847115c9b81SGreg Rose u32 ext_filter_mask = 0; 1848dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1849dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1850dc599f76SDavid Ahern int master_idx = 0; 185179e1ad14SJiri Benc int netnsid = -1; 1852973462bbSDavid Gibson int err; 1853e5eca6d4SMichal Schmidt int hdrlen; 18541da177e4SLinus Torvalds 18557c28bd0bSEric Dumazet s_h = cb->args[0]; 18567c28bd0bSEric Dumazet s_idx = cb->args[1]; 18577c28bd0bSEric Dumazet 1858e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1859e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1860e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1861e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1862e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1863e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1864e5eca6d4SMichal Schmidt */ 1865e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1866e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1867e5eca6d4SMichal Schmidt 1868fceb6435SJohannes Berg if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1869fceb6435SJohannes Berg ifla_policy, NULL) >= 0) { 187079e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 187179e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1872f428fe4aSAndrei Vagin tgt_net = get_target_net(skb->sk, netnsid); 187379e1ad14SJiri Benc if (IS_ERR(tgt_net)) { 187479e1ad14SJiri Benc tgt_net = net; 187579e1ad14SJiri Benc netnsid = -1; 187679e1ad14SJiri Benc } 187779e1ad14SJiri Benc } 187879e1ad14SJiri Benc 1879115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1880115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1881dc599f76SDavid Ahern 1882dc599f76SDavid Ahern if (tb[IFLA_MASTER]) 1883dc599f76SDavid Ahern master_idx = nla_get_u32(tb[IFLA_MASTER]); 1884dc599f76SDavid Ahern 1885dc599f76SDavid Ahern if (tb[IFLA_LINKINFO]) 1886dc599f76SDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1887dc599f76SDavid Ahern 1888dc599f76SDavid Ahern if (master_idx || kind_ops) 1889dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1890a4b64fbeSEric Dumazet } 1891115c9b81SGreg Rose 18927c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 18937562f876SPavel Emelianov idx = 0; 189479e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1895cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1896dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 18973f0ae05dSZhang Shengju goto cont; 18981da177e4SLinus Torvalds if (idx < s_idx) 18997562f876SPavel Emelianov goto cont; 190079e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 190179e1ad14SJiri Benc RTM_NEWLINK, 190215e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 19037c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1904dc599f76SDavid Ahern flags, 190538e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 190679e1ad14SJiri Benc netnsid); 1907973462bbSDavid Gibson 1908f6c5775fSDavid Ahern if (err < 0) { 1909f6c5775fSDavid Ahern if (likely(skb->len)) 19107c28bd0bSEric Dumazet goto out; 19114e985adaSThomas Graf 1912f6c5775fSDavid Ahern goto out_err; 1913f6c5775fSDavid Ahern } 19147562f876SPavel Emelianov cont: 19157562f876SPavel Emelianov idx++; 19161da177e4SLinus Torvalds } 19177c28bd0bSEric Dumazet } 19187c28bd0bSEric Dumazet out: 1919f6c5775fSDavid Ahern err = skb->len; 1920f6c5775fSDavid Ahern out_err: 19217c28bd0bSEric Dumazet cb->args[1] = idx; 19227c28bd0bSEric Dumazet cb->args[0] = h; 1923d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 1924d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 192579e1ad14SJiri Benc if (netnsid >= 0) 192679e1ad14SJiri Benc put_net(tgt_net); 19271da177e4SLinus Torvalds 1928f6c5775fSDavid Ahern return err; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 1931fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1932fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 1933f7b12606SJiri Pirko { 1934fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1935f7b12606SJiri Pirko } 1936f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 193757b61080SScott Feldman 193881adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 193981adee47SEric W. Biederman { 194081adee47SEric W. Biederman struct net *net; 194181adee47SEric W. Biederman /* Examine the link attributes and figure out which 194281adee47SEric W. Biederman * network namespace we are talking about. 194381adee47SEric W. Biederman */ 194481adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 194581adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1946f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1947f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 194881adee47SEric W. Biederman else 194981adee47SEric W. Biederman net = get_net(src_net); 195081adee47SEric W. Biederman return net; 195181adee47SEric W. Biederman } 195281adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 195381adee47SEric W. Biederman 19547c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 19557c4f63baSChristian Brauner * examining the link attributes in the following order: 19567c4f63baSChristian Brauner * 19577c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 19587c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 19597c4f63baSChristian Brauner * 3. IFLA_IF_NETNSID 19607c4f63baSChristian Brauner */ 19617c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 19627c4f63baSChristian Brauner struct nlattr *tb[]) 19637c4f63baSChristian Brauner { 19647c4f63baSChristian Brauner struct net *net; 19657c4f63baSChristian Brauner 19667c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 19677c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 19687c4f63baSChristian Brauner 19697c4f63baSChristian Brauner if (!tb[IFLA_IF_NETNSID]) 19707c4f63baSChristian Brauner return get_net(src_net); 19717c4f63baSChristian Brauner 19727c4f63baSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 19737c4f63baSChristian Brauner if (!net) 19747c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 19757c4f63baSChristian Brauner 19767c4f63baSChristian Brauner return net; 19777c4f63baSChristian Brauner } 19787c4f63baSChristian Brauner 19797c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 19807c4f63baSChristian Brauner struct net *src_net, 19817c4f63baSChristian Brauner struct nlattr *tb[], int cap) 19827c4f63baSChristian Brauner { 19837c4f63baSChristian Brauner struct net *net; 19847c4f63baSChristian Brauner 19857c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 19867c4f63baSChristian Brauner if (IS_ERR(net)) 19877c4f63baSChristian Brauner return net; 19887c4f63baSChristian Brauner 19897c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 19907c4f63baSChristian Brauner put_net(net); 19917c4f63baSChristian Brauner return ERR_PTR(-EPERM); 19927c4f63baSChristian Brauner } 19937c4f63baSChristian Brauner 19947c4f63baSChristian Brauner return net; 19957c4f63baSChristian Brauner } 19967c4f63baSChristian Brauner 19974ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 19984ff66caeSChristian Brauner * potentially referring to different network namespaces. 19994ff66caeSChristian Brauner */ 20004ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 20014ff66caeSChristian Brauner struct netlink_ext_ack *extack, 20024ff66caeSChristian Brauner bool netns_id_only) 20034ff66caeSChristian Brauner { 20044ff66caeSChristian Brauner 20054ff66caeSChristian Brauner if (netns_id_only) { 20064ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 20074ff66caeSChristian Brauner return 0; 20084ff66caeSChristian Brauner 20094ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 20104ff66caeSChristian Brauner return -EOPNOTSUPP; 20114ff66caeSChristian Brauner } 20124ff66caeSChristian Brauner 20134ff66caeSChristian Brauner if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 20144ff66caeSChristian Brauner goto invalid_attr; 20154ff66caeSChristian Brauner 20164ff66caeSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 20174ff66caeSChristian Brauner goto invalid_attr; 20184ff66caeSChristian Brauner 20194ff66caeSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 20204ff66caeSChristian Brauner goto invalid_attr; 20214ff66caeSChristian Brauner 20224ff66caeSChristian Brauner return 0; 20234ff66caeSChristian Brauner 20244ff66caeSChristian Brauner invalid_attr: 20254ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 20264ff66caeSChristian Brauner return -EINVAL; 20274ff66caeSChristian Brauner } 20284ff66caeSChristian Brauner 20291840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 20301840bb13SThomas Graf { 20311840bb13SThomas Graf if (dev) { 20321840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 20331840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 20341840bb13SThomas Graf return -EINVAL; 20351840bb13SThomas Graf 20361840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 20371840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 20381840bb13SThomas Graf return -EINVAL; 20391840bb13SThomas Graf } 20401840bb13SThomas Graf 2041cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2042cf7afbfeSThomas Graf struct nlattr *af; 2043cf7afbfeSThomas Graf int rem, err; 2044cf7afbfeSThomas Graf 2045cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2046cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2047cf7afbfeSThomas Graf 20485fa85a09SFlorian Westphal rcu_read_lock(); 20495fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 20505fa85a09SFlorian Westphal if (!af_ops) { 20515fa85a09SFlorian Westphal rcu_read_unlock(); 2052cf7afbfeSThomas Graf return -EAFNOSUPPORT; 20535fa85a09SFlorian Westphal } 2054cf7afbfeSThomas Graf 20555fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 20565fa85a09SFlorian Westphal rcu_read_unlock(); 2057cf7afbfeSThomas Graf return -EOPNOTSUPP; 20585fa85a09SFlorian Westphal } 2059cf7afbfeSThomas Graf 2060cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 20616d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 20625fa85a09SFlorian Westphal if (err < 0) { 20635fa85a09SFlorian Westphal rcu_read_unlock(); 2064cf7afbfeSThomas Graf return err; 2065cf7afbfeSThomas Graf } 2066cf7afbfeSThomas Graf } 20675fa85a09SFlorian Westphal 20685fa85a09SFlorian Westphal rcu_read_unlock(); 20695fa85a09SFlorian Westphal } 2070cf7afbfeSThomas Graf } 2071cf7afbfeSThomas Graf 20721840bb13SThomas Graf return 0; 20731840bb13SThomas Graf } 20741840bb13SThomas Graf 2075cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2076cc8e27ccSEli Cohen int guid_type) 2077cc8e27ccSEli Cohen { 2078cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2079cc8e27ccSEli Cohen 2080cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2081cc8e27ccSEli Cohen } 2082cc8e27ccSEli Cohen 2083cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2084cc8e27ccSEli Cohen { 2085cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2086cc8e27ccSEli Cohen return -EOPNOTSUPP; 2087cc8e27ccSEli Cohen 2088cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2089cc8e27ccSEli Cohen } 2090cc8e27ccSEli Cohen 20914f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2092c02db8c6SChris Wright { 2093c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 20944f7d2cdfSDaniel Borkmann int err = -EINVAL; 2095c02db8c6SChris Wright 20964f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 20974f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 20984f7d2cdfSDaniel Borkmann 2099c02db8c6SChris Wright err = -EOPNOTSUPP; 2100c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2101c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2102c02db8c6SChris Wright ivm->mac); 21034f7d2cdfSDaniel Borkmann if (err < 0) 21044f7d2cdfSDaniel Borkmann return err; 2105c02db8c6SChris Wright } 21064f7d2cdfSDaniel Borkmann 21074f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 21084f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 21094f7d2cdfSDaniel Borkmann 2110c02db8c6SChris Wright err = -EOPNOTSUPP; 2111c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 21124f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 211379aab093SMoshe Shemesh ivv->qos, 211479aab093SMoshe Shemesh htons(ETH_P_8021Q)); 211579aab093SMoshe Shemesh if (err < 0) 211679aab093SMoshe Shemesh return err; 211779aab093SMoshe Shemesh } 211879aab093SMoshe Shemesh 211979aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 212079aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 212179aab093SMoshe Shemesh struct nlattr *attr; 212279aab093SMoshe Shemesh int rem, len = 0; 212379aab093SMoshe Shemesh 212479aab093SMoshe Shemesh err = -EOPNOTSUPP; 212579aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 212679aab093SMoshe Shemesh return err; 212779aab093SMoshe Shemesh 212879aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 212979aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 213079aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 213179aab093SMoshe Shemesh return -EINVAL; 213279aab093SMoshe Shemesh } 213379aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 213479aab093SMoshe Shemesh return -EOPNOTSUPP; 213579aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 213679aab093SMoshe Shemesh 213779aab093SMoshe Shemesh len++; 213879aab093SMoshe Shemesh } 2139fa34cd94SArnd Bergmann if (len == 0) 2140fa34cd94SArnd Bergmann return -EINVAL; 2141fa34cd94SArnd Bergmann 214279aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 214379aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 21444f7d2cdfSDaniel Borkmann if (err < 0) 21454f7d2cdfSDaniel Borkmann return err; 2146c02db8c6SChris Wright } 21474f7d2cdfSDaniel Borkmann 21484f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 21494f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2150ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 21514f7d2cdfSDaniel Borkmann 2152c02db8c6SChris Wright err = -EOPNOTSUPP; 2153ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 21544f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 21554f7d2cdfSDaniel Borkmann if (err < 0) 21564f7d2cdfSDaniel Borkmann return err; 21574f7d2cdfSDaniel Borkmann 2158ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2159ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2160ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2161ed616689SSucheta Chakraborty ivf.min_tx_rate, 2162c02db8c6SChris Wright ivt->rate); 21634f7d2cdfSDaniel Borkmann if (err < 0) 21644f7d2cdfSDaniel Borkmann return err; 2165c02db8c6SChris Wright } 21664f7d2cdfSDaniel Borkmann 21674f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 21684f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 21694f7d2cdfSDaniel Borkmann 2170ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2171ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2172ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2173ed616689SSucheta Chakraborty ivt->min_tx_rate, 2174ed616689SSucheta Chakraborty ivt->max_tx_rate); 21754f7d2cdfSDaniel Borkmann if (err < 0) 21764f7d2cdfSDaniel Borkmann return err; 2177ed616689SSucheta Chakraborty } 21784f7d2cdfSDaniel Borkmann 21794f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 21804f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 21814f7d2cdfSDaniel Borkmann 21825f8444a3SGreg Rose err = -EOPNOTSUPP; 21835f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 21845f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 21855f8444a3SGreg Rose ivs->setting); 21864f7d2cdfSDaniel Borkmann if (err < 0) 21874f7d2cdfSDaniel Borkmann return err; 21885f8444a3SGreg Rose } 21894f7d2cdfSDaniel Borkmann 21904f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 21914f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 21924f7d2cdfSDaniel Borkmann 21931d8faf48SRony Efraim err = -EOPNOTSUPP; 21941d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 21951d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 21961d8faf48SRony Efraim ivl->link_state); 21974f7d2cdfSDaniel Borkmann if (err < 0) 21984f7d2cdfSDaniel Borkmann return err; 21991d8faf48SRony Efraim } 22004f7d2cdfSDaniel Borkmann 22014f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 220201a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 220301a3d796SVlad Zolotarov 220401a3d796SVlad Zolotarov err = -EOPNOTSUPP; 22054f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 220601a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 22074f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 220801a3d796SVlad Zolotarov ivrssq_en->setting); 22094f7d2cdfSDaniel Borkmann if (err < 0) 22104f7d2cdfSDaniel Borkmann return err; 221101a3d796SVlad Zolotarov } 22124f7d2cdfSDaniel Borkmann 2213dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2214dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2215dd461d6aSHiroshi Shimamoto 2216dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2217dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2218dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2219dd461d6aSHiroshi Shimamoto if (err < 0) 2220dd461d6aSHiroshi Shimamoto return err; 2221dd461d6aSHiroshi Shimamoto } 2222dd461d6aSHiroshi Shimamoto 2223cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2224cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2225cc8e27ccSEli Cohen 2226cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2227cc8e27ccSEli Cohen return -EOPNOTSUPP; 2228cc8e27ccSEli Cohen 2229cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2230cc8e27ccSEli Cohen } 2231cc8e27ccSEli Cohen 2232cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2233cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2234cc8e27ccSEli Cohen 2235cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2236cc8e27ccSEli Cohen return -EOPNOTSUPP; 2237cc8e27ccSEli Cohen 2238cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2239cc8e27ccSEli Cohen } 2240cc8e27ccSEli Cohen 2241c02db8c6SChris Wright return err; 2242c02db8c6SChris Wright } 2243c02db8c6SChris Wright 224433eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 224533eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2246fbaec0eaSJiri Pirko { 2247898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2248fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2249fbaec0eaSJiri Pirko int err; 2250fbaec0eaSJiri Pirko 2251898e5061SJiri Pirko if (upper_dev) { 2252898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2253fbaec0eaSJiri Pirko return 0; 2254898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2255fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2256898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2257fbaec0eaSJiri Pirko if (err) 2258fbaec0eaSJiri Pirko return err; 2259fbaec0eaSJiri Pirko } else { 2260fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2261fbaec0eaSJiri Pirko } 2262fbaec0eaSJiri Pirko } 2263fbaec0eaSJiri Pirko 2264fbaec0eaSJiri Pirko if (ifindex) { 2265898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2266898e5061SJiri Pirko if (!upper_dev) 2267fbaec0eaSJiri Pirko return -EINVAL; 2268898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2269fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 227033eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2271fbaec0eaSJiri Pirko if (err) 2272fbaec0eaSJiri Pirko return err; 2273fbaec0eaSJiri Pirko } else { 2274fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2275fbaec0eaSJiri Pirko } 2276fbaec0eaSJiri Pirko } 2277fbaec0eaSJiri Pirko return 0; 2278fbaec0eaSJiri Pirko } 2279fbaec0eaSJiri Pirko 228090c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2281ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2282ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 228390f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 228490f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2285ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 228690c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 22870157f60cSPatrick McHardy { 2288d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 22890157f60cSPatrick McHardy int err; 22900157f60cSPatrick McHardy 2291644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2292644c7eebSEric Dumazet if (err < 0) 2293644c7eebSEric Dumazet return err; 2294644c7eebSEric Dumazet 22957c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 22967c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 22977c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2298d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2299d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2300d8a5ec67SEric W. Biederman goto errout; 2301d8a5ec67SEric W. Biederman } 23027c4f63baSChristian Brauner 2303d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2304d8a5ec67SEric W. Biederman put_net(net); 2305d8a5ec67SEric W. Biederman if (err) 2306d8a5ec67SEric W. Biederman goto errout; 230790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2308d8a5ec67SEric W. Biederman } 2309d8a5ec67SEric W. Biederman 23100157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 23110157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 23120157f60cSPatrick McHardy struct ifmap k_map; 23130157f60cSPatrick McHardy 2314d314774cSStephen Hemminger if (!ops->ndo_set_config) { 23150157f60cSPatrick McHardy err = -EOPNOTSUPP; 23160157f60cSPatrick McHardy goto errout; 23170157f60cSPatrick McHardy } 23180157f60cSPatrick McHardy 23190157f60cSPatrick McHardy if (!netif_device_present(dev)) { 23200157f60cSPatrick McHardy err = -ENODEV; 23210157f60cSPatrick McHardy goto errout; 23220157f60cSPatrick McHardy } 23230157f60cSPatrick McHardy 23240157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 23250157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 23260157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 23270157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 23280157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 23290157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 23300157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 23310157f60cSPatrick McHardy 2332d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 23330157f60cSPatrick McHardy if (err < 0) 23340157f60cSPatrick McHardy goto errout; 23350157f60cSPatrick McHardy 2336ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23370157f60cSPatrick McHardy } 23380157f60cSPatrick McHardy 23390157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 23400157f60cSPatrick McHardy struct sockaddr *sa; 23410157f60cSPatrick McHardy int len; 23420157f60cSPatrick McHardy 2343153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2344153711f9SWANG Cong sizeof(*sa)); 23450157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 23460157f60cSPatrick McHardy if (!sa) { 23470157f60cSPatrick McHardy err = -ENOMEM; 23480157f60cSPatrick McHardy goto errout; 23490157f60cSPatrick McHardy } 23500157f60cSPatrick McHardy sa->sa_family = dev->type; 23510157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 23520157f60cSPatrick McHardy dev->addr_len); 2353e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 23540157f60cSPatrick McHardy kfree(sa); 23550157f60cSPatrick McHardy if (err) 23560157f60cSPatrick McHardy goto errout; 235790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23580157f60cSPatrick McHardy } 23590157f60cSPatrick McHardy 23600157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 23610157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 23620157f60cSPatrick McHardy if (err < 0) 23630157f60cSPatrick McHardy goto errout; 236490c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23650157f60cSPatrick McHardy } 23660157f60cSPatrick McHardy 2367cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2368cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2369ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2370cbda10faSVlad Dogaru } 2371cbda10faSVlad Dogaru 23720157f60cSPatrick McHardy /* 23730157f60cSPatrick McHardy * Interface selected by interface index but interface 23740157f60cSPatrick McHardy * name provided implies that a name change has been 23750157f60cSPatrick McHardy * requested. 23760157f60cSPatrick McHardy */ 23770157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 23780157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 23790157f60cSPatrick McHardy if (err < 0) 23800157f60cSPatrick McHardy goto errout; 238190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23820157f60cSPatrick McHardy } 23830157f60cSPatrick McHardy 23840b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 23850b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 23860b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 23870b815a1aSStephen Hemminger if (err < 0) 23880b815a1aSStephen Hemminger goto errout; 2389ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23900b815a1aSStephen Hemminger } 23910b815a1aSStephen Hemminger 23920157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 23930157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2394e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 23950157f60cSPatrick McHardy } 23960157f60cSPatrick McHardy 23970157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 23983729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 23995f9021cfSJohannes Berg if (err < 0) 24005f9021cfSJohannes Berg goto errout; 24010157f60cSPatrick McHardy } 24020157f60cSPatrick McHardy 2403fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 240433eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2405fbaec0eaSJiri Pirko if (err) 2406fbaec0eaSJiri Pirko goto errout; 240790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2408fbaec0eaSJiri Pirko } 2409fbaec0eaSJiri Pirko 24109a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 24119a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 24129a57247fSJiri Pirko if (err) 24139a57247fSJiri Pirko goto errout; 241490c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24159a57247fSJiri Pirko } 24169a57247fSJiri Pirko 24175d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 24180cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 24195d1180fcSNicolas Dichtel 24206a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 24216a643ddbSCong Wang if (err) 242208294a26SJason Wang goto errout; 24232d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 242408294a26SJason Wang } 24250157f60cSPatrick McHardy 242646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 242746e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 242846e6b992SStephen Hemminger 242946e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 243046e6b992SStephen Hemminger err = -EINVAL; 243146e6b992SStephen Hemminger goto errout; 243246e6b992SStephen Hemminger } 243346e6b992SStephen Hemminger 243446e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 243546e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 243646e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 243746e6b992SStephen Hemminger } 243846e6b992SStephen Hemminger } 243946e6b992SStephen Hemminger 244046e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 244146e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 244246e6b992SStephen Hemminger 244346e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 244446e6b992SStephen Hemminger err = -EINVAL; 244546e6b992SStephen Hemminger goto errout; 244646e6b992SStephen Hemminger } 244746e6b992SStephen Hemminger 244846e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 244946e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 245046e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 245146e6b992SStephen Hemminger } 245246e6b992SStephen Hemminger } 245346e6b992SStephen Hemminger 24540157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 245593b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 24560157f60cSPatrick McHardy 24570157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 24581889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 24591889b0e7SNicolas Dichtel 24600157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 24611889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2462ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24631889b0e7SNicolas Dichtel dev->link_mode = value; 246493b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 24650157f60cSPatrick McHardy } 24660157f60cSPatrick McHardy 2467c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 24684f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2469c02db8c6SChris Wright struct nlattr *attr; 2470c02db8c6SChris Wright int rem; 24714f7d2cdfSDaniel Borkmann 2472c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 24734f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 24744f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2475253683bbSDavid Howells err = -EINVAL; 2476c02db8c6SChris Wright goto errout; 2477253683bbSDavid Howells } 24784f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2479fceb6435SJohannes Berg ifla_vf_policy, NULL); 24804f7d2cdfSDaniel Borkmann if (err < 0) 24814f7d2cdfSDaniel Borkmann goto errout; 24824f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2483ebc08a6fSWilliams, Mitch A if (err < 0) 2484ebc08a6fSWilliams, Mitch A goto errout; 2485ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2486ebc08a6fSWilliams, Mitch A } 2487ebc08a6fSWilliams, Mitch A } 24880157f60cSPatrick McHardy err = 0; 24890157f60cSPatrick McHardy 249057b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 249157b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 249257b61080SScott Feldman struct nlattr *attr; 249357b61080SScott Feldman int vf; 249457b61080SScott Feldman int rem; 249557b61080SScott Feldman 249657b61080SScott Feldman err = -EOPNOTSUPP; 249757b61080SScott Feldman if (!ops->ndo_set_vf_port) 249857b61080SScott Feldman goto errout; 249957b61080SScott Feldman 250057b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2501035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2502035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2503035d210fSDaniel Borkmann err = -EINVAL; 2504035d210fSDaniel Borkmann goto errout; 2505035d210fSDaniel Borkmann } 2506035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2507fceb6435SJohannes Berg ifla_port_policy, NULL); 250857b61080SScott Feldman if (err < 0) 250957b61080SScott Feldman goto errout; 251057b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 251157b61080SScott Feldman err = -EOPNOTSUPP; 251257b61080SScott Feldman goto errout; 251357b61080SScott Feldman } 251457b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 251557b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 251657b61080SScott Feldman if (err < 0) 251757b61080SScott Feldman goto errout; 2518ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 251957b61080SScott Feldman } 252057b61080SScott Feldman } 252157b61080SScott Feldman err = 0; 252257b61080SScott Feldman 252357b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 252457b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 252557b61080SScott Feldman 252657b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2527fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2528fceb6435SJohannes Berg NULL); 252957b61080SScott Feldman if (err < 0) 253057b61080SScott Feldman goto errout; 253157b61080SScott Feldman 253257b61080SScott Feldman err = -EOPNOTSUPP; 253357b61080SScott Feldman if (ops->ndo_set_vf_port) 253457b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 253557b61080SScott Feldman if (err < 0) 253657b61080SScott Feldman goto errout; 2537ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 253857b61080SScott Feldman } 2539f8ff182cSThomas Graf 2540f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2541f8ff182cSThomas Graf struct nlattr *af; 2542f8ff182cSThomas Graf int rem; 2543f8ff182cSThomas Graf 2544f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2545f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2546f8ff182cSThomas Graf 25475fa85a09SFlorian Westphal rcu_read_lock(); 25485fa85a09SFlorian Westphal 2549058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2550f8ff182cSThomas Graf 2551cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 25525fa85a09SFlorian Westphal if (err < 0) { 25535fa85a09SFlorian Westphal rcu_read_unlock(); 2554f8ff182cSThomas Graf goto errout; 25555fa85a09SFlorian Westphal } 2556f8ff182cSThomas Graf 25575fa85a09SFlorian Westphal rcu_read_unlock(); 2558ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2559f8ff182cSThomas Graf } 2560f8ff182cSThomas Graf } 256157b61080SScott Feldman err = 0; 256257b61080SScott Feldman 256388d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 256488d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 256588d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 256688d6378bSAnuradha Karuppiah if (err) 256788d6378bSAnuradha Karuppiah goto errout; 256888d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 256988d6378bSAnuradha Karuppiah } 257088d6378bSAnuradha Karuppiah 2571d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2572d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 257385de8576SDaniel Borkmann u32 xdp_flags = 0; 2574d1fdd913SBrenden Blanco 2575d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2576fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2577d1fdd913SBrenden Blanco if (err < 0) 2578d1fdd913SBrenden Blanco goto errout; 2579d1fdd913SBrenden Blanco 258058038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2581262d8625SBrenden Blanco err = -EINVAL; 2582262d8625SBrenden Blanco goto errout; 2583262d8625SBrenden Blanco } 258485de8576SDaniel Borkmann 258585de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 258685de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 258785de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 258885de8576SDaniel Borkmann err = -EINVAL; 258985de8576SDaniel Borkmann goto errout; 259085de8576SDaniel Borkmann } 2591ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 25920489df9aSDaniel Borkmann err = -EINVAL; 25930489df9aSDaniel Borkmann goto errout; 25940489df9aSDaniel Borkmann } 259585de8576SDaniel Borkmann } 259685de8576SDaniel Borkmann 2597d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2598ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 259985de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 260085de8576SDaniel Borkmann xdp_flags); 2601d1fdd913SBrenden Blanco if (err) 2602d1fdd913SBrenden Blanco goto errout; 2603d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2604d1fdd913SBrenden Blanco } 2605d1fdd913SBrenden Blanco } 2606d1fdd913SBrenden Blanco 26070157f60cSPatrick McHardy errout: 2608ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 260964ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2610ba998906SNicolas Dichtel netdev_state_change(dev); 2611ba998906SNicolas Dichtel 2612ba998906SNicolas Dichtel if (err < 0) 2613e87cc472SJoe 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", 2614e87cc472SJoe Perches dev->name); 2615ba998906SNicolas Dichtel } 26160157f60cSPatrick McHardy 26170157f60cSPatrick McHardy return err; 26180157f60cSPatrick McHardy } 26190157f60cSPatrick McHardy 2620c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2621c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2622da5e0494SThomas Graf { 26233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2624da5e0494SThomas Graf struct ifinfomsg *ifm; 2625da5e0494SThomas Graf struct net_device *dev; 26260157f60cSPatrick McHardy int err; 2627da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 26281da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 26291da177e4SLinus Torvalds 2630c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2631c21ef3e3SDavid Ahern extack); 2632da5e0494SThomas Graf if (err < 0) 2633da5e0494SThomas Graf goto errout; 26341da177e4SLinus Torvalds 26354ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 26364ff66caeSChristian Brauner if (err < 0) 26374ff66caeSChristian Brauner goto errout; 26384ff66caeSChristian Brauner 26395176f91eSThomas Graf if (tb[IFLA_IFNAME]) 26405176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 264178e5b891SPatrick McHardy else 264278e5b891SPatrick McHardy ifname[0] = '\0'; 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds err = -EINVAL; 2645da5e0494SThomas Graf ifm = nlmsg_data(nlh); 264651055be8SPatrick McHardy if (ifm->ifi_index > 0) 2647a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2648da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2649a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2650da5e0494SThomas Graf else 2651da5e0494SThomas Graf goto errout; 26521da177e4SLinus Torvalds 2653da5e0494SThomas Graf if (dev == NULL) { 2654da5e0494SThomas Graf err = -ENODEV; 2655da5e0494SThomas Graf goto errout; 2656da5e0494SThomas Graf } 26571da177e4SLinus Torvalds 2658ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2659da5e0494SThomas Graf errout: 26601da177e4SLinus Torvalds return err; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 266366400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 266466400d54SWANG Cong { 266566400d54SWANG Cong struct net_device *dev, *aux; 266666400d54SWANG Cong LIST_HEAD(list_kill); 266766400d54SWANG Cong bool found = false; 266866400d54SWANG Cong 266966400d54SWANG Cong if (!group) 267066400d54SWANG Cong return -EPERM; 267166400d54SWANG Cong 267266400d54SWANG Cong for_each_netdev(net, dev) { 267366400d54SWANG Cong if (dev->group == group) { 267466400d54SWANG Cong const struct rtnl_link_ops *ops; 267566400d54SWANG Cong 267666400d54SWANG Cong found = true; 267766400d54SWANG Cong ops = dev->rtnl_link_ops; 267866400d54SWANG Cong if (!ops || !ops->dellink) 267966400d54SWANG Cong return -EOPNOTSUPP; 268066400d54SWANG Cong } 268166400d54SWANG Cong } 268266400d54SWANG Cong 268366400d54SWANG Cong if (!found) 268466400d54SWANG Cong return -ENODEV; 268566400d54SWANG Cong 268666400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 268766400d54SWANG Cong if (dev->group == group) { 268866400d54SWANG Cong const struct rtnl_link_ops *ops; 268966400d54SWANG Cong 269066400d54SWANG Cong ops = dev->rtnl_link_ops; 269166400d54SWANG Cong ops->dellink(dev, &list_kill); 269266400d54SWANG Cong } 269366400d54SWANG Cong } 269466400d54SWANG Cong unregister_netdevice_many(&list_kill); 269566400d54SWANG Cong 269666400d54SWANG Cong return 0; 269766400d54SWANG Cong } 269866400d54SWANG Cong 2699614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2700614732eaSThomas Graf { 2701614732eaSThomas Graf const struct rtnl_link_ops *ops; 2702614732eaSThomas Graf LIST_HEAD(list_kill); 2703614732eaSThomas Graf 2704614732eaSThomas Graf ops = dev->rtnl_link_ops; 2705614732eaSThomas Graf if (!ops || !ops->dellink) 2706614732eaSThomas Graf return -EOPNOTSUPP; 2707614732eaSThomas Graf 2708614732eaSThomas Graf ops->dellink(dev, &list_kill); 2709614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2710614732eaSThomas Graf 2711614732eaSThomas Graf return 0; 2712614732eaSThomas Graf } 2713614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2714614732eaSThomas Graf 2715c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2716c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 271738f7b870SPatrick McHardy { 27183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2719b61ad68aSChristian Brauner struct net *tgt_net = net; 2720b61ad68aSChristian Brauner struct net_device *dev = NULL; 272138f7b870SPatrick McHardy struct ifinfomsg *ifm; 272238f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 272338f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 272438f7b870SPatrick McHardy int err; 2725b61ad68aSChristian Brauner int netnsid = -1; 272638f7b870SPatrick McHardy 2727c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 272838f7b870SPatrick McHardy if (err < 0) 272938f7b870SPatrick McHardy return err; 273038f7b870SPatrick McHardy 27314ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 27324ff66caeSChristian Brauner if (err < 0) 27334ff66caeSChristian Brauner return err; 27344ff66caeSChristian Brauner 273538f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 273638f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 273738f7b870SPatrick McHardy 2738b61ad68aSChristian Brauner if (tb[IFLA_IF_NETNSID]) { 2739b61ad68aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2740b61ad68aSChristian Brauner tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2741b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2742b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2743b61ad68aSChristian Brauner } 2744b61ad68aSChristian Brauner 2745b61ad68aSChristian Brauner err = -EINVAL; 274638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 274738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2748b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 274938f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2750b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 275166400d54SWANG Cong else if (tb[IFLA_GROUP]) 2752b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 275338f7b870SPatrick McHardy else 2754b61ad68aSChristian Brauner goto out; 275538f7b870SPatrick McHardy 2756b61ad68aSChristian Brauner if (!dev) { 2757b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2758b61ad68aSChristian Brauner err = -ENODEV; 275938f7b870SPatrick McHardy 2760b61ad68aSChristian Brauner goto out; 2761b61ad68aSChristian Brauner } 2762b61ad68aSChristian Brauner 2763b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2764b61ad68aSChristian Brauner 2765b61ad68aSChristian Brauner out: 2766b61ad68aSChristian Brauner if (netnsid >= 0) 2767b61ad68aSChristian Brauner put_net(tgt_net); 2768b61ad68aSChristian Brauner 2769b61ad68aSChristian Brauner return err; 277038f7b870SPatrick McHardy } 277138f7b870SPatrick McHardy 27723729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 27733729d502SPatrick McHardy { 27743729d502SPatrick McHardy unsigned int old_flags; 27753729d502SPatrick McHardy int err; 27763729d502SPatrick McHardy 27773729d502SPatrick McHardy old_flags = dev->flags; 27783729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 27793729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 27803729d502SPatrick McHardy if (err < 0) 27813729d502SPatrick McHardy return err; 27823729d502SPatrick McHardy } 27833729d502SPatrick McHardy 27843729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 27853729d502SPatrick McHardy 2786a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 27873729d502SPatrick McHardy return 0; 27883729d502SPatrick McHardy } 27893729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 27903729d502SPatrick McHardy 2791c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 279278ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 27935517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2794e7199288SPavel Emelianov { 2795e7199288SPavel Emelianov struct net_device *dev; 2796d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2797d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2798e7199288SPavel Emelianov 279976ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 280076ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 280176ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2802d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 280376ff5cc9SJiri Pirko 280476ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 280576ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 280676ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2807d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2808efacb309Sstephen hemminger 28095517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2810c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2811e7199288SPavel Emelianov if (!dev) 2812d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2813e7199288SPavel Emelianov 281481adee47SEric W. Biederman dev_net_set(dev, net); 281581adee47SEric W. Biederman dev->rtnl_link_ops = ops; 28163729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 281781adee47SEric W. Biederman 2818e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2819e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 28202afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2821e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2822e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 28232afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 28242afb9b53SJiri Pirko } 2825e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2826e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2827e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2828e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2829e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2830e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 283193b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2832e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2833e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2834ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2835ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 283646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 283746e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 283846e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2839a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2840e7199288SPavel Emelianov 2841e7199288SPavel Emelianov return dev; 2842e7199288SPavel Emelianov } 2843e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2844e7199288SPavel Emelianov 284590f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 284690f62cf3SEric W. Biederman struct net *net, int group, 2847e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2848ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2849e7ed828fSVlad Dogaru struct nlattr **tb) 2850e7ed828fSVlad Dogaru { 2851d079535dSWANG Cong struct net_device *dev, *aux; 2852e7ed828fSVlad Dogaru int err; 2853e7ed828fSVlad Dogaru 2854d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2855e7ed828fSVlad Dogaru if (dev->group == group) { 2856ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2857e7ed828fSVlad Dogaru if (err < 0) 2858e7ed828fSVlad Dogaru return err; 2859e7ed828fSVlad Dogaru } 2860e7ed828fSVlad Dogaru } 2861e7ed828fSVlad Dogaru 2862e7ed828fSVlad Dogaru return 0; 2863e7ed828fSVlad Dogaru } 2864e7ed828fSVlad Dogaru 2865c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2866c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 286738f7b870SPatrick McHardy { 28683b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 286938f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2870ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 287138f7b870SPatrick McHardy struct net_device *dev; 2872ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 287338f7b870SPatrick McHardy struct ifinfomsg *ifm; 287438f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 287538f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 287638f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 287738f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 28785517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 287938f7b870SPatrick McHardy int err; 288038f7b870SPatrick McHardy 288195a5afcaSJohannes Berg #ifdef CONFIG_MODULES 288238f7b870SPatrick McHardy replay: 28838072f085SThomas Graf #endif 2884c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 288538f7b870SPatrick McHardy if (err < 0) 288638f7b870SPatrick McHardy return err; 288738f7b870SPatrick McHardy 28884ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 28894ff66caeSChristian Brauner if (err < 0) 28904ff66caeSChristian Brauner return err; 28914ff66caeSChristian Brauner 289238f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 289338f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 289438f7b870SPatrick McHardy else 289538f7b870SPatrick McHardy ifname[0] = '\0'; 289638f7b870SPatrick McHardy 289738f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 289838f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2899881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 2900e7ed828fSVlad Dogaru else { 2901e7ed828fSVlad Dogaru if (ifname[0]) 2902881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 290338f7b870SPatrick McHardy else 290438f7b870SPatrick McHardy dev = NULL; 2905e7ed828fSVlad Dogaru } 290638f7b870SPatrick McHardy 2907ba7d49b1SJiri Pirko if (dev) { 2908ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 2909ba7d49b1SJiri Pirko if (master_dev) 2910ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 2911ba7d49b1SJiri Pirko } 2912ba7d49b1SJiri Pirko 2913e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2914e0d087afSEric Dumazet if (err < 0) 29151840bb13SThomas Graf return err; 29161840bb13SThomas Graf 291738f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 291838f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2919fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 2920fceb6435SJohannes Berg NULL); 292138f7b870SPatrick McHardy if (err < 0) 292238f7b870SPatrick McHardy return err; 292338f7b870SPatrick McHardy } else 292438f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 292538f7b870SPatrick McHardy 292638f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 292738f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 292838f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 292938f7b870SPatrick McHardy } else { 293038f7b870SPatrick McHardy kind[0] = '\0'; 293138f7b870SPatrick McHardy ops = NULL; 293238f7b870SPatrick McHardy } 293338f7b870SPatrick McHardy 293438f7b870SPatrick McHardy if (1) { 2935ccf8dbcdSKees Cook struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2936ccf8dbcdSKees Cook struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2937ba7d49b1SJiri Pirko struct nlattr **data = NULL; 2938ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 2939317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 294038f7b870SPatrick McHardy 294138f7b870SPatrick McHardy if (ops) { 2942ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 2943ccf8dbcdSKees Cook return -EINVAL; 2944ccf8dbcdSKees Cook 294538f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 294638f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 294738f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 2948fceb6435SJohannes Berg ops->policy, NULL); 294938f7b870SPatrick McHardy if (err < 0) 295038f7b870SPatrick McHardy return err; 295138f7b870SPatrick McHardy data = attr; 295238f7b870SPatrick McHardy } 295338f7b870SPatrick McHardy if (ops->validate) { 2954a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 295538f7b870SPatrick McHardy if (err < 0) 295638f7b870SPatrick McHardy return err; 295738f7b870SPatrick McHardy } 295838f7b870SPatrick McHardy } 295938f7b870SPatrick McHardy 2960ba7d49b1SJiri Pirko if (m_ops) { 2961af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2962ccf8dbcdSKees Cook return -EINVAL; 2963ccf8dbcdSKees Cook 2964ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 2965ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 2966ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 2967ba7d49b1SJiri Pirko m_ops->slave_maxtype, 2968ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 2969fceb6435SJohannes Berg m_ops->slave_policy, 2970fceb6435SJohannes Berg NULL); 2971ba7d49b1SJiri Pirko if (err < 0) 2972ba7d49b1SJiri Pirko return err; 2973ba7d49b1SJiri Pirko slave_data = slave_attr; 2974ba7d49b1SJiri Pirko } 2975ba7d49b1SJiri Pirko } 2976ba7d49b1SJiri Pirko 297738f7b870SPatrick McHardy if (dev) { 297890c325e3SNicolas Dichtel int status = 0; 297938f7b870SPatrick McHardy 298038f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 298138f7b870SPatrick McHardy return -EEXIST; 298238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 298338f7b870SPatrick McHardy return -EOPNOTSUPP; 298438f7b870SPatrick McHardy 298538f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 298638f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 298738f7b870SPatrick McHardy !ops->changelink) 298838f7b870SPatrick McHardy return -EOPNOTSUPP; 298938f7b870SPatrick McHardy 2990ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 299138f7b870SPatrick McHardy if (err < 0) 299238f7b870SPatrick McHardy return err; 2993ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 299438f7b870SPatrick McHardy } 299538f7b870SPatrick McHardy 2996ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2997ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 2998ba7d49b1SJiri Pirko return -EOPNOTSUPP; 2999ba7d49b1SJiri Pirko 3000ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 300117dd0ec4SMatthias Schiffer tb, slave_data, 300217dd0ec4SMatthias Schiffer extack); 3003ba7d49b1SJiri Pirko if (err < 0) 3004ba7d49b1SJiri Pirko return err; 3005ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3006ba7d49b1SJiri Pirko } 3007ba7d49b1SJiri Pirko 3008ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 3009ddf9f970SJakub Kicinski status); 301038f7b870SPatrick McHardy } 301138f7b870SPatrick McHardy 3012ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3013ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 301490f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3015ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3016ddf9f970SJakub Kicinski ifm, extack, tb); 301738f7b870SPatrick McHardy return -ENODEV; 3018ffa934f1SPatrick McHardy } 301938f7b870SPatrick McHardy 3020160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 302138f7b870SPatrick McHardy return -EOPNOTSUPP; 302238f7b870SPatrick McHardy 302338f7b870SPatrick McHardy if (!ops) { 302495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 302538f7b870SPatrick McHardy if (kind[0]) { 302638f7b870SPatrick McHardy __rtnl_unlock(); 302738f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 302838f7b870SPatrick McHardy rtnl_lock(); 302938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 303038f7b870SPatrick McHardy if (ops) 303138f7b870SPatrick McHardy goto replay; 303238f7b870SPatrick McHardy } 303338f7b870SPatrick McHardy #endif 303438f7b870SPatrick McHardy return -EOPNOTSUPP; 303538f7b870SPatrick McHardy } 303638f7b870SPatrick McHardy 3037b0ab2fabSJiri Pirko if (!ops->setup) 3038b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3039b0ab2fabSJiri Pirko 30405517750fSTom Gundersen if (!ifname[0]) { 304138f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 30425517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 30435517750fSTom Gundersen } 304438f7b870SPatrick McHardy 30455bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 304613ad1774SEric W. Biederman if (IS_ERR(dest_net)) 304713ad1774SEric W. Biederman return PTR_ERR(dest_net); 304813ad1774SEric W. Biederman 3049317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3050317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3051317f4810SNicolas Dichtel 3052317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3053317f4810SNicolas Dichtel if (!link_net) { 3054317f4810SNicolas Dichtel err = -EINVAL; 3055317f4810SNicolas Dichtel goto out; 3056317f4810SNicolas Dichtel } 305706615bedSEric W. Biederman err = -EPERM; 305806615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 305906615bedSEric W. Biederman goto out; 3060317f4810SNicolas Dichtel } 3061317f4810SNicolas Dichtel 3062317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3063317f4810SNicolas Dichtel name_assign_type, ops, tb); 30649c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3065e7199288SPavel Emelianov err = PTR_ERR(dev); 30669c7dafbfSPavel Emelyanov goto out; 30679c7dafbfSPavel Emelyanov } 30689c7dafbfSPavel Emelyanov 30699c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 30709c7dafbfSPavel Emelyanov 30710e0eee24SCong Wang if (ops->newlink) { 30727a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 30737a3f4a18SMatthias Schiffer extack); 30740e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3075e51fb152SCong Wang * and unregister it on failure after registration 3076e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 30770e0eee24SCong Wang */ 3078e51fb152SCong Wang if (err < 0) { 3079e51fb152SCong Wang /* If device is not registered at all, free it now */ 3080e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3081e51fb152SCong Wang free_netdev(dev); 30820e0eee24SCong Wang goto out; 3083e51fb152SCong Wang } 30840e0eee24SCong Wang } else { 30852d85cba2SPatrick McHardy err = register_netdevice(dev); 3086fce9b9beSDan Carpenter if (err < 0) { 308738f7b870SPatrick McHardy free_netdev(dev); 30883729d502SPatrick McHardy goto out; 3089fce9b9beSDan Carpenter } 30900e0eee24SCong Wang } 30913729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 309243638900SDavid S. Miller if (err < 0) 309343638900SDavid S. Miller goto out_unregister; 309443638900SDavid S. Miller if (link_net) { 309543638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 309643638900SDavid S. Miller if (err < 0) 309743638900SDavid S. Miller goto out_unregister; 309843638900SDavid S. Miller } 3099160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 310033eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 310133eaf2a6SDavid Ahern extack); 3102160ca014STheuns Verwoerd if (err) 3103160ca014STheuns Verwoerd goto out_unregister; 3104160ca014STheuns Verwoerd } 310543638900SDavid S. Miller out: 310643638900SDavid S. Miller if (link_net) 310743638900SDavid S. Miller put_net(link_net); 310843638900SDavid S. Miller put_net(dest_net); 310943638900SDavid S. Miller return err; 311043638900SDavid S. Miller out_unregister: 31117afb8886SWANG Cong if (ops->newlink) { 31127afb8886SWANG Cong LIST_HEAD(list_kill); 31137afb8886SWANG Cong 31147afb8886SWANG Cong ops->dellink(dev, &list_kill); 31157afb8886SWANG Cong unregister_netdevice_many(&list_kill); 31167afb8886SWANG Cong } else { 31173729d502SPatrick McHardy unregister_netdevice(dev); 31187afb8886SWANG Cong } 3119317f4810SNicolas Dichtel goto out; 3120317f4810SNicolas Dichtel } 312138f7b870SPatrick McHardy } 312238f7b870SPatrick McHardy 3123c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3124c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3125711e2c33SJean Tourrilhes { 31263b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 312779e1ad14SJiri Benc struct net *tgt_net = net; 3128b60c5115SThomas Graf struct ifinfomsg *ifm; 3129a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3130b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3131b60c5115SThomas Graf struct net_device *dev = NULL; 3132b60c5115SThomas Graf struct sk_buff *nskb; 313379e1ad14SJiri Benc int netnsid = -1; 3134339bf98fSThomas Graf int err; 3135115c9b81SGreg Rose u32 ext_filter_mask = 0; 3136711e2c33SJean Tourrilhes 3137c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3138b60c5115SThomas Graf if (err < 0) 31399918f230SEric Sesterhenn return err; 3140b60c5115SThomas Graf 31414ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31424ff66caeSChristian Brauner if (err < 0) 31434ff66caeSChristian Brauner return err; 31444ff66caeSChristian Brauner 314579e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 314679e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3147f428fe4aSAndrei Vagin tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 314879e1ad14SJiri Benc if (IS_ERR(tgt_net)) 314979e1ad14SJiri Benc return PTR_ERR(tgt_net); 315079e1ad14SJiri Benc } 315179e1ad14SJiri Benc 3152a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3153a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3154a3d12891SEric Dumazet 3155115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3156115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3157115c9b81SGreg Rose 315879e1ad14SJiri Benc err = -EINVAL; 3159b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3160a3d12891SEric Dumazet if (ifm->ifi_index > 0) 316179e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3162a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 316379e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3164a3d12891SEric Dumazet else 316579e1ad14SJiri Benc goto out; 3166b60c5115SThomas Graf 316779e1ad14SJiri Benc err = -ENODEV; 3168a3d12891SEric Dumazet if (dev == NULL) 316979e1ad14SJiri Benc goto out; 3170a3d12891SEric Dumazet 317179e1ad14SJiri Benc err = -ENOBUFS; 3172115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3173a3d12891SEric Dumazet if (nskb == NULL) 317479e1ad14SJiri Benc goto out; 3175711e2c33SJean Tourrilhes 317679e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 317779e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 317879e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 317938e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 318026932566SPatrick McHardy if (err < 0) { 318126932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 318226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 318326932566SPatrick McHardy kfree_skb(nskb); 3184a3d12891SEric Dumazet } else 318515e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 318679e1ad14SJiri Benc out: 318779e1ad14SJiri Benc if (netnsid >= 0) 318879e1ad14SJiri Benc put_net(tgt_net); 3189b60c5115SThomas Graf 3190711e2c33SJean Tourrilhes return err; 3191711e2c33SJean Tourrilhes } 3192711e2c33SJean Tourrilhes 3193115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3194c7ac8679SGreg Rose { 3195115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3196115c9b81SGreg Rose struct net_device *dev; 3197115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3198115c9b81SGreg Rose u32 ext_filter_mask = 0; 3199115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3200e5eca6d4SMichal Schmidt int hdrlen; 3201115c9b81SGreg Rose 3202e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3203e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3204e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3205e5eca6d4SMichal Schmidt 3206fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3207115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3208115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3209a4b64fbeSEric Dumazet } 3210115c9b81SGreg Rose 3211115c9b81SGreg Rose if (!ext_filter_mask) 3212115c9b81SGreg Rose return NLMSG_GOODSIZE; 3213115c9b81SGreg Rose /* 3214115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3215115c9b81SGreg Rose * buffer size based upon the filter mask. 3216115c9b81SGreg Rose */ 32176853dd48SFlorian Westphal rcu_read_lock(); 32186853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3219115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3220115c9b81SGreg Rose if_nlmsg_size(dev, 3221115c9b81SGreg Rose ext_filter_mask)); 3222115c9b81SGreg Rose } 32236853dd48SFlorian Westphal rcu_read_unlock(); 3224115c9b81SGreg Rose 322593af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3226c7ac8679SGreg Rose } 3227c7ac8679SGreg Rose 322842bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 32291da177e4SLinus Torvalds { 32301da177e4SLinus Torvalds int idx; 32311da177e4SLinus Torvalds int s_idx = cb->family; 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds if (s_idx == 0) 32341da177e4SLinus Torvalds s_idx = 1; 32356853dd48SFlorian Westphal 323625239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3237addf9b90SFlorian Westphal struct rtnl_link **tab; 32381da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 3239addf9b90SFlorian Westphal struct rtnl_link *link; 32406853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 32416853dd48SFlorian Westphal 32421da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 32431da177e4SLinus Torvalds continue; 32446853dd48SFlorian Westphal 3245addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 32461da177e4SLinus Torvalds continue; 32476853dd48SFlorian Westphal 3248addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3249addf9b90SFlorian Westphal if (!tab) 3250addf9b90SFlorian Westphal continue; 3251addf9b90SFlorian Westphal 3252addf9b90SFlorian Westphal link = tab[type]; 3253addf9b90SFlorian Westphal if (!link) 3254addf9b90SFlorian Westphal continue; 3255addf9b90SFlorian Westphal 3256addf9b90SFlorian Westphal dumpit = link->dumpit; 32576853dd48SFlorian Westphal if (!dumpit) 32586853dd48SFlorian Westphal continue; 32596853dd48SFlorian Westphal 32600465277fSNicolas Dichtel if (idx > s_idx) { 32611da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 32620465277fSNicolas Dichtel cb->prev_seq = 0; 32630465277fSNicolas Dichtel cb->seq = 0; 32640465277fSNicolas Dichtel } 32656853dd48SFlorian Westphal if (dumpit(skb, cb)) 32661da177e4SLinus Torvalds break; 32671da177e4SLinus Torvalds } 32681da177e4SLinus Torvalds cb->family = idx; 32691da177e4SLinus Torvalds 32701da177e4SLinus Torvalds return skb->len; 32711da177e4SLinus Torvalds } 32721da177e4SLinus Torvalds 3273395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 32743d3ea5afSVlad Yasevich unsigned int change, 327538e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 327638e01b30SNicolas Dichtel int new_ifindex) 32771da177e4SLinus Torvalds { 3278c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 32791da177e4SLinus Torvalds struct sk_buff *skb; 32800ec6d3f4SThomas Graf int err = -ENOBUFS; 3281c7ac8679SGreg Rose size_t if_info_size; 32821da177e4SLinus Torvalds 32837f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 32840ec6d3f4SThomas Graf if (skb == NULL) 32850ec6d3f4SThomas Graf goto errout; 32861da177e4SLinus Torvalds 328779e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 328879e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 328938e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 329026932566SPatrick McHardy if (err < 0) { 329126932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 329226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 329326932566SPatrick McHardy kfree_skb(skb); 329426932566SPatrick McHardy goto errout; 329526932566SPatrick McHardy } 3296395eea6cSMahesh Bandewar return skb; 32970ec6d3f4SThomas Graf errout: 32980ec6d3f4SThomas Graf if (err < 0) 32994b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3300395eea6cSMahesh Bandewar return NULL; 3301395eea6cSMahesh Bandewar } 3302395eea6cSMahesh Bandewar 3303395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3304395eea6cSMahesh Bandewar { 3305395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3306395eea6cSMahesh Bandewar 3307395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3308395eea6cSMahesh Bandewar } 3309395eea6cSMahesh Bandewar 33103d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 33113d3ea5afSVlad Yasevich unsigned int change, u32 event, 331238e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3313395eea6cSMahesh Bandewar { 3314395eea6cSMahesh Bandewar struct sk_buff *skb; 3315395eea6cSMahesh Bandewar 3316ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3317ed2a80abSNicolas Dichtel return; 3318ed2a80abSNicolas Dichtel 331938e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 332038e01b30SNicolas Dichtel new_ifindex); 3321395eea6cSMahesh Bandewar if (skb) 3322395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 33231da177e4SLinus Torvalds } 33243d3ea5afSVlad Yasevich 33253d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 33263d3ea5afSVlad Yasevich gfp_t flags) 33273d3ea5afSVlad Yasevich { 332838e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 332938e01b30SNicolas Dichtel NULL, 0); 33303d3ea5afSVlad Yasevich } 33311da177e4SLinus Torvalds 33326621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 333338e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 33346621dd29SNicolas Dichtel { 33356621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 333638e01b30SNicolas Dichtel new_nsid, new_ifindex); 33376621dd29SNicolas Dichtel } 33386621dd29SNicolas Dichtel 3339d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3340d83b0603SJohn Fastabend struct net_device *dev, 33411e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 33421c104a6bSNicolas Dichtel int type, unsigned int flags, 3343b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3344d83b0603SJohn Fastabend { 3345d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3346d83b0603SJohn Fastabend struct ndmsg *ndm; 3347d83b0603SJohn Fastabend 33481c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3349d83b0603SJohn Fastabend if (!nlh) 3350d83b0603SJohn Fastabend return -EMSGSIZE; 3351d83b0603SJohn Fastabend 3352d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3353d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3354d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3355d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3356d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3357d83b0603SJohn Fastabend ndm->ndm_type = 0; 3358d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3359b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3360d83b0603SJohn Fastabend 3361d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3362d83b0603SJohn Fastabend goto nla_put_failure; 33631e53d5bbSHubert Sokolowski if (vid) 33641e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 33651e53d5bbSHubert Sokolowski goto nla_put_failure; 3366d83b0603SJohn Fastabend 3367053c095aSJohannes Berg nlmsg_end(skb, nlh); 3368053c095aSJohannes Berg return 0; 3369d83b0603SJohn Fastabend 3370d83b0603SJohn Fastabend nla_put_failure: 3371d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3372d83b0603SJohn Fastabend return -EMSGSIZE; 3373d83b0603SJohn Fastabend } 3374d83b0603SJohn Fastabend 33753ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 33763ff661c3SJohn Fastabend { 3377f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3378f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3379f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3380f82ef3e1SSabrina Dubroca 0; 33813ff661c3SJohn Fastabend } 33823ff661c3SJohn Fastabend 3383b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3384b3379041SHubert Sokolowski u16 ndm_state) 33853ff661c3SJohn Fastabend { 33863ff661c3SJohn Fastabend struct net *net = dev_net(dev); 33873ff661c3SJohn Fastabend struct sk_buff *skb; 33883ff661c3SJohn Fastabend int err = -ENOBUFS; 33893ff661c3SJohn Fastabend 33903ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 33913ff661c3SJohn Fastabend if (!skb) 33923ff661c3SJohn Fastabend goto errout; 33933ff661c3SJohn Fastabend 33941e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3395b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 33963ff661c3SJohn Fastabend if (err < 0) { 33973ff661c3SJohn Fastabend kfree_skb(skb); 33983ff661c3SJohn Fastabend goto errout; 33993ff661c3SJohn Fastabend } 34003ff661c3SJohn Fastabend 34013ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 34023ff661c3SJohn Fastabend return; 34033ff661c3SJohn Fastabend errout: 34043ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 34053ff661c3SJohn Fastabend } 34063ff661c3SJohn Fastabend 3407090096bfSVlad Yasevich /** 3408090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3409090096bfSVlad Yasevich */ 3410090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3411090096bfSVlad Yasevich struct nlattr *tb[], 3412090096bfSVlad Yasevich struct net_device *dev, 3413f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3414090096bfSVlad Yasevich u16 flags) 3415090096bfSVlad Yasevich { 3416090096bfSVlad Yasevich int err = -EINVAL; 3417090096bfSVlad Yasevich 3418090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3419090096bfSVlad Yasevich * implement its own handler for this. 3420090096bfSVlad Yasevich */ 3421090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3422090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3423090096bfSVlad Yasevich return err; 3424090096bfSVlad Yasevich } 3425090096bfSVlad Yasevich 342665891feaSOr Gerlitz if (vid) { 342765891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 342865891feaSOr Gerlitz return err; 342965891feaSOr Gerlitz } 343065891feaSOr Gerlitz 3431090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3432090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3433090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3434090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3435090096bfSVlad Yasevich 3436090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3437090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3438090096bfSVlad Yasevich err = 0; 3439090096bfSVlad Yasevich 3440090096bfSVlad Yasevich return err; 3441090096bfSVlad Yasevich } 3442090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3443090096bfSVlad Yasevich 3444b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3445b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3446f6f6424bSJiri Pirko { 3447f6f6424bSJiri Pirko u16 vid = 0; 3448f6f6424bSJiri Pirko 3449f6f6424bSJiri Pirko if (vlan_attr) { 3450f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3451b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3452f6f6424bSJiri Pirko return -EINVAL; 3453f6f6424bSJiri Pirko } 3454f6f6424bSJiri Pirko 3455f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3456f6f6424bSJiri Pirko 3457f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3458b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3459f6f6424bSJiri Pirko return -EINVAL; 3460f6f6424bSJiri Pirko } 3461f6f6424bSJiri Pirko } 3462f6f6424bSJiri Pirko *p_vid = vid; 3463f6f6424bSJiri Pirko return 0; 3464f6f6424bSJiri Pirko } 3465f6f6424bSJiri Pirko 3466c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3467c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 346877162022SJohn Fastabend { 346977162022SJohn Fastabend struct net *net = sock_net(skb->sk); 347077162022SJohn Fastabend struct ndmsg *ndm; 347177162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 347277162022SJohn Fastabend struct net_device *dev; 347377162022SJohn Fastabend u8 *addr; 3474f6f6424bSJiri Pirko u16 vid; 347577162022SJohn Fastabend int err; 347677162022SJohn Fastabend 3477c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 347877162022SJohn Fastabend if (err < 0) 347977162022SJohn Fastabend return err; 348077162022SJohn Fastabend 348177162022SJohn Fastabend ndm = nlmsg_data(nlh); 348277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3483b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 348477162022SJohn Fastabend return -EINVAL; 348577162022SJohn Fastabend } 348677162022SJohn Fastabend 348777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 348877162022SJohn Fastabend if (dev == NULL) { 3489b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 349077162022SJohn Fastabend return -ENODEV; 349177162022SJohn Fastabend } 349277162022SJohn Fastabend 349377162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3494b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 349577162022SJohn Fastabend return -EINVAL; 349677162022SJohn Fastabend } 349777162022SJohn Fastabend 349877162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 349977162022SJohn Fastabend 3500b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3501f6f6424bSJiri Pirko if (err) 3502f6f6424bSJiri Pirko return err; 3503f6f6424bSJiri Pirko 350477162022SJohn Fastabend err = -EOPNOTSUPP; 350577162022SJohn Fastabend 350677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 350777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 350877162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3509898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3510898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3511898e5061SJiri Pirko 3512f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3513f6f6424bSJiri Pirko nlh->nlmsg_flags); 351477162022SJohn Fastabend if (err) 351577162022SJohn Fastabend goto out; 351677162022SJohn Fastabend else 351777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 351877162022SJohn Fastabend } 351977162022SJohn Fastabend 352077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3521090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3522090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3523090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3524f6f6424bSJiri Pirko vid, 3525090096bfSVlad Yasevich nlh->nlmsg_flags); 3526090096bfSVlad Yasevich else 3527f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 352877162022SJohn Fastabend nlh->nlmsg_flags); 352977162022SJohn Fastabend 35303ff661c3SJohn Fastabend if (!err) { 3531b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3532b3379041SHubert Sokolowski ndm->ndm_state); 353377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 353477162022SJohn Fastabend } 35353ff661c3SJohn Fastabend } 353677162022SJohn Fastabend out: 353777162022SJohn Fastabend return err; 353877162022SJohn Fastabend } 353977162022SJohn Fastabend 3540090096bfSVlad Yasevich /** 3541090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3542090096bfSVlad Yasevich */ 3543090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3544090096bfSVlad Yasevich struct nlattr *tb[], 3545090096bfSVlad Yasevich struct net_device *dev, 3546f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3547090096bfSVlad Yasevich { 3548c8a89c4aSAlexander Duyck int err = -EINVAL; 3549090096bfSVlad Yasevich 3550090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3551090096bfSVlad Yasevich * implement its own handler for this. 3552090096bfSVlad Yasevich */ 355364535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3554090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3555c8a89c4aSAlexander Duyck return err; 3556090096bfSVlad Yasevich } 3557090096bfSVlad Yasevich 3558090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3559090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3560090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3561090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3562090096bfSVlad Yasevich 3563090096bfSVlad Yasevich return err; 3564090096bfSVlad Yasevich } 3565090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3566090096bfSVlad Yasevich 3567c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3568c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 356977162022SJohn Fastabend { 357077162022SJohn Fastabend struct net *net = sock_net(skb->sk); 357177162022SJohn Fastabend struct ndmsg *ndm; 35721690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 357377162022SJohn Fastabend struct net_device *dev; 357477162022SJohn Fastabend int err = -EINVAL; 357577162022SJohn Fastabend __u8 *addr; 3576f6f6424bSJiri Pirko u16 vid; 357777162022SJohn Fastabend 357890f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 35791690be63SVlad Yasevich return -EPERM; 35801690be63SVlad Yasevich 3581c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 35821690be63SVlad Yasevich if (err < 0) 35831690be63SVlad Yasevich return err; 358477162022SJohn Fastabend 358577162022SJohn Fastabend ndm = nlmsg_data(nlh); 358677162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3587b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 358877162022SJohn Fastabend return -EINVAL; 358977162022SJohn Fastabend } 359077162022SJohn Fastabend 359177162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 359277162022SJohn Fastabend if (dev == NULL) { 3593b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 359477162022SJohn Fastabend return -ENODEV; 359577162022SJohn Fastabend } 359677162022SJohn Fastabend 35971690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3598b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 359977162022SJohn Fastabend return -EINVAL; 360077162022SJohn Fastabend } 360177162022SJohn Fastabend 36021690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 36031690be63SVlad Yasevich 3604b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3605f6f6424bSJiri Pirko if (err) 3606f6f6424bSJiri Pirko return err; 3607f6f6424bSJiri Pirko 360877162022SJohn Fastabend err = -EOPNOTSUPP; 360977162022SJohn Fastabend 361077162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 361177162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 361277162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3613898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3614898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 361577162022SJohn Fastabend 3616898e5061SJiri Pirko if (ops->ndo_fdb_del) 3617f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 361877162022SJohn Fastabend 361977162022SJohn Fastabend if (err) 362077162022SJohn Fastabend goto out; 362177162022SJohn Fastabend else 362277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 362377162022SJohn Fastabend } 362477162022SJohn Fastabend 362577162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3626090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3627090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3628f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3629f6f6424bSJiri Pirko vid); 3630090096bfSVlad Yasevich else 3631f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 363277162022SJohn Fastabend 36333ff661c3SJohn Fastabend if (!err) { 3634b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3635b3379041SHubert Sokolowski ndm->ndm_state); 363677162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 363777162022SJohn Fastabend } 36383ff661c3SJohn Fastabend } 363977162022SJohn Fastabend out: 364077162022SJohn Fastabend return err; 364177162022SJohn Fastabend } 364277162022SJohn Fastabend 3643d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3644d83b0603SJohn Fastabend struct netlink_callback *cb, 3645d83b0603SJohn Fastabend struct net_device *dev, 3646d83b0603SJohn Fastabend int *idx, 3647d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3648d83b0603SJohn Fastabend { 3649d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3650d83b0603SJohn Fastabend int err; 365115e47304SEric W. Biederman u32 portid, seq; 3652d83b0603SJohn Fastabend 365315e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3654d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3655d83b0603SJohn Fastabend 3656d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3657d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3658d83b0603SJohn Fastabend goto skip; 3659d83b0603SJohn Fastabend 36601e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3661a7a558feSJohn Fastabend portid, seq, 36621c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3663b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3664d83b0603SJohn Fastabend if (err < 0) 3665d83b0603SJohn Fastabend return err; 3666d83b0603SJohn Fastabend skip: 3667d83b0603SJohn Fastabend *idx += 1; 3668d83b0603SJohn Fastabend } 3669d83b0603SJohn Fastabend return 0; 3670d83b0603SJohn Fastabend } 3671d83b0603SJohn Fastabend 3672d83b0603SJohn Fastabend /** 36732c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3674d83b0603SJohn Fastabend * @nlh: netlink message header 3675d83b0603SJohn Fastabend * @dev: netdevice 3676d83b0603SJohn Fastabend * 3677d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 367891f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3679d83b0603SJohn Fastabend */ 3680d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3681d83b0603SJohn Fastabend struct netlink_callback *cb, 3682d83b0603SJohn Fastabend struct net_device *dev, 36835d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3684d297653dSRoopa Prabhu int *idx) 3685d83b0603SJohn Fastabend { 3686d83b0603SJohn Fastabend int err; 3687d83b0603SJohn Fastabend 3688d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3689d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3690d83b0603SJohn Fastabend if (err) 3691d83b0603SJohn Fastabend goto out; 36922934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3693d83b0603SJohn Fastabend out: 3694d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3695d297653dSRoopa Prabhu return err; 3696d83b0603SJohn Fastabend } 3697d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3698d83b0603SJohn Fastabend 369977162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 370077162022SJohn Fastabend { 370177162022SJohn Fastabend struct net_device *dev; 37025e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 37035e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 37045e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 37055e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 37065e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 37075e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3708d297653dSRoopa Prabhu struct hlist_head *head; 37095e6d2435SJamal Hadi Salim int brport_idx = 0; 37105e6d2435SJamal Hadi Salim int br_idx = 0; 3711d297653dSRoopa Prabhu int h, s_h; 3712d297653dSRoopa Prabhu int idx = 0, s_idx; 3713d297653dSRoopa Prabhu int err = 0; 3714d297653dSRoopa Prabhu int fidx = 0; 371577162022SJohn Fastabend 37160ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 37170ff50e83SAlexander Potapenko IFLA_MAX, ifla_policy, NULL); 37180ff50e83SAlexander Potapenko if (err < 0) { 37190ff50e83SAlexander Potapenko return -EINVAL; 37200ff50e83SAlexander Potapenko } else if (err == 0) { 37215e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 37225e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 37235e6d2435SJamal Hadi Salim } 372477162022SJohn Fastabend 37255e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 37265e6d2435SJamal Hadi Salim 37275e6d2435SJamal Hadi Salim if (br_idx) { 37285e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 37295e6d2435SJamal Hadi Salim if (!br_dev) 37305e6d2435SJamal Hadi Salim return -ENODEV; 37315e6d2435SJamal Hadi Salim 3732898e5061SJiri Pirko ops = br_dev->netdev_ops; 37335e6d2435SJamal Hadi Salim } 37345e6d2435SJamal Hadi Salim 3735d297653dSRoopa Prabhu s_h = cb->args[0]; 3736d297653dSRoopa Prabhu s_idx = cb->args[1]; 3737d297653dSRoopa Prabhu 3738d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3739d297653dSRoopa Prabhu idx = 0; 3740d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3741d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3742d297653dSRoopa Prabhu 37435e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 37445e6d2435SJamal Hadi Salim continue; 37455e6d2435SJamal Hadi Salim 37465e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 37475e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 37485e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 37495e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 37505e6d2435SJamal Hadi Salim } 37515e6d2435SJamal Hadi Salim } else { 37525e6d2435SJamal Hadi Salim if (dev != br_dev && 37535e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 37545e6d2435SJamal Hadi Salim continue; 37555e6d2435SJamal Hadi Salim 37565e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 37575e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 37585e6d2435SJamal Hadi Salim continue; 37595e6d2435SJamal Hadi Salim cops = ops; 37605e6d2435SJamal Hadi Salim } 37615e6d2435SJamal Hadi Salim 3762d297653dSRoopa Prabhu if (idx < s_idx) 3763d297653dSRoopa Prabhu goto cont; 3764d297653dSRoopa Prabhu 37655e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3766d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3767d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3768d297653dSRoopa Prabhu br_dev, dev, 3769d297653dSRoopa Prabhu &fidx); 3770d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3771d297653dSRoopa Prabhu goto out; 377277162022SJohn Fastabend } 3773d297653dSRoopa Prabhu } 377477162022SJohn Fastabend 37755e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3776d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3777d297653dSRoopa Prabhu dev, NULL, 3778d297653dSRoopa Prabhu &fidx); 37796cb69742SHubert Sokolowski else 3780d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3781d297653dSRoopa Prabhu &fidx); 3782d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3783d297653dSRoopa Prabhu goto out; 37845e6d2435SJamal Hadi Salim 37855e6d2435SJamal Hadi Salim cops = NULL; 3786d297653dSRoopa Prabhu 3787d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3788d297653dSRoopa Prabhu cb->args[2] = 0; 3789d297653dSRoopa Prabhu fidx = 0; 3790d297653dSRoopa Prabhu cont: 3791d297653dSRoopa Prabhu idx++; 3792d297653dSRoopa Prabhu } 379377162022SJohn Fastabend } 379477162022SJohn Fastabend 3795d297653dSRoopa Prabhu out: 3796d297653dSRoopa Prabhu cb->args[0] = h; 3797d297653dSRoopa Prabhu cb->args[1] = idx; 3798d297653dSRoopa Prabhu cb->args[2] = fidx; 3799d297653dSRoopa Prabhu 380077162022SJohn Fastabend return skb->len; 380177162022SJohn Fastabend } 380277162022SJohn Fastabend 38032c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 38042c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 38052c3c031cSScott Feldman { 38062c3c031cSScott Feldman if (mask & flag) 38072c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 38082c3c031cSScott Feldman return 0; 38092c3c031cSScott Feldman } 38102c3c031cSScott Feldman 3811815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 38122c3c031cSScott Feldman struct net_device *dev, u16 mode, 38137d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 38147d4f8d87SScott Feldman u32 filter_mask, 38157d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 38167d4f8d87SScott Feldman struct net_device *dev, 38177d4f8d87SScott Feldman u32 filter_mask)) 3818815cccbfSJohn Fastabend { 3819815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3820815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3821815cccbfSJohn Fastabend struct nlattr *br_afspec; 38222c3c031cSScott Feldman struct nlattr *protinfo; 3823815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3824898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 38257d4f8d87SScott Feldman int err = 0; 3826815cccbfSJohn Fastabend 382746c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3828815cccbfSJohn Fastabend if (nlh == NULL) 3829815cccbfSJohn Fastabend return -EMSGSIZE; 3830815cccbfSJohn Fastabend 3831815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3832815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3833815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3834815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3835815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3836815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3837815cccbfSJohn Fastabend ifm->ifi_change = 0; 3838815cccbfSJohn Fastabend 3839815cccbfSJohn Fastabend 3840815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3841815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3842815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3843898e5061SJiri Pirko (br_dev && 3844898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3845815cccbfSJohn Fastabend (dev->addr_len && 3846815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3847a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3848a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3849815cccbfSJohn Fastabend goto nla_put_failure; 3850815cccbfSJohn Fastabend 3851815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3852815cccbfSJohn Fastabend if (!br_afspec) 3853815cccbfSJohn Fastabend goto nla_put_failure; 3854815cccbfSJohn Fastabend 38551d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3856815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3857815cccbfSJohn Fastabend goto nla_put_failure; 3858815cccbfSJohn Fastabend } 38591d460b98SRoopa Prabhu 38601d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 38611d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 38621d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 38631d460b98SRoopa Prabhu goto nla_put_failure; 38641d460b98SRoopa Prabhu } 38651d460b98SRoopa Prabhu } 38667d4f8d87SScott Feldman if (vlan_fill) { 38677d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 38687d4f8d87SScott Feldman if (err) { 38697d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 38707d4f8d87SScott Feldman goto nla_put_failure; 38717d4f8d87SScott Feldman } 38727d4f8d87SScott Feldman } 3873815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3874815cccbfSJohn Fastabend 38752c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 38762c3c031cSScott Feldman if (!protinfo) 38772c3c031cSScott Feldman goto nla_put_failure; 38782c3c031cSScott Feldman 38792c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 38802c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 38812c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38822c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 38832c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38842c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 38852c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 38862c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38872c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 38882c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38892c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 38902c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38912c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 38922c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38932c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 38942c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38952c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 38962c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 38972c3c031cSScott Feldman goto nla_put_failure; 38982c3c031cSScott Feldman } 38992c3c031cSScott Feldman 39002c3c031cSScott Feldman nla_nest_end(skb, protinfo); 39012c3c031cSScott Feldman 3902053c095aSJohannes Berg nlmsg_end(skb, nlh); 3903053c095aSJohannes Berg return 0; 3904815cccbfSJohn Fastabend nla_put_failure: 3905815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 39067d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 3907815cccbfSJohn Fastabend } 39087d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3909815cccbfSJohn Fastabend 3910e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3911e5a55a89SJohn Fastabend { 3912e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3913e5a55a89SJohn Fastabend struct net_device *dev; 3914e5a55a89SJohn Fastabend int idx = 0; 3915e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 3916e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 39176cbdceebSVlad Yasevich u32 filter_mask = 0; 3918d64f69b0SRoopa Prabhu int err; 39196cbdceebSVlad Yasevich 3920aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3921aa68c20fSThomas Graf struct nlattr *extfilt; 3922aa68c20fSThomas Graf 39233e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 39246cbdceebSVlad Yasevich IFLA_EXT_MASK); 3925aa68c20fSThomas Graf if (extfilt) { 3926aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 3927aa68c20fSThomas Graf return -EINVAL; 3928aa68c20fSThomas Graf 39296cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 3930aa68c20fSThomas Graf } 3931aa68c20fSThomas Graf } 3932e5a55a89SJohn Fastabend 3933e5a55a89SJohn Fastabend rcu_read_lock(); 3934e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 3935e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 3936898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3937e5a55a89SJohn Fastabend 3938898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3939d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3940d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 3941d64f69b0SRoopa Prabhu skb, portid, seq, dev, 3942d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 3943f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3944f6c5775fSDavid Ahern if (likely(skb->len)) 3945e5a55a89SJohn Fastabend break; 3946f6c5775fSDavid Ahern 3947f6c5775fSDavid Ahern goto out_err; 3948f6c5775fSDavid Ahern } 3949d64f69b0SRoopa Prabhu } 3950e5a55a89SJohn Fastabend idx++; 3951e5a55a89SJohn Fastabend } 3952e5a55a89SJohn Fastabend 3953e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 3954d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3955d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 3956d64f69b0SRoopa Prabhu seq, dev, 395746c264daSNicolas Dichtel filter_mask, 3958d64f69b0SRoopa Prabhu NLM_F_MULTI); 3959f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3960f6c5775fSDavid Ahern if (likely(skb->len)) 3961e5a55a89SJohn Fastabend break; 3962f6c5775fSDavid Ahern 3963f6c5775fSDavid Ahern goto out_err; 3964f6c5775fSDavid Ahern } 3965d64f69b0SRoopa Prabhu } 3966e5a55a89SJohn Fastabend idx++; 3967e5a55a89SJohn Fastabend } 3968e5a55a89SJohn Fastabend } 3969f6c5775fSDavid Ahern err = skb->len; 3970f6c5775fSDavid Ahern out_err: 3971e5a55a89SJohn Fastabend rcu_read_unlock(); 3972e5a55a89SJohn Fastabend cb->args[0] = idx; 3973e5a55a89SJohn Fastabend 3974f6c5775fSDavid Ahern return err; 3975e5a55a89SJohn Fastabend } 3976e5a55a89SJohn Fastabend 39772469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 39782469ffd7SJohn Fastabend { 39792469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 39802469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 39812469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 39822469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 39832469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 39842469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 39852469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 39862469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 39872469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 39882469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 39892469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 39902469ffd7SJohn Fastabend } 39912469ffd7SJohn Fastabend 399202dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 39932469ffd7SJohn Fastabend { 39942469ffd7SJohn Fastabend struct net *net = dev_net(dev); 39952469ffd7SJohn Fastabend struct sk_buff *skb; 39962469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 39972469ffd7SJohn Fastabend 399802dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 399902dba438SRoopa Prabhu return 0; 400002dba438SRoopa Prabhu 40012469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 40022469ffd7SJohn Fastabend if (!skb) { 40032469ffd7SJohn Fastabend err = -ENOMEM; 40042469ffd7SJohn Fastabend goto errout; 40052469ffd7SJohn Fastabend } 40062469ffd7SJohn Fastabend 400746c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4008c38e01b8SJohn Fastabend if (err < 0) 4009c38e01b8SJohn Fastabend goto errout; 40102469ffd7SJohn Fastabend 401159ccaaaaSRoopa Prabhu if (!skb->len) 401259ccaaaaSRoopa Prabhu goto errout; 401359ccaaaaSRoopa Prabhu 40142469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 40152469ffd7SJohn Fastabend return 0; 40162469ffd7SJohn Fastabend errout: 40172469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 40182469ffd7SJohn Fastabend kfree_skb(skb); 401959ccaaaaSRoopa Prabhu if (err) 40202469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 40212469ffd7SJohn Fastabend return err; 40222469ffd7SJohn Fastabend } 40232469ffd7SJohn Fastabend 4024c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4025c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4026e5a55a89SJohn Fastabend { 4027e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4028e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4029e5a55a89SJohn Fastabend struct net_device *dev; 40302469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 40312469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 40324de8b413SRosen, Rami u16 flags = 0; 4033c38e01b8SJohn Fastabend bool have_flags = false; 4034e5a55a89SJohn Fastabend 4035e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4036e5a55a89SJohn Fastabend return -EINVAL; 4037e5a55a89SJohn Fastabend 4038e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4039e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4040e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4041e5a55a89SJohn Fastabend 4042e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4043e5a55a89SJohn Fastabend if (!dev) { 4044b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4045e5a55a89SJohn Fastabend return -ENODEV; 4046e5a55a89SJohn Fastabend } 4047e5a55a89SJohn Fastabend 40482469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 40492469ffd7SJohn Fastabend if (br_spec) { 40502469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 40512469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40526e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40536e8d1c55SThomas Graf return -EINVAL; 40546e8d1c55SThomas Graf 4055c38e01b8SJohn Fastabend have_flags = true; 40562469ffd7SJohn Fastabend flags = nla_get_u16(attr); 40572469ffd7SJohn Fastabend break; 40582469ffd7SJohn Fastabend } 40592469ffd7SJohn Fastabend } 40602469ffd7SJohn Fastabend } 40612469ffd7SJohn Fastabend 40622469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4063898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4064898e5061SJiri Pirko 4065898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 40662469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4067e5a55a89SJohn Fastabend goto out; 4068e5a55a89SJohn Fastabend } 4069e5a55a89SJohn Fastabend 4070add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 40712469ffd7SJohn Fastabend if (err) 40722469ffd7SJohn Fastabend goto out; 40732469ffd7SJohn Fastabend 40742469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 40752469ffd7SJohn Fastabend } 40762469ffd7SJohn Fastabend 40772469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 40782469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 40792469ffd7SJohn Fastabend err = -EOPNOTSUPP; 40802469ffd7SJohn Fastabend else 4081add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4082add511b3SRoopa Prabhu flags); 408302dba438SRoopa Prabhu if (!err) { 40842469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 408502dba438SRoopa Prabhu 408602dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 408702dba438SRoopa Prabhu * change 408802dba438SRoopa Prabhu */ 408902dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 409002dba438SRoopa Prabhu } 40912469ffd7SJohn Fastabend } 40922469ffd7SJohn Fastabend 4093c38e01b8SJohn Fastabend if (have_flags) 40942469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4095e5a55a89SJohn Fastabend out: 4096e5a55a89SJohn Fastabend return err; 4097e5a55a89SJohn Fastabend } 4098e5a55a89SJohn Fastabend 4099c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4100c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4101407af329SVlad Yasevich { 4102407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4103407af329SVlad Yasevich struct ifinfomsg *ifm; 4104407af329SVlad Yasevich struct net_device *dev; 4105407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4106407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 41074de8b413SRosen, Rami u16 flags = 0; 4108407af329SVlad Yasevich bool have_flags = false; 4109407af329SVlad Yasevich 4110407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4111407af329SVlad Yasevich return -EINVAL; 4112407af329SVlad Yasevich 4113407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4114407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4115407af329SVlad Yasevich return -EPFNOSUPPORT; 4116407af329SVlad Yasevich 4117407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4118407af329SVlad Yasevich if (!dev) { 4119b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4120407af329SVlad Yasevich return -ENODEV; 4121407af329SVlad Yasevich } 4122407af329SVlad Yasevich 4123407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4124407af329SVlad Yasevich if (br_spec) { 4125407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4126407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 41276e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 41286e8d1c55SThomas Graf return -EINVAL; 41296e8d1c55SThomas Graf 4130407af329SVlad Yasevich have_flags = true; 4131407af329SVlad Yasevich flags = nla_get_u16(attr); 4132407af329SVlad Yasevich break; 4133407af329SVlad Yasevich } 4134407af329SVlad Yasevich } 4135407af329SVlad Yasevich } 4136407af329SVlad Yasevich 4137407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4138407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4139407af329SVlad Yasevich 4140407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4141407af329SVlad Yasevich err = -EOPNOTSUPP; 4142407af329SVlad Yasevich goto out; 4143407af329SVlad Yasevich } 4144407af329SVlad Yasevich 4145add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4146407af329SVlad Yasevich if (err) 4147407af329SVlad Yasevich goto out; 4148407af329SVlad Yasevich 4149407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4150407af329SVlad Yasevich } 4151407af329SVlad Yasevich 4152407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4153407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4154407af329SVlad Yasevich err = -EOPNOTSUPP; 4155407af329SVlad Yasevich else 4156add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4157add511b3SRoopa Prabhu flags); 4158407af329SVlad Yasevich 415902dba438SRoopa Prabhu if (!err) { 4160407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 416102dba438SRoopa Prabhu 416202dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 416302dba438SRoopa Prabhu * change 416402dba438SRoopa Prabhu */ 416502dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 416602dba438SRoopa Prabhu } 4167407af329SVlad Yasevich } 4168407af329SVlad Yasevich 4169407af329SVlad Yasevich if (have_flags) 4170407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4171407af329SVlad Yasevich out: 4172407af329SVlad Yasevich return err; 4173407af329SVlad Yasevich } 4174407af329SVlad Yasevich 4175e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4176e8872a25SNikolay Aleksandrov { 4177e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4178e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4179e8872a25SNikolay Aleksandrov } 4180e8872a25SNikolay Aleksandrov 418169ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 418269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 418369ae6ad2SNogah Frankel { 418469ae6ad2SNogah Frankel switch (attr_id) { 418569ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 418669ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 418769ae6ad2SNogah Frankel } 418869ae6ad2SNogah Frankel 418969ae6ad2SNogah Frankel return 0; 419069ae6ad2SNogah Frankel } 419169ae6ad2SNogah Frankel 419269ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 419369ae6ad2SNogah Frankel int *prividx) 419469ae6ad2SNogah Frankel { 419569ae6ad2SNogah Frankel struct nlattr *attr = NULL; 419669ae6ad2SNogah Frankel int attr_id, size; 419769ae6ad2SNogah Frankel void *attr_data; 419869ae6ad2SNogah Frankel int err; 419969ae6ad2SNogah Frankel 420069ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 420169ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 420269ae6ad2SNogah Frankel return -ENODATA; 420369ae6ad2SNogah Frankel 420469ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 420569ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 420669ae6ad2SNogah Frankel if (attr_id < *prividx) 420769ae6ad2SNogah Frankel continue; 420869ae6ad2SNogah Frankel 420969ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 421069ae6ad2SNogah Frankel if (!size) 421169ae6ad2SNogah Frankel continue; 421269ae6ad2SNogah Frankel 42133df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 421469ae6ad2SNogah Frankel continue; 421569ae6ad2SNogah Frankel 421669ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 421769ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 421869ae6ad2SNogah Frankel if (!attr) 421969ae6ad2SNogah Frankel goto nla_put_failure; 422069ae6ad2SNogah Frankel 422169ae6ad2SNogah Frankel attr_data = nla_data(attr); 422269ae6ad2SNogah Frankel memset(attr_data, 0, size); 422369ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 422469ae6ad2SNogah Frankel attr_data); 422569ae6ad2SNogah Frankel if (err) 422669ae6ad2SNogah Frankel goto get_offload_stats_failure; 422769ae6ad2SNogah Frankel } 422869ae6ad2SNogah Frankel 422969ae6ad2SNogah Frankel if (!attr) 423069ae6ad2SNogah Frankel return -ENODATA; 423169ae6ad2SNogah Frankel 423269ae6ad2SNogah Frankel *prividx = 0; 423369ae6ad2SNogah Frankel return 0; 423469ae6ad2SNogah Frankel 423569ae6ad2SNogah Frankel nla_put_failure: 423669ae6ad2SNogah Frankel err = -EMSGSIZE; 423769ae6ad2SNogah Frankel get_offload_stats_failure: 423869ae6ad2SNogah Frankel *prividx = attr_id; 423969ae6ad2SNogah Frankel return err; 424069ae6ad2SNogah Frankel } 424169ae6ad2SNogah Frankel 424269ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 424369ae6ad2SNogah Frankel { 424469ae6ad2SNogah Frankel int nla_size = 0; 424569ae6ad2SNogah Frankel int attr_id; 424669ae6ad2SNogah Frankel int size; 424769ae6ad2SNogah Frankel 424869ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 424969ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 425069ae6ad2SNogah Frankel return 0; 425169ae6ad2SNogah Frankel 425269ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 425369ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 42543df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 425569ae6ad2SNogah Frankel continue; 425669ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 425769ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 425869ae6ad2SNogah Frankel } 425969ae6ad2SNogah Frankel 426069ae6ad2SNogah Frankel if (nla_size != 0) 426169ae6ad2SNogah Frankel nla_size += nla_total_size(0); 426269ae6ad2SNogah Frankel 426369ae6ad2SNogah Frankel return nla_size; 426469ae6ad2SNogah Frankel } 426569ae6ad2SNogah Frankel 426610c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 426710c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4268e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4269e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 427010c9ead9SRoopa Prabhu { 427110c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 427210c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 427310c9ead9SRoopa Prabhu struct nlattr *attr; 4274e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 427569ae6ad2SNogah Frankel int err; 427610c9ead9SRoopa Prabhu 427710c9ead9SRoopa Prabhu ASSERT_RTNL(); 427810c9ead9SRoopa Prabhu 427910c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 428010c9ead9SRoopa Prabhu if (!nlh) 428110c9ead9SRoopa Prabhu return -EMSGSIZE; 428210c9ead9SRoopa Prabhu 428310c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4284ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4285ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4286ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 428710c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 428810c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 428910c9ead9SRoopa Prabhu 4290e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 429110c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 429210c9ead9SRoopa Prabhu 429358414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 429458414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 429558414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 429610c9ead9SRoopa Prabhu if (!attr) 429710c9ead9SRoopa Prabhu goto nla_put_failure; 429810c9ead9SRoopa Prabhu 429910c9ead9SRoopa Prabhu sp = nla_data(attr); 430010c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 430110c9ead9SRoopa Prabhu } 430210c9ead9SRoopa Prabhu 430397a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 430497a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 430597a47facSNikolay Aleksandrov 430697a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 430797a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 430897a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 430997a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 431097a47facSNikolay Aleksandrov if (!attr) 431197a47facSNikolay Aleksandrov goto nla_put_failure; 431297a47facSNikolay Aleksandrov 431380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 431480e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 431580e73cc5SNikolay Aleksandrov if (err) 431680e73cc5SNikolay Aleksandrov goto nla_put_failure; 431780e73cc5SNikolay Aleksandrov *idxattr = 0; 431880e73cc5SNikolay Aleksandrov } 431980e73cc5SNikolay Aleksandrov } 432080e73cc5SNikolay Aleksandrov 432180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 432280e73cc5SNikolay Aleksandrov *idxattr)) { 432380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 432480e73cc5SNikolay Aleksandrov const struct net_device *master; 432580e73cc5SNikolay Aleksandrov 432680e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 432780e73cc5SNikolay Aleksandrov if (master) 432880e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 432980e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 433080e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 433180e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 433280e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 433380e73cc5SNikolay Aleksandrov if (!attr) 433480e73cc5SNikolay Aleksandrov goto nla_put_failure; 433580e73cc5SNikolay Aleksandrov 433680e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 433797a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 433897a47facSNikolay Aleksandrov if (err) 433997a47facSNikolay Aleksandrov goto nla_put_failure; 434097a47facSNikolay Aleksandrov *idxattr = 0; 434197a47facSNikolay Aleksandrov } 434297a47facSNikolay Aleksandrov } 434397a47facSNikolay Aleksandrov 434469ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 434569ae6ad2SNogah Frankel *idxattr)) { 434669ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 434769ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 434869ae6ad2SNogah Frankel if (!attr) 434969ae6ad2SNogah Frankel goto nla_put_failure; 435069ae6ad2SNogah Frankel 435169ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 435269ae6ad2SNogah Frankel if (err == -ENODATA) 435369ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 435469ae6ad2SNogah Frankel else 435569ae6ad2SNogah Frankel nla_nest_end(skb, attr); 435669ae6ad2SNogah Frankel 435769ae6ad2SNogah Frankel if (err && err != -ENODATA) 435869ae6ad2SNogah Frankel goto nla_put_failure; 435969ae6ad2SNogah Frankel *idxattr = 0; 436069ae6ad2SNogah Frankel } 436169ae6ad2SNogah Frankel 4362aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4363aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4364aefb4d4aSRobert Shearman 4365aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4366aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4367aefb4d4aSRobert Shearman if (!attr) 4368aefb4d4aSRobert Shearman goto nla_put_failure; 4369aefb4d4aSRobert Shearman 43705fa85a09SFlorian Westphal rcu_read_lock(); 43715fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4372aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4373aefb4d4aSRobert Shearman struct nlattr *af; 4374aefb4d4aSRobert Shearman int err; 4375aefb4d4aSRobert Shearman 4376aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 43775fa85a09SFlorian Westphal if (!af) { 43785fa85a09SFlorian Westphal rcu_read_unlock(); 4379aefb4d4aSRobert Shearman goto nla_put_failure; 43805fa85a09SFlorian Westphal } 4381aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4382aefb4d4aSRobert Shearman 43835fa85a09SFlorian Westphal if (err == -ENODATA) { 4384aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 43855fa85a09SFlorian Westphal } else if (err < 0) { 43865fa85a09SFlorian Westphal rcu_read_unlock(); 4387aefb4d4aSRobert Shearman goto nla_put_failure; 43885fa85a09SFlorian Westphal } 4389aefb4d4aSRobert Shearman 4390aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4391aefb4d4aSRobert Shearman } 4392aefb4d4aSRobert Shearman } 43935fa85a09SFlorian Westphal rcu_read_unlock(); 4394aefb4d4aSRobert Shearman 4395aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4396aefb4d4aSRobert Shearman 4397aefb4d4aSRobert Shearman *idxattr = 0; 4398aefb4d4aSRobert Shearman } 4399aefb4d4aSRobert Shearman 440010c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 440110c9ead9SRoopa Prabhu 440210c9ead9SRoopa Prabhu return 0; 440310c9ead9SRoopa Prabhu 440410c9ead9SRoopa Prabhu nla_put_failure: 4405e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4406e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 440710c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4408e8872a25SNikolay Aleksandrov else 4409e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 441010c9ead9SRoopa Prabhu 441110c9ead9SRoopa Prabhu return -EMSGSIZE; 441210c9ead9SRoopa Prabhu } 441310c9ead9SRoopa Prabhu 441410c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 441510c9ead9SRoopa Prabhu u32 filter_mask) 441610c9ead9SRoopa Prabhu { 441710c9ead9SRoopa Prabhu size_t size = 0; 441810c9ead9SRoopa Prabhu 4419e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 442010c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 442110c9ead9SRoopa Prabhu 442297a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 442397a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 442480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 442597a47facSNikolay Aleksandrov 442697a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 442780e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 442880e73cc5SNikolay Aleksandrov attr)); 442997a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 443097a47facSNikolay Aleksandrov size += nla_total_size(0); 443197a47facSNikolay Aleksandrov } 443297a47facSNikolay Aleksandrov } 443397a47facSNikolay Aleksandrov 443480e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 443580e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 443680e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 443780e73cc5SNikolay Aleksandrov const struct net_device *master; 443880e73cc5SNikolay Aleksandrov 443980e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 444080e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 444180e73cc5SNikolay Aleksandrov if (master) 444280e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 444380e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 444480e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 444580e73cc5SNikolay Aleksandrov 444680e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 444780e73cc5SNikolay Aleksandrov attr)); 444880e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 444980e73cc5SNikolay Aleksandrov size += nla_total_size(0); 445080e73cc5SNikolay Aleksandrov } 445180e73cc5SNikolay Aleksandrov } 445280e73cc5SNikolay Aleksandrov 445369ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 445469ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 445569ae6ad2SNogah Frankel 4456aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4457aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4458aefb4d4aSRobert Shearman 4459aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4460aefb4d4aSRobert Shearman size += nla_total_size(0); 4461aefb4d4aSRobert Shearman 44625fa85a09SFlorian Westphal rcu_read_lock(); 44635fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4464aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4465aefb4d4aSRobert Shearman size += nla_total_size( 4466aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4467aefb4d4aSRobert Shearman 4468aefb4d4aSRobert Shearman /* for AF_* */ 4469aefb4d4aSRobert Shearman size += nla_total_size(0); 4470aefb4d4aSRobert Shearman } 4471aefb4d4aSRobert Shearman } 44725fa85a09SFlorian Westphal rcu_read_unlock(); 4473aefb4d4aSRobert Shearman } 4474aefb4d4aSRobert Shearman 447510c9ead9SRoopa Prabhu return size; 447610c9ead9SRoopa Prabhu } 447710c9ead9SRoopa Prabhu 4478c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4479c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 448010c9ead9SRoopa Prabhu { 448110c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 448210c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4483e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4484e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 448510c9ead9SRoopa Prabhu struct sk_buff *nskb; 448610c9ead9SRoopa Prabhu u32 filter_mask; 448710c9ead9SRoopa Prabhu int err; 448810c9ead9SRoopa Prabhu 44894775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 44904775cc1fSMathias Krause return -EINVAL; 44914775cc1fSMathias Krause 449210c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 449310c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 449410c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 449510c9ead9SRoopa Prabhu else 449610c9ead9SRoopa Prabhu return -EINVAL; 449710c9ead9SRoopa Prabhu 449810c9ead9SRoopa Prabhu if (!dev) 449910c9ead9SRoopa Prabhu return -ENODEV; 450010c9ead9SRoopa Prabhu 450110c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 450210c9ead9SRoopa Prabhu if (!filter_mask) 450310c9ead9SRoopa Prabhu return -EINVAL; 450410c9ead9SRoopa Prabhu 450510c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 450610c9ead9SRoopa Prabhu if (!nskb) 450710c9ead9SRoopa Prabhu return -ENOBUFS; 450810c9ead9SRoopa Prabhu 450910c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 451010c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4511e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 451210c9ead9SRoopa Prabhu if (err < 0) { 451310c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 451410c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 451510c9ead9SRoopa Prabhu kfree_skb(nskb); 451610c9ead9SRoopa Prabhu } else { 451710c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 451810c9ead9SRoopa Prabhu } 451910c9ead9SRoopa Prabhu 452010c9ead9SRoopa Prabhu return err; 452110c9ead9SRoopa Prabhu } 452210c9ead9SRoopa Prabhu 452310c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 452410c9ead9SRoopa Prabhu { 4525e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 452610c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 452710c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4528e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4529e8872a25SNikolay Aleksandrov struct hlist_head *head; 4530e8872a25SNikolay Aleksandrov struct net_device *dev; 453110c9ead9SRoopa Prabhu u32 filter_mask = 0; 4532e8872a25SNikolay Aleksandrov int idx = 0; 453310c9ead9SRoopa Prabhu 453410c9ead9SRoopa Prabhu s_h = cb->args[0]; 453510c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4536e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4537e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 453810c9ead9SRoopa Prabhu 453910c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 454010c9ead9SRoopa Prabhu 45414775cc1fSMathias Krause if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 45424775cc1fSMathias Krause return -EINVAL; 45434775cc1fSMathias Krause 454410c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 454510c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 454610c9ead9SRoopa Prabhu if (!filter_mask) 454710c9ead9SRoopa Prabhu return -EINVAL; 454810c9ead9SRoopa Prabhu 454910c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 455010c9ead9SRoopa Prabhu idx = 0; 455110c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 455210c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 455310c9ead9SRoopa Prabhu if (idx < s_idx) 455410c9ead9SRoopa Prabhu goto cont; 455510c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 455610c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 455710c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4558e8872a25SNikolay Aleksandrov flags, filter_mask, 4559e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 456010c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 456110c9ead9SRoopa Prabhu * we're in trouble 456210c9ead9SRoopa Prabhu */ 456310c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 456410c9ead9SRoopa Prabhu 456510c9ead9SRoopa Prabhu if (err < 0) 456610c9ead9SRoopa Prabhu goto out; 4567e8872a25SNikolay Aleksandrov s_prividx = 0; 4568e8872a25SNikolay Aleksandrov s_idxattr = 0; 456910c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 457010c9ead9SRoopa Prabhu cont: 457110c9ead9SRoopa Prabhu idx++; 457210c9ead9SRoopa Prabhu } 457310c9ead9SRoopa Prabhu } 457410c9ead9SRoopa Prabhu out: 4575e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4576e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 457710c9ead9SRoopa Prabhu cb->args[1] = idx; 457810c9ead9SRoopa Prabhu cb->args[0] = h; 457910c9ead9SRoopa Prabhu 458010c9ead9SRoopa Prabhu return skb->len; 458110c9ead9SRoopa Prabhu } 458210c9ead9SRoopa Prabhu 45831da177e4SLinus Torvalds /* Process one rtnetlink message. */ 45841da177e4SLinus Torvalds 45852d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 45862d4bc933SJohannes Berg struct netlink_ext_ack *extack) 45871da177e4SLinus Torvalds { 45883b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4589addf9b90SFlorian Westphal struct rtnl_link *link; 4590e4202511SFlorian Westphal struct module *owner; 45916853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4592e2849863SThomas Graf rtnl_doit_func doit; 459362256f98SFlorian Westphal unsigned int flags; 4594617cfc75SAlexander Kuleshov int kind; 45951da177e4SLinus Torvalds int family; 45961da177e4SLinus Torvalds int type; 45971da177e4SLinus Torvalds 45981da177e4SLinus Torvalds type = nlh->nlmsg_type; 45991da177e4SLinus Torvalds if (type > RTM_MAX) 4600038890feSThomas Graf return -EOPNOTSUPP; 46011da177e4SLinus Torvalds 46021da177e4SLinus Torvalds type -= RTM_BASE; 46031da177e4SLinus Torvalds 46041da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4605573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 46061da177e4SLinus Torvalds return 0; 46071da177e4SLinus Torvalds 4608573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 46091da177e4SLinus Torvalds kind = type&3; 46101da177e4SLinus Torvalds 461190f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 46121d00a4ebSThomas Graf return -EPERM; 46131da177e4SLinus Torvalds 46146853dd48SFlorian Westphal rcu_read_lock(); 4615b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 461697c53cacSDenis V. Lunev struct sock *rtnl; 4617e2849863SThomas Graf rtnl_dumpit_func dumpit; 4618c7ac8679SGreg Rose u16 min_dump_alloc = 0; 46191da177e4SLinus Torvalds 4620addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4621addf9b90SFlorian Westphal if (!link || !link->dumpit) { 46226853dd48SFlorian Westphal family = PF_UNSPEC; 4623addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4624addf9b90SFlorian Westphal if (!link || !link->dumpit) 46256853dd48SFlorian Westphal goto err_unlock; 46266853dd48SFlorian Westphal } 4627e4202511SFlorian Westphal owner = link->owner; 4628addf9b90SFlorian Westphal dumpit = link->dumpit; 46296853dd48SFlorian Westphal 46305c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4631e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 46321da177e4SLinus Torvalds 4633e4202511SFlorian Westphal err = 0; 4634e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4635e4202511SFlorian Westphal if (!try_module_get(owner)) 4636e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4637e4202511SFlorian Westphal 46386853dd48SFlorian Westphal rcu_read_unlock(); 46396853dd48SFlorian Westphal 464097c53cacSDenis V. Lunev rtnl = net->rtnl; 4641e4202511SFlorian Westphal if (err == 0) { 464280d326faSPablo Neira Ayuso struct netlink_dump_control c = { 464380d326faSPablo Neira Ayuso .dump = dumpit, 464480d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4645e4202511SFlorian Westphal .module = owner, 464680d326faSPablo Neira Ayuso }; 464780d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4648e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4649e4202511SFlorian Westphal * module if dump is still in progress. 4650e4202511SFlorian Westphal */ 4651e4202511SFlorian Westphal module_put(owner); 465280d326faSPablo Neira Ayuso } 46532907c35fSEric Dumazet return err; 46541da177e4SLinus Torvalds } 46551da177e4SLinus Torvalds 4656addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4657addf9b90SFlorian Westphal if (!link || !link->doit) { 46588caa38b5SFlorian Westphal family = PF_UNSPEC; 4659addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4660addf9b90SFlorian Westphal if (!link || !link->doit) 4661addf9b90SFlorian Westphal goto out_unlock; 46628caa38b5SFlorian Westphal } 46638caa38b5SFlorian Westphal 4664e4202511SFlorian Westphal owner = link->owner; 4665e4202511SFlorian Westphal if (!try_module_get(owner)) { 4666e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4667e4202511SFlorian Westphal goto out_unlock; 4668e4202511SFlorian Westphal } 4669e4202511SFlorian Westphal 4670addf9b90SFlorian Westphal flags = link->flags; 467162256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4672addf9b90SFlorian Westphal doit = link->doit; 467362256f98SFlorian Westphal rcu_read_unlock(); 467462256f98SFlorian Westphal if (doit) 467562256f98SFlorian Westphal err = doit(skb, nlh, extack); 4676e4202511SFlorian Westphal module_put(owner); 467762256f98SFlorian Westphal return err; 467862256f98SFlorian Westphal } 46796853dd48SFlorian Westphal rcu_read_unlock(); 46806853dd48SFlorian Westphal 46810cc09020SFlorian Westphal rtnl_lock(); 4682addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4683addf9b90SFlorian Westphal if (link && link->doit) 4684addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 46850cc09020SFlorian Westphal rtnl_unlock(); 4686addf9b90SFlorian Westphal 4687e4202511SFlorian Westphal module_put(owner); 4688e4202511SFlorian Westphal 4689addf9b90SFlorian Westphal return err; 4690addf9b90SFlorian Westphal 4691addf9b90SFlorian Westphal out_unlock: 4692addf9b90SFlorian Westphal rcu_read_unlock(); 46930cc09020SFlorian Westphal return err; 46940cc09020SFlorian Westphal 46950cc09020SFlorian Westphal err_unlock: 46966853dd48SFlorian Westphal rcu_read_unlock(); 46970cc09020SFlorian Westphal return -EOPNOTSUPP; 46981da177e4SLinus Torvalds } 46991da177e4SLinus Torvalds 4700cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 47011da177e4SLinus Torvalds { 4702cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 47031da177e4SLinus Torvalds } 47041da177e4SLinus Torvalds 47055f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 47065f729eaaSJulien Gomes { 47075f729eaaSJulien Gomes switch (group) { 47085f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 47095f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 47105f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 47115f729eaaSJulien Gomes return -EPERM; 47125f729eaaSJulien Gomes break; 47135f729eaaSJulien Gomes } 47145f729eaaSJulien Gomes return 0; 47155f729eaaSJulien Gomes } 47165f729eaaSJulien Gomes 47171da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 47181da177e4SLinus Torvalds { 4719351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4720e9dc8653SEric W. Biederman 47211da177e4SLinus Torvalds switch (event) { 47225138e86fSVlad Yasevich case NETDEV_REBOOT: 47238a212589SXin Long case NETDEV_CHANGEMTU: 47243753654eSDavid Ahern case NETDEV_CHANGEADDR: 47255138e86fSVlad Yasevich case NETDEV_CHANGENAME: 47265138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 47275138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4728e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 47295138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4730dc709f37SXin Long case NETDEV_CHANGEUPPER: 47315138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 47325138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4733eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4734ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 47353d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 473638e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 47371da177e4SLinus Torvalds break; 47381da177e4SLinus Torvalds default: 47391da177e4SLinus Torvalds break; 47401da177e4SLinus Torvalds } 47411da177e4SLinus Torvalds return NOTIFY_DONE; 47421da177e4SLinus Torvalds } 47431da177e4SLinus Torvalds 47441da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 47451da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 47461da177e4SLinus Torvalds }; 47471da177e4SLinus Torvalds 474897c53cacSDenis V. Lunev 47492c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 475097c53cacSDenis V. Lunev { 475197c53cacSDenis V. Lunev struct sock *sk; 4752a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4753a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4754a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4755a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 47569785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 47575f729eaaSJulien Gomes .bind = rtnetlink_bind, 4758a31f2d17SPablo Neira Ayuso }; 4759a31f2d17SPablo Neira Ayuso 47609f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 476197c53cacSDenis V. Lunev if (!sk) 476297c53cacSDenis V. Lunev return -ENOMEM; 476397c53cacSDenis V. Lunev net->rtnl = sk; 476497c53cacSDenis V. Lunev return 0; 476597c53cacSDenis V. Lunev } 476697c53cacSDenis V. Lunev 47672c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 476897c53cacSDenis V. Lunev { 4769b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 477097c53cacSDenis V. Lunev net->rtnl = NULL; 477197c53cacSDenis V. Lunev } 477297c53cacSDenis V. Lunev 477397c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 477497c53cacSDenis V. Lunev .init = rtnetlink_net_init, 477597c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 477697c53cacSDenis V. Lunev }; 477797c53cacSDenis V. Lunev 47781da177e4SLinus Torvalds void __init rtnetlink_init(void) 47791da177e4SLinus Torvalds { 478097c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 47811da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 478297c53cacSDenis V. Lunev 47831da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4784340d17fcSThomas Graf 4785c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4786b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4787b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4788b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4789b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4790687ad8ccSThomas Graf 4791b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4792b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4793b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 479477162022SJohn Fastabend 4795b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4796b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4797b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4798e5a55a89SJohn Fastabend 4799b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4800b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4801b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 480210c9ead9SRoopa Prabhu 480310c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4804b97bac64SFlorian Westphal 0); 48051da177e4SLinus Torvalds } 4806