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]; 327f707ef61SSabrina Dubroca if (!tab) { 328f707ef61SSabrina Dubroca rtnl_unlock(); 329f707ef61SSabrina Dubroca return; 330f707ef61SSabrina Dubroca } 3316853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 332addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 333addf9b90SFlorian Westphal link = tab[msgindex]; 334addf9b90SFlorian Westphal if (!link) 335addf9b90SFlorian Westphal continue; 336addf9b90SFlorian Westphal 337addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 338addf9b90SFlorian Westphal kfree_rcu(link, rcu); 339addf9b90SFlorian Westphal } 340019a3169SFlorian Westphal rtnl_unlock(); 341019a3169SFlorian Westphal 3426853dd48SFlorian Westphal synchronize_net(); 3436853dd48SFlorian Westphal 344addf9b90SFlorian Westphal kfree(tab); 345e2849863SThomas Graf } 346e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3471da177e4SLinus Torvalds 34838f7b870SPatrick McHardy static LIST_HEAD(link_ops); 34938f7b870SPatrick McHardy 350c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 351c63044f0SEric Dumazet { 352c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 353c63044f0SEric Dumazet 354c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 355c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 356c63044f0SEric Dumazet return ops; 357c63044f0SEric Dumazet } 358c63044f0SEric Dumazet return NULL; 359c63044f0SEric Dumazet } 360c63044f0SEric Dumazet 36138f7b870SPatrick McHardy /** 36238f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 36338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 36438f7b870SPatrick McHardy * 36538f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 36638f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 36738f7b870SPatrick McHardy * must be called before registering the devices. 36838f7b870SPatrick McHardy * 36938f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 37038f7b870SPatrick McHardy */ 37138f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 37238f7b870SPatrick McHardy { 373c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 374c63044f0SEric Dumazet return -EEXIST; 375c63044f0SEric Dumazet 376b0ab2fabSJiri Pirko /* The check for setup is here because if ops 377b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 378b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 379b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 380b0ab2fabSJiri Pirko */ 381b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 38223289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3832d85cba2SPatrick McHardy 38438f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 38538f7b870SPatrick McHardy return 0; 38638f7b870SPatrick McHardy } 38738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 38838f7b870SPatrick McHardy 38938f7b870SPatrick McHardy /** 39038f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 39138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 39238f7b870SPatrick McHardy * 39338f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 39438f7b870SPatrick McHardy */ 39538f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 39638f7b870SPatrick McHardy { 39738f7b870SPatrick McHardy int err; 39838f7b870SPatrick McHardy 399ccf8dbcdSKees Cook /* Sanity-check max sizes to avoid stack buffer overflow. */ 400ccf8dbcdSKees Cook if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 401ccf8dbcdSKees Cook ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 402ccf8dbcdSKees Cook return -EINVAL; 403ccf8dbcdSKees Cook 40438f7b870SPatrick McHardy rtnl_lock(); 40538f7b870SPatrick McHardy err = __rtnl_link_register(ops); 40638f7b870SPatrick McHardy rtnl_unlock(); 40738f7b870SPatrick McHardy return err; 40838f7b870SPatrick McHardy } 40938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 41038f7b870SPatrick McHardy 411669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 412669f87baSPavel Emelyanov { 413669f87baSPavel Emelyanov struct net_device *dev; 41423289a37SEric Dumazet LIST_HEAD(list_kill); 41523289a37SEric Dumazet 416669f87baSPavel Emelyanov for_each_netdev(net, dev) { 41723289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 41823289a37SEric Dumazet ops->dellink(dev, &list_kill); 419669f87baSPavel Emelyanov } 42023289a37SEric Dumazet unregister_netdevice_many(&list_kill); 421669f87baSPavel Emelyanov } 422669f87baSPavel Emelyanov 42338f7b870SPatrick McHardy /** 42438f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 42538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 42638f7b870SPatrick McHardy * 427554873e5SKirill Tkhai * The caller must hold the rtnl_mutex and guarantee net_namespace_list 428554873e5SKirill Tkhai * integrity (hold pernet_ops_rwsem for writing to close the race 429554873e5SKirill Tkhai * with setup_net() and cleanup_net()). 43038f7b870SPatrick McHardy */ 43138f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 43238f7b870SPatrick McHardy { 433881d966bSEric W. Biederman struct net *net; 4342d85cba2SPatrick McHardy 435881d966bSEric W. Biederman for_each_net(net) { 436669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 437881d966bSEric W. Biederman } 43838f7b870SPatrick McHardy list_del(&ops->list); 43938f7b870SPatrick McHardy } 44038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 44138f7b870SPatrick McHardy 442200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 443200b916fSCong Wang * devices unregistering in any network namespace. 444200b916fSCong Wang */ 445200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 446200b916fSCong Wang { 447200b916fSCong Wang struct net *net; 448200b916fSCong Wang bool unregistering; 449ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 450200b916fSCong Wang 451ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 452200b916fSCong Wang for (;;) { 453200b916fSCong Wang unregistering = false; 454200b916fSCong Wang rtnl_lock(); 455f0b07bb1SKirill Tkhai /* We held write locked pernet_ops_rwsem, and parallel 456f0b07bb1SKirill Tkhai * setup_net() and cleanup_net() are not possible. 457f0b07bb1SKirill Tkhai */ 458200b916fSCong Wang for_each_net(net) { 459200b916fSCong Wang if (net->dev_unreg_count > 0) { 460200b916fSCong Wang unregistering = true; 461200b916fSCong Wang break; 462200b916fSCong Wang } 463200b916fSCong Wang } 464200b916fSCong Wang if (!unregistering) 465200b916fSCong Wang break; 466200b916fSCong Wang __rtnl_unlock(); 467ff960a73SPeter Zijlstra 468ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 469200b916fSCong Wang } 470ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 471200b916fSCong Wang } 472200b916fSCong Wang 47338f7b870SPatrick McHardy /** 47438f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 47538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 47638f7b870SPatrick McHardy */ 47738f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 47838f7b870SPatrick McHardy { 4798518e9bbSKirill Tkhai /* Close the race with setup_net() and cleanup_net() */ 4804420bf21SKirill Tkhai down_write(&pernet_ops_rwsem); 481200b916fSCong Wang rtnl_lock_unregistering_all(); 48238f7b870SPatrick McHardy __rtnl_link_unregister(ops); 48338f7b870SPatrick McHardy rtnl_unlock(); 4844420bf21SKirill Tkhai up_write(&pernet_ops_rwsem); 48538f7b870SPatrick McHardy } 48638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 48738f7b870SPatrick McHardy 488ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 489ba7d49b1SJiri Pirko { 490ba7d49b1SJiri Pirko struct net_device *master_dev; 491ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4928515ae38SFlorian Westphal size_t size = 0; 493ba7d49b1SJiri Pirko 4948515ae38SFlorian Westphal rcu_read_lock(); 4958515ae38SFlorian Westphal 4968515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 497ba7d49b1SJiri Pirko if (!master_dev) 4988515ae38SFlorian Westphal goto out; 4998515ae38SFlorian Westphal 500ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 5016049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 5028515ae38SFlorian Westphal goto out; 503ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 5048515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 505ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 5068515ae38SFlorian Westphal 5078515ae38SFlorian Westphal out: 5088515ae38SFlorian Westphal rcu_read_unlock(); 5098515ae38SFlorian Westphal return size; 510ba7d49b1SJiri Pirko } 511ba7d49b1SJiri Pirko 51238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 51338f7b870SPatrick McHardy { 51438f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 51538f7b870SPatrick McHardy size_t size; 51638f7b870SPatrick McHardy 51738f7b870SPatrick McHardy if (!ops) 51838f7b870SPatrick McHardy return 0; 51938f7b870SPatrick McHardy 520369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 521369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 52238f7b870SPatrick McHardy 52338f7b870SPatrick McHardy if (ops->get_size) 52438f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 525369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 52638f7b870SPatrick McHardy ops->get_size(dev); 52738f7b870SPatrick McHardy 52838f7b870SPatrick McHardy if (ops->get_xstats_size) 529369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 530369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 53138f7b870SPatrick McHardy 532ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 533ba7d49b1SJiri Pirko 53438f7b870SPatrick McHardy return size; 53538f7b870SPatrick McHardy } 53638f7b870SPatrick McHardy 537f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 538f8ff182cSThomas Graf 539f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 540f8ff182cSThomas Graf { 541f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 542f8ff182cSThomas Graf 5435fa85a09SFlorian Westphal list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 544f8ff182cSThomas Graf if (ops->family == family) 545f8ff182cSThomas Graf return ops; 546f8ff182cSThomas Graf } 547f8ff182cSThomas Graf 548f8ff182cSThomas Graf return NULL; 549f8ff182cSThomas Graf } 550f8ff182cSThomas Graf 551f8ff182cSThomas Graf /** 552f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 553f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 554f8ff182cSThomas Graf * 555f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 556f8ff182cSThomas Graf */ 5573678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 558f8ff182cSThomas Graf { 559f8ff182cSThomas Graf rtnl_lock(); 5605fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 561f8ff182cSThomas Graf rtnl_unlock(); 562f8ff182cSThomas Graf } 563f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 564f8ff182cSThomas Graf 565f8ff182cSThomas Graf /** 566f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 567f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 568f8ff182cSThomas Graf */ 569f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 570f8ff182cSThomas Graf { 571f8ff182cSThomas Graf rtnl_lock(); 5725fa85a09SFlorian Westphal list_del_rcu(&ops->list); 573f8ff182cSThomas Graf rtnl_unlock(); 5745fa85a09SFlorian Westphal 5755fa85a09SFlorian Westphal synchronize_rcu(); 576f8ff182cSThomas Graf } 577f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 578f8ff182cSThomas Graf 579b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 580b1974ed0SArad, Ronen u32 ext_filter_mask) 581f8ff182cSThomas Graf { 582f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 583f8ff182cSThomas Graf size_t size; 584f8ff182cSThomas Graf 585f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 586f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 587f8ff182cSThomas Graf 5885fa85a09SFlorian Westphal rcu_read_lock(); 5895fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 590f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 591f8ff182cSThomas Graf /* AF_* + nested data */ 592f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 593b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 594f8ff182cSThomas Graf } 595f8ff182cSThomas Graf } 5965fa85a09SFlorian Westphal rcu_read_unlock(); 597f8ff182cSThomas Graf 598f8ff182cSThomas Graf return size; 599f8ff182cSThomas Graf } 600f8ff182cSThomas Graf 601ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 602ba7d49b1SJiri Pirko { 603ba7d49b1SJiri Pirko struct net_device *master_dev; 6044c82a95eSFlorian Westphal bool ret = false; 605ba7d49b1SJiri Pirko 6064c82a95eSFlorian Westphal rcu_read_lock(); 6074c82a95eSFlorian Westphal 6084c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 609813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 6104c82a95eSFlorian Westphal ret = true; 6114c82a95eSFlorian Westphal rcu_read_unlock(); 6124c82a95eSFlorian Westphal return ret; 613ba7d49b1SJiri Pirko } 614ba7d49b1SJiri Pirko 615ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 616ba7d49b1SJiri Pirko const struct net_device *dev) 617ba7d49b1SJiri Pirko { 618ba7d49b1SJiri Pirko struct net_device *master_dev; 619ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 620ba7d49b1SJiri Pirko struct nlattr *slave_data; 621ba7d49b1SJiri Pirko int err; 622ba7d49b1SJiri Pirko 623ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 624ba7d49b1SJiri Pirko if (!master_dev) 625ba7d49b1SJiri Pirko return 0; 626ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 627ba7d49b1SJiri Pirko if (!ops) 628ba7d49b1SJiri Pirko return 0; 629ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 630ba7d49b1SJiri Pirko return -EMSGSIZE; 631ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 632ba7d49b1SJiri Pirko slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 633ba7d49b1SJiri Pirko if (!slave_data) 634ba7d49b1SJiri Pirko return -EMSGSIZE; 635ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 636ba7d49b1SJiri Pirko if (err < 0) 637ba7d49b1SJiri Pirko goto err_cancel_slave_data; 638ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 639ba7d49b1SJiri Pirko } 640ba7d49b1SJiri Pirko return 0; 641ba7d49b1SJiri Pirko 642ba7d49b1SJiri Pirko err_cancel_slave_data: 643ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 644ba7d49b1SJiri Pirko return err; 645ba7d49b1SJiri Pirko } 646ba7d49b1SJiri Pirko 647ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 648ba7d49b1SJiri Pirko const struct net_device *dev) 64938f7b870SPatrick McHardy { 65038f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 651ba7d49b1SJiri Pirko struct nlattr *data; 652ba7d49b1SJiri Pirko int err; 653ba7d49b1SJiri Pirko 654ba7d49b1SJiri Pirko if (!ops) 655ba7d49b1SJiri Pirko return 0; 656ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 657ba7d49b1SJiri Pirko return -EMSGSIZE; 658ba7d49b1SJiri Pirko if (ops->fill_xstats) { 659ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 660ba7d49b1SJiri Pirko if (err < 0) 661ba7d49b1SJiri Pirko return err; 662ba7d49b1SJiri Pirko } 663ba7d49b1SJiri Pirko if (ops->fill_info) { 664ba7d49b1SJiri Pirko data = nla_nest_start(skb, IFLA_INFO_DATA); 665ba7d49b1SJiri Pirko if (data == NULL) 666ba7d49b1SJiri Pirko return -EMSGSIZE; 667ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 668ba7d49b1SJiri Pirko if (err < 0) 669ba7d49b1SJiri Pirko goto err_cancel_data; 670ba7d49b1SJiri Pirko nla_nest_end(skb, data); 671ba7d49b1SJiri Pirko } 672ba7d49b1SJiri Pirko return 0; 673ba7d49b1SJiri Pirko 674ba7d49b1SJiri Pirko err_cancel_data: 675ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 676ba7d49b1SJiri Pirko return err; 677ba7d49b1SJiri Pirko } 678ba7d49b1SJiri Pirko 679ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 680ba7d49b1SJiri Pirko { 681ba7d49b1SJiri Pirko struct nlattr *linkinfo; 68238f7b870SPatrick McHardy int err = -EMSGSIZE; 68338f7b870SPatrick McHardy 68438f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 68538f7b870SPatrick McHardy if (linkinfo == NULL) 68638f7b870SPatrick McHardy goto out; 68738f7b870SPatrick McHardy 688ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 68938f7b870SPatrick McHardy if (err < 0) 69038f7b870SPatrick McHardy goto err_cancel_link; 691ba7d49b1SJiri Pirko 692ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 69338f7b870SPatrick McHardy if (err < 0) 694ba7d49b1SJiri Pirko goto err_cancel_link; 69538f7b870SPatrick McHardy 69638f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 69738f7b870SPatrick McHardy return 0; 69838f7b870SPatrick McHardy 69938f7b870SPatrick McHardy err_cancel_link: 70038f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 70138f7b870SPatrick McHardy out: 70238f7b870SPatrick McHardy return err; 70338f7b870SPatrick McHardy } 70438f7b870SPatrick McHardy 70595c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 7061da177e4SLinus Torvalds { 70797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 7081da177e4SLinus Torvalds int err = 0; 7091da177e4SLinus Torvalds 710ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 7111da177e4SLinus Torvalds if (echo) 71263354797SReshetova, Elena refcount_inc(&skb->users); 7131da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 7141da177e4SLinus Torvalds if (echo) 7151da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 7161da177e4SLinus Torvalds return err; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 71997c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7202942e900SThomas Graf { 72197c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72297c53cacSDenis V. Lunev 7232942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7242942e900SThomas Graf } 725e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7262942e900SThomas Graf 7271ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 72897676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 72997676b6bSThomas Graf { 73097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 73197676b6bSThomas Graf int report = 0; 73297676b6bSThomas Graf 73397676b6bSThomas Graf if (nlh) 73497676b6bSThomas Graf report = nlmsg_report(nlh); 73597676b6bSThomas Graf 7361ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 73797676b6bSThomas Graf } 738e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 73997676b6bSThomas Graf 74097c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 74197676b6bSThomas Graf { 74297c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 74397c53cacSDenis V. Lunev 74497676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 74597676b6bSThomas Graf } 746e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 74797676b6bSThomas Graf 7481da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7491da177e4SLinus Torvalds { 7502d7202bfSThomas Graf struct nlattr *mx; 7512d7202bfSThomas Graf int i, valid = 0; 7521da177e4SLinus Torvalds 7532d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 7542d7202bfSThomas Graf if (mx == NULL) 7552d7202bfSThomas Graf return -ENOBUFS; 7562d7202bfSThomas Graf 7571da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7582d7202bfSThomas Graf if (metrics[i]) { 759ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 760ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 761ea697639SDaniel Borkmann 762ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 763ea697639SDaniel Borkmann if (!name) 764ea697639SDaniel Borkmann continue; 765ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 766ea697639SDaniel Borkmann goto nla_put_failure; 767c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 768c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 769c3a8d947SDaniel Borkmann 770f8edcd12SPhil Sutter if (!user_features) 771f8edcd12SPhil Sutter continue; 772c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 773c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 774c3a8d947SDaniel Borkmann goto nla_put_failure; 775ea697639SDaniel Borkmann } else { 776a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 777a6574349SDavid S. Miller goto nla_put_failure; 7781da177e4SLinus Torvalds } 779ea697639SDaniel Borkmann valid++; 780ea697639SDaniel Borkmann } 7812d7202bfSThomas Graf } 7821da177e4SLinus Torvalds 783a57d27fcSDavid S. Miller if (!valid) { 784a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 785a57d27fcSDavid S. Miller return 0; 786a57d27fcSDavid S. Miller } 7872d7202bfSThomas Graf 7882d7202bfSThomas Graf return nla_nest_end(skb, mx); 7892d7202bfSThomas Graf 7902d7202bfSThomas Graf nla_put_failure: 791bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 792bc3ed28cSThomas Graf return -EMSGSIZE; 7931da177e4SLinus Torvalds } 794e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 7951da177e4SLinus Torvalds 796e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 79787a50699SDavid S. Miller long expires, u32 error) 798e3703b3dSThomas Graf { 799e3703b3dSThomas Graf struct rta_cacheinfo ci = { 800e3703b3dSThomas Graf .rta_error = error, 801e3703b3dSThomas Graf .rta_id = id, 802e3703b3dSThomas Graf }; 803e3703b3dSThomas Graf 8043940746dSDavid Ahern if (dst) { 8053940746dSDavid Ahern ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 8063940746dSDavid Ahern ci.rta_used = dst->__use; 8073940746dSDavid Ahern ci.rta_clntref = atomic_read(&dst->__refcnt); 8083940746dSDavid Ahern } 8098253947eSLi Wei if (expires) { 8108253947eSLi Wei unsigned long clock; 811e3703b3dSThomas Graf 8128253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 8138253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 8148253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 8158253947eSLi Wei } 816e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 817e3703b3dSThomas Graf } 818e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8191da177e4SLinus Torvalds 82093b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 821b00055aaSStefan Rompf { 822b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 823b00055aaSStefan Rompf 824b00055aaSStefan Rompf switch (transition) { 825b00055aaSStefan Rompf case IF_OPER_UP: 826b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 827b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 828b00055aaSStefan Rompf !netif_dormant(dev)) 829b00055aaSStefan Rompf operstate = IF_OPER_UP; 830b00055aaSStefan Rompf break; 831b00055aaSStefan Rompf 832b00055aaSStefan Rompf case IF_OPER_DORMANT: 833b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 834b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 835b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 836b00055aaSStefan Rompf break; 8373ff50b79SStephen Hemminger } 838b00055aaSStefan Rompf 839b00055aaSStefan Rompf if (dev->operstate != operstate) { 840b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 841b00055aaSStefan Rompf dev->operstate = operstate; 842b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 843b00055aaSStefan Rompf netdev_state_change(dev); 84493b2d4a2SDavid S. Miller } 845b00055aaSStefan Rompf } 846b00055aaSStefan Rompf 847b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 848b1beb681SJiri Benc { 849b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 850b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 851b1beb681SJiri Benc } 852b1beb681SJiri Benc 8533729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8543729d502SPatrick McHardy const struct ifinfomsg *ifm) 8553729d502SPatrick McHardy { 8563729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8573729d502SPatrick McHardy 8583729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8593729d502SPatrick McHardy if (ifm->ifi_change) 8603729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 861b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8623729d502SPatrick McHardy 8633729d502SPatrick McHardy return flags; 8643729d502SPatrick McHardy } 8653729d502SPatrick McHardy 866b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 867be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8681da177e4SLinus Torvalds { 869b60c5115SThomas Graf a->rx_packets = b->rx_packets; 870b60c5115SThomas Graf a->tx_packets = b->tx_packets; 871b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 872b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 873b60c5115SThomas Graf a->rx_errors = b->rx_errors; 874b60c5115SThomas Graf a->tx_errors = b->tx_errors; 875b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 876b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 877b60c5115SThomas Graf 878b60c5115SThomas Graf a->multicast = b->multicast; 879b60c5115SThomas Graf a->collisions = b->collisions; 880b60c5115SThomas Graf 881b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 882b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 883b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 884b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 885b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 886b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 887b60c5115SThomas Graf 888b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 889b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 890b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 891b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 892b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 893b60c5115SThomas Graf 894b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 895b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 8966e7333d3SJarod Wilson 8976e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 89810708f37SJan Engelhardt } 89910708f37SJan Engelhardt 900c02db8c6SChris Wright /* All VF info */ 901115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 902115c9b81SGreg Rose u32 ext_filter_mask) 903ebc08a6fSWilliams, Mitch A { 9049af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 905c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 9067e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 907045de01aSScott Feldman size += num_vfs * 9087e75f74aSSabrina Dubroca (nla_total_size(0) + 9097e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 9107e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 9117e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 91279aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 91379aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 914ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 9157e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 916945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 91701a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 9183b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9197e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9203b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 921343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9223b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 923343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9243b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 925343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9263b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 927343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9283b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 929343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9303b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 931343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 932c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 933c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 934c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 935c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 936dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 937c02db8c6SChris Wright return size; 938c02db8c6SChris Wright } else 939ebc08a6fSWilliams, Mitch A return 0; 940ebc08a6fSWilliams, Mitch A } 941ebc08a6fSWilliams, Mitch A 942c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 943c53864fdSDavid Gibson u32 ext_filter_mask) 94457b61080SScott Feldman { 94557b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 94657b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 94757b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 94857b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 94957b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 95057b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 95157b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 95257b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 95357b61080SScott Feldman + port_size; 95457b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 95557b61080SScott Feldman + port_size; 95657b61080SScott Feldman 957c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 958c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 95957b61080SScott Feldman return 0; 96057b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 96157b61080SScott Feldman return port_self_size + vf_ports_size + 96257b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 96357b61080SScott Feldman else 96457b61080SScott Feldman return port_self_size; 96557b61080SScott Feldman } 96657b61080SScott Feldman 967b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 968d1fdd913SBrenden Blanco { 969b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 97058038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 971a25717d2SJakub Kicinski nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 9724f91da26SJakub Kicinski nla_total_size(4); /* XDP_<mode>_PROG_ID */ 973d1fdd913SBrenden Blanco 974d1fdd913SBrenden Blanco return xdp_size; 975d1fdd913SBrenden Blanco } 976d1fdd913SBrenden Blanco 977115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 978115c9b81SGreg Rose u32 ext_filter_mask) 979339bf98fSThomas Graf { 980339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 981339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9820b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 983339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 984270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 985339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 98635c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 987339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 988339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 989339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 990339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 991339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 992339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 993339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9949a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 995edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 99676ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 99776ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 9986919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 9996919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1000339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 100138f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 10022d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1003d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1004db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 1005115c9b81SGreg Rose + nla_total_size(ext_filter_mask 1006115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1007115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1008c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1009f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1010b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 101182f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 101288d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1013c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1014b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 10153d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 10166621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 101738e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 101803ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 101979e1ad14SJiri Benc + nla_total_size(4) /* IFLA_IF_NETNSID */ 1020b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1021b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 10223e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MIN_MTU */ 10233e7a50ceSStephen Hemminger + nla_total_size(4) /* IFLA_MAX_MTU */ 102479e1ad14SJiri Benc + 0; 1025339bf98fSThomas Graf } 1026339bf98fSThomas Graf 102757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 102857b61080SScott Feldman { 102957b61080SScott Feldman struct nlattr *vf_ports; 103057b61080SScott Feldman struct nlattr *vf_port; 103157b61080SScott Feldman int vf; 103257b61080SScott Feldman int err; 103357b61080SScott Feldman 103457b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 103557b61080SScott Feldman if (!vf_ports) 103657b61080SScott Feldman return -EMSGSIZE; 103757b61080SScott Feldman 103857b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 103957b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10408ca94183SScott Feldman if (!vf_port) 10418ca94183SScott Feldman goto nla_put_failure; 1042a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1043a6574349SDavid S. Miller goto nla_put_failure; 104457b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10458ca94183SScott Feldman if (err == -EMSGSIZE) 10468ca94183SScott Feldman goto nla_put_failure; 104757b61080SScott Feldman if (err) { 104857b61080SScott Feldman nla_nest_cancel(skb, vf_port); 104957b61080SScott Feldman continue; 105057b61080SScott Feldman } 105157b61080SScott Feldman nla_nest_end(skb, vf_port); 105257b61080SScott Feldman } 105357b61080SScott Feldman 105457b61080SScott Feldman nla_nest_end(skb, vf_ports); 105557b61080SScott Feldman 105657b61080SScott Feldman return 0; 10578ca94183SScott Feldman 10588ca94183SScott Feldman nla_put_failure: 10598ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10608ca94183SScott Feldman return -EMSGSIZE; 106157b61080SScott Feldman } 106257b61080SScott Feldman 106357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 106457b61080SScott Feldman { 106557b61080SScott Feldman struct nlattr *port_self; 106657b61080SScott Feldman int err; 106757b61080SScott Feldman 106857b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 106957b61080SScott Feldman if (!port_self) 107057b61080SScott Feldman return -EMSGSIZE; 107157b61080SScott Feldman 107257b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 107357b61080SScott Feldman if (err) { 107457b61080SScott Feldman nla_nest_cancel(skb, port_self); 10758ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 107657b61080SScott Feldman } 107757b61080SScott Feldman 107857b61080SScott Feldman nla_nest_end(skb, port_self); 107957b61080SScott Feldman 108057b61080SScott Feldman return 0; 108157b61080SScott Feldman } 108257b61080SScott Feldman 1083c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1084c53864fdSDavid Gibson u32 ext_filter_mask) 108557b61080SScott Feldman { 108657b61080SScott Feldman int err; 108757b61080SScott Feldman 1088c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1089c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 109057b61080SScott Feldman return 0; 109157b61080SScott Feldman 109257b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 109357b61080SScott Feldman if (err) 109457b61080SScott Feldman return err; 109557b61080SScott Feldman 109657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 109757b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 109857b61080SScott Feldman if (err) 109957b61080SScott Feldman return err; 110057b61080SScott Feldman } 110157b61080SScott Feldman 110257b61080SScott Feldman return 0; 110357b61080SScott Feldman } 110457b61080SScott Feldman 110566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 110666cae9edSJiri Pirko { 110766cae9edSJiri Pirko int err; 110802637fceSJiri Pirko struct netdev_phys_item_id ppid; 110966cae9edSJiri Pirko 111066cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 111166cae9edSJiri Pirko if (err) { 111266cae9edSJiri Pirko if (err == -EOPNOTSUPP) 111366cae9edSJiri Pirko return 0; 111466cae9edSJiri Pirko return err; 111566cae9edSJiri Pirko } 111666cae9edSJiri Pirko 111766cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 111866cae9edSJiri Pirko return -EMSGSIZE; 111966cae9edSJiri Pirko 112066cae9edSJiri Pirko return 0; 112166cae9edSJiri Pirko } 112266cae9edSJiri Pirko 1123db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1124db24a904SDavid Ahern { 1125db24a904SDavid Ahern char name[IFNAMSIZ]; 1126db24a904SDavid Ahern int err; 1127db24a904SDavid Ahern 1128db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1129db24a904SDavid Ahern if (err) { 1130db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1131db24a904SDavid Ahern return 0; 1132db24a904SDavid Ahern return err; 1133db24a904SDavid Ahern } 1134db24a904SDavid Ahern 113577ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1136db24a904SDavid Ahern return -EMSGSIZE; 1137db24a904SDavid Ahern 1138db24a904SDavid Ahern return 0; 1139db24a904SDavid Ahern } 1140db24a904SDavid Ahern 114182f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 114282f28412SJiri Pirko { 114382f28412SJiri Pirko int err; 1144f8e20a9fSScott Feldman struct switchdev_attr attr = { 11456ff64f6fSIdo Schimmel .orig_dev = dev, 11461f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1147f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1148f8e20a9fSScott Feldman }; 114982f28412SJiri Pirko 1150f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 115182f28412SJiri Pirko if (err) { 115282f28412SJiri Pirko if (err == -EOPNOTSUPP) 115382f28412SJiri Pirko return 0; 115482f28412SJiri Pirko return err; 115582f28412SJiri Pirko } 115682f28412SJiri Pirko 115742275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 115842275bd8SScott Feldman attr.u.ppid.id)) 115982f28412SJiri Pirko return -EMSGSIZE; 116082f28412SJiri Pirko 116182f28412SJiri Pirko return 0; 116282f28412SJiri Pirko } 116382f28412SJiri Pirko 1164b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1165b22b941bSHannes Frederic Sowa struct net_device *dev) 1166b22b941bSHannes Frederic Sowa { 1167550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1168b22b941bSHannes Frederic Sowa struct nlattr *attr; 1169b22b941bSHannes Frederic Sowa 117058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 117158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1172b22b941bSHannes Frederic Sowa if (!attr) 1173b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1174b22b941bSHannes Frederic Sowa 1175550bce59SRoopa Prabhu sp = nla_data(attr); 1176550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1177550bce59SRoopa Prabhu 1178550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1179550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1180550bce59SRoopa Prabhu if (!attr) 1181550bce59SRoopa Prabhu return -EMSGSIZE; 1182550bce59SRoopa Prabhu 1183550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1184b22b941bSHannes Frederic Sowa 1185b22b941bSHannes Frederic Sowa return 0; 1186b22b941bSHannes Frederic Sowa } 1187b22b941bSHannes Frederic Sowa 1188b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1189b22b941bSHannes Frederic Sowa struct net_device *dev, 1190b22b941bSHannes Frederic Sowa int vfs_num, 1191b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1192b22b941bSHannes Frederic Sowa { 1193b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 119479aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1195b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 119679aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1197b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1198b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1199b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1200b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1201b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1202b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1203b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1204b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1205b22b941bSHannes Frederic Sowa 12060eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 12070eed9cf5SMintz, Yuval 1208b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1209b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1210b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1211b22b941bSHannes Frederic Sowa * didn't report anything. 1212b22b941bSHannes Frederic Sowa */ 1213b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1214b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1215b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1216b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1217b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1218b22b941bSHannes Frederic Sowa */ 1219b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 122079aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 122179aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1222b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1223b22b941bSHannes Frederic Sowa return 0; 1224b22b941bSHannes Frederic Sowa 1225775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1226775f4f05SDan Carpenter 1227b22b941bSHannes Frederic Sowa vf_mac.vf = 1228b22b941bSHannes Frederic Sowa vf_vlan.vf = 122979aab093SMoshe Shemesh vf_vlan_info.vf = 1230b22b941bSHannes Frederic Sowa vf_rate.vf = 1231b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1232b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1233b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1234b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1235b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1236b22b941bSHannes Frederic Sowa 1237b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1238b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1239b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 124079aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 124179aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 124279aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1243b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1244b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1245b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1246b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1247b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1248b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1249b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1250b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 125179aab093SMoshe Shemesh if (!vf) 125279aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1253b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1254b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1255b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1256b22b941bSHannes Frederic Sowa &vf_rate) || 1257b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1258b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1259b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1260b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1261b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1262b22b941bSHannes Frederic Sowa &vf_linkstate) || 1263b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1264b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1265b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1266b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1267b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 126879aab093SMoshe Shemesh goto nla_put_vf_failure; 126979aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 127079aab093SMoshe Shemesh if (!vfvlanlist) 127179aab093SMoshe Shemesh goto nla_put_vf_failure; 127279aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 127379aab093SMoshe Shemesh &vf_vlan_info)) { 127479aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 127579aab093SMoshe Shemesh goto nla_put_vf_failure; 127679aab093SMoshe Shemesh } 127779aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1278b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1279b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1280b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1281b22b941bSHannes Frederic Sowa &vf_stats); 1282b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 128379aab093SMoshe Shemesh if (!vfstats) 128479aab093SMoshe Shemesh goto nla_put_vf_failure; 1285343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1286343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1287343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1288343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1289343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1290343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1291343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1292343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1293343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1294343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1295343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1296c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1297c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1298c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1299c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1300c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 130179aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 130279aab093SMoshe Shemesh goto nla_put_vf_failure; 130379aab093SMoshe Shemesh } 1304b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1305b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1306b22b941bSHannes Frederic Sowa return 0; 130779aab093SMoshe Shemesh 130879aab093SMoshe Shemesh nla_put_vf_failure: 130979aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 131079aab093SMoshe Shemesh nla_put_vfinfo_failure: 131179aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 131279aab093SMoshe Shemesh return -EMSGSIZE; 1313b22b941bSHannes Frederic Sowa } 1314b22b941bSHannes Frederic Sowa 1315250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1316250fc3dfSFlorian Westphal struct net_device *dev, 1317250fc3dfSFlorian Westphal u32 ext_filter_mask) 1318250fc3dfSFlorian Westphal { 1319250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1320250fc3dfSFlorian Westphal int i, num_vfs; 1321250fc3dfSFlorian Westphal 1322250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1323250fc3dfSFlorian Westphal return 0; 1324250fc3dfSFlorian Westphal 1325250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1326250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1327250fc3dfSFlorian Westphal return -EMSGSIZE; 1328250fc3dfSFlorian Westphal 1329250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1330250fc3dfSFlorian Westphal return 0; 1331250fc3dfSFlorian Westphal 1332250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1333250fc3dfSFlorian Westphal if (!vfinfo) 1334250fc3dfSFlorian Westphal return -EMSGSIZE; 1335250fc3dfSFlorian Westphal 1336250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1337250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1338250fc3dfSFlorian Westphal return -EMSGSIZE; 1339250fc3dfSFlorian Westphal } 1340250fc3dfSFlorian Westphal 1341250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1342250fc3dfSFlorian Westphal return 0; 1343250fc3dfSFlorian Westphal } 1344250fc3dfSFlorian Westphal 1345b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1346b22b941bSHannes Frederic Sowa { 13475f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13485f8e4474SKangjie Lu 13495f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13505f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13515f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13525f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13535f8e4474SKangjie Lu map.irq = dev->irq; 13545f8e4474SKangjie Lu map.dma = dev->dma; 13555f8e4474SKangjie Lu map.port = dev->if_port; 13565f8e4474SKangjie Lu 1357270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1358b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1359b22b941bSHannes Frederic Sowa 1360b22b941bSHannes Frederic Sowa return 0; 1361b22b941bSHannes Frederic Sowa } 1362b22b941bSHannes Frederic Sowa 1363a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1364d67b9cd2SDaniel Borkmann { 136558038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1366d67b9cd2SDaniel Borkmann 1367d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1368d67b9cd2SDaniel Borkmann 136958038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1370a25717d2SJakub Kicinski if (!generic_xdp_prog) 1371a25717d2SJakub Kicinski return 0; 1372a25717d2SJakub Kicinski return generic_xdp_prog->aux->id; 137358038695SMartin KaFai Lau } 1374ce158e58SJakub Kicinski 1375a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1376a25717d2SJakub Kicinski { 1377a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG); 1378a25717d2SJakub Kicinski } 1379118b4aa2SJakub Kicinski 1380a25717d2SJakub Kicinski static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1381a25717d2SJakub Kicinski { 1382a25717d2SJakub Kicinski return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, 1383a25717d2SJakub Kicinski XDP_QUERY_PROG_HW); 1384a25717d2SJakub Kicinski } 1385a25717d2SJakub Kicinski 1386a25717d2SJakub Kicinski static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1387a25717d2SJakub Kicinski u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1388a25717d2SJakub Kicinski u32 (*get_prog_id)(struct net_device *dev)) 1389a25717d2SJakub Kicinski { 1390a25717d2SJakub Kicinski u32 curr_id; 1391a25717d2SJakub Kicinski int err; 1392a25717d2SJakub Kicinski 1393a25717d2SJakub Kicinski curr_id = get_prog_id(dev); 1394a25717d2SJakub Kicinski if (!curr_id) 1395a25717d2SJakub Kicinski return 0; 1396a25717d2SJakub Kicinski 1397a25717d2SJakub Kicinski *prog_id = curr_id; 1398a25717d2SJakub Kicinski err = nla_put_u32(skb, attr, curr_id); 1399a25717d2SJakub Kicinski if (err) 1400a25717d2SJakub Kicinski return err; 1401a25717d2SJakub Kicinski 1402a25717d2SJakub Kicinski if (*mode != XDP_ATTACHED_NONE) 1403a25717d2SJakub Kicinski *mode = XDP_ATTACHED_MULTI; 1404a25717d2SJakub Kicinski else 1405a25717d2SJakub Kicinski *mode = tgt_mode; 1406a25717d2SJakub Kicinski 1407a25717d2SJakub Kicinski return 0; 1408d67b9cd2SDaniel Borkmann } 1409d67b9cd2SDaniel Borkmann 1410d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1411d1fdd913SBrenden Blanco { 1412d1fdd913SBrenden Blanco struct nlattr *xdp; 141358038695SMartin KaFai Lau u32 prog_id; 1414d1fdd913SBrenden Blanco int err; 14154f91da26SJakub Kicinski u8 mode; 1416d1fdd913SBrenden Blanco 1417d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1418d1fdd913SBrenden Blanco if (!xdp) 1419d1fdd913SBrenden Blanco return -EMSGSIZE; 1420b5cdae32SDavid S. Miller 1421a25717d2SJakub Kicinski prog_id = 0; 1422a25717d2SJakub Kicinski mode = XDP_ATTACHED_NONE; 1423202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1424202aabe8SJakub Kicinski IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1425202aabe8SJakub Kicinski if (err) 1426a25717d2SJakub Kicinski goto err_cancel; 1427202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1428202aabe8SJakub Kicinski IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1429202aabe8SJakub Kicinski if (err) 1430a25717d2SJakub Kicinski goto err_cancel; 1431202aabe8SJakub Kicinski err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1432202aabe8SJakub Kicinski IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1433202aabe8SJakub Kicinski if (err) 1434a25717d2SJakub Kicinski goto err_cancel; 1435a25717d2SJakub Kicinski 14364f91da26SJakub Kicinski err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1437d1fdd913SBrenden Blanco if (err) 1438d1fdd913SBrenden Blanco goto err_cancel; 1439d1fdd913SBrenden Blanco 1440a25717d2SJakub Kicinski if (prog_id && mode != XDP_ATTACHED_MULTI) { 144158038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 144258038695SMartin KaFai Lau if (err) 144358038695SMartin KaFai Lau goto err_cancel; 144458038695SMartin KaFai Lau } 144558038695SMartin KaFai Lau 1446d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1447d1fdd913SBrenden Blanco return 0; 1448d1fdd913SBrenden Blanco 1449d1fdd913SBrenden Blanco err_cancel: 1450d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1451d1fdd913SBrenden Blanco return err; 1452d1fdd913SBrenden Blanco } 1453d1fdd913SBrenden Blanco 14543d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 14553d3ea5afSVlad Yasevich { 14563d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 14573d3ea5afSVlad Yasevich 14583d3ea5afSVlad Yasevich switch (event) { 14593d3ea5afSVlad Yasevich case NETDEV_REBOOT: 14603d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14613d3ea5afSVlad Yasevich break; 14623d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14633d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14643d3ea5afSVlad Yasevich break; 14653d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14663d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14673d3ea5afSVlad Yasevich break; 14683d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14693d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14703d3ea5afSVlad Yasevich break; 14713d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14723d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14733d3ea5afSVlad Yasevich break; 14743d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14753d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14763d3ea5afSVlad Yasevich break; 14773d3ea5afSVlad Yasevich default: 14783d3ea5afSVlad Yasevich break; 14793d3ea5afSVlad Yasevich } 14803d3ea5afSVlad Yasevich 14813d3ea5afSVlad Yasevich return rtnl_event_type; 14823d3ea5afSVlad Yasevich } 14833d3ea5afSVlad Yasevich 148479110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 148579110a04SFlorian Westphal { 148679110a04SFlorian Westphal const struct net_device *upper_dev; 148779110a04SFlorian Westphal int ret = 0; 148879110a04SFlorian Westphal 148979110a04SFlorian Westphal rcu_read_lock(); 149079110a04SFlorian Westphal 149179110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 149279110a04SFlorian Westphal if (upper_dev) 149379110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 149479110a04SFlorian Westphal 149579110a04SFlorian Westphal rcu_read_unlock(); 149679110a04SFlorian Westphal return ret; 149779110a04SFlorian Westphal } 149879110a04SFlorian Westphal 149979110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 150079110a04SFlorian Westphal { 150179110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 150279110a04SFlorian Westphal 150379110a04SFlorian Westphal if (dev->ifindex == ifindex) 150479110a04SFlorian Westphal return 0; 150579110a04SFlorian Westphal 150679110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 150779110a04SFlorian Westphal } 150879110a04SFlorian Westphal 15096c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 15106c557001SFlorian Westphal struct net_device *dev) 15116c557001SFlorian Westphal { 15126c557001SFlorian Westphal char buf[IFALIASZ]; 15136c557001SFlorian Westphal int ret; 15146c557001SFlorian Westphal 15156c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 15166c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 15176c557001SFlorian Westphal } 15186c557001SFlorian Westphal 1519b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 152079e1ad14SJiri Benc const struct net_device *dev, 152179e1ad14SJiri Benc struct net *src_net) 1522b1e66b9aSFlorian Westphal { 1523b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1524b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1525b1e66b9aSFlorian Westphal 1526b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 152779e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1528b1e66b9aSFlorian Westphal 1529b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1530b1e66b9aSFlorian Westphal return -EMSGSIZE; 1531b1e66b9aSFlorian Westphal } 1532b1e66b9aSFlorian Westphal } 1533b1e66b9aSFlorian Westphal 1534b1e66b9aSFlorian Westphal return 0; 1535b1e66b9aSFlorian Westphal } 1536b1e66b9aSFlorian Westphal 1537070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1538070cbf5bSFlorian Westphal const struct net_device *dev, 1539070cbf5bSFlorian Westphal u32 ext_filter_mask) 1540070cbf5bSFlorian Westphal { 1541070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1542070cbf5bSFlorian Westphal struct nlattr *af_spec; 1543070cbf5bSFlorian Westphal 1544070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1545070cbf5bSFlorian Westphal if (!af_spec) 1546070cbf5bSFlorian Westphal return -EMSGSIZE; 1547070cbf5bSFlorian Westphal 15485fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1549070cbf5bSFlorian Westphal struct nlattr *af; 1550070cbf5bSFlorian Westphal int err; 1551070cbf5bSFlorian Westphal 1552070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1553070cbf5bSFlorian Westphal continue; 1554070cbf5bSFlorian Westphal 1555070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1556070cbf5bSFlorian Westphal if (!af) 1557070cbf5bSFlorian Westphal return -EMSGSIZE; 1558070cbf5bSFlorian Westphal 1559070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1560070cbf5bSFlorian Westphal /* 1561070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1562070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1563070cbf5bSFlorian Westphal * means we should trim the attribute header and 1564070cbf5bSFlorian Westphal * continue. 1565070cbf5bSFlorian Westphal */ 1566070cbf5bSFlorian Westphal if (err == -ENODATA) 1567070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1568070cbf5bSFlorian Westphal else if (err < 0) 1569070cbf5bSFlorian Westphal return -EMSGSIZE; 1570070cbf5bSFlorian Westphal 1571070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1572070cbf5bSFlorian Westphal } 1573070cbf5bSFlorian Westphal 1574070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1575070cbf5bSFlorian Westphal return 0; 1576070cbf5bSFlorian Westphal } 1577070cbf5bSFlorian Westphal 157879e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 157979e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1580575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15813d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 158238e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 158338e01b30SNicolas Dichtel int tgt_netnsid) 1584b60c5115SThomas Graf { 1585b60c5115SThomas Graf struct ifinfomsg *ifm; 15861da177e4SLinus Torvalds struct nlmsghdr *nlh; 15871da177e4SLinus Torvalds 15882907c35fSEric Dumazet ASSERT_RTNL(); 1589b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1590b60c5115SThomas Graf if (nlh == NULL) 159126932566SPatrick McHardy return -EMSGSIZE; 15921da177e4SLinus Torvalds 1593b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1594b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1595b60c5115SThomas Graf ifm->__ifi_pad = 0; 1596b60c5115SThomas Graf ifm->ifi_type = dev->type; 1597b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1598b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1599b60c5115SThomas Graf ifm->ifi_change = change; 16001da177e4SLinus Torvalds 160179e1ad14SJiri Benc if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 160279e1ad14SJiri Benc goto nla_put_failure; 160379e1ad14SJiri Benc 1604a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1605a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1606a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1607a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1608a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1609a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 16103e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 16113e7a50ceSStephen Hemminger nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1612a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1613edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 161476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1615c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1616c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 16171d69c2b3SMark A. Greer #ifdef CONFIG_RPS 161876ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 16191d69c2b3SMark A. Greer #endif 162079110a04SFlorian Westphal nla_put_iflink(skb, dev) || 162179110a04SFlorian Westphal put_master_ifindex(skb, dev) || 16229a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1623a6574349SDavid S. Miller (dev->qdisc && 1624a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 16256c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 16262d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1627b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1628b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1629b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1630b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1631b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1632b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1633b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1634a6574349SDavid S. Miller goto nla_put_failure; 16350b815a1aSStephen Hemminger 16363d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 16373d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 16383d3ea5afSVlad Yasevich goto nla_put_failure; 16393d3ea5afSVlad Yasevich } 16403d3ea5afSVlad Yasevich 1641b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1642a6574349SDavid S. Miller goto nla_put_failure; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds if (dev->addr_len) { 1645a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1646a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1647a6574349SDavid S. Miller goto nla_put_failure; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 165066cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 165166cae9edSJiri Pirko goto nla_put_failure; 165266cae9edSJiri Pirko 1653db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1654db24a904SDavid Ahern goto nla_put_failure; 1655db24a904SDavid Ahern 165682f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 165782f28412SJiri Pirko goto nla_put_failure; 165882f28412SJiri Pirko 1659b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1660b60c5115SThomas Graf goto nla_put_failure; 1661b60c5115SThomas Graf 1662250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1663a6574349SDavid S. Miller goto nla_put_failure; 166457b61080SScott Feldman 1665c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 166657b61080SScott Feldman goto nla_put_failure; 166757b61080SScott Feldman 1668d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1669d1fdd913SBrenden Blanco goto nla_put_failure; 1670d1fdd913SBrenden Blanco 1671ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 167238f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 167338f7b870SPatrick McHardy goto nla_put_failure; 167438f7b870SPatrick McHardy } 167538f7b870SPatrick McHardy 167679e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1677d37512a2SNicolas Dichtel goto nla_put_failure; 1678d37512a2SNicolas Dichtel 16796621dd29SNicolas Dichtel if (new_nsid && 16806621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16816621dd29SNicolas Dichtel goto nla_put_failure; 168238e01b30SNicolas Dichtel if (new_ifindex && 168338e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 168438e01b30SNicolas Dichtel goto nla_put_failure; 168538e01b30SNicolas Dichtel 16866621dd29SNicolas Dichtel 16875fa85a09SFlorian Westphal rcu_read_lock(); 1688070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16895fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16905fa85a09SFlorian Westphal rcu_read_unlock(); 1691f8ff182cSThomas Graf 1692053c095aSJohannes Berg nlmsg_end(skb, nlh); 1693053c095aSJohannes Berg return 0; 1694b60c5115SThomas Graf 16955fa85a09SFlorian Westphal nla_put_failure_rcu: 16965fa85a09SFlorian Westphal rcu_read_unlock(); 1697b60c5115SThomas Graf nla_put_failure: 169826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 169926932566SPatrick McHardy return -EMSGSIZE; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 1702f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1703f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1704f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1705f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1706f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1707f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1708f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1709f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1710f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1711f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1712f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1713f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1714f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1715f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1716f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1717f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 17182459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 17192459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 17202459b4c6SNicolas Dichtel */ 17212459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1722f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1723f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1724f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1725f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1726f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1727f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1728f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1729f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 173046e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 173146e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 173202637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 17332d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 173482f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1735317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 173688d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1737d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 17383d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1739db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 174079e1ad14SJiri Benc [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1741b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1742b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 17433e7a50ceSStephen Hemminger [IFLA_MIN_MTU] = { .type = NLA_U32 }, 17443e7a50ceSStephen Hemminger [IFLA_MAX_MTU] = { .type = NLA_U32 }, 1745f7b12606SJiri Pirko }; 1746f7b12606SJiri Pirko 1747f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1748f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1749f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1750f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1751f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1752f7b12606SJiri Pirko }; 1753f7b12606SJiri Pirko 1754f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1755364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1756364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 175779aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1758364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1759364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1760364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1761364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 176201a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 17633b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1764dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1765cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1766cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17673b766cd8SEran Ben Elisha }; 17683b766cd8SEran Ben Elisha 1769f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1770f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1771f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1772f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1773f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1774f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1775f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1776f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1777f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1778f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1779025331dfSDaniel Borkmann 1780025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1781025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1782025331dfSDaniel Borkmann * combination with structs. 1783025331dfSDaniel Borkmann */ 1784025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1785025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1786f7b12606SJiri Pirko }; 1787f7b12606SJiri Pirko 1788d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1789d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1790d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 179185de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 179258038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1793d1fdd913SBrenden Blanco }; 1794d1fdd913SBrenden Blanco 1795dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1796dc599f76SDavid Ahern { 1797dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1798dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1799dc599f76SDavid Ahern 1800fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1801fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1802dc599f76SDavid Ahern return NULL; 1803dc599f76SDavid Ahern 1804dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1805dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1806dc599f76SDavid Ahern 1807dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1808dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1809dc599f76SDavid Ahern } 1810dc599f76SDavid Ahern 1811dc599f76SDavid Ahern return ops; 1812dc599f76SDavid Ahern } 1813dc599f76SDavid Ahern 1814dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1815dc599f76SDavid Ahern { 1816dc599f76SDavid Ahern struct net_device *master; 1817dc599f76SDavid Ahern 1818dc599f76SDavid Ahern if (!master_idx) 1819dc599f76SDavid Ahern return false; 1820dc599f76SDavid Ahern 1821dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1822dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1823dc599f76SDavid Ahern return true; 1824dc599f76SDavid Ahern 1825dc599f76SDavid Ahern return false; 1826dc599f76SDavid Ahern } 1827dc599f76SDavid Ahern 1828dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1829dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1830dc599f76SDavid Ahern { 1831dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1832dc599f76SDavid Ahern return true; 1833dc599f76SDavid Ahern 1834dc599f76SDavid Ahern return false; 1835dc599f76SDavid Ahern } 1836dc599f76SDavid Ahern 1837dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1838dc599f76SDavid Ahern int master_idx, 1839dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1840dc599f76SDavid Ahern { 1841dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1842dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1843dc599f76SDavid Ahern return true; 1844dc599f76SDavid Ahern 1845dc599f76SDavid Ahern return false; 1846dc599f76SDavid Ahern } 1847dc599f76SDavid Ahern 1848f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid) 184979e1ad14SJiri Benc { 185079e1ad14SJiri Benc struct net *net; 185179e1ad14SJiri Benc 1852f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 185379e1ad14SJiri Benc if (!net) 185479e1ad14SJiri Benc return ERR_PTR(-EINVAL); 185579e1ad14SJiri Benc 185679e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 185779e1ad14SJiri Benc * the user namespace owning the target net ns. 185879e1ad14SJiri Benc */ 1859f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 186079e1ad14SJiri Benc put_net(net); 186179e1ad14SJiri Benc return ERR_PTR(-EACCES); 186279e1ad14SJiri Benc } 186379e1ad14SJiri Benc return net; 186479e1ad14SJiri Benc } 186579e1ad14SJiri Benc 1866b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 18671da177e4SLinus Torvalds { 18683b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 186979e1ad14SJiri Benc struct net *tgt_net = net; 18707c28bd0bSEric Dumazet int h, s_h; 18717c28bd0bSEric Dumazet int idx = 0, s_idx; 18721da177e4SLinus Torvalds struct net_device *dev; 18737c28bd0bSEric Dumazet struct hlist_head *head; 1874115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1875115c9b81SGreg Rose u32 ext_filter_mask = 0; 1876dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1877dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1878dc599f76SDavid Ahern int master_idx = 0; 187979e1ad14SJiri Benc int netnsid = -1; 1880973462bbSDavid Gibson int err; 1881e5eca6d4SMichal Schmidt int hdrlen; 18821da177e4SLinus Torvalds 18837c28bd0bSEric Dumazet s_h = cb->args[0]; 18847c28bd0bSEric Dumazet s_idx = cb->args[1]; 18857c28bd0bSEric Dumazet 1886e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1887e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1888e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1889e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1890e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1891e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1892e5eca6d4SMichal Schmidt */ 1893e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1894e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1895e5eca6d4SMichal Schmidt 1896fceb6435SJohannes Berg if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1897fceb6435SJohannes Berg ifla_policy, NULL) >= 0) { 189879e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 189979e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1900f428fe4aSAndrei Vagin tgt_net = get_target_net(skb->sk, netnsid); 1901893626d6SDavid Ahern if (IS_ERR(tgt_net)) 1902893626d6SDavid Ahern return PTR_ERR(tgt_net); 190379e1ad14SJiri Benc } 190479e1ad14SJiri Benc 1905115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1906115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1907dc599f76SDavid Ahern 1908dc599f76SDavid Ahern if (tb[IFLA_MASTER]) 1909dc599f76SDavid Ahern master_idx = nla_get_u32(tb[IFLA_MASTER]); 1910dc599f76SDavid Ahern 1911dc599f76SDavid Ahern if (tb[IFLA_LINKINFO]) 1912dc599f76SDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1913dc599f76SDavid Ahern 1914dc599f76SDavid Ahern if (master_idx || kind_ops) 1915dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1916a4b64fbeSEric Dumazet } 1917115c9b81SGreg Rose 19187c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 19197562f876SPavel Emelianov idx = 0; 192079e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1921cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1922dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 19233f0ae05dSZhang Shengju goto cont; 19241da177e4SLinus Torvalds if (idx < s_idx) 19257562f876SPavel Emelianov goto cont; 192679e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 192779e1ad14SJiri Benc RTM_NEWLINK, 192815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 19297c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1930dc599f76SDavid Ahern flags, 193138e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 193279e1ad14SJiri Benc netnsid); 1933973462bbSDavid Gibson 1934f6c5775fSDavid Ahern if (err < 0) { 1935f6c5775fSDavid Ahern if (likely(skb->len)) 19367c28bd0bSEric Dumazet goto out; 19374e985adaSThomas Graf 1938f6c5775fSDavid Ahern goto out_err; 1939f6c5775fSDavid Ahern } 19407562f876SPavel Emelianov cont: 19417562f876SPavel Emelianov idx++; 19421da177e4SLinus Torvalds } 19437c28bd0bSEric Dumazet } 19447c28bd0bSEric Dumazet out: 1945f6c5775fSDavid Ahern err = skb->len; 1946f6c5775fSDavid Ahern out_err: 19477c28bd0bSEric Dumazet cb->args[1] = idx; 19487c28bd0bSEric Dumazet cb->args[0] = h; 1949d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 1950d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 195179e1ad14SJiri Benc if (netnsid >= 0) 195279e1ad14SJiri Benc put_net(tgt_net); 19531da177e4SLinus Torvalds 1954f6c5775fSDavid Ahern return err; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 1957fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1958fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 1959f7b12606SJiri Pirko { 1960fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1961f7b12606SJiri Pirko } 1962f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 196357b61080SScott Feldman 196481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 196581adee47SEric W. Biederman { 196681adee47SEric W. Biederman struct net *net; 196781adee47SEric W. Biederman /* Examine the link attributes and figure out which 196881adee47SEric W. Biederman * network namespace we are talking about. 196981adee47SEric W. Biederman */ 197081adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 197181adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1972f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1973f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 197481adee47SEric W. Biederman else 197581adee47SEric W. Biederman net = get_net(src_net); 197681adee47SEric W. Biederman return net; 197781adee47SEric W. Biederman } 197881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 197981adee47SEric W. Biederman 19807c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 19817c4f63baSChristian Brauner * examining the link attributes in the following order: 19827c4f63baSChristian Brauner * 19837c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 19847c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 19857c4f63baSChristian Brauner * 3. IFLA_IF_NETNSID 19867c4f63baSChristian Brauner */ 19877c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 19887c4f63baSChristian Brauner struct nlattr *tb[]) 19897c4f63baSChristian Brauner { 19907c4f63baSChristian Brauner struct net *net; 19917c4f63baSChristian Brauner 19927c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 19937c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 19947c4f63baSChristian Brauner 19957c4f63baSChristian Brauner if (!tb[IFLA_IF_NETNSID]) 19967c4f63baSChristian Brauner return get_net(src_net); 19977c4f63baSChristian Brauner 19987c4f63baSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 19997c4f63baSChristian Brauner if (!net) 20007c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 20017c4f63baSChristian Brauner 20027c4f63baSChristian Brauner return net; 20037c4f63baSChristian Brauner } 20047c4f63baSChristian Brauner 20057c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 20067c4f63baSChristian Brauner struct net *src_net, 20077c4f63baSChristian Brauner struct nlattr *tb[], int cap) 20087c4f63baSChristian Brauner { 20097c4f63baSChristian Brauner struct net *net; 20107c4f63baSChristian Brauner 20117c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 20127c4f63baSChristian Brauner if (IS_ERR(net)) 20137c4f63baSChristian Brauner return net; 20147c4f63baSChristian Brauner 20157c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 20167c4f63baSChristian Brauner put_net(net); 20177c4f63baSChristian Brauner return ERR_PTR(-EPERM); 20187c4f63baSChristian Brauner } 20197c4f63baSChristian Brauner 20207c4f63baSChristian Brauner return net; 20217c4f63baSChristian Brauner } 20227c4f63baSChristian Brauner 20234ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 20244ff66caeSChristian Brauner * potentially referring to different network namespaces. 20254ff66caeSChristian Brauner */ 20264ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 20274ff66caeSChristian Brauner struct netlink_ext_ack *extack, 20284ff66caeSChristian Brauner bool netns_id_only) 20294ff66caeSChristian Brauner { 20304ff66caeSChristian Brauner 20314ff66caeSChristian Brauner if (netns_id_only) { 20324ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 20334ff66caeSChristian Brauner return 0; 20344ff66caeSChristian Brauner 20354ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 20364ff66caeSChristian Brauner return -EOPNOTSUPP; 20374ff66caeSChristian Brauner } 20384ff66caeSChristian Brauner 20394ff66caeSChristian Brauner if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 20404ff66caeSChristian Brauner goto invalid_attr; 20414ff66caeSChristian Brauner 20424ff66caeSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 20434ff66caeSChristian Brauner goto invalid_attr; 20444ff66caeSChristian Brauner 20454ff66caeSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 20464ff66caeSChristian Brauner goto invalid_attr; 20474ff66caeSChristian Brauner 20484ff66caeSChristian Brauner return 0; 20494ff66caeSChristian Brauner 20504ff66caeSChristian Brauner invalid_attr: 20514ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 20524ff66caeSChristian Brauner return -EINVAL; 20534ff66caeSChristian Brauner } 20544ff66caeSChristian Brauner 20551840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 20561840bb13SThomas Graf { 20571840bb13SThomas Graf if (dev) { 20581840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 20591840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 20601840bb13SThomas Graf return -EINVAL; 20611840bb13SThomas Graf 20621840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 20631840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 20641840bb13SThomas Graf return -EINVAL; 20651840bb13SThomas Graf } 20661840bb13SThomas Graf 2067cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2068cf7afbfeSThomas Graf struct nlattr *af; 2069cf7afbfeSThomas Graf int rem, err; 2070cf7afbfeSThomas Graf 2071cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2072cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2073cf7afbfeSThomas Graf 20745fa85a09SFlorian Westphal rcu_read_lock(); 20755fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 20765fa85a09SFlorian Westphal if (!af_ops) { 20775fa85a09SFlorian Westphal rcu_read_unlock(); 2078cf7afbfeSThomas Graf return -EAFNOSUPPORT; 20795fa85a09SFlorian Westphal } 2080cf7afbfeSThomas Graf 20815fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 20825fa85a09SFlorian Westphal rcu_read_unlock(); 2083cf7afbfeSThomas Graf return -EOPNOTSUPP; 20845fa85a09SFlorian Westphal } 2085cf7afbfeSThomas Graf 2086cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 20876d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 20885fa85a09SFlorian Westphal if (err < 0) { 20895fa85a09SFlorian Westphal rcu_read_unlock(); 2090cf7afbfeSThomas Graf return err; 2091cf7afbfeSThomas Graf } 2092cf7afbfeSThomas Graf } 20935fa85a09SFlorian Westphal 20945fa85a09SFlorian Westphal rcu_read_unlock(); 20955fa85a09SFlorian Westphal } 2096cf7afbfeSThomas Graf } 2097cf7afbfeSThomas Graf 20981840bb13SThomas Graf return 0; 20991840bb13SThomas Graf } 21001840bb13SThomas Graf 2101cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2102cc8e27ccSEli Cohen int guid_type) 2103cc8e27ccSEli Cohen { 2104cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2105cc8e27ccSEli Cohen 2106cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2107cc8e27ccSEli Cohen } 2108cc8e27ccSEli Cohen 2109cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2110cc8e27ccSEli Cohen { 2111cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2112cc8e27ccSEli Cohen return -EOPNOTSUPP; 2113cc8e27ccSEli Cohen 2114cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2115cc8e27ccSEli Cohen } 2116cc8e27ccSEli Cohen 21174f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2118c02db8c6SChris Wright { 2119c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 21204f7d2cdfSDaniel Borkmann int err = -EINVAL; 2121c02db8c6SChris Wright 21224f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 21234f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 21244f7d2cdfSDaniel Borkmann 2125c02db8c6SChris Wright err = -EOPNOTSUPP; 2126c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2127c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2128c02db8c6SChris Wright ivm->mac); 21294f7d2cdfSDaniel Borkmann if (err < 0) 21304f7d2cdfSDaniel Borkmann return err; 2131c02db8c6SChris Wright } 21324f7d2cdfSDaniel Borkmann 21334f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 21344f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 21354f7d2cdfSDaniel Borkmann 2136c02db8c6SChris Wright err = -EOPNOTSUPP; 2137c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 21384f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 213979aab093SMoshe Shemesh ivv->qos, 214079aab093SMoshe Shemesh htons(ETH_P_8021Q)); 214179aab093SMoshe Shemesh if (err < 0) 214279aab093SMoshe Shemesh return err; 214379aab093SMoshe Shemesh } 214479aab093SMoshe Shemesh 214579aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 214679aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 214779aab093SMoshe Shemesh struct nlattr *attr; 214879aab093SMoshe Shemesh int rem, len = 0; 214979aab093SMoshe Shemesh 215079aab093SMoshe Shemesh err = -EOPNOTSUPP; 215179aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 215279aab093SMoshe Shemesh return err; 215379aab093SMoshe Shemesh 215479aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 215579aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 215679aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 215779aab093SMoshe Shemesh return -EINVAL; 215879aab093SMoshe Shemesh } 215979aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 216079aab093SMoshe Shemesh return -EOPNOTSUPP; 216179aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 216279aab093SMoshe Shemesh 216379aab093SMoshe Shemesh len++; 216479aab093SMoshe Shemesh } 2165fa34cd94SArnd Bergmann if (len == 0) 2166fa34cd94SArnd Bergmann return -EINVAL; 2167fa34cd94SArnd Bergmann 216879aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 216979aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 21704f7d2cdfSDaniel Borkmann if (err < 0) 21714f7d2cdfSDaniel Borkmann return err; 2172c02db8c6SChris Wright } 21734f7d2cdfSDaniel Borkmann 21744f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 21754f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2176ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 21774f7d2cdfSDaniel Borkmann 2178c02db8c6SChris Wright err = -EOPNOTSUPP; 2179ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 21804f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 21814f7d2cdfSDaniel Borkmann if (err < 0) 21824f7d2cdfSDaniel Borkmann return err; 21834f7d2cdfSDaniel Borkmann 2184ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2185ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2186ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2187ed616689SSucheta Chakraborty ivf.min_tx_rate, 2188c02db8c6SChris Wright ivt->rate); 21894f7d2cdfSDaniel Borkmann if (err < 0) 21904f7d2cdfSDaniel Borkmann return err; 2191c02db8c6SChris Wright } 21924f7d2cdfSDaniel Borkmann 21934f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 21944f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 21954f7d2cdfSDaniel Borkmann 2196ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2197ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2198ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2199ed616689SSucheta Chakraborty ivt->min_tx_rate, 2200ed616689SSucheta Chakraborty ivt->max_tx_rate); 22014f7d2cdfSDaniel Borkmann if (err < 0) 22024f7d2cdfSDaniel Borkmann return err; 2203ed616689SSucheta Chakraborty } 22044f7d2cdfSDaniel Borkmann 22054f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 22064f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 22074f7d2cdfSDaniel Borkmann 22085f8444a3SGreg Rose err = -EOPNOTSUPP; 22095f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 22105f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 22115f8444a3SGreg Rose ivs->setting); 22124f7d2cdfSDaniel Borkmann if (err < 0) 22134f7d2cdfSDaniel Borkmann return err; 22145f8444a3SGreg Rose } 22154f7d2cdfSDaniel Borkmann 22164f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 22174f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 22184f7d2cdfSDaniel Borkmann 22191d8faf48SRony Efraim err = -EOPNOTSUPP; 22201d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 22211d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 22221d8faf48SRony Efraim ivl->link_state); 22234f7d2cdfSDaniel Borkmann if (err < 0) 22244f7d2cdfSDaniel Borkmann return err; 22251d8faf48SRony Efraim } 22264f7d2cdfSDaniel Borkmann 22274f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 222801a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 222901a3d796SVlad Zolotarov 223001a3d796SVlad Zolotarov err = -EOPNOTSUPP; 22314f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 223201a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 22334f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 223401a3d796SVlad Zolotarov ivrssq_en->setting); 22354f7d2cdfSDaniel Borkmann if (err < 0) 22364f7d2cdfSDaniel Borkmann return err; 223701a3d796SVlad Zolotarov } 22384f7d2cdfSDaniel Borkmann 2239dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2240dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2241dd461d6aSHiroshi Shimamoto 2242dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2243dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2244dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2245dd461d6aSHiroshi Shimamoto if (err < 0) 2246dd461d6aSHiroshi Shimamoto return err; 2247dd461d6aSHiroshi Shimamoto } 2248dd461d6aSHiroshi Shimamoto 2249cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2250cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2251cc8e27ccSEli Cohen 2252cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2253cc8e27ccSEli Cohen return -EOPNOTSUPP; 2254cc8e27ccSEli Cohen 2255cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2256cc8e27ccSEli Cohen } 2257cc8e27ccSEli Cohen 2258cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2259cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2260cc8e27ccSEli Cohen 2261cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2262cc8e27ccSEli Cohen return -EOPNOTSUPP; 2263cc8e27ccSEli Cohen 2264cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2265cc8e27ccSEli Cohen } 2266cc8e27ccSEli Cohen 2267c02db8c6SChris Wright return err; 2268c02db8c6SChris Wright } 2269c02db8c6SChris Wright 227033eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 227133eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2272fbaec0eaSJiri Pirko { 2273898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2274fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2275fbaec0eaSJiri Pirko int err; 2276fbaec0eaSJiri Pirko 2277898e5061SJiri Pirko if (upper_dev) { 2278898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2279fbaec0eaSJiri Pirko return 0; 2280898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2281fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2282898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2283fbaec0eaSJiri Pirko if (err) 2284fbaec0eaSJiri Pirko return err; 2285fbaec0eaSJiri Pirko } else { 2286fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2287fbaec0eaSJiri Pirko } 2288fbaec0eaSJiri Pirko } 2289fbaec0eaSJiri Pirko 2290fbaec0eaSJiri Pirko if (ifindex) { 2291898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2292898e5061SJiri Pirko if (!upper_dev) 2293fbaec0eaSJiri Pirko return -EINVAL; 2294898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2295fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 229633eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2297fbaec0eaSJiri Pirko if (err) 2298fbaec0eaSJiri Pirko return err; 2299fbaec0eaSJiri Pirko } else { 2300fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2301fbaec0eaSJiri Pirko } 2302fbaec0eaSJiri Pirko } 2303fbaec0eaSJiri Pirko return 0; 2304fbaec0eaSJiri Pirko } 2305fbaec0eaSJiri Pirko 230690c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2307ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2308ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 230990f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 231090f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2311ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 231290c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 23130157f60cSPatrick McHardy { 2314d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 23150157f60cSPatrick McHardy int err; 23160157f60cSPatrick McHardy 2317644c7eebSEric Dumazet err = validate_linkmsg(dev, tb); 2318644c7eebSEric Dumazet if (err < 0) 2319644c7eebSEric Dumazet return err; 2320644c7eebSEric Dumazet 23217c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 23227c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 23237c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2324d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2325d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2326d8a5ec67SEric W. Biederman goto errout; 2327d8a5ec67SEric W. Biederman } 23287c4f63baSChristian Brauner 2329d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2330d8a5ec67SEric W. Biederman put_net(net); 2331d8a5ec67SEric W. Biederman if (err) 2332d8a5ec67SEric W. Biederman goto errout; 233390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2334d8a5ec67SEric W. Biederman } 2335d8a5ec67SEric W. Biederman 23360157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 23370157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 23380157f60cSPatrick McHardy struct ifmap k_map; 23390157f60cSPatrick McHardy 2340d314774cSStephen Hemminger if (!ops->ndo_set_config) { 23410157f60cSPatrick McHardy err = -EOPNOTSUPP; 23420157f60cSPatrick McHardy goto errout; 23430157f60cSPatrick McHardy } 23440157f60cSPatrick McHardy 23450157f60cSPatrick McHardy if (!netif_device_present(dev)) { 23460157f60cSPatrick McHardy err = -ENODEV; 23470157f60cSPatrick McHardy goto errout; 23480157f60cSPatrick McHardy } 23490157f60cSPatrick McHardy 23500157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 23510157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 23520157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 23530157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 23540157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 23550157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 23560157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 23570157f60cSPatrick McHardy 2358d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 23590157f60cSPatrick McHardy if (err < 0) 23600157f60cSPatrick McHardy goto errout; 23610157f60cSPatrick McHardy 2362ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23630157f60cSPatrick McHardy } 23640157f60cSPatrick McHardy 23650157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 23660157f60cSPatrick McHardy struct sockaddr *sa; 23670157f60cSPatrick McHardy int len; 23680157f60cSPatrick McHardy 2369153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2370153711f9SWANG Cong sizeof(*sa)); 23710157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 23720157f60cSPatrick McHardy if (!sa) { 23730157f60cSPatrick McHardy err = -ENOMEM; 23740157f60cSPatrick McHardy goto errout; 23750157f60cSPatrick McHardy } 23760157f60cSPatrick McHardy sa->sa_family = dev->type; 23770157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 23780157f60cSPatrick McHardy dev->addr_len); 2379e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 23800157f60cSPatrick McHardy kfree(sa); 23810157f60cSPatrick McHardy if (err) 23820157f60cSPatrick McHardy goto errout; 238390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23840157f60cSPatrick McHardy } 23850157f60cSPatrick McHardy 23860157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 23877a4c53beSStephen Hemminger err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 23880157f60cSPatrick McHardy if (err < 0) 23890157f60cSPatrick McHardy goto errout; 239090c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23910157f60cSPatrick McHardy } 23920157f60cSPatrick McHardy 2393cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2394cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2395ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2396cbda10faSVlad Dogaru } 2397cbda10faSVlad Dogaru 23980157f60cSPatrick McHardy /* 23990157f60cSPatrick McHardy * Interface selected by interface index but interface 24000157f60cSPatrick McHardy * name provided implies that a name change has been 24010157f60cSPatrick McHardy * requested. 24020157f60cSPatrick McHardy */ 24030157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 24040157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 24050157f60cSPatrick McHardy if (err < 0) 24060157f60cSPatrick McHardy goto errout; 240790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24080157f60cSPatrick McHardy } 24090157f60cSPatrick McHardy 24100b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 24110b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 24120b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 24130b815a1aSStephen Hemminger if (err < 0) 24140b815a1aSStephen Hemminger goto errout; 2415ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24160b815a1aSStephen Hemminger } 24170b815a1aSStephen Hemminger 24180157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 24190157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2420e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 24210157f60cSPatrick McHardy } 24220157f60cSPatrick McHardy 24230157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 24243729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 24255f9021cfSJohannes Berg if (err < 0) 24265f9021cfSJohannes Berg goto errout; 24270157f60cSPatrick McHardy } 24280157f60cSPatrick McHardy 2429fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 243033eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2431fbaec0eaSJiri Pirko if (err) 2432fbaec0eaSJiri Pirko goto errout; 243390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2434fbaec0eaSJiri Pirko } 2435fbaec0eaSJiri Pirko 24369a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 24379a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 24389a57247fSJiri Pirko if (err) 24399a57247fSJiri Pirko goto errout; 244090c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 24419a57247fSJiri Pirko } 24429a57247fSJiri Pirko 24435d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 24440cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 24455d1180fcSNicolas Dichtel 24466a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 24476a643ddbSCong Wang if (err) 244808294a26SJason Wang goto errout; 24492d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 245008294a26SJason Wang } 24510157f60cSPatrick McHardy 245246e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 245346e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 245446e6b992SStephen Hemminger 245546e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 245646e6b992SStephen Hemminger err = -EINVAL; 245746e6b992SStephen Hemminger goto errout; 245846e6b992SStephen Hemminger } 245946e6b992SStephen Hemminger 246046e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 246146e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 246246e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 246346e6b992SStephen Hemminger } 246446e6b992SStephen Hemminger } 246546e6b992SStephen Hemminger 246646e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 246746e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 246846e6b992SStephen Hemminger 246946e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 247046e6b992SStephen Hemminger err = -EINVAL; 247146e6b992SStephen Hemminger goto errout; 247246e6b992SStephen Hemminger } 247346e6b992SStephen Hemminger 247446e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 247546e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 247646e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 247746e6b992SStephen Hemminger } 247846e6b992SStephen Hemminger } 247946e6b992SStephen Hemminger 24800157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 248193b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 24820157f60cSPatrick McHardy 24830157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 24841889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 24851889b0e7SNicolas Dichtel 24860157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 24871889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2488ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24891889b0e7SNicolas Dichtel dev->link_mode = value; 249093b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 24910157f60cSPatrick McHardy } 24920157f60cSPatrick McHardy 2493c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 24944f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2495c02db8c6SChris Wright struct nlattr *attr; 2496c02db8c6SChris Wright int rem; 24974f7d2cdfSDaniel Borkmann 2498c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 24994f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 25004f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2501253683bbSDavid Howells err = -EINVAL; 2502c02db8c6SChris Wright goto errout; 2503253683bbSDavid Howells } 25044f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2505fceb6435SJohannes Berg ifla_vf_policy, NULL); 25064f7d2cdfSDaniel Borkmann if (err < 0) 25074f7d2cdfSDaniel Borkmann goto errout; 25084f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2509ebc08a6fSWilliams, Mitch A if (err < 0) 2510ebc08a6fSWilliams, Mitch A goto errout; 2511ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2512ebc08a6fSWilliams, Mitch A } 2513ebc08a6fSWilliams, Mitch A } 25140157f60cSPatrick McHardy err = 0; 25150157f60cSPatrick McHardy 251657b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 251757b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 251857b61080SScott Feldman struct nlattr *attr; 251957b61080SScott Feldman int vf; 252057b61080SScott Feldman int rem; 252157b61080SScott Feldman 252257b61080SScott Feldman err = -EOPNOTSUPP; 252357b61080SScott Feldman if (!ops->ndo_set_vf_port) 252457b61080SScott Feldman goto errout; 252557b61080SScott Feldman 252657b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2527035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2528035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2529035d210fSDaniel Borkmann err = -EINVAL; 2530035d210fSDaniel Borkmann goto errout; 2531035d210fSDaniel Borkmann } 2532035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2533fceb6435SJohannes Berg ifla_port_policy, NULL); 253457b61080SScott Feldman if (err < 0) 253557b61080SScott Feldman goto errout; 253657b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 253757b61080SScott Feldman err = -EOPNOTSUPP; 253857b61080SScott Feldman goto errout; 253957b61080SScott Feldman } 254057b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 254157b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 254257b61080SScott Feldman if (err < 0) 254357b61080SScott Feldman goto errout; 2544ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 254557b61080SScott Feldman } 254657b61080SScott Feldman } 254757b61080SScott Feldman err = 0; 254857b61080SScott Feldman 254957b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 255057b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 255157b61080SScott Feldman 255257b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2553fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2554fceb6435SJohannes Berg NULL); 255557b61080SScott Feldman if (err < 0) 255657b61080SScott Feldman goto errout; 255757b61080SScott Feldman 255857b61080SScott Feldman err = -EOPNOTSUPP; 255957b61080SScott Feldman if (ops->ndo_set_vf_port) 256057b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 256157b61080SScott Feldman if (err < 0) 256257b61080SScott Feldman goto errout; 2563ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 256457b61080SScott Feldman } 2565f8ff182cSThomas Graf 2566f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2567f8ff182cSThomas Graf struct nlattr *af; 2568f8ff182cSThomas Graf int rem; 2569f8ff182cSThomas Graf 2570f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2571f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2572f8ff182cSThomas Graf 25735fa85a09SFlorian Westphal rcu_read_lock(); 25745fa85a09SFlorian Westphal 2575058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2576f8ff182cSThomas Graf 2577cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 25785fa85a09SFlorian Westphal if (err < 0) { 25795fa85a09SFlorian Westphal rcu_read_unlock(); 2580f8ff182cSThomas Graf goto errout; 25815fa85a09SFlorian Westphal } 2582f8ff182cSThomas Graf 25835fa85a09SFlorian Westphal rcu_read_unlock(); 2584ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2585f8ff182cSThomas Graf } 2586f8ff182cSThomas Graf } 258757b61080SScott Feldman err = 0; 258857b61080SScott Feldman 258988d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 259088d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 259188d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 259288d6378bSAnuradha Karuppiah if (err) 259388d6378bSAnuradha Karuppiah goto errout; 259488d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 259588d6378bSAnuradha Karuppiah } 259688d6378bSAnuradha Karuppiah 2597d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2598d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 259985de8576SDaniel Borkmann u32 xdp_flags = 0; 2600d1fdd913SBrenden Blanco 2601d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2602fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2603d1fdd913SBrenden Blanco if (err < 0) 2604d1fdd913SBrenden Blanco goto errout; 2605d1fdd913SBrenden Blanco 260658038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2607262d8625SBrenden Blanco err = -EINVAL; 2608262d8625SBrenden Blanco goto errout; 2609262d8625SBrenden Blanco } 261085de8576SDaniel Borkmann 261185de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 261285de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 261385de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 261485de8576SDaniel Borkmann err = -EINVAL; 261585de8576SDaniel Borkmann goto errout; 261685de8576SDaniel Borkmann } 2617ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 26180489df9aSDaniel Borkmann err = -EINVAL; 26190489df9aSDaniel Borkmann goto errout; 26200489df9aSDaniel Borkmann } 262185de8576SDaniel Borkmann } 262285de8576SDaniel Borkmann 2623d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2624ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 262585de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 262685de8576SDaniel Borkmann xdp_flags); 2627d1fdd913SBrenden Blanco if (err) 2628d1fdd913SBrenden Blanco goto errout; 2629d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2630d1fdd913SBrenden Blanco } 2631d1fdd913SBrenden Blanco } 2632d1fdd913SBrenden Blanco 26330157f60cSPatrick McHardy errout: 2634ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 263564ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2636ba998906SNicolas Dichtel netdev_state_change(dev); 2637ba998906SNicolas Dichtel 2638ba998906SNicolas Dichtel if (err < 0) 2639e87cc472SJoe 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", 2640e87cc472SJoe Perches dev->name); 2641ba998906SNicolas Dichtel } 26420157f60cSPatrick McHardy 26430157f60cSPatrick McHardy return err; 26440157f60cSPatrick McHardy } 26450157f60cSPatrick McHardy 2646c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2647c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2648da5e0494SThomas Graf { 26493b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2650da5e0494SThomas Graf struct ifinfomsg *ifm; 2651da5e0494SThomas Graf struct net_device *dev; 26520157f60cSPatrick McHardy int err; 2653da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 26541da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 26551da177e4SLinus Torvalds 2656c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2657c21ef3e3SDavid Ahern extack); 2658da5e0494SThomas Graf if (err < 0) 2659da5e0494SThomas Graf goto errout; 26601da177e4SLinus Torvalds 26614ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 26624ff66caeSChristian Brauner if (err < 0) 26634ff66caeSChristian Brauner goto errout; 26644ff66caeSChristian Brauner 26655176f91eSThomas Graf if (tb[IFLA_IFNAME]) 26665176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 266778e5b891SPatrick McHardy else 266878e5b891SPatrick McHardy ifname[0] = '\0'; 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds err = -EINVAL; 2671da5e0494SThomas Graf ifm = nlmsg_data(nlh); 267251055be8SPatrick McHardy if (ifm->ifi_index > 0) 2673a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2674da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2675a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2676da5e0494SThomas Graf else 2677da5e0494SThomas Graf goto errout; 26781da177e4SLinus Torvalds 2679da5e0494SThomas Graf if (dev == NULL) { 2680da5e0494SThomas Graf err = -ENODEV; 2681da5e0494SThomas Graf goto errout; 2682da5e0494SThomas Graf } 26831da177e4SLinus Torvalds 2684ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2685da5e0494SThomas Graf errout: 26861da177e4SLinus Torvalds return err; 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 268966400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 269066400d54SWANG Cong { 269166400d54SWANG Cong struct net_device *dev, *aux; 269266400d54SWANG Cong LIST_HEAD(list_kill); 269366400d54SWANG Cong bool found = false; 269466400d54SWANG Cong 269566400d54SWANG Cong if (!group) 269666400d54SWANG Cong return -EPERM; 269766400d54SWANG Cong 269866400d54SWANG Cong for_each_netdev(net, dev) { 269966400d54SWANG Cong if (dev->group == group) { 270066400d54SWANG Cong const struct rtnl_link_ops *ops; 270166400d54SWANG Cong 270266400d54SWANG Cong found = true; 270366400d54SWANG Cong ops = dev->rtnl_link_ops; 270466400d54SWANG Cong if (!ops || !ops->dellink) 270566400d54SWANG Cong return -EOPNOTSUPP; 270666400d54SWANG Cong } 270766400d54SWANG Cong } 270866400d54SWANG Cong 270966400d54SWANG Cong if (!found) 271066400d54SWANG Cong return -ENODEV; 271166400d54SWANG Cong 271266400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 271366400d54SWANG Cong if (dev->group == group) { 271466400d54SWANG Cong const struct rtnl_link_ops *ops; 271566400d54SWANG Cong 271666400d54SWANG Cong ops = dev->rtnl_link_ops; 271766400d54SWANG Cong ops->dellink(dev, &list_kill); 271866400d54SWANG Cong } 271966400d54SWANG Cong } 272066400d54SWANG Cong unregister_netdevice_many(&list_kill); 272166400d54SWANG Cong 272266400d54SWANG Cong return 0; 272366400d54SWANG Cong } 272466400d54SWANG Cong 2725614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2726614732eaSThomas Graf { 2727614732eaSThomas Graf const struct rtnl_link_ops *ops; 2728614732eaSThomas Graf LIST_HEAD(list_kill); 2729614732eaSThomas Graf 2730614732eaSThomas Graf ops = dev->rtnl_link_ops; 2731614732eaSThomas Graf if (!ops || !ops->dellink) 2732614732eaSThomas Graf return -EOPNOTSUPP; 2733614732eaSThomas Graf 2734614732eaSThomas Graf ops->dellink(dev, &list_kill); 2735614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2736614732eaSThomas Graf 2737614732eaSThomas Graf return 0; 2738614732eaSThomas Graf } 2739614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2740614732eaSThomas Graf 2741c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2742c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 274338f7b870SPatrick McHardy { 27443b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2745b61ad68aSChristian Brauner struct net *tgt_net = net; 2746b61ad68aSChristian Brauner struct net_device *dev = NULL; 274738f7b870SPatrick McHardy struct ifinfomsg *ifm; 274838f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 274938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 275038f7b870SPatrick McHardy int err; 2751b61ad68aSChristian Brauner int netnsid = -1; 275238f7b870SPatrick McHardy 2753c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 275438f7b870SPatrick McHardy if (err < 0) 275538f7b870SPatrick McHardy return err; 275638f7b870SPatrick McHardy 27574ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 27584ff66caeSChristian Brauner if (err < 0) 27594ff66caeSChristian Brauner return err; 27604ff66caeSChristian Brauner 276138f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 276238f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 276338f7b870SPatrick McHardy 2764b61ad68aSChristian Brauner if (tb[IFLA_IF_NETNSID]) { 2765b61ad68aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2766b61ad68aSChristian Brauner tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2767b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2768b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2769b61ad68aSChristian Brauner } 2770b61ad68aSChristian Brauner 2771b61ad68aSChristian Brauner err = -EINVAL; 277238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 277338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2774b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 277538f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2776b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 277766400d54SWANG Cong else if (tb[IFLA_GROUP]) 2778b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 277938f7b870SPatrick McHardy else 2780b61ad68aSChristian Brauner goto out; 278138f7b870SPatrick McHardy 2782b61ad68aSChristian Brauner if (!dev) { 2783b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2784b61ad68aSChristian Brauner err = -ENODEV; 278538f7b870SPatrick McHardy 2786b61ad68aSChristian Brauner goto out; 2787b61ad68aSChristian Brauner } 2788b61ad68aSChristian Brauner 2789b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2790b61ad68aSChristian Brauner 2791b61ad68aSChristian Brauner out: 2792b61ad68aSChristian Brauner if (netnsid >= 0) 2793b61ad68aSChristian Brauner put_net(tgt_net); 2794b61ad68aSChristian Brauner 2795b61ad68aSChristian Brauner return err; 279638f7b870SPatrick McHardy } 279738f7b870SPatrick McHardy 27983729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 27993729d502SPatrick McHardy { 28003729d502SPatrick McHardy unsigned int old_flags; 28013729d502SPatrick McHardy int err; 28023729d502SPatrick McHardy 28033729d502SPatrick McHardy old_flags = dev->flags; 28043729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 28053729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 28063729d502SPatrick McHardy if (err < 0) 28073729d502SPatrick McHardy return err; 28083729d502SPatrick McHardy } 28093729d502SPatrick McHardy 28108d356b89SRoopa Prabhu if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 281156a49d70SRoopa Prabhu __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 28128d356b89SRoopa Prabhu } else { 28133729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2814a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 28158d356b89SRoopa Prabhu } 28163729d502SPatrick McHardy return 0; 28173729d502SPatrick McHardy } 28183729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 28193729d502SPatrick McHardy 2820c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 282178ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 28225517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2823e7199288SPavel Emelianov { 2824e7199288SPavel Emelianov struct net_device *dev; 2825d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2826d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2827e7199288SPavel Emelianov 282876ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 282976ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 283076ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2831d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 283276ff5cc9SJiri Pirko 283376ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 283476ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 283576ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2836d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2837efacb309Sstephen hemminger 2838*0e1d6ecaSEric Dumazet if (num_tx_queues < 1 || num_tx_queues > 4096) 2839*0e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2840*0e1d6ecaSEric Dumazet 2841*0e1d6ecaSEric Dumazet if (num_rx_queues < 1 || num_rx_queues > 4096) 2842*0e1d6ecaSEric Dumazet return ERR_PTR(-EINVAL); 2843*0e1d6ecaSEric Dumazet 28445517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2845c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2846e7199288SPavel Emelianov if (!dev) 2847d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2848e7199288SPavel Emelianov 284981adee47SEric W. Biederman dev_net_set(dev, net); 285081adee47SEric W. Biederman dev->rtnl_link_ops = ops; 28513729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 285281adee47SEric W. Biederman 2853e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2854e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 28552afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2856e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2857e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 28582afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 28592afb9b53SJiri Pirko } 2860e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2861e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2862e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2863e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2864e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2865e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 286693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2867e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2868e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2869ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2870ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 287146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 287246e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 287346e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2874a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2875e7199288SPavel Emelianov 2876e7199288SPavel Emelianov return dev; 2877e7199288SPavel Emelianov } 2878e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2879e7199288SPavel Emelianov 288090f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 288190f62cf3SEric W. Biederman struct net *net, int group, 2882e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2883ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2884e7ed828fSVlad Dogaru struct nlattr **tb) 2885e7ed828fSVlad Dogaru { 2886d079535dSWANG Cong struct net_device *dev, *aux; 2887e7ed828fSVlad Dogaru int err; 2888e7ed828fSVlad Dogaru 2889d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2890e7ed828fSVlad Dogaru if (dev->group == group) { 2891ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2892e7ed828fSVlad Dogaru if (err < 0) 2893e7ed828fSVlad Dogaru return err; 2894e7ed828fSVlad Dogaru } 2895e7ed828fSVlad Dogaru } 2896e7ed828fSVlad Dogaru 2897e7ed828fSVlad Dogaru return 0; 2898e7ed828fSVlad Dogaru } 2899e7ed828fSVlad Dogaru 2900c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2901c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 290238f7b870SPatrick McHardy { 29033b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 290438f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2905ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 290638f7b870SPatrick McHardy struct net_device *dev; 2907ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 290838f7b870SPatrick McHardy struct ifinfomsg *ifm; 290938f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 291038f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 291138f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 291238f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 29135517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 291438f7b870SPatrick McHardy int err; 291538f7b870SPatrick McHardy 291695a5afcaSJohannes Berg #ifdef CONFIG_MODULES 291738f7b870SPatrick McHardy replay: 29188072f085SThomas Graf #endif 2919c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 292038f7b870SPatrick McHardy if (err < 0) 292138f7b870SPatrick McHardy return err; 292238f7b870SPatrick McHardy 29234ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 29244ff66caeSChristian Brauner if (err < 0) 29254ff66caeSChristian Brauner return err; 29264ff66caeSChristian Brauner 292738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 292838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 292938f7b870SPatrick McHardy else 293038f7b870SPatrick McHardy ifname[0] = '\0'; 293138f7b870SPatrick McHardy 293238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 293338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2934881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 2935e7ed828fSVlad Dogaru else { 2936e7ed828fSVlad Dogaru if (ifname[0]) 2937881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 293838f7b870SPatrick McHardy else 293938f7b870SPatrick McHardy dev = NULL; 2940e7ed828fSVlad Dogaru } 294138f7b870SPatrick McHardy 2942ba7d49b1SJiri Pirko if (dev) { 2943ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 2944ba7d49b1SJiri Pirko if (master_dev) 2945ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 2946ba7d49b1SJiri Pirko } 2947ba7d49b1SJiri Pirko 2948e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2949e0d087afSEric Dumazet if (err < 0) 29501840bb13SThomas Graf return err; 29511840bb13SThomas Graf 295238f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 295338f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2954fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 2955fceb6435SJohannes Berg NULL); 295638f7b870SPatrick McHardy if (err < 0) 295738f7b870SPatrick McHardy return err; 295838f7b870SPatrick McHardy } else 295938f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 296038f7b870SPatrick McHardy 296138f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 296238f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 296338f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 296438f7b870SPatrick McHardy } else { 296538f7b870SPatrick McHardy kind[0] = '\0'; 296638f7b870SPatrick McHardy ops = NULL; 296738f7b870SPatrick McHardy } 296838f7b870SPatrick McHardy 296938f7b870SPatrick McHardy if (1) { 2970ccf8dbcdSKees Cook struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2971ccf8dbcdSKees Cook struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2972ba7d49b1SJiri Pirko struct nlattr **data = NULL; 2973ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 2974317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 297538f7b870SPatrick McHardy 297638f7b870SPatrick McHardy if (ops) { 2977ccf8dbcdSKees Cook if (ops->maxtype > RTNL_MAX_TYPE) 2978ccf8dbcdSKees Cook return -EINVAL; 2979ccf8dbcdSKees Cook 298038f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 298138f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 298238f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 2983fceb6435SJohannes Berg ops->policy, NULL); 298438f7b870SPatrick McHardy if (err < 0) 298538f7b870SPatrick McHardy return err; 298638f7b870SPatrick McHardy data = attr; 298738f7b870SPatrick McHardy } 298838f7b870SPatrick McHardy if (ops->validate) { 2989a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 299038f7b870SPatrick McHardy if (err < 0) 299138f7b870SPatrick McHardy return err; 299238f7b870SPatrick McHardy } 299338f7b870SPatrick McHardy } 299438f7b870SPatrick McHardy 2995ba7d49b1SJiri Pirko if (m_ops) { 2996af066ed3SPrashant Bhole if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2997ccf8dbcdSKees Cook return -EINVAL; 2998ccf8dbcdSKees Cook 2999ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 3000ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 3001ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 3002ba7d49b1SJiri Pirko m_ops->slave_maxtype, 3003ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 3004fceb6435SJohannes Berg m_ops->slave_policy, 3005fceb6435SJohannes Berg NULL); 3006ba7d49b1SJiri Pirko if (err < 0) 3007ba7d49b1SJiri Pirko return err; 3008ba7d49b1SJiri Pirko slave_data = slave_attr; 3009ba7d49b1SJiri Pirko } 3010ba7d49b1SJiri Pirko } 3011ba7d49b1SJiri Pirko 301238f7b870SPatrick McHardy if (dev) { 301390c325e3SNicolas Dichtel int status = 0; 301438f7b870SPatrick McHardy 301538f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 301638f7b870SPatrick McHardy return -EEXIST; 301738f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 301838f7b870SPatrick McHardy return -EOPNOTSUPP; 301938f7b870SPatrick McHardy 302038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 302138f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 302238f7b870SPatrick McHardy !ops->changelink) 302338f7b870SPatrick McHardy return -EOPNOTSUPP; 302438f7b870SPatrick McHardy 3025ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 302638f7b870SPatrick McHardy if (err < 0) 302738f7b870SPatrick McHardy return err; 3028ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 302938f7b870SPatrick McHardy } 303038f7b870SPatrick McHardy 3031ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3032ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 3033ba7d49b1SJiri Pirko return -EOPNOTSUPP; 3034ba7d49b1SJiri Pirko 3035ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 303617dd0ec4SMatthias Schiffer tb, slave_data, 303717dd0ec4SMatthias Schiffer extack); 3038ba7d49b1SJiri Pirko if (err < 0) 3039ba7d49b1SJiri Pirko return err; 3040ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 3041ba7d49b1SJiri Pirko } 3042ba7d49b1SJiri Pirko 3043ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 3044ddf9f970SJakub Kicinski status); 304538f7b870SPatrick McHardy } 304638f7b870SPatrick McHardy 3047ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3048ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 304990f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 3050ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 3051ddf9f970SJakub Kicinski ifm, extack, tb); 305238f7b870SPatrick McHardy return -ENODEV; 3053ffa934f1SPatrick McHardy } 305438f7b870SPatrick McHardy 3055160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 305638f7b870SPatrick McHardy return -EOPNOTSUPP; 305738f7b870SPatrick McHardy 305838f7b870SPatrick McHardy if (!ops) { 305995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 306038f7b870SPatrick McHardy if (kind[0]) { 306138f7b870SPatrick McHardy __rtnl_unlock(); 306238f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 306338f7b870SPatrick McHardy rtnl_lock(); 306438f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 306538f7b870SPatrick McHardy if (ops) 306638f7b870SPatrick McHardy goto replay; 306738f7b870SPatrick McHardy } 306838f7b870SPatrick McHardy #endif 306938f7b870SPatrick McHardy return -EOPNOTSUPP; 307038f7b870SPatrick McHardy } 307138f7b870SPatrick McHardy 3072b0ab2fabSJiri Pirko if (!ops->setup) 3073b0ab2fabSJiri Pirko return -EOPNOTSUPP; 3074b0ab2fabSJiri Pirko 30755517750fSTom Gundersen if (!ifname[0]) { 307638f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 30775517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 30785517750fSTom Gundersen } 307938f7b870SPatrick McHardy 30805bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 308113ad1774SEric W. Biederman if (IS_ERR(dest_net)) 308213ad1774SEric W. Biederman return PTR_ERR(dest_net); 308313ad1774SEric W. Biederman 3084317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3085317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3086317f4810SNicolas Dichtel 3087317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3088317f4810SNicolas Dichtel if (!link_net) { 3089317f4810SNicolas Dichtel err = -EINVAL; 3090317f4810SNicolas Dichtel goto out; 3091317f4810SNicolas Dichtel } 309206615bedSEric W. Biederman err = -EPERM; 309306615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 309406615bedSEric W. Biederman goto out; 3095317f4810SNicolas Dichtel } 3096317f4810SNicolas Dichtel 3097317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3098317f4810SNicolas Dichtel name_assign_type, ops, tb); 30999c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3100e7199288SPavel Emelianov err = PTR_ERR(dev); 31019c7dafbfSPavel Emelyanov goto out; 31029c7dafbfSPavel Emelyanov } 31039c7dafbfSPavel Emelyanov 31049c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 31059c7dafbfSPavel Emelyanov 31060e0eee24SCong Wang if (ops->newlink) { 31077a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 31087a3f4a18SMatthias Schiffer extack); 31090e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3110e51fb152SCong Wang * and unregister it on failure after registration 3111e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 31120e0eee24SCong Wang */ 3113e51fb152SCong Wang if (err < 0) { 3114e51fb152SCong Wang /* If device is not registered at all, free it now */ 3115e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3116e51fb152SCong Wang free_netdev(dev); 31170e0eee24SCong Wang goto out; 3118e51fb152SCong Wang } 31190e0eee24SCong Wang } else { 31202d85cba2SPatrick McHardy err = register_netdevice(dev); 3121fce9b9beSDan Carpenter if (err < 0) { 312238f7b870SPatrick McHardy free_netdev(dev); 31233729d502SPatrick McHardy goto out; 3124fce9b9beSDan Carpenter } 31250e0eee24SCong Wang } 31263729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 312743638900SDavid S. Miller if (err < 0) 312843638900SDavid S. Miller goto out_unregister; 312943638900SDavid S. Miller if (link_net) { 313043638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 313143638900SDavid S. Miller if (err < 0) 313243638900SDavid S. Miller goto out_unregister; 313343638900SDavid S. Miller } 3134160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 313533eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 313633eaf2a6SDavid Ahern extack); 3137160ca014STheuns Verwoerd if (err) 3138160ca014STheuns Verwoerd goto out_unregister; 3139160ca014STheuns Verwoerd } 314043638900SDavid S. Miller out: 314143638900SDavid S. Miller if (link_net) 314243638900SDavid S. Miller put_net(link_net); 314343638900SDavid S. Miller put_net(dest_net); 314443638900SDavid S. Miller return err; 314543638900SDavid S. Miller out_unregister: 31467afb8886SWANG Cong if (ops->newlink) { 31477afb8886SWANG Cong LIST_HEAD(list_kill); 31487afb8886SWANG Cong 31497afb8886SWANG Cong ops->dellink(dev, &list_kill); 31507afb8886SWANG Cong unregister_netdevice_many(&list_kill); 31517afb8886SWANG Cong } else { 31523729d502SPatrick McHardy unregister_netdevice(dev); 31537afb8886SWANG Cong } 3154317f4810SNicolas Dichtel goto out; 3155317f4810SNicolas Dichtel } 315638f7b870SPatrick McHardy } 315738f7b870SPatrick McHardy 3158c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3159c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3160711e2c33SJean Tourrilhes { 31613b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 316279e1ad14SJiri Benc struct net *tgt_net = net; 3163b60c5115SThomas Graf struct ifinfomsg *ifm; 3164a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3165b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3166b60c5115SThomas Graf struct net_device *dev = NULL; 3167b60c5115SThomas Graf struct sk_buff *nskb; 316879e1ad14SJiri Benc int netnsid = -1; 3169339bf98fSThomas Graf int err; 3170115c9b81SGreg Rose u32 ext_filter_mask = 0; 3171711e2c33SJean Tourrilhes 3172c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3173b60c5115SThomas Graf if (err < 0) 31749918f230SEric Sesterhenn return err; 3175b60c5115SThomas Graf 31764ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 31774ff66caeSChristian Brauner if (err < 0) 31784ff66caeSChristian Brauner return err; 31794ff66caeSChristian Brauner 318079e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 318179e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3182f428fe4aSAndrei Vagin tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 318379e1ad14SJiri Benc if (IS_ERR(tgt_net)) 318479e1ad14SJiri Benc return PTR_ERR(tgt_net); 318579e1ad14SJiri Benc } 318679e1ad14SJiri Benc 3187a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3188a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3189a3d12891SEric Dumazet 3190115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3191115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3192115c9b81SGreg Rose 319379e1ad14SJiri Benc err = -EINVAL; 3194b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3195a3d12891SEric Dumazet if (ifm->ifi_index > 0) 319679e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3197a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 319879e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3199a3d12891SEric Dumazet else 320079e1ad14SJiri Benc goto out; 3201b60c5115SThomas Graf 320279e1ad14SJiri Benc err = -ENODEV; 3203a3d12891SEric Dumazet if (dev == NULL) 320479e1ad14SJiri Benc goto out; 3205a3d12891SEric Dumazet 320679e1ad14SJiri Benc err = -ENOBUFS; 3207115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3208a3d12891SEric Dumazet if (nskb == NULL) 320979e1ad14SJiri Benc goto out; 3210711e2c33SJean Tourrilhes 321179e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 321279e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 321379e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 321438e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 321526932566SPatrick McHardy if (err < 0) { 321626932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 321726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 321826932566SPatrick McHardy kfree_skb(nskb); 3219a3d12891SEric Dumazet } else 322015e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 322179e1ad14SJiri Benc out: 322279e1ad14SJiri Benc if (netnsid >= 0) 322379e1ad14SJiri Benc put_net(tgt_net); 3224b60c5115SThomas Graf 3225711e2c33SJean Tourrilhes return err; 3226711e2c33SJean Tourrilhes } 3227711e2c33SJean Tourrilhes 3228115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3229c7ac8679SGreg Rose { 3230115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3231115c9b81SGreg Rose struct net_device *dev; 3232115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3233115c9b81SGreg Rose u32 ext_filter_mask = 0; 3234115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3235e5eca6d4SMichal Schmidt int hdrlen; 3236115c9b81SGreg Rose 3237e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3238e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3239e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3240e5eca6d4SMichal Schmidt 3241fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3242115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3243115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3244a4b64fbeSEric Dumazet } 3245115c9b81SGreg Rose 3246115c9b81SGreg Rose if (!ext_filter_mask) 3247115c9b81SGreg Rose return NLMSG_GOODSIZE; 3248115c9b81SGreg Rose /* 3249115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3250115c9b81SGreg Rose * buffer size based upon the filter mask. 3251115c9b81SGreg Rose */ 32526853dd48SFlorian Westphal rcu_read_lock(); 32536853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3254115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3255115c9b81SGreg Rose if_nlmsg_size(dev, 3256115c9b81SGreg Rose ext_filter_mask)); 3257115c9b81SGreg Rose } 32586853dd48SFlorian Westphal rcu_read_unlock(); 3259115c9b81SGreg Rose 326093af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3261c7ac8679SGreg Rose } 3262c7ac8679SGreg Rose 326342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 32641da177e4SLinus Torvalds { 32651da177e4SLinus Torvalds int idx; 32661da177e4SLinus Torvalds int s_idx = cb->family; 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds if (s_idx == 0) 32691da177e4SLinus Torvalds s_idx = 1; 32706853dd48SFlorian Westphal 327125239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3272addf9b90SFlorian Westphal struct rtnl_link **tab; 32731da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 3274addf9b90SFlorian Westphal struct rtnl_link *link; 32756853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 32766853dd48SFlorian Westphal 32771da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 32781da177e4SLinus Torvalds continue; 32796853dd48SFlorian Westphal 3280addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 32811da177e4SLinus Torvalds continue; 32826853dd48SFlorian Westphal 3283addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3284addf9b90SFlorian Westphal if (!tab) 3285addf9b90SFlorian Westphal continue; 3286addf9b90SFlorian Westphal 3287addf9b90SFlorian Westphal link = tab[type]; 3288addf9b90SFlorian Westphal if (!link) 3289addf9b90SFlorian Westphal continue; 3290addf9b90SFlorian Westphal 3291addf9b90SFlorian Westphal dumpit = link->dumpit; 32926853dd48SFlorian Westphal if (!dumpit) 32936853dd48SFlorian Westphal continue; 32946853dd48SFlorian Westphal 32950465277fSNicolas Dichtel if (idx > s_idx) { 32961da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 32970465277fSNicolas Dichtel cb->prev_seq = 0; 32980465277fSNicolas Dichtel cb->seq = 0; 32990465277fSNicolas Dichtel } 33006853dd48SFlorian Westphal if (dumpit(skb, cb)) 33011da177e4SLinus Torvalds break; 33021da177e4SLinus Torvalds } 33031da177e4SLinus Torvalds cb->family = idx; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds return skb->len; 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 3308395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 33093d3ea5afSVlad Yasevich unsigned int change, 331038e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 331138e01b30SNicolas Dichtel int new_ifindex) 33121da177e4SLinus Torvalds { 3313c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 33141da177e4SLinus Torvalds struct sk_buff *skb; 33150ec6d3f4SThomas Graf int err = -ENOBUFS; 3316c7ac8679SGreg Rose size_t if_info_size; 33171da177e4SLinus Torvalds 33187f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 33190ec6d3f4SThomas Graf if (skb == NULL) 33200ec6d3f4SThomas Graf goto errout; 33211da177e4SLinus Torvalds 332279e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 332379e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 332438e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 332526932566SPatrick McHardy if (err < 0) { 332626932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 332726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 332826932566SPatrick McHardy kfree_skb(skb); 332926932566SPatrick McHardy goto errout; 333026932566SPatrick McHardy } 3331395eea6cSMahesh Bandewar return skb; 33320ec6d3f4SThomas Graf errout: 33330ec6d3f4SThomas Graf if (err < 0) 33344b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3335395eea6cSMahesh Bandewar return NULL; 3336395eea6cSMahesh Bandewar } 3337395eea6cSMahesh Bandewar 3338395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3339395eea6cSMahesh Bandewar { 3340395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3341395eea6cSMahesh Bandewar 3342395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3343395eea6cSMahesh Bandewar } 3344395eea6cSMahesh Bandewar 33453d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 33463d3ea5afSVlad Yasevich unsigned int change, u32 event, 334738e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3348395eea6cSMahesh Bandewar { 3349395eea6cSMahesh Bandewar struct sk_buff *skb; 3350395eea6cSMahesh Bandewar 3351ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3352ed2a80abSNicolas Dichtel return; 3353ed2a80abSNicolas Dichtel 335438e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 335538e01b30SNicolas Dichtel new_ifindex); 3356395eea6cSMahesh Bandewar if (skb) 3357395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 33581da177e4SLinus Torvalds } 33593d3ea5afSVlad Yasevich 33603d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 33613d3ea5afSVlad Yasevich gfp_t flags) 33623d3ea5afSVlad Yasevich { 336338e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 336438e01b30SNicolas Dichtel NULL, 0); 33653d3ea5afSVlad Yasevich } 33661da177e4SLinus Torvalds 33676621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 336838e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 33696621dd29SNicolas Dichtel { 33706621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 337138e01b30SNicolas Dichtel new_nsid, new_ifindex); 33726621dd29SNicolas Dichtel } 33736621dd29SNicolas Dichtel 3374d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3375d83b0603SJohn Fastabend struct net_device *dev, 33761e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 33771c104a6bSNicolas Dichtel int type, unsigned int flags, 3378b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3379d83b0603SJohn Fastabend { 3380d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3381d83b0603SJohn Fastabend struct ndmsg *ndm; 3382d83b0603SJohn Fastabend 33831c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3384d83b0603SJohn Fastabend if (!nlh) 3385d83b0603SJohn Fastabend return -EMSGSIZE; 3386d83b0603SJohn Fastabend 3387d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3388d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3389d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3390d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3391d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3392d83b0603SJohn Fastabend ndm->ndm_type = 0; 3393d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3394b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3395d83b0603SJohn Fastabend 3396d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3397d83b0603SJohn Fastabend goto nla_put_failure; 33981e53d5bbSHubert Sokolowski if (vid) 33991e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 34001e53d5bbSHubert Sokolowski goto nla_put_failure; 3401d83b0603SJohn Fastabend 3402053c095aSJohannes Berg nlmsg_end(skb, nlh); 3403053c095aSJohannes Berg return 0; 3404d83b0603SJohn Fastabend 3405d83b0603SJohn Fastabend nla_put_failure: 3406d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3407d83b0603SJohn Fastabend return -EMSGSIZE; 3408d83b0603SJohn Fastabend } 3409d83b0603SJohn Fastabend 34103ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 34113ff661c3SJohn Fastabend { 3412f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3413f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3414f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3415f82ef3e1SSabrina Dubroca 0; 34163ff661c3SJohn Fastabend } 34173ff661c3SJohn Fastabend 3418b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3419b3379041SHubert Sokolowski u16 ndm_state) 34203ff661c3SJohn Fastabend { 34213ff661c3SJohn Fastabend struct net *net = dev_net(dev); 34223ff661c3SJohn Fastabend struct sk_buff *skb; 34233ff661c3SJohn Fastabend int err = -ENOBUFS; 34243ff661c3SJohn Fastabend 34253ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 34263ff661c3SJohn Fastabend if (!skb) 34273ff661c3SJohn Fastabend goto errout; 34283ff661c3SJohn Fastabend 34291e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3430b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 34313ff661c3SJohn Fastabend if (err < 0) { 34323ff661c3SJohn Fastabend kfree_skb(skb); 34333ff661c3SJohn Fastabend goto errout; 34343ff661c3SJohn Fastabend } 34353ff661c3SJohn Fastabend 34363ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 34373ff661c3SJohn Fastabend return; 34383ff661c3SJohn Fastabend errout: 34393ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 34403ff661c3SJohn Fastabend } 34413ff661c3SJohn Fastabend 3442090096bfSVlad Yasevich /** 3443090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3444090096bfSVlad Yasevich */ 3445090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3446090096bfSVlad Yasevich struct nlattr *tb[], 3447090096bfSVlad Yasevich struct net_device *dev, 3448f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3449090096bfSVlad Yasevich u16 flags) 3450090096bfSVlad Yasevich { 3451090096bfSVlad Yasevich int err = -EINVAL; 3452090096bfSVlad Yasevich 3453090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3454090096bfSVlad Yasevich * implement its own handler for this. 3455090096bfSVlad Yasevich */ 3456090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3457090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3458090096bfSVlad Yasevich return err; 3459090096bfSVlad Yasevich } 3460090096bfSVlad Yasevich 346165891feaSOr Gerlitz if (vid) { 346265891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 346365891feaSOr Gerlitz return err; 346465891feaSOr Gerlitz } 346565891feaSOr Gerlitz 3466090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3467090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3468090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3469090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3470090096bfSVlad Yasevich 3471090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3472090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3473090096bfSVlad Yasevich err = 0; 3474090096bfSVlad Yasevich 3475090096bfSVlad Yasevich return err; 3476090096bfSVlad Yasevich } 3477090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3478090096bfSVlad Yasevich 3479b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3480b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3481f6f6424bSJiri Pirko { 3482f6f6424bSJiri Pirko u16 vid = 0; 3483f6f6424bSJiri Pirko 3484f6f6424bSJiri Pirko if (vlan_attr) { 3485f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3486b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3487f6f6424bSJiri Pirko return -EINVAL; 3488f6f6424bSJiri Pirko } 3489f6f6424bSJiri Pirko 3490f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3491f6f6424bSJiri Pirko 3492f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3493b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3494f6f6424bSJiri Pirko return -EINVAL; 3495f6f6424bSJiri Pirko } 3496f6f6424bSJiri Pirko } 3497f6f6424bSJiri Pirko *p_vid = vid; 3498f6f6424bSJiri Pirko return 0; 3499f6f6424bSJiri Pirko } 3500f6f6424bSJiri Pirko 3501c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3502c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 350377162022SJohn Fastabend { 350477162022SJohn Fastabend struct net *net = sock_net(skb->sk); 350577162022SJohn Fastabend struct ndmsg *ndm; 350677162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 350777162022SJohn Fastabend struct net_device *dev; 350877162022SJohn Fastabend u8 *addr; 3509f6f6424bSJiri Pirko u16 vid; 351077162022SJohn Fastabend int err; 351177162022SJohn Fastabend 3512c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 351377162022SJohn Fastabend if (err < 0) 351477162022SJohn Fastabend return err; 351577162022SJohn Fastabend 351677162022SJohn Fastabend ndm = nlmsg_data(nlh); 351777162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3518b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 351977162022SJohn Fastabend return -EINVAL; 352077162022SJohn Fastabend } 352177162022SJohn Fastabend 352277162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 352377162022SJohn Fastabend if (dev == NULL) { 3524b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 352577162022SJohn Fastabend return -ENODEV; 352677162022SJohn Fastabend } 352777162022SJohn Fastabend 352877162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3529b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 353077162022SJohn Fastabend return -EINVAL; 353177162022SJohn Fastabend } 353277162022SJohn Fastabend 353377162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 353477162022SJohn Fastabend 3535b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3536f6f6424bSJiri Pirko if (err) 3537f6f6424bSJiri Pirko return err; 3538f6f6424bSJiri Pirko 353977162022SJohn Fastabend err = -EOPNOTSUPP; 354077162022SJohn Fastabend 354177162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 354277162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 354377162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3544898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3545898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3546898e5061SJiri Pirko 3547f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3548f6f6424bSJiri Pirko nlh->nlmsg_flags); 354977162022SJohn Fastabend if (err) 355077162022SJohn Fastabend goto out; 355177162022SJohn Fastabend else 355277162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 355377162022SJohn Fastabend } 355477162022SJohn Fastabend 355577162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3556090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3557090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3558090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3559f6f6424bSJiri Pirko vid, 3560090096bfSVlad Yasevich nlh->nlmsg_flags); 3561090096bfSVlad Yasevich else 3562f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 356377162022SJohn Fastabend nlh->nlmsg_flags); 356477162022SJohn Fastabend 35653ff661c3SJohn Fastabend if (!err) { 3566b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3567b3379041SHubert Sokolowski ndm->ndm_state); 356877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 356977162022SJohn Fastabend } 35703ff661c3SJohn Fastabend } 357177162022SJohn Fastabend out: 357277162022SJohn Fastabend return err; 357377162022SJohn Fastabend } 357477162022SJohn Fastabend 3575090096bfSVlad Yasevich /** 3576090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3577090096bfSVlad Yasevich */ 3578090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3579090096bfSVlad Yasevich struct nlattr *tb[], 3580090096bfSVlad Yasevich struct net_device *dev, 3581f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3582090096bfSVlad Yasevich { 3583c8a89c4aSAlexander Duyck int err = -EINVAL; 3584090096bfSVlad Yasevich 3585090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3586090096bfSVlad Yasevich * implement its own handler for this. 3587090096bfSVlad Yasevich */ 358864535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3589090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3590c8a89c4aSAlexander Duyck return err; 3591090096bfSVlad Yasevich } 3592090096bfSVlad Yasevich 3593090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3594090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3595090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3596090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3597090096bfSVlad Yasevich 3598090096bfSVlad Yasevich return err; 3599090096bfSVlad Yasevich } 3600090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3601090096bfSVlad Yasevich 3602c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3603c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 360477162022SJohn Fastabend { 360577162022SJohn Fastabend struct net *net = sock_net(skb->sk); 360677162022SJohn Fastabend struct ndmsg *ndm; 36071690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 360877162022SJohn Fastabend struct net_device *dev; 360977162022SJohn Fastabend int err = -EINVAL; 361077162022SJohn Fastabend __u8 *addr; 3611f6f6424bSJiri Pirko u16 vid; 361277162022SJohn Fastabend 361390f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 36141690be63SVlad Yasevich return -EPERM; 36151690be63SVlad Yasevich 3616c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 36171690be63SVlad Yasevich if (err < 0) 36181690be63SVlad Yasevich return err; 361977162022SJohn Fastabend 362077162022SJohn Fastabend ndm = nlmsg_data(nlh); 362177162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3622b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 362377162022SJohn Fastabend return -EINVAL; 362477162022SJohn Fastabend } 362577162022SJohn Fastabend 362677162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 362777162022SJohn Fastabend if (dev == NULL) { 3628b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 362977162022SJohn Fastabend return -ENODEV; 363077162022SJohn Fastabend } 363177162022SJohn Fastabend 36321690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3633b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 363477162022SJohn Fastabend return -EINVAL; 363577162022SJohn Fastabend } 363677162022SJohn Fastabend 36371690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 36381690be63SVlad Yasevich 3639b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3640f6f6424bSJiri Pirko if (err) 3641f6f6424bSJiri Pirko return err; 3642f6f6424bSJiri Pirko 364377162022SJohn Fastabend err = -EOPNOTSUPP; 364477162022SJohn Fastabend 364577162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 364677162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 364777162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3648898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3649898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 365077162022SJohn Fastabend 3651898e5061SJiri Pirko if (ops->ndo_fdb_del) 3652f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 365377162022SJohn Fastabend 365477162022SJohn Fastabend if (err) 365577162022SJohn Fastabend goto out; 365677162022SJohn Fastabend else 365777162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 365877162022SJohn Fastabend } 365977162022SJohn Fastabend 366077162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3661090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3662090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3663f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3664f6f6424bSJiri Pirko vid); 3665090096bfSVlad Yasevich else 3666f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 366777162022SJohn Fastabend 36683ff661c3SJohn Fastabend if (!err) { 3669b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3670b3379041SHubert Sokolowski ndm->ndm_state); 367177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 367277162022SJohn Fastabend } 36733ff661c3SJohn Fastabend } 367477162022SJohn Fastabend out: 367577162022SJohn Fastabend return err; 367677162022SJohn Fastabend } 367777162022SJohn Fastabend 3678d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3679d83b0603SJohn Fastabend struct netlink_callback *cb, 3680d83b0603SJohn Fastabend struct net_device *dev, 3681d83b0603SJohn Fastabend int *idx, 3682d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3683d83b0603SJohn Fastabend { 3684d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3685d83b0603SJohn Fastabend int err; 368615e47304SEric W. Biederman u32 portid, seq; 3687d83b0603SJohn Fastabend 368815e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3689d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3690d83b0603SJohn Fastabend 3691d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3692d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3693d83b0603SJohn Fastabend goto skip; 3694d83b0603SJohn Fastabend 36951e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3696a7a558feSJohn Fastabend portid, seq, 36971c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3698b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3699d83b0603SJohn Fastabend if (err < 0) 3700d83b0603SJohn Fastabend return err; 3701d83b0603SJohn Fastabend skip: 3702d83b0603SJohn Fastabend *idx += 1; 3703d83b0603SJohn Fastabend } 3704d83b0603SJohn Fastabend return 0; 3705d83b0603SJohn Fastabend } 3706d83b0603SJohn Fastabend 3707d83b0603SJohn Fastabend /** 37082c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3709d83b0603SJohn Fastabend * @nlh: netlink message header 3710d83b0603SJohn Fastabend * @dev: netdevice 3711d83b0603SJohn Fastabend * 3712d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 371391f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3714d83b0603SJohn Fastabend */ 3715d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3716d83b0603SJohn Fastabend struct netlink_callback *cb, 3717d83b0603SJohn Fastabend struct net_device *dev, 37185d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3719d297653dSRoopa Prabhu int *idx) 3720d83b0603SJohn Fastabend { 3721d83b0603SJohn Fastabend int err; 3722d83b0603SJohn Fastabend 3723d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3724d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3725d83b0603SJohn Fastabend if (err) 3726d83b0603SJohn Fastabend goto out; 37272934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3728d83b0603SJohn Fastabend out: 3729d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3730d297653dSRoopa Prabhu return err; 3731d83b0603SJohn Fastabend } 3732d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3733d83b0603SJohn Fastabend 373477162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 373577162022SJohn Fastabend { 373677162022SJohn Fastabend struct net_device *dev; 37375e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 37385e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 37395e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 37405e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 37415e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 37425e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3743d297653dSRoopa Prabhu struct hlist_head *head; 37445e6d2435SJamal Hadi Salim int brport_idx = 0; 37455e6d2435SJamal Hadi Salim int br_idx = 0; 3746d297653dSRoopa Prabhu int h, s_h; 3747d297653dSRoopa Prabhu int idx = 0, s_idx; 3748d297653dSRoopa Prabhu int err = 0; 3749d297653dSRoopa Prabhu int fidx = 0; 375077162022SJohn Fastabend 37510ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 37520ff50e83SAlexander Potapenko IFLA_MAX, ifla_policy, NULL); 37530ff50e83SAlexander Potapenko if (err < 0) { 37540ff50e83SAlexander Potapenko return -EINVAL; 37550ff50e83SAlexander Potapenko } else if (err == 0) { 37565e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 37575e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 37585e6d2435SJamal Hadi Salim } 375977162022SJohn Fastabend 37605e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 37615e6d2435SJamal Hadi Salim 37625e6d2435SJamal Hadi Salim if (br_idx) { 37635e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 37645e6d2435SJamal Hadi Salim if (!br_dev) 37655e6d2435SJamal Hadi Salim return -ENODEV; 37665e6d2435SJamal Hadi Salim 3767898e5061SJiri Pirko ops = br_dev->netdev_ops; 37685e6d2435SJamal Hadi Salim } 37695e6d2435SJamal Hadi Salim 3770d297653dSRoopa Prabhu s_h = cb->args[0]; 3771d297653dSRoopa Prabhu s_idx = cb->args[1]; 3772d297653dSRoopa Prabhu 3773d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3774d297653dSRoopa Prabhu idx = 0; 3775d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3776d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3777d297653dSRoopa Prabhu 37785e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 37795e6d2435SJamal Hadi Salim continue; 37805e6d2435SJamal Hadi Salim 37815e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 37825e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 37835e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 37845e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 37855e6d2435SJamal Hadi Salim } 37865e6d2435SJamal Hadi Salim } else { 37875e6d2435SJamal Hadi Salim if (dev != br_dev && 37885e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 37895e6d2435SJamal Hadi Salim continue; 37905e6d2435SJamal Hadi Salim 37915e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 37925e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 37935e6d2435SJamal Hadi Salim continue; 37945e6d2435SJamal Hadi Salim cops = ops; 37955e6d2435SJamal Hadi Salim } 37965e6d2435SJamal Hadi Salim 3797d297653dSRoopa Prabhu if (idx < s_idx) 3798d297653dSRoopa Prabhu goto cont; 3799d297653dSRoopa Prabhu 38005e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3801d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3802d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3803d297653dSRoopa Prabhu br_dev, dev, 3804d297653dSRoopa Prabhu &fidx); 3805d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3806d297653dSRoopa Prabhu goto out; 380777162022SJohn Fastabend } 3808d297653dSRoopa Prabhu } 380977162022SJohn Fastabend 38105e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3811d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3812d297653dSRoopa Prabhu dev, NULL, 3813d297653dSRoopa Prabhu &fidx); 38146cb69742SHubert Sokolowski else 3815d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3816d297653dSRoopa Prabhu &fidx); 3817d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3818d297653dSRoopa Prabhu goto out; 38195e6d2435SJamal Hadi Salim 38205e6d2435SJamal Hadi Salim cops = NULL; 3821d297653dSRoopa Prabhu 3822d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3823d297653dSRoopa Prabhu cb->args[2] = 0; 3824d297653dSRoopa Prabhu fidx = 0; 3825d297653dSRoopa Prabhu cont: 3826d297653dSRoopa Prabhu idx++; 3827d297653dSRoopa Prabhu } 382877162022SJohn Fastabend } 382977162022SJohn Fastabend 3830d297653dSRoopa Prabhu out: 3831d297653dSRoopa Prabhu cb->args[0] = h; 3832d297653dSRoopa Prabhu cb->args[1] = idx; 3833d297653dSRoopa Prabhu cb->args[2] = fidx; 3834d297653dSRoopa Prabhu 383577162022SJohn Fastabend return skb->len; 383677162022SJohn Fastabend } 383777162022SJohn Fastabend 38382c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 38392c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 38402c3c031cSScott Feldman { 38412c3c031cSScott Feldman if (mask & flag) 38422c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 38432c3c031cSScott Feldman return 0; 38442c3c031cSScott Feldman } 38452c3c031cSScott Feldman 3846815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 38472c3c031cSScott Feldman struct net_device *dev, u16 mode, 38487d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 38497d4f8d87SScott Feldman u32 filter_mask, 38507d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 38517d4f8d87SScott Feldman struct net_device *dev, 38527d4f8d87SScott Feldman u32 filter_mask)) 3853815cccbfSJohn Fastabend { 3854815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3855815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3856815cccbfSJohn Fastabend struct nlattr *br_afspec; 38572c3c031cSScott Feldman struct nlattr *protinfo; 3858815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3859898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 38607d4f8d87SScott Feldman int err = 0; 3861815cccbfSJohn Fastabend 386246c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3863815cccbfSJohn Fastabend if (nlh == NULL) 3864815cccbfSJohn Fastabend return -EMSGSIZE; 3865815cccbfSJohn Fastabend 3866815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3867815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3868815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3869815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3870815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3871815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3872815cccbfSJohn Fastabend ifm->ifi_change = 0; 3873815cccbfSJohn Fastabend 3874815cccbfSJohn Fastabend 3875815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3876815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3877815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3878898e5061SJiri Pirko (br_dev && 3879898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3880815cccbfSJohn Fastabend (dev->addr_len && 3881815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3882a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3883a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3884815cccbfSJohn Fastabend goto nla_put_failure; 3885815cccbfSJohn Fastabend 3886815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3887815cccbfSJohn Fastabend if (!br_afspec) 3888815cccbfSJohn Fastabend goto nla_put_failure; 3889815cccbfSJohn Fastabend 38901d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3891815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3892815cccbfSJohn Fastabend goto nla_put_failure; 3893815cccbfSJohn Fastabend } 38941d460b98SRoopa Prabhu 38951d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 38961d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 38971d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 38981d460b98SRoopa Prabhu goto nla_put_failure; 38991d460b98SRoopa Prabhu } 39001d460b98SRoopa Prabhu } 39017d4f8d87SScott Feldman if (vlan_fill) { 39027d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 39037d4f8d87SScott Feldman if (err) { 39047d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 39057d4f8d87SScott Feldman goto nla_put_failure; 39067d4f8d87SScott Feldman } 39077d4f8d87SScott Feldman } 3908815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3909815cccbfSJohn Fastabend 39102c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 39112c3c031cSScott Feldman if (!protinfo) 39122c3c031cSScott Feldman goto nla_put_failure; 39132c3c031cSScott Feldman 39142c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 39152c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 39162c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39172c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 39182c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39192c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 39202c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 39212c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39222c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 39232c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39242c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 39252c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39262c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 39272c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39282c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 39292c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 39302c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 39312c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 39322c3c031cSScott Feldman goto nla_put_failure; 39332c3c031cSScott Feldman } 39342c3c031cSScott Feldman 39352c3c031cSScott Feldman nla_nest_end(skb, protinfo); 39362c3c031cSScott Feldman 3937053c095aSJohannes Berg nlmsg_end(skb, nlh); 3938053c095aSJohannes Berg return 0; 3939815cccbfSJohn Fastabend nla_put_failure: 3940815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 39417d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 3942815cccbfSJohn Fastabend } 39437d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3944815cccbfSJohn Fastabend 3945e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3946e5a55a89SJohn Fastabend { 3947e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3948e5a55a89SJohn Fastabend struct net_device *dev; 3949e5a55a89SJohn Fastabend int idx = 0; 3950e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 3951e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 39526cbdceebSVlad Yasevich u32 filter_mask = 0; 3953d64f69b0SRoopa Prabhu int err; 39546cbdceebSVlad Yasevich 3955aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3956aa68c20fSThomas Graf struct nlattr *extfilt; 3957aa68c20fSThomas Graf 39583e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 39596cbdceebSVlad Yasevich IFLA_EXT_MASK); 3960aa68c20fSThomas Graf if (extfilt) { 3961aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 3962aa68c20fSThomas Graf return -EINVAL; 3963aa68c20fSThomas Graf 39646cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 3965aa68c20fSThomas Graf } 3966aa68c20fSThomas Graf } 3967e5a55a89SJohn Fastabend 3968e5a55a89SJohn Fastabend rcu_read_lock(); 3969e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 3970e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 3971898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3972e5a55a89SJohn Fastabend 3973898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3974d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3975d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 3976d64f69b0SRoopa Prabhu skb, portid, seq, dev, 3977d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 3978f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3979f6c5775fSDavid Ahern if (likely(skb->len)) 3980e5a55a89SJohn Fastabend break; 3981f6c5775fSDavid Ahern 3982f6c5775fSDavid Ahern goto out_err; 3983f6c5775fSDavid Ahern } 3984d64f69b0SRoopa Prabhu } 3985e5a55a89SJohn Fastabend idx++; 3986e5a55a89SJohn Fastabend } 3987e5a55a89SJohn Fastabend 3988e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 3989d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3990d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 3991d64f69b0SRoopa Prabhu seq, dev, 399246c264daSNicolas Dichtel filter_mask, 3993d64f69b0SRoopa Prabhu NLM_F_MULTI); 3994f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3995f6c5775fSDavid Ahern if (likely(skb->len)) 3996e5a55a89SJohn Fastabend break; 3997f6c5775fSDavid Ahern 3998f6c5775fSDavid Ahern goto out_err; 3999f6c5775fSDavid Ahern } 4000d64f69b0SRoopa Prabhu } 4001e5a55a89SJohn Fastabend idx++; 4002e5a55a89SJohn Fastabend } 4003e5a55a89SJohn Fastabend } 4004f6c5775fSDavid Ahern err = skb->len; 4005f6c5775fSDavid Ahern out_err: 4006e5a55a89SJohn Fastabend rcu_read_unlock(); 4007e5a55a89SJohn Fastabend cb->args[0] = idx; 4008e5a55a89SJohn Fastabend 4009f6c5775fSDavid Ahern return err; 4010e5a55a89SJohn Fastabend } 4011e5a55a89SJohn Fastabend 40122469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 40132469ffd7SJohn Fastabend { 40142469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 40152469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 40162469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 40172469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 40182469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 40192469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 40202469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 40212469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 40222469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 40232469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 40242469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 40252469ffd7SJohn Fastabend } 40262469ffd7SJohn Fastabend 402702dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 40282469ffd7SJohn Fastabend { 40292469ffd7SJohn Fastabend struct net *net = dev_net(dev); 40302469ffd7SJohn Fastabend struct sk_buff *skb; 40312469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 40322469ffd7SJohn Fastabend 403302dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 403402dba438SRoopa Prabhu return 0; 403502dba438SRoopa Prabhu 40362469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 40372469ffd7SJohn Fastabend if (!skb) { 40382469ffd7SJohn Fastabend err = -ENOMEM; 40392469ffd7SJohn Fastabend goto errout; 40402469ffd7SJohn Fastabend } 40412469ffd7SJohn Fastabend 404246c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4043c38e01b8SJohn Fastabend if (err < 0) 4044c38e01b8SJohn Fastabend goto errout; 40452469ffd7SJohn Fastabend 404659ccaaaaSRoopa Prabhu if (!skb->len) 404759ccaaaaSRoopa Prabhu goto errout; 404859ccaaaaSRoopa Prabhu 40492469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 40502469ffd7SJohn Fastabend return 0; 40512469ffd7SJohn Fastabend errout: 40522469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 40532469ffd7SJohn Fastabend kfree_skb(skb); 405459ccaaaaSRoopa Prabhu if (err) 40552469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 40562469ffd7SJohn Fastabend return err; 40572469ffd7SJohn Fastabend } 40582469ffd7SJohn Fastabend 4059c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4060c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4061e5a55a89SJohn Fastabend { 4062e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 4063e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 4064e5a55a89SJohn Fastabend struct net_device *dev; 40652469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 40662469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 40674de8b413SRosen, Rami u16 flags = 0; 4068c38e01b8SJohn Fastabend bool have_flags = false; 4069e5a55a89SJohn Fastabend 4070e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 4071e5a55a89SJohn Fastabend return -EINVAL; 4072e5a55a89SJohn Fastabend 4073e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 4074e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 4075e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 4076e5a55a89SJohn Fastabend 4077e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4078e5a55a89SJohn Fastabend if (!dev) { 4079b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4080e5a55a89SJohn Fastabend return -ENODEV; 4081e5a55a89SJohn Fastabend } 4082e5a55a89SJohn Fastabend 40832469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 40842469ffd7SJohn Fastabend if (br_spec) { 40852469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 40862469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40876e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40886e8d1c55SThomas Graf return -EINVAL; 40896e8d1c55SThomas Graf 4090c38e01b8SJohn Fastabend have_flags = true; 40912469ffd7SJohn Fastabend flags = nla_get_u16(attr); 40922469ffd7SJohn Fastabend break; 40932469ffd7SJohn Fastabend } 40942469ffd7SJohn Fastabend } 40952469ffd7SJohn Fastabend } 40962469ffd7SJohn Fastabend 40972469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4098898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4099898e5061SJiri Pirko 4100898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 41012469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4102e5a55a89SJohn Fastabend goto out; 4103e5a55a89SJohn Fastabend } 4104e5a55a89SJohn Fastabend 4105add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 41062469ffd7SJohn Fastabend if (err) 41072469ffd7SJohn Fastabend goto out; 41082469ffd7SJohn Fastabend 41092469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 41102469ffd7SJohn Fastabend } 41112469ffd7SJohn Fastabend 41122469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 41132469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 41142469ffd7SJohn Fastabend err = -EOPNOTSUPP; 41152469ffd7SJohn Fastabend else 4116add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4117add511b3SRoopa Prabhu flags); 411802dba438SRoopa Prabhu if (!err) { 41192469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 412002dba438SRoopa Prabhu 412102dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 412202dba438SRoopa Prabhu * change 412302dba438SRoopa Prabhu */ 412402dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 412502dba438SRoopa Prabhu } 41262469ffd7SJohn Fastabend } 41272469ffd7SJohn Fastabend 4128c38e01b8SJohn Fastabend if (have_flags) 41292469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4130e5a55a89SJohn Fastabend out: 4131e5a55a89SJohn Fastabend return err; 4132e5a55a89SJohn Fastabend } 4133e5a55a89SJohn Fastabend 4134c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4135c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4136407af329SVlad Yasevich { 4137407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4138407af329SVlad Yasevich struct ifinfomsg *ifm; 4139407af329SVlad Yasevich struct net_device *dev; 4140407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4141407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 41424de8b413SRosen, Rami u16 flags = 0; 4143407af329SVlad Yasevich bool have_flags = false; 4144407af329SVlad Yasevich 4145407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4146407af329SVlad Yasevich return -EINVAL; 4147407af329SVlad Yasevich 4148407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4149407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4150407af329SVlad Yasevich return -EPFNOSUPPORT; 4151407af329SVlad Yasevich 4152407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4153407af329SVlad Yasevich if (!dev) { 4154b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4155407af329SVlad Yasevich return -ENODEV; 4156407af329SVlad Yasevich } 4157407af329SVlad Yasevich 4158407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4159407af329SVlad Yasevich if (br_spec) { 4160407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4161407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 41626e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 41636e8d1c55SThomas Graf return -EINVAL; 41646e8d1c55SThomas Graf 4165407af329SVlad Yasevich have_flags = true; 4166407af329SVlad Yasevich flags = nla_get_u16(attr); 4167407af329SVlad Yasevich break; 4168407af329SVlad Yasevich } 4169407af329SVlad Yasevich } 4170407af329SVlad Yasevich } 4171407af329SVlad Yasevich 4172407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4173407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4174407af329SVlad Yasevich 4175407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4176407af329SVlad Yasevich err = -EOPNOTSUPP; 4177407af329SVlad Yasevich goto out; 4178407af329SVlad Yasevich } 4179407af329SVlad Yasevich 4180add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4181407af329SVlad Yasevich if (err) 4182407af329SVlad Yasevich goto out; 4183407af329SVlad Yasevich 4184407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4185407af329SVlad Yasevich } 4186407af329SVlad Yasevich 4187407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4188407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4189407af329SVlad Yasevich err = -EOPNOTSUPP; 4190407af329SVlad Yasevich else 4191add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4192add511b3SRoopa Prabhu flags); 4193407af329SVlad Yasevich 419402dba438SRoopa Prabhu if (!err) { 4195407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 419602dba438SRoopa Prabhu 419702dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 419802dba438SRoopa Prabhu * change 419902dba438SRoopa Prabhu */ 420002dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 420102dba438SRoopa Prabhu } 4202407af329SVlad Yasevich } 4203407af329SVlad Yasevich 4204407af329SVlad Yasevich if (have_flags) 4205407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4206407af329SVlad Yasevich out: 4207407af329SVlad Yasevich return err; 4208407af329SVlad Yasevich } 4209407af329SVlad Yasevich 4210e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4211e8872a25SNikolay Aleksandrov { 4212e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4213e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4214e8872a25SNikolay Aleksandrov } 4215e8872a25SNikolay Aleksandrov 421669ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 421769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 421869ae6ad2SNogah Frankel { 421969ae6ad2SNogah Frankel switch (attr_id) { 422069ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 422169ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 422269ae6ad2SNogah Frankel } 422369ae6ad2SNogah Frankel 422469ae6ad2SNogah Frankel return 0; 422569ae6ad2SNogah Frankel } 422669ae6ad2SNogah Frankel 422769ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 422869ae6ad2SNogah Frankel int *prividx) 422969ae6ad2SNogah Frankel { 423069ae6ad2SNogah Frankel struct nlattr *attr = NULL; 423169ae6ad2SNogah Frankel int attr_id, size; 423269ae6ad2SNogah Frankel void *attr_data; 423369ae6ad2SNogah Frankel int err; 423469ae6ad2SNogah Frankel 423569ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 423669ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 423769ae6ad2SNogah Frankel return -ENODATA; 423869ae6ad2SNogah Frankel 423969ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 424069ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 424169ae6ad2SNogah Frankel if (attr_id < *prividx) 424269ae6ad2SNogah Frankel continue; 424369ae6ad2SNogah Frankel 424469ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 424569ae6ad2SNogah Frankel if (!size) 424669ae6ad2SNogah Frankel continue; 424769ae6ad2SNogah Frankel 42483df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 424969ae6ad2SNogah Frankel continue; 425069ae6ad2SNogah Frankel 425169ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 425269ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 425369ae6ad2SNogah Frankel if (!attr) 425469ae6ad2SNogah Frankel goto nla_put_failure; 425569ae6ad2SNogah Frankel 425669ae6ad2SNogah Frankel attr_data = nla_data(attr); 425769ae6ad2SNogah Frankel memset(attr_data, 0, size); 425869ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 425969ae6ad2SNogah Frankel attr_data); 426069ae6ad2SNogah Frankel if (err) 426169ae6ad2SNogah Frankel goto get_offload_stats_failure; 426269ae6ad2SNogah Frankel } 426369ae6ad2SNogah Frankel 426469ae6ad2SNogah Frankel if (!attr) 426569ae6ad2SNogah Frankel return -ENODATA; 426669ae6ad2SNogah Frankel 426769ae6ad2SNogah Frankel *prividx = 0; 426869ae6ad2SNogah Frankel return 0; 426969ae6ad2SNogah Frankel 427069ae6ad2SNogah Frankel nla_put_failure: 427169ae6ad2SNogah Frankel err = -EMSGSIZE; 427269ae6ad2SNogah Frankel get_offload_stats_failure: 427369ae6ad2SNogah Frankel *prividx = attr_id; 427469ae6ad2SNogah Frankel return err; 427569ae6ad2SNogah Frankel } 427669ae6ad2SNogah Frankel 427769ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 427869ae6ad2SNogah Frankel { 427969ae6ad2SNogah Frankel int nla_size = 0; 428069ae6ad2SNogah Frankel int attr_id; 428169ae6ad2SNogah Frankel int size; 428269ae6ad2SNogah Frankel 428369ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 428469ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 428569ae6ad2SNogah Frankel return 0; 428669ae6ad2SNogah Frankel 428769ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 428869ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 42893df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 429069ae6ad2SNogah Frankel continue; 429169ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 429269ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 429369ae6ad2SNogah Frankel } 429469ae6ad2SNogah Frankel 429569ae6ad2SNogah Frankel if (nla_size != 0) 429669ae6ad2SNogah Frankel nla_size += nla_total_size(0); 429769ae6ad2SNogah Frankel 429869ae6ad2SNogah Frankel return nla_size; 429969ae6ad2SNogah Frankel } 430069ae6ad2SNogah Frankel 430110c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 430210c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4303e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4304e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 430510c9ead9SRoopa Prabhu { 430610c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 430710c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 430810c9ead9SRoopa Prabhu struct nlattr *attr; 4309e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 431069ae6ad2SNogah Frankel int err; 431110c9ead9SRoopa Prabhu 431210c9ead9SRoopa Prabhu ASSERT_RTNL(); 431310c9ead9SRoopa Prabhu 431410c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 431510c9ead9SRoopa Prabhu if (!nlh) 431610c9ead9SRoopa Prabhu return -EMSGSIZE; 431710c9ead9SRoopa Prabhu 431810c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4319ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4320ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4321ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 432210c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 432310c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 432410c9ead9SRoopa Prabhu 4325e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 432610c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 432710c9ead9SRoopa Prabhu 432858414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 432958414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 433058414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 433110c9ead9SRoopa Prabhu if (!attr) 433210c9ead9SRoopa Prabhu goto nla_put_failure; 433310c9ead9SRoopa Prabhu 433410c9ead9SRoopa Prabhu sp = nla_data(attr); 433510c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 433610c9ead9SRoopa Prabhu } 433710c9ead9SRoopa Prabhu 433897a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 433997a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 434097a47facSNikolay Aleksandrov 434197a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 434297a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 434397a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 434497a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 434597a47facSNikolay Aleksandrov if (!attr) 434697a47facSNikolay Aleksandrov goto nla_put_failure; 434797a47facSNikolay Aleksandrov 434880e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 434980e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 435080e73cc5SNikolay Aleksandrov if (err) 435180e73cc5SNikolay Aleksandrov goto nla_put_failure; 435280e73cc5SNikolay Aleksandrov *idxattr = 0; 435380e73cc5SNikolay Aleksandrov } 435480e73cc5SNikolay Aleksandrov } 435580e73cc5SNikolay Aleksandrov 435680e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 435780e73cc5SNikolay Aleksandrov *idxattr)) { 435880e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 435980e73cc5SNikolay Aleksandrov const struct net_device *master; 436080e73cc5SNikolay Aleksandrov 436180e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 436280e73cc5SNikolay Aleksandrov if (master) 436380e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 436480e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 436580e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 436680e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 436780e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 436880e73cc5SNikolay Aleksandrov if (!attr) 436980e73cc5SNikolay Aleksandrov goto nla_put_failure; 437080e73cc5SNikolay Aleksandrov 437180e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 437297a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 437397a47facSNikolay Aleksandrov if (err) 437497a47facSNikolay Aleksandrov goto nla_put_failure; 437597a47facSNikolay Aleksandrov *idxattr = 0; 437697a47facSNikolay Aleksandrov } 437797a47facSNikolay Aleksandrov } 437897a47facSNikolay Aleksandrov 437969ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 438069ae6ad2SNogah Frankel *idxattr)) { 438169ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 438269ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 438369ae6ad2SNogah Frankel if (!attr) 438469ae6ad2SNogah Frankel goto nla_put_failure; 438569ae6ad2SNogah Frankel 438669ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 438769ae6ad2SNogah Frankel if (err == -ENODATA) 438869ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 438969ae6ad2SNogah Frankel else 439069ae6ad2SNogah Frankel nla_nest_end(skb, attr); 439169ae6ad2SNogah Frankel 439269ae6ad2SNogah Frankel if (err && err != -ENODATA) 439369ae6ad2SNogah Frankel goto nla_put_failure; 439469ae6ad2SNogah Frankel *idxattr = 0; 439569ae6ad2SNogah Frankel } 439669ae6ad2SNogah Frankel 4397aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4398aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4399aefb4d4aSRobert Shearman 4400aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4401aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4402aefb4d4aSRobert Shearman if (!attr) 4403aefb4d4aSRobert Shearman goto nla_put_failure; 4404aefb4d4aSRobert Shearman 44055fa85a09SFlorian Westphal rcu_read_lock(); 44065fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4407aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4408aefb4d4aSRobert Shearman struct nlattr *af; 4409aefb4d4aSRobert Shearman int err; 4410aefb4d4aSRobert Shearman 4411aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 44125fa85a09SFlorian Westphal if (!af) { 44135fa85a09SFlorian Westphal rcu_read_unlock(); 4414aefb4d4aSRobert Shearman goto nla_put_failure; 44155fa85a09SFlorian Westphal } 4416aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4417aefb4d4aSRobert Shearman 44185fa85a09SFlorian Westphal if (err == -ENODATA) { 4419aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 44205fa85a09SFlorian Westphal } else if (err < 0) { 44215fa85a09SFlorian Westphal rcu_read_unlock(); 4422aefb4d4aSRobert Shearman goto nla_put_failure; 44235fa85a09SFlorian Westphal } 4424aefb4d4aSRobert Shearman 4425aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4426aefb4d4aSRobert Shearman } 4427aefb4d4aSRobert Shearman } 44285fa85a09SFlorian Westphal rcu_read_unlock(); 4429aefb4d4aSRobert Shearman 4430aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4431aefb4d4aSRobert Shearman 4432aefb4d4aSRobert Shearman *idxattr = 0; 4433aefb4d4aSRobert Shearman } 4434aefb4d4aSRobert Shearman 443510c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 443610c9ead9SRoopa Prabhu 443710c9ead9SRoopa Prabhu return 0; 443810c9ead9SRoopa Prabhu 443910c9ead9SRoopa Prabhu nla_put_failure: 4440e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4441e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 444210c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4443e8872a25SNikolay Aleksandrov else 4444e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 444510c9ead9SRoopa Prabhu 444610c9ead9SRoopa Prabhu return -EMSGSIZE; 444710c9ead9SRoopa Prabhu } 444810c9ead9SRoopa Prabhu 444910c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 445010c9ead9SRoopa Prabhu u32 filter_mask) 445110c9ead9SRoopa Prabhu { 445210c9ead9SRoopa Prabhu size_t size = 0; 445310c9ead9SRoopa Prabhu 4454e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 445510c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 445610c9ead9SRoopa Prabhu 445797a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 445897a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 445980e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 446097a47facSNikolay Aleksandrov 446197a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 446280e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 446380e73cc5SNikolay Aleksandrov attr)); 446497a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 446597a47facSNikolay Aleksandrov size += nla_total_size(0); 446697a47facSNikolay Aleksandrov } 446797a47facSNikolay Aleksandrov } 446897a47facSNikolay Aleksandrov 446980e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 447080e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 447180e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 447280e73cc5SNikolay Aleksandrov const struct net_device *master; 447380e73cc5SNikolay Aleksandrov 447480e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 447580e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 447680e73cc5SNikolay Aleksandrov if (master) 447780e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 447880e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 447980e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 448080e73cc5SNikolay Aleksandrov 448180e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 448280e73cc5SNikolay Aleksandrov attr)); 448380e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 448480e73cc5SNikolay Aleksandrov size += nla_total_size(0); 448580e73cc5SNikolay Aleksandrov } 448680e73cc5SNikolay Aleksandrov } 448780e73cc5SNikolay Aleksandrov 448869ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 448969ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 449069ae6ad2SNogah Frankel 4491aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4492aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4493aefb4d4aSRobert Shearman 4494aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4495aefb4d4aSRobert Shearman size += nla_total_size(0); 4496aefb4d4aSRobert Shearman 44975fa85a09SFlorian Westphal rcu_read_lock(); 44985fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4499aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4500aefb4d4aSRobert Shearman size += nla_total_size( 4501aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4502aefb4d4aSRobert Shearman 4503aefb4d4aSRobert Shearman /* for AF_* */ 4504aefb4d4aSRobert Shearman size += nla_total_size(0); 4505aefb4d4aSRobert Shearman } 4506aefb4d4aSRobert Shearman } 45075fa85a09SFlorian Westphal rcu_read_unlock(); 4508aefb4d4aSRobert Shearman } 4509aefb4d4aSRobert Shearman 451010c9ead9SRoopa Prabhu return size; 451110c9ead9SRoopa Prabhu } 451210c9ead9SRoopa Prabhu 4513c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4514c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 451510c9ead9SRoopa Prabhu { 451610c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 451710c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4518e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4519e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 452010c9ead9SRoopa Prabhu struct sk_buff *nskb; 452110c9ead9SRoopa Prabhu u32 filter_mask; 452210c9ead9SRoopa Prabhu int err; 452310c9ead9SRoopa Prabhu 45244775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 45254775cc1fSMathias Krause return -EINVAL; 45264775cc1fSMathias Krause 452710c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 452810c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 452910c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 453010c9ead9SRoopa Prabhu else 453110c9ead9SRoopa Prabhu return -EINVAL; 453210c9ead9SRoopa Prabhu 453310c9ead9SRoopa Prabhu if (!dev) 453410c9ead9SRoopa Prabhu return -ENODEV; 453510c9ead9SRoopa Prabhu 453610c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 453710c9ead9SRoopa Prabhu if (!filter_mask) 453810c9ead9SRoopa Prabhu return -EINVAL; 453910c9ead9SRoopa Prabhu 454010c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 454110c9ead9SRoopa Prabhu if (!nskb) 454210c9ead9SRoopa Prabhu return -ENOBUFS; 454310c9ead9SRoopa Prabhu 454410c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 454510c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4546e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 454710c9ead9SRoopa Prabhu if (err < 0) { 454810c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 454910c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 455010c9ead9SRoopa Prabhu kfree_skb(nskb); 455110c9ead9SRoopa Prabhu } else { 455210c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 455310c9ead9SRoopa Prabhu } 455410c9ead9SRoopa Prabhu 455510c9ead9SRoopa Prabhu return err; 455610c9ead9SRoopa Prabhu } 455710c9ead9SRoopa Prabhu 455810c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 455910c9ead9SRoopa Prabhu { 4560e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 456110c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 456210c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4563e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4564e8872a25SNikolay Aleksandrov struct hlist_head *head; 4565e8872a25SNikolay Aleksandrov struct net_device *dev; 456610c9ead9SRoopa Prabhu u32 filter_mask = 0; 4567e8872a25SNikolay Aleksandrov int idx = 0; 456810c9ead9SRoopa Prabhu 456910c9ead9SRoopa Prabhu s_h = cb->args[0]; 457010c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4571e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4572e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 457310c9ead9SRoopa Prabhu 457410c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 457510c9ead9SRoopa Prabhu 45764775cc1fSMathias Krause if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 45774775cc1fSMathias Krause return -EINVAL; 45784775cc1fSMathias Krause 457910c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 458010c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 458110c9ead9SRoopa Prabhu if (!filter_mask) 458210c9ead9SRoopa Prabhu return -EINVAL; 458310c9ead9SRoopa Prabhu 458410c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 458510c9ead9SRoopa Prabhu idx = 0; 458610c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 458710c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 458810c9ead9SRoopa Prabhu if (idx < s_idx) 458910c9ead9SRoopa Prabhu goto cont; 459010c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 459110c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 459210c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4593e8872a25SNikolay Aleksandrov flags, filter_mask, 4594e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 459510c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 459610c9ead9SRoopa Prabhu * we're in trouble 459710c9ead9SRoopa Prabhu */ 459810c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 459910c9ead9SRoopa Prabhu 460010c9ead9SRoopa Prabhu if (err < 0) 460110c9ead9SRoopa Prabhu goto out; 4602e8872a25SNikolay Aleksandrov s_prividx = 0; 4603e8872a25SNikolay Aleksandrov s_idxattr = 0; 460410c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 460510c9ead9SRoopa Prabhu cont: 460610c9ead9SRoopa Prabhu idx++; 460710c9ead9SRoopa Prabhu } 460810c9ead9SRoopa Prabhu } 460910c9ead9SRoopa Prabhu out: 4610e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4611e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 461210c9ead9SRoopa Prabhu cb->args[1] = idx; 461310c9ead9SRoopa Prabhu cb->args[0] = h; 461410c9ead9SRoopa Prabhu 461510c9ead9SRoopa Prabhu return skb->len; 461610c9ead9SRoopa Prabhu } 461710c9ead9SRoopa Prabhu 46181da177e4SLinus Torvalds /* Process one rtnetlink message. */ 46191da177e4SLinus Torvalds 46202d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 46212d4bc933SJohannes Berg struct netlink_ext_ack *extack) 46221da177e4SLinus Torvalds { 46233b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4624addf9b90SFlorian Westphal struct rtnl_link *link; 4625e4202511SFlorian Westphal struct module *owner; 46266853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4627e2849863SThomas Graf rtnl_doit_func doit; 462862256f98SFlorian Westphal unsigned int flags; 4629617cfc75SAlexander Kuleshov int kind; 46301da177e4SLinus Torvalds int family; 46311da177e4SLinus Torvalds int type; 46321da177e4SLinus Torvalds 46331da177e4SLinus Torvalds type = nlh->nlmsg_type; 46341da177e4SLinus Torvalds if (type > RTM_MAX) 4635038890feSThomas Graf return -EOPNOTSUPP; 46361da177e4SLinus Torvalds 46371da177e4SLinus Torvalds type -= RTM_BASE; 46381da177e4SLinus Torvalds 46391da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4640573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 46411da177e4SLinus Torvalds return 0; 46421da177e4SLinus Torvalds 4643573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 46441da177e4SLinus Torvalds kind = type&3; 46451da177e4SLinus Torvalds 464690f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 46471d00a4ebSThomas Graf return -EPERM; 46481da177e4SLinus Torvalds 46496853dd48SFlorian Westphal rcu_read_lock(); 4650b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 465197c53cacSDenis V. Lunev struct sock *rtnl; 4652e2849863SThomas Graf rtnl_dumpit_func dumpit; 4653c7ac8679SGreg Rose u16 min_dump_alloc = 0; 46541da177e4SLinus Torvalds 4655addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4656addf9b90SFlorian Westphal if (!link || !link->dumpit) { 46576853dd48SFlorian Westphal family = PF_UNSPEC; 4658addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4659addf9b90SFlorian Westphal if (!link || !link->dumpit) 46606853dd48SFlorian Westphal goto err_unlock; 46616853dd48SFlorian Westphal } 4662e4202511SFlorian Westphal owner = link->owner; 4663addf9b90SFlorian Westphal dumpit = link->dumpit; 46646853dd48SFlorian Westphal 46655c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4666e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 46671da177e4SLinus Torvalds 4668e4202511SFlorian Westphal err = 0; 4669e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4670e4202511SFlorian Westphal if (!try_module_get(owner)) 4671e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4672e4202511SFlorian Westphal 46736853dd48SFlorian Westphal rcu_read_unlock(); 46746853dd48SFlorian Westphal 467597c53cacSDenis V. Lunev rtnl = net->rtnl; 4676e4202511SFlorian Westphal if (err == 0) { 467780d326faSPablo Neira Ayuso struct netlink_dump_control c = { 467880d326faSPablo Neira Ayuso .dump = dumpit, 467980d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4680e4202511SFlorian Westphal .module = owner, 468180d326faSPablo Neira Ayuso }; 468280d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4683e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4684e4202511SFlorian Westphal * module if dump is still in progress. 4685e4202511SFlorian Westphal */ 4686e4202511SFlorian Westphal module_put(owner); 468780d326faSPablo Neira Ayuso } 46882907c35fSEric Dumazet return err; 46891da177e4SLinus Torvalds } 46901da177e4SLinus Torvalds 4691addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4692addf9b90SFlorian Westphal if (!link || !link->doit) { 46938caa38b5SFlorian Westphal family = PF_UNSPEC; 4694addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4695addf9b90SFlorian Westphal if (!link || !link->doit) 4696addf9b90SFlorian Westphal goto out_unlock; 46978caa38b5SFlorian Westphal } 46988caa38b5SFlorian Westphal 4699e4202511SFlorian Westphal owner = link->owner; 4700e4202511SFlorian Westphal if (!try_module_get(owner)) { 4701e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4702e4202511SFlorian Westphal goto out_unlock; 4703e4202511SFlorian Westphal } 4704e4202511SFlorian Westphal 4705addf9b90SFlorian Westphal flags = link->flags; 470662256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4707addf9b90SFlorian Westphal doit = link->doit; 470862256f98SFlorian Westphal rcu_read_unlock(); 470962256f98SFlorian Westphal if (doit) 471062256f98SFlorian Westphal err = doit(skb, nlh, extack); 4711e4202511SFlorian Westphal module_put(owner); 471262256f98SFlorian Westphal return err; 471362256f98SFlorian Westphal } 47146853dd48SFlorian Westphal rcu_read_unlock(); 47156853dd48SFlorian Westphal 47160cc09020SFlorian Westphal rtnl_lock(); 4717addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4718addf9b90SFlorian Westphal if (link && link->doit) 4719addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 47200cc09020SFlorian Westphal rtnl_unlock(); 4721addf9b90SFlorian Westphal 4722e4202511SFlorian Westphal module_put(owner); 4723e4202511SFlorian Westphal 4724addf9b90SFlorian Westphal return err; 4725addf9b90SFlorian Westphal 4726addf9b90SFlorian Westphal out_unlock: 4727addf9b90SFlorian Westphal rcu_read_unlock(); 47280cc09020SFlorian Westphal return err; 47290cc09020SFlorian Westphal 47300cc09020SFlorian Westphal err_unlock: 47316853dd48SFlorian Westphal rcu_read_unlock(); 47320cc09020SFlorian Westphal return -EOPNOTSUPP; 47331da177e4SLinus Torvalds } 47341da177e4SLinus Torvalds 4735cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 47361da177e4SLinus Torvalds { 4737cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 47381da177e4SLinus Torvalds } 47391da177e4SLinus Torvalds 47405f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 47415f729eaaSJulien Gomes { 47425f729eaaSJulien Gomes switch (group) { 47435f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 47445f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 47455f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 47465f729eaaSJulien Gomes return -EPERM; 47475f729eaaSJulien Gomes break; 47485f729eaaSJulien Gomes } 47495f729eaaSJulien Gomes return 0; 47505f729eaaSJulien Gomes } 47515f729eaaSJulien Gomes 47521da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 47531da177e4SLinus Torvalds { 4754351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4755e9dc8653SEric W. Biederman 47561da177e4SLinus Torvalds switch (event) { 47575138e86fSVlad Yasevich case NETDEV_REBOOT: 47588a212589SXin Long case NETDEV_CHANGEMTU: 47593753654eSDavid Ahern case NETDEV_CHANGEADDR: 47605138e86fSVlad Yasevich case NETDEV_CHANGENAME: 47615138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 47625138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4763e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 47645138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4765dc709f37SXin Long case NETDEV_CHANGEUPPER: 47665138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 47675138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4768eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4769ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 47703d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 477138e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 47721da177e4SLinus Torvalds break; 47731da177e4SLinus Torvalds default: 47741da177e4SLinus Torvalds break; 47751da177e4SLinus Torvalds } 47761da177e4SLinus Torvalds return NOTIFY_DONE; 47771da177e4SLinus Torvalds } 47781da177e4SLinus Torvalds 47791da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 47801da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 47811da177e4SLinus Torvalds }; 47821da177e4SLinus Torvalds 478397c53cacSDenis V. Lunev 47842c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 478597c53cacSDenis V. Lunev { 478697c53cacSDenis V. Lunev struct sock *sk; 4787a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4788a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4789a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4790a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 47919785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 47925f729eaaSJulien Gomes .bind = rtnetlink_bind, 4793a31f2d17SPablo Neira Ayuso }; 4794a31f2d17SPablo Neira Ayuso 47959f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 479697c53cacSDenis V. Lunev if (!sk) 479797c53cacSDenis V. Lunev return -ENOMEM; 479897c53cacSDenis V. Lunev net->rtnl = sk; 479997c53cacSDenis V. Lunev return 0; 480097c53cacSDenis V. Lunev } 480197c53cacSDenis V. Lunev 48022c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 480397c53cacSDenis V. Lunev { 4804b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 480597c53cacSDenis V. Lunev net->rtnl = NULL; 480697c53cacSDenis V. Lunev } 480797c53cacSDenis V. Lunev 480897c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 480997c53cacSDenis V. Lunev .init = rtnetlink_net_init, 481097c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 481197c53cacSDenis V. Lunev }; 481297c53cacSDenis V. Lunev 48131da177e4SLinus Torvalds void __init rtnetlink_init(void) 48141da177e4SLinus Torvalds { 481597c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 48161da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 481797c53cacSDenis V. Lunev 48181da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4819340d17fcSThomas Graf 4820c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4821b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4822b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4823b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4824b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4825687ad8ccSThomas Graf 4826b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4827b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4828b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 482977162022SJohn Fastabend 4830b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4831b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4832b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4833e5a55a89SJohn Fastabend 4834b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4835b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4836b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 483710c9ead9SRoopa Prabhu 483810c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4839b97bac64SFlorian Westphal 0); 48401da177e4SLinus Torvalds } 4841