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 62e0d087afSEric Dumazet struct rtnl_link { 63e2849863SThomas Graf rtnl_doit_func doit; 64e2849863SThomas Graf rtnl_dumpit_func dumpit; 65e4202511SFlorian Westphal struct module *owner; 6662256f98SFlorian Westphal unsigned int flags; 67addf9b90SFlorian Westphal struct rcu_head rcu; 68e2849863SThomas Graf }; 69e2849863SThomas Graf 706756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds void rtnl_lock(void) 731da177e4SLinus Torvalds { 746756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 751da177e4SLinus Torvalds } 76e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 771da177e4SLinus Torvalds 78*79ffdfc6SKirill Tkhai int rtnl_lock_killable(void) 79*79ffdfc6SKirill Tkhai { 80*79ffdfc6SKirill Tkhai return mutex_lock_killable(&rtnl_mutex); 81*79ffdfc6SKirill Tkhai } 82*79ffdfc6SKirill Tkhai EXPORT_SYMBOL(rtnl_lock_killable); 83*79ffdfc6SKirill Tkhai 841b5c5493SEric Dumazet static struct sk_buff *defer_kfree_skb_list; 851b5c5493SEric Dumazet void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 861b5c5493SEric Dumazet { 871b5c5493SEric Dumazet if (head && tail) { 881b5c5493SEric Dumazet tail->next = defer_kfree_skb_list; 891b5c5493SEric Dumazet defer_kfree_skb_list = head; 901b5c5493SEric Dumazet } 911b5c5493SEric Dumazet } 921b5c5493SEric Dumazet EXPORT_SYMBOL(rtnl_kfree_skbs); 931b5c5493SEric Dumazet 946756ae4bSStephen Hemminger void __rtnl_unlock(void) 951da177e4SLinus Torvalds { 961b5c5493SEric Dumazet struct sk_buff *head = defer_kfree_skb_list; 971b5c5493SEric Dumazet 981b5c5493SEric Dumazet defer_kfree_skb_list = NULL; 991b5c5493SEric Dumazet 1006756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 1011b5c5493SEric Dumazet 1021b5c5493SEric Dumazet while (head) { 1031b5c5493SEric Dumazet struct sk_buff *next = head->next; 1041b5c5493SEric Dumazet 1051b5c5493SEric Dumazet kfree_skb(head); 1061b5c5493SEric Dumazet cond_resched(); 1071b5c5493SEric Dumazet head = next; 1081b5c5493SEric Dumazet } 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds void rtnl_unlock(void) 1121da177e4SLinus Torvalds { 11358ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 1141da177e4SLinus Torvalds netdev_run_todo(); 1151da177e4SLinus Torvalds } 116e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 1171da177e4SLinus Torvalds 1186756ae4bSStephen Hemminger int rtnl_trylock(void) 1196756ae4bSStephen Hemminger { 1206756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 1216756ae4bSStephen Hemminger } 122e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 1236756ae4bSStephen Hemminger 124c9c1014bSPatrick McHardy int rtnl_is_locked(void) 125c9c1014bSPatrick McHardy { 126c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 127c9c1014bSPatrick McHardy } 128e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 129c9c1014bSPatrick McHardy 130a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 1310cbf3343SYaowei Bai bool lockdep_rtnl_is_held(void) 132a898def2SPaul E. McKenney { 133a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 134a898def2SPaul E. McKenney } 135a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 136a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 137a898def2SPaul E. McKenney 138b0e9fe1bSFlorian Westphal static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 139e2849863SThomas Graf 140e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 141e2849863SThomas Graf { 142e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 143e2849863SThomas Graf 144e2849863SThomas Graf /* 145e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 146e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 147e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 148e2849863SThomas Graf */ 149e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 150e2849863SThomas Graf 151e2849863SThomas Graf return msgindex; 152e2849863SThomas Graf } 153e2849863SThomas Graf 154addf9b90SFlorian Westphal static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 155addf9b90SFlorian Westphal { 156addf9b90SFlorian Westphal struct rtnl_link **tab; 157addf9b90SFlorian Westphal 158addf9b90SFlorian Westphal if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 159addf9b90SFlorian Westphal protocol = PF_UNSPEC; 160addf9b90SFlorian Westphal 161addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 162addf9b90SFlorian Westphal if (!tab) 163addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 164addf9b90SFlorian Westphal 165addf9b90SFlorian Westphal return tab[msgtype]; 166addf9b90SFlorian Westphal } 167addf9b90SFlorian Westphal 168e4202511SFlorian Westphal static int rtnl_register_internal(struct module *owner, 169e4202511SFlorian Westphal int protocol, int msgtype, 170c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 171b97bac64SFlorian Westphal unsigned int flags) 172e2849863SThomas Graf { 173b0e9fe1bSFlorian Westphal struct rtnl_link *link, *old; 174b0e9fe1bSFlorian Westphal struct rtnl_link __rcu **tab; 175e2849863SThomas Graf int msgindex; 176addf9b90SFlorian Westphal int ret = -ENOBUFS; 177e2849863SThomas Graf 17825239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 179e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 180e2849863SThomas Graf 181addf9b90SFlorian Westphal rtnl_lock(); 182addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 183e2849863SThomas Graf if (tab == NULL) { 184addf9b90SFlorian Westphal tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 185addf9b90SFlorian Westphal if (!tab) 186addf9b90SFlorian Westphal goto unlock; 187e2849863SThomas Graf 188addf9b90SFlorian Westphal /* ensures we see the 0 stores */ 1896853dd48SFlorian Westphal rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 190e2849863SThomas Graf } 191e2849863SThomas Graf 192addf9b90SFlorian Westphal old = rtnl_dereference(tab[msgindex]); 193addf9b90SFlorian Westphal if (old) { 194addf9b90SFlorian Westphal link = kmemdup(old, sizeof(*old), GFP_KERNEL); 195addf9b90SFlorian Westphal if (!link) 196addf9b90SFlorian Westphal goto unlock; 197addf9b90SFlorian Westphal } else { 198addf9b90SFlorian Westphal link = kzalloc(sizeof(*link), GFP_KERNEL); 199addf9b90SFlorian Westphal if (!link) 200addf9b90SFlorian Westphal goto unlock; 201addf9b90SFlorian Westphal } 202e2849863SThomas Graf 203e4202511SFlorian Westphal WARN_ON(link->owner && link->owner != owner); 204e4202511SFlorian Westphal link->owner = owner; 205e4202511SFlorian Westphal 206addf9b90SFlorian Westphal WARN_ON(doit && link->doit && link->doit != doit); 207addf9b90SFlorian Westphal if (doit) 208addf9b90SFlorian Westphal link->doit = doit; 209addf9b90SFlorian Westphal WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 210addf9b90SFlorian Westphal if (dumpit) 211addf9b90SFlorian Westphal link->dumpit = dumpit; 212addf9b90SFlorian Westphal 213addf9b90SFlorian Westphal link->flags |= flags; 214addf9b90SFlorian Westphal 215addf9b90SFlorian Westphal /* publish protocol:msgtype */ 216addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], link); 217addf9b90SFlorian Westphal ret = 0; 218addf9b90SFlorian Westphal if (old) 219addf9b90SFlorian Westphal kfree_rcu(old, rcu); 220addf9b90SFlorian Westphal unlock: 221addf9b90SFlorian Westphal rtnl_unlock(); 222addf9b90SFlorian Westphal return ret; 223e2849863SThomas Graf } 224e4202511SFlorian Westphal 225e4202511SFlorian Westphal /** 226e4202511SFlorian Westphal * rtnl_register_module - Register a rtnetlink message type 227e4202511SFlorian Westphal * 228e4202511SFlorian Westphal * @owner: module registering the hook (THIS_MODULE) 229e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 230e4202511SFlorian Westphal * @msgtype: rtnetlink message type 231e4202511SFlorian Westphal * @doit: Function pointer called for each request message 232e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 233e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 234e4202511SFlorian Westphal * 235e4202511SFlorian Westphal * Like rtnl_register, but for use by removable modules. 236e4202511SFlorian Westphal */ 237e4202511SFlorian Westphal int rtnl_register_module(struct module *owner, 238e4202511SFlorian Westphal int protocol, int msgtype, 239e4202511SFlorian Westphal rtnl_doit_func doit, rtnl_dumpit_func dumpit, 240e4202511SFlorian Westphal unsigned int flags) 241e4202511SFlorian Westphal { 242e4202511SFlorian Westphal return rtnl_register_internal(owner, protocol, msgtype, 243e4202511SFlorian Westphal doit, dumpit, flags); 244e4202511SFlorian Westphal } 245e4202511SFlorian Westphal EXPORT_SYMBOL_GPL(rtnl_register_module); 246e4202511SFlorian Westphal 247e4202511SFlorian Westphal /** 24816feebcfSFlorian Westphal * rtnl_register - Register a rtnetlink message type 249e4202511SFlorian Westphal * @protocol: Protocol family or PF_UNSPEC 250e4202511SFlorian Westphal * @msgtype: rtnetlink message type 251e4202511SFlorian Westphal * @doit: Function pointer called for each request message 252e4202511SFlorian Westphal * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 253e4202511SFlorian Westphal * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions 254e4202511SFlorian Westphal * 255e4202511SFlorian Westphal * Registers the specified function pointers (at least one of them has 256e4202511SFlorian Westphal * to be non-NULL) to be called whenever a request message for the 257e4202511SFlorian Westphal * specified protocol family and message type is received. 258e4202511SFlorian Westphal * 259e4202511SFlorian Westphal * The special protocol family PF_UNSPEC may be used to define fallback 260e4202511SFlorian Westphal * function pointers for the case when no entry for the specific protocol 261e4202511SFlorian Westphal * family exists. 262e2849863SThomas Graf */ 263e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 264c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 265b97bac64SFlorian Westphal unsigned int flags) 266e2849863SThomas Graf { 26716feebcfSFlorian Westphal int err; 26816feebcfSFlorian Westphal 26916feebcfSFlorian Westphal err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit, 27016feebcfSFlorian Westphal flags); 27116feebcfSFlorian Westphal if (err) 27216feebcfSFlorian Westphal pr_err("Unable to register rtnetlink message handler, " 27316feebcfSFlorian Westphal "protocol = %d, message type = %d\n", protocol, msgtype); 274e2849863SThomas Graf } 275e2849863SThomas Graf 276e2849863SThomas Graf /** 277e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 278e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 279e2849863SThomas Graf * @msgtype: rtnetlink message type 280e2849863SThomas Graf * 281e2849863SThomas Graf * Returns 0 on success or a negative error code. 282e2849863SThomas Graf */ 283e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 284e2849863SThomas Graf { 285addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 286e2849863SThomas Graf int msgindex; 287e2849863SThomas Graf 28825239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 289e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 290e2849863SThomas Graf 2916853dd48SFlorian Westphal rtnl_lock(); 292addf9b90SFlorian Westphal tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 293addf9b90SFlorian Westphal if (!tab) { 2946853dd48SFlorian Westphal rtnl_unlock(); 295e2849863SThomas Graf return -ENOENT; 2966853dd48SFlorian Westphal } 297e2849863SThomas Graf 298addf9b90SFlorian Westphal link = tab[msgindex]; 299addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 3006853dd48SFlorian Westphal rtnl_unlock(); 301e2849863SThomas Graf 302addf9b90SFlorian Westphal kfree_rcu(link, rcu); 303addf9b90SFlorian Westphal 304e2849863SThomas Graf return 0; 305e2849863SThomas Graf } 306e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 307e2849863SThomas Graf 308e2849863SThomas Graf /** 309e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 310e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 311e2849863SThomas Graf * 312e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 313e2849863SThomas Graf * of a certain protocol family. 314e2849863SThomas Graf */ 315e2849863SThomas Graf void rtnl_unregister_all(int protocol) 316e2849863SThomas Graf { 317addf9b90SFlorian Westphal struct rtnl_link **tab, *link; 318addf9b90SFlorian Westphal int msgindex; 319019a3169SFlorian Westphal 32025239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 321e2849863SThomas Graf 322019a3169SFlorian Westphal rtnl_lock(); 323addf9b90SFlorian Westphal tab = rtnl_msg_handlers[protocol]; 3246853dd48SFlorian Westphal RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL); 325addf9b90SFlorian Westphal for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 326addf9b90SFlorian Westphal link = tab[msgindex]; 327addf9b90SFlorian Westphal if (!link) 328addf9b90SFlorian Westphal continue; 329addf9b90SFlorian Westphal 330addf9b90SFlorian Westphal rcu_assign_pointer(tab[msgindex], NULL); 331addf9b90SFlorian Westphal kfree_rcu(link, rcu); 332addf9b90SFlorian Westphal } 333019a3169SFlorian Westphal rtnl_unlock(); 334019a3169SFlorian Westphal 3356853dd48SFlorian Westphal synchronize_net(); 3366853dd48SFlorian Westphal 337addf9b90SFlorian Westphal kfree(tab); 338e2849863SThomas Graf } 339e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 3401da177e4SLinus Torvalds 34138f7b870SPatrick McHardy static LIST_HEAD(link_ops); 34238f7b870SPatrick McHardy 343c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 344c63044f0SEric Dumazet { 345c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 346c63044f0SEric Dumazet 347c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 348c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 349c63044f0SEric Dumazet return ops; 350c63044f0SEric Dumazet } 351c63044f0SEric Dumazet return NULL; 352c63044f0SEric Dumazet } 353c63044f0SEric Dumazet 35438f7b870SPatrick McHardy /** 35538f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 35638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 35738f7b870SPatrick McHardy * 35838f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 35938f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 36038f7b870SPatrick McHardy * must be called before registering the devices. 36138f7b870SPatrick McHardy * 36238f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 36338f7b870SPatrick McHardy */ 36438f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 36538f7b870SPatrick McHardy { 366c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 367c63044f0SEric Dumazet return -EEXIST; 368c63044f0SEric Dumazet 369b0ab2fabSJiri Pirko /* The check for setup is here because if ops 370b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 371b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 372b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 373b0ab2fabSJiri Pirko */ 374b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 37523289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3762d85cba2SPatrick McHardy 37738f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 37838f7b870SPatrick McHardy return 0; 37938f7b870SPatrick McHardy } 38038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 38138f7b870SPatrick McHardy 38238f7b870SPatrick McHardy /** 38338f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 38438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 38538f7b870SPatrick McHardy * 38638f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 38738f7b870SPatrick McHardy */ 38838f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 38938f7b870SPatrick McHardy { 39038f7b870SPatrick McHardy int err; 39138f7b870SPatrick McHardy 39238f7b870SPatrick McHardy rtnl_lock(); 39338f7b870SPatrick McHardy err = __rtnl_link_register(ops); 39438f7b870SPatrick McHardy rtnl_unlock(); 39538f7b870SPatrick McHardy return err; 39638f7b870SPatrick McHardy } 39738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 39838f7b870SPatrick McHardy 399669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 400669f87baSPavel Emelyanov { 401669f87baSPavel Emelyanov struct net_device *dev; 40223289a37SEric Dumazet LIST_HEAD(list_kill); 40323289a37SEric Dumazet 404669f87baSPavel Emelyanov for_each_netdev(net, dev) { 40523289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 40623289a37SEric Dumazet ops->dellink(dev, &list_kill); 407669f87baSPavel Emelyanov } 40823289a37SEric Dumazet unregister_netdevice_many(&list_kill); 409669f87baSPavel Emelyanov } 410669f87baSPavel Emelyanov 41138f7b870SPatrick McHardy /** 41238f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 41338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 41438f7b870SPatrick McHardy * 4152d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 41638f7b870SPatrick McHardy */ 41738f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 41838f7b870SPatrick McHardy { 419881d966bSEric W. Biederman struct net *net; 4202d85cba2SPatrick McHardy 421881d966bSEric W. Biederman for_each_net(net) { 422669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 423881d966bSEric W. Biederman } 42438f7b870SPatrick McHardy list_del(&ops->list); 42538f7b870SPatrick McHardy } 42638f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 42738f7b870SPatrick McHardy 428200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 429200b916fSCong Wang * devices unregistering in any network namespace. 430200b916fSCong Wang */ 431200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 432200b916fSCong Wang { 433200b916fSCong Wang struct net *net; 434200b916fSCong Wang bool unregistering; 435ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 436200b916fSCong Wang 437ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 438200b916fSCong Wang for (;;) { 439200b916fSCong Wang unregistering = false; 440200b916fSCong Wang rtnl_lock(); 441200b916fSCong Wang for_each_net(net) { 442200b916fSCong Wang if (net->dev_unreg_count > 0) { 443200b916fSCong Wang unregistering = true; 444200b916fSCong Wang break; 445200b916fSCong Wang } 446200b916fSCong Wang } 447200b916fSCong Wang if (!unregistering) 448200b916fSCong Wang break; 449200b916fSCong Wang __rtnl_unlock(); 450ff960a73SPeter Zijlstra 451ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 452200b916fSCong Wang } 453ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 454200b916fSCong Wang } 455200b916fSCong Wang 45638f7b870SPatrick McHardy /** 45738f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 45838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 45938f7b870SPatrick McHardy */ 46038f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 46138f7b870SPatrick McHardy { 462200b916fSCong Wang /* Close the race with cleanup_net() */ 4631a57feb8SKirill Tkhai down_write(&net_sem); 464200b916fSCong Wang rtnl_lock_unregistering_all(); 46538f7b870SPatrick McHardy __rtnl_link_unregister(ops); 46638f7b870SPatrick McHardy rtnl_unlock(); 4671a57feb8SKirill Tkhai up_write(&net_sem); 46838f7b870SPatrick McHardy } 46938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 47038f7b870SPatrick McHardy 471ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 472ba7d49b1SJiri Pirko { 473ba7d49b1SJiri Pirko struct net_device *master_dev; 474ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 4758515ae38SFlorian Westphal size_t size = 0; 476ba7d49b1SJiri Pirko 4778515ae38SFlorian Westphal rcu_read_lock(); 4788515ae38SFlorian Westphal 4798515ae38SFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 480ba7d49b1SJiri Pirko if (!master_dev) 4818515ae38SFlorian Westphal goto out; 4828515ae38SFlorian Westphal 483ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 4846049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 4858515ae38SFlorian Westphal goto out; 486ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 4878515ae38SFlorian Westphal size = nla_total_size(sizeof(struct nlattr)) + 488ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 4898515ae38SFlorian Westphal 4908515ae38SFlorian Westphal out: 4918515ae38SFlorian Westphal rcu_read_unlock(); 4928515ae38SFlorian Westphal return size; 493ba7d49b1SJiri Pirko } 494ba7d49b1SJiri Pirko 49538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 49638f7b870SPatrick McHardy { 49738f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 49838f7b870SPatrick McHardy size_t size; 49938f7b870SPatrick McHardy 50038f7b870SPatrick McHardy if (!ops) 50138f7b870SPatrick McHardy return 0; 50238f7b870SPatrick McHardy 503369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 504369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 50538f7b870SPatrick McHardy 50638f7b870SPatrick McHardy if (ops->get_size) 50738f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 508369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 50938f7b870SPatrick McHardy ops->get_size(dev); 51038f7b870SPatrick McHardy 51138f7b870SPatrick McHardy if (ops->get_xstats_size) 512369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 513369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 51438f7b870SPatrick McHardy 515ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 516ba7d49b1SJiri Pirko 51738f7b870SPatrick McHardy return size; 51838f7b870SPatrick McHardy } 51938f7b870SPatrick McHardy 520f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 521f8ff182cSThomas Graf 522f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 523f8ff182cSThomas Graf { 524f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 525f8ff182cSThomas Graf 5265fa85a09SFlorian Westphal list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 527f8ff182cSThomas Graf if (ops->family == family) 528f8ff182cSThomas Graf return ops; 529f8ff182cSThomas Graf } 530f8ff182cSThomas Graf 531f8ff182cSThomas Graf return NULL; 532f8ff182cSThomas Graf } 533f8ff182cSThomas Graf 534f8ff182cSThomas Graf /** 535f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 536f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 537f8ff182cSThomas Graf * 538f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 539f8ff182cSThomas Graf */ 5403678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 541f8ff182cSThomas Graf { 542f8ff182cSThomas Graf rtnl_lock(); 5435fa85a09SFlorian Westphal list_add_tail_rcu(&ops->list, &rtnl_af_ops); 544f8ff182cSThomas Graf rtnl_unlock(); 545f8ff182cSThomas Graf } 546f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 547f8ff182cSThomas Graf 548f8ff182cSThomas Graf /** 549f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 550f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 551f8ff182cSThomas Graf */ 552f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 553f8ff182cSThomas Graf { 554f8ff182cSThomas Graf rtnl_lock(); 5555fa85a09SFlorian Westphal list_del_rcu(&ops->list); 556f8ff182cSThomas Graf rtnl_unlock(); 5575fa85a09SFlorian Westphal 5585fa85a09SFlorian Westphal synchronize_rcu(); 559f8ff182cSThomas Graf } 560f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 561f8ff182cSThomas Graf 562b1974ed0SArad, Ronen static size_t rtnl_link_get_af_size(const struct net_device *dev, 563b1974ed0SArad, Ronen u32 ext_filter_mask) 564f8ff182cSThomas Graf { 565f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 566f8ff182cSThomas Graf size_t size; 567f8ff182cSThomas Graf 568f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 569f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 570f8ff182cSThomas Graf 5715fa85a09SFlorian Westphal rcu_read_lock(); 5725fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 573f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 574f8ff182cSThomas Graf /* AF_* + nested data */ 575f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 576b1974ed0SArad, Ronen af_ops->get_link_af_size(dev, ext_filter_mask); 577f8ff182cSThomas Graf } 578f8ff182cSThomas Graf } 5795fa85a09SFlorian Westphal rcu_read_unlock(); 580f8ff182cSThomas Graf 581f8ff182cSThomas Graf return size; 582f8ff182cSThomas Graf } 583f8ff182cSThomas Graf 584ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 585ba7d49b1SJiri Pirko { 586ba7d49b1SJiri Pirko struct net_device *master_dev; 5874c82a95eSFlorian Westphal bool ret = false; 588ba7d49b1SJiri Pirko 5894c82a95eSFlorian Westphal rcu_read_lock(); 5904c82a95eSFlorian Westphal 5914c82a95eSFlorian Westphal master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 592813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 5934c82a95eSFlorian Westphal ret = true; 5944c82a95eSFlorian Westphal rcu_read_unlock(); 5954c82a95eSFlorian Westphal return ret; 596ba7d49b1SJiri Pirko } 597ba7d49b1SJiri Pirko 598ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 599ba7d49b1SJiri Pirko const struct net_device *dev) 600ba7d49b1SJiri Pirko { 601ba7d49b1SJiri Pirko struct net_device *master_dev; 602ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 603ba7d49b1SJiri Pirko struct nlattr *slave_data; 604ba7d49b1SJiri Pirko int err; 605ba7d49b1SJiri Pirko 606ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 607ba7d49b1SJiri Pirko if (!master_dev) 608ba7d49b1SJiri Pirko return 0; 609ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 610ba7d49b1SJiri Pirko if (!ops) 611ba7d49b1SJiri Pirko return 0; 612ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 613ba7d49b1SJiri Pirko return -EMSGSIZE; 614ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 615ba7d49b1SJiri Pirko slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 616ba7d49b1SJiri Pirko if (!slave_data) 617ba7d49b1SJiri Pirko return -EMSGSIZE; 618ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 619ba7d49b1SJiri Pirko if (err < 0) 620ba7d49b1SJiri Pirko goto err_cancel_slave_data; 621ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 622ba7d49b1SJiri Pirko } 623ba7d49b1SJiri Pirko return 0; 624ba7d49b1SJiri Pirko 625ba7d49b1SJiri Pirko err_cancel_slave_data: 626ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 627ba7d49b1SJiri Pirko return err; 628ba7d49b1SJiri Pirko } 629ba7d49b1SJiri Pirko 630ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 631ba7d49b1SJiri Pirko const struct net_device *dev) 63238f7b870SPatrick McHardy { 63338f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 634ba7d49b1SJiri Pirko struct nlattr *data; 635ba7d49b1SJiri Pirko int err; 636ba7d49b1SJiri Pirko 637ba7d49b1SJiri Pirko if (!ops) 638ba7d49b1SJiri Pirko return 0; 639ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 640ba7d49b1SJiri Pirko return -EMSGSIZE; 641ba7d49b1SJiri Pirko if (ops->fill_xstats) { 642ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 643ba7d49b1SJiri Pirko if (err < 0) 644ba7d49b1SJiri Pirko return err; 645ba7d49b1SJiri Pirko } 646ba7d49b1SJiri Pirko if (ops->fill_info) { 647ba7d49b1SJiri Pirko data = nla_nest_start(skb, IFLA_INFO_DATA); 648ba7d49b1SJiri Pirko if (data == NULL) 649ba7d49b1SJiri Pirko return -EMSGSIZE; 650ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 651ba7d49b1SJiri Pirko if (err < 0) 652ba7d49b1SJiri Pirko goto err_cancel_data; 653ba7d49b1SJiri Pirko nla_nest_end(skb, data); 654ba7d49b1SJiri Pirko } 655ba7d49b1SJiri Pirko return 0; 656ba7d49b1SJiri Pirko 657ba7d49b1SJiri Pirko err_cancel_data: 658ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 659ba7d49b1SJiri Pirko return err; 660ba7d49b1SJiri Pirko } 661ba7d49b1SJiri Pirko 662ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 663ba7d49b1SJiri Pirko { 664ba7d49b1SJiri Pirko struct nlattr *linkinfo; 66538f7b870SPatrick McHardy int err = -EMSGSIZE; 66638f7b870SPatrick McHardy 66738f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 66838f7b870SPatrick McHardy if (linkinfo == NULL) 66938f7b870SPatrick McHardy goto out; 67038f7b870SPatrick McHardy 671ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 67238f7b870SPatrick McHardy if (err < 0) 67338f7b870SPatrick McHardy goto err_cancel_link; 674ba7d49b1SJiri Pirko 675ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 67638f7b870SPatrick McHardy if (err < 0) 677ba7d49b1SJiri Pirko goto err_cancel_link; 67838f7b870SPatrick McHardy 67938f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 68038f7b870SPatrick McHardy return 0; 68138f7b870SPatrick McHardy 68238f7b870SPatrick McHardy err_cancel_link: 68338f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 68438f7b870SPatrick McHardy out: 68538f7b870SPatrick McHardy return err; 68638f7b870SPatrick McHardy } 68738f7b870SPatrick McHardy 68895c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 6891da177e4SLinus Torvalds { 69097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 6911da177e4SLinus Torvalds int err = 0; 6921da177e4SLinus Torvalds 693ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 6941da177e4SLinus Torvalds if (echo) 69563354797SReshetova, Elena refcount_inc(&skb->users); 6961da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 6971da177e4SLinus Torvalds if (echo) 6981da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 6991da177e4SLinus Torvalds return err; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 70297c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 7032942e900SThomas Graf { 70497c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 70597c53cacSDenis V. Lunev 7062942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 7072942e900SThomas Graf } 708e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 7092942e900SThomas Graf 7101ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 71197676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 71297676b6bSThomas Graf { 71397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 71497676b6bSThomas Graf int report = 0; 71597676b6bSThomas Graf 71697676b6bSThomas Graf if (nlh) 71797676b6bSThomas Graf report = nlmsg_report(nlh); 71897676b6bSThomas Graf 7191ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 72097676b6bSThomas Graf } 721e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 72297676b6bSThomas Graf 72397c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 72497676b6bSThomas Graf { 72597c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 72697c53cacSDenis V. Lunev 72797676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 72897676b6bSThomas Graf } 729e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 73097676b6bSThomas Graf 7311da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 7321da177e4SLinus Torvalds { 7332d7202bfSThomas Graf struct nlattr *mx; 7342d7202bfSThomas Graf int i, valid = 0; 7351da177e4SLinus Torvalds 7362d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 7372d7202bfSThomas Graf if (mx == NULL) 7382d7202bfSThomas Graf return -ENOBUFS; 7392d7202bfSThomas Graf 7401da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 7412d7202bfSThomas Graf if (metrics[i]) { 742ea697639SDaniel Borkmann if (i == RTAX_CC_ALGO - 1) { 743ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX], *name; 744ea697639SDaniel Borkmann 745ea697639SDaniel Borkmann name = tcp_ca_get_name_by_key(metrics[i], tmp); 746ea697639SDaniel Borkmann if (!name) 747ea697639SDaniel Borkmann continue; 748ea697639SDaniel Borkmann if (nla_put_string(skb, i + 1, name)) 749ea697639SDaniel Borkmann goto nla_put_failure; 750c3a8d947SDaniel Borkmann } else if (i == RTAX_FEATURES - 1) { 751c3a8d947SDaniel Borkmann u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 752c3a8d947SDaniel Borkmann 753f8edcd12SPhil Sutter if (!user_features) 754f8edcd12SPhil Sutter continue; 755c3a8d947SDaniel Borkmann BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 756c3a8d947SDaniel Borkmann if (nla_put_u32(skb, i + 1, user_features)) 757c3a8d947SDaniel Borkmann goto nla_put_failure; 758ea697639SDaniel Borkmann } else { 759a6574349SDavid S. Miller if (nla_put_u32(skb, i + 1, metrics[i])) 760a6574349SDavid S. Miller goto nla_put_failure; 7611da177e4SLinus Torvalds } 762ea697639SDaniel Borkmann valid++; 763ea697639SDaniel Borkmann } 7642d7202bfSThomas Graf } 7651da177e4SLinus Torvalds 766a57d27fcSDavid S. Miller if (!valid) { 767a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 768a57d27fcSDavid S. Miller return 0; 769a57d27fcSDavid S. Miller } 7702d7202bfSThomas Graf 7712d7202bfSThomas Graf return nla_nest_end(skb, mx); 7722d7202bfSThomas Graf 7732d7202bfSThomas Graf nla_put_failure: 774bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 775bc3ed28cSThomas Graf return -EMSGSIZE; 7761da177e4SLinus Torvalds } 777e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 7781da177e4SLinus Torvalds 779e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 78087a50699SDavid S. Miller long expires, u32 error) 781e3703b3dSThomas Graf { 782e3703b3dSThomas Graf struct rta_cacheinfo ci = { 783a399a805SEric Dumazet .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), 784e3703b3dSThomas Graf .rta_used = dst->__use, 785e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 786e3703b3dSThomas Graf .rta_error = error, 787e3703b3dSThomas Graf .rta_id = id, 788e3703b3dSThomas Graf }; 789e3703b3dSThomas Graf 7908253947eSLi Wei if (expires) { 7918253947eSLi Wei unsigned long clock; 792e3703b3dSThomas Graf 7938253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 7948253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 7958253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 7968253947eSLi Wei } 797e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 798e3703b3dSThomas Graf } 799e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 8001da177e4SLinus Torvalds 80193b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 802b00055aaSStefan Rompf { 803b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 804b00055aaSStefan Rompf 805b00055aaSStefan Rompf switch (transition) { 806b00055aaSStefan Rompf case IF_OPER_UP: 807b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 808b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 809b00055aaSStefan Rompf !netif_dormant(dev)) 810b00055aaSStefan Rompf operstate = IF_OPER_UP; 811b00055aaSStefan Rompf break; 812b00055aaSStefan Rompf 813b00055aaSStefan Rompf case IF_OPER_DORMANT: 814b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 815b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 816b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 817b00055aaSStefan Rompf break; 8183ff50b79SStephen Hemminger } 819b00055aaSStefan Rompf 820b00055aaSStefan Rompf if (dev->operstate != operstate) { 821b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 822b00055aaSStefan Rompf dev->operstate = operstate; 823b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 824b00055aaSStefan Rompf netdev_state_change(dev); 82593b2d4a2SDavid S. Miller } 826b00055aaSStefan Rompf } 827b00055aaSStefan Rompf 828b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 829b1beb681SJiri Benc { 830b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 831b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 832b1beb681SJiri Benc } 833b1beb681SJiri Benc 8343729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 8353729d502SPatrick McHardy const struct ifinfomsg *ifm) 8363729d502SPatrick McHardy { 8373729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 8383729d502SPatrick McHardy 8393729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8403729d502SPatrick McHardy if (ifm->ifi_change) 8413729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 842b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 8433729d502SPatrick McHardy 8443729d502SPatrick McHardy return flags; 8453729d502SPatrick McHardy } 8463729d502SPatrick McHardy 847b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 848be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 8491da177e4SLinus Torvalds { 850b60c5115SThomas Graf a->rx_packets = b->rx_packets; 851b60c5115SThomas Graf a->tx_packets = b->tx_packets; 852b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 853b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 854b60c5115SThomas Graf a->rx_errors = b->rx_errors; 855b60c5115SThomas Graf a->tx_errors = b->tx_errors; 856b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 857b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 858b60c5115SThomas Graf 859b60c5115SThomas Graf a->multicast = b->multicast; 860b60c5115SThomas Graf a->collisions = b->collisions; 861b60c5115SThomas Graf 862b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 863b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 864b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 865b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 866b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 867b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 868b60c5115SThomas Graf 869b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 870b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 871b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 872b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 873b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 874b60c5115SThomas Graf 875b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 876b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 8776e7333d3SJarod Wilson 8786e7333d3SJarod Wilson a->rx_nohandler = b->rx_nohandler; 87910708f37SJan Engelhardt } 88010708f37SJan Engelhardt 881c02db8c6SChris Wright /* All VF info */ 882115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 883115c9b81SGreg Rose u32 ext_filter_mask) 884ebc08a6fSWilliams, Mitch A { 8859af15c38SPhil Sutter if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 886c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 8877e75f74aSSabrina Dubroca size_t size = nla_total_size(0); 888045de01aSScott Feldman size += num_vfs * 8897e75f74aSSabrina Dubroca (nla_total_size(0) + 8907e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_mac)) + 8917e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_vlan)) + 8927e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 89379aab093SMoshe Shemesh nla_total_size(MAX_VLAN_LIST_LEN * 89479aab093SMoshe Shemesh sizeof(struct ifla_vf_vlan_info)) + 895ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 8967e75f74aSSabrina Dubroca nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 897945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 89801a3d796SVlad Zolotarov nla_total_size(sizeof(struct ifla_vf_link_state)) + 8993b766cd8SEran Ben Elisha nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 9007e75f74aSSabrina Dubroca nla_total_size(0) + /* nest IFLA_VF_STATS */ 9013b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_PACKETS */ 902343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9033b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_PACKETS */ 904343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9053b766cd8SEran Ben Elisha /* IFLA_VF_STATS_RX_BYTES */ 906343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9073b766cd8SEran Ben Elisha /* IFLA_VF_STATS_TX_BYTES */ 908343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9093b766cd8SEran Ben Elisha /* IFLA_VF_STATS_BROADCAST */ 910343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 9113b766cd8SEran Ben Elisha /* IFLA_VF_STATS_MULTICAST */ 912343a6d8eSNicolas Dichtel nla_total_size_64bit(sizeof(__u64)) + 913c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_RX_DROPPED */ 914c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 915c5a9f6f0SEugenia Emantayev /* IFLA_VF_STATS_TX_DROPPED */ 916c5a9f6f0SEugenia Emantayev nla_total_size_64bit(sizeof(__u64)) + 917dd461d6aSHiroshi Shimamoto nla_total_size(sizeof(struct ifla_vf_trust))); 918c02db8c6SChris Wright return size; 919c02db8c6SChris Wright } else 920ebc08a6fSWilliams, Mitch A return 0; 921ebc08a6fSWilliams, Mitch A } 922ebc08a6fSWilliams, Mitch A 923c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 924c53864fdSDavid Gibson u32 ext_filter_mask) 92557b61080SScott Feldman { 92657b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 92757b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 92857b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 92957b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 93057b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 93157b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 93257b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 93357b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 93457b61080SScott Feldman + port_size; 93557b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 93657b61080SScott Feldman + port_size; 93757b61080SScott Feldman 938c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 939c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 94057b61080SScott Feldman return 0; 94157b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 94257b61080SScott Feldman return port_self_size + vf_ports_size + 94357b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 94457b61080SScott Feldman else 94557b61080SScott Feldman return port_self_size; 94657b61080SScott Feldman } 94757b61080SScott Feldman 948b5cdae32SDavid S. Miller static size_t rtnl_xdp_size(void) 949d1fdd913SBrenden Blanco { 950b3cfaa31SSabrina Dubroca size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 95158038695SMartin KaFai Lau nla_total_size(1) + /* XDP_ATTACHED */ 95258038695SMartin KaFai Lau nla_total_size(4); /* XDP_PROG_ID */ 953d1fdd913SBrenden Blanco 954d1fdd913SBrenden Blanco return xdp_size; 955d1fdd913SBrenden Blanco } 956d1fdd913SBrenden Blanco 957115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 958115c9b81SGreg Rose u32 ext_filter_mask) 959339bf98fSThomas Graf { 960339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 961339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 9620b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 963339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 964270cb4d0SNicolas Dichtel + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 965339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 96635c58459SDavid S. Miller + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 967339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 968339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 969339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 970339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 971339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 972339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 973339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 9749a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 975edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 97676ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 97776ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 9786919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 9796919756cSTobias Klauser + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 980339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 98138f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 9822d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 983d37512a2SNicolas Dichtel + nla_total_size(4) /* IFLA_LINK_NETNSID */ 984db833d40SSerhey Popovych + nla_total_size(4) /* IFLA_GROUP */ 985115c9b81SGreg Rose + nla_total_size(ext_filter_mask 986115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 987115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 988c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 989f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 990b1974ed0SArad, Ronen + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 99182f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 99288d6378bSAnuradha Karuppiah + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 993c57c7a95SNicolas Dichtel + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 994b5cdae32SDavid S. Miller + rtnl_xdp_size() /* IFLA_XDP */ 9953d3ea5afSVlad Yasevich + nla_total_size(4) /* IFLA_EVENT */ 9966621dd29SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_NETNSID */ 99738e01b30SNicolas Dichtel + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 99803ac738dSColin Ian King + nla_total_size(1) /* IFLA_PROTO_DOWN */ 99979e1ad14SJiri Benc + nla_total_size(4) /* IFLA_IF_NETNSID */ 1000b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1001b2d3bcfaSDavid Decotigny + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 100279e1ad14SJiri Benc + 0; 1003339bf98fSThomas Graf } 1004339bf98fSThomas Graf 100557b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 100657b61080SScott Feldman { 100757b61080SScott Feldman struct nlattr *vf_ports; 100857b61080SScott Feldman struct nlattr *vf_port; 100957b61080SScott Feldman int vf; 101057b61080SScott Feldman int err; 101157b61080SScott Feldman 101257b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 101357b61080SScott Feldman if (!vf_ports) 101457b61080SScott Feldman return -EMSGSIZE; 101557b61080SScott Feldman 101657b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 101757b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 10188ca94183SScott Feldman if (!vf_port) 10198ca94183SScott Feldman goto nla_put_failure; 1020a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1021a6574349SDavid S. Miller goto nla_put_failure; 102257b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 10238ca94183SScott Feldman if (err == -EMSGSIZE) 10248ca94183SScott Feldman goto nla_put_failure; 102557b61080SScott Feldman if (err) { 102657b61080SScott Feldman nla_nest_cancel(skb, vf_port); 102757b61080SScott Feldman continue; 102857b61080SScott Feldman } 102957b61080SScott Feldman nla_nest_end(skb, vf_port); 103057b61080SScott Feldman } 103157b61080SScott Feldman 103257b61080SScott Feldman nla_nest_end(skb, vf_ports); 103357b61080SScott Feldman 103457b61080SScott Feldman return 0; 10358ca94183SScott Feldman 10368ca94183SScott Feldman nla_put_failure: 10378ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 10388ca94183SScott Feldman return -EMSGSIZE; 103957b61080SScott Feldman } 104057b61080SScott Feldman 104157b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 104257b61080SScott Feldman { 104357b61080SScott Feldman struct nlattr *port_self; 104457b61080SScott Feldman int err; 104557b61080SScott Feldman 104657b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 104757b61080SScott Feldman if (!port_self) 104857b61080SScott Feldman return -EMSGSIZE; 104957b61080SScott Feldman 105057b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 105157b61080SScott Feldman if (err) { 105257b61080SScott Feldman nla_nest_cancel(skb, port_self); 10538ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 105457b61080SScott Feldman } 105557b61080SScott Feldman 105657b61080SScott Feldman nla_nest_end(skb, port_self); 105757b61080SScott Feldman 105857b61080SScott Feldman return 0; 105957b61080SScott Feldman } 106057b61080SScott Feldman 1061c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1062c53864fdSDavid Gibson u32 ext_filter_mask) 106357b61080SScott Feldman { 106457b61080SScott Feldman int err; 106557b61080SScott Feldman 1066c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1067c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 106857b61080SScott Feldman return 0; 106957b61080SScott Feldman 107057b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 107157b61080SScott Feldman if (err) 107257b61080SScott Feldman return err; 107357b61080SScott Feldman 107457b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 107557b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 107657b61080SScott Feldman if (err) 107757b61080SScott Feldman return err; 107857b61080SScott Feldman } 107957b61080SScott Feldman 108057b61080SScott Feldman return 0; 108157b61080SScott Feldman } 108257b61080SScott Feldman 108366cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 108466cae9edSJiri Pirko { 108566cae9edSJiri Pirko int err; 108602637fceSJiri Pirko struct netdev_phys_item_id ppid; 108766cae9edSJiri Pirko 108866cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 108966cae9edSJiri Pirko if (err) { 109066cae9edSJiri Pirko if (err == -EOPNOTSUPP) 109166cae9edSJiri Pirko return 0; 109266cae9edSJiri Pirko return err; 109366cae9edSJiri Pirko } 109466cae9edSJiri Pirko 109566cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 109666cae9edSJiri Pirko return -EMSGSIZE; 109766cae9edSJiri Pirko 109866cae9edSJiri Pirko return 0; 109966cae9edSJiri Pirko } 110066cae9edSJiri Pirko 1101db24a904SDavid Ahern static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1102db24a904SDavid Ahern { 1103db24a904SDavid Ahern char name[IFNAMSIZ]; 1104db24a904SDavid Ahern int err; 1105db24a904SDavid Ahern 1106db24a904SDavid Ahern err = dev_get_phys_port_name(dev, name, sizeof(name)); 1107db24a904SDavid Ahern if (err) { 1108db24a904SDavid Ahern if (err == -EOPNOTSUPP) 1109db24a904SDavid Ahern return 0; 1110db24a904SDavid Ahern return err; 1111db24a904SDavid Ahern } 1112db24a904SDavid Ahern 111377ef033bSMichal Schmidt if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1114db24a904SDavid Ahern return -EMSGSIZE; 1115db24a904SDavid Ahern 1116db24a904SDavid Ahern return 0; 1117db24a904SDavid Ahern } 1118db24a904SDavid Ahern 111982f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 112082f28412SJiri Pirko { 112182f28412SJiri Pirko int err; 1122f8e20a9fSScott Feldman struct switchdev_attr attr = { 11236ff64f6fSIdo Schimmel .orig_dev = dev, 11241f868398SJiri Pirko .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1125f8e20a9fSScott Feldman .flags = SWITCHDEV_F_NO_RECURSE, 1126f8e20a9fSScott Feldman }; 112782f28412SJiri Pirko 1128f8e20a9fSScott Feldman err = switchdev_port_attr_get(dev, &attr); 112982f28412SJiri Pirko if (err) { 113082f28412SJiri Pirko if (err == -EOPNOTSUPP) 113182f28412SJiri Pirko return 0; 113282f28412SJiri Pirko return err; 113382f28412SJiri Pirko } 113482f28412SJiri Pirko 113542275bd8SScott Feldman if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 113642275bd8SScott Feldman attr.u.ppid.id)) 113782f28412SJiri Pirko return -EMSGSIZE; 113882f28412SJiri Pirko 113982f28412SJiri Pirko return 0; 114082f28412SJiri Pirko } 114182f28412SJiri Pirko 1142b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1143b22b941bSHannes Frederic Sowa struct net_device *dev) 1144b22b941bSHannes Frederic Sowa { 1145550bce59SRoopa Prabhu struct rtnl_link_stats64 *sp; 1146b22b941bSHannes Frederic Sowa struct nlattr *attr; 1147b22b941bSHannes Frederic Sowa 114858414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS64, 114958414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), IFLA_PAD); 1150b22b941bSHannes Frederic Sowa if (!attr) 1151b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1152b22b941bSHannes Frederic Sowa 1153550bce59SRoopa Prabhu sp = nla_data(attr); 1154550bce59SRoopa Prabhu dev_get_stats(dev, sp); 1155550bce59SRoopa Prabhu 1156550bce59SRoopa Prabhu attr = nla_reserve(skb, IFLA_STATS, 1157550bce59SRoopa Prabhu sizeof(struct rtnl_link_stats)); 1158550bce59SRoopa Prabhu if (!attr) 1159550bce59SRoopa Prabhu return -EMSGSIZE; 1160550bce59SRoopa Prabhu 1161550bce59SRoopa Prabhu copy_rtnl_link_stats(nla_data(attr), sp); 1162b22b941bSHannes Frederic Sowa 1163b22b941bSHannes Frederic Sowa return 0; 1164b22b941bSHannes Frederic Sowa } 1165b22b941bSHannes Frederic Sowa 1166b22b941bSHannes Frederic Sowa static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1167b22b941bSHannes Frederic Sowa struct net_device *dev, 1168b22b941bSHannes Frederic Sowa int vfs_num, 1169b22b941bSHannes Frederic Sowa struct nlattr *vfinfo) 1170b22b941bSHannes Frederic Sowa { 1171b22b941bSHannes Frederic Sowa struct ifla_vf_rss_query_en vf_rss_query_en; 117279aab093SMoshe Shemesh struct nlattr *vf, *vfstats, *vfvlanlist; 1173b22b941bSHannes Frederic Sowa struct ifla_vf_link_state vf_linkstate; 117479aab093SMoshe Shemesh struct ifla_vf_vlan_info vf_vlan_info; 1175b22b941bSHannes Frederic Sowa struct ifla_vf_spoofchk vf_spoofchk; 1176b22b941bSHannes Frederic Sowa struct ifla_vf_tx_rate vf_tx_rate; 1177b22b941bSHannes Frederic Sowa struct ifla_vf_stats vf_stats; 1178b22b941bSHannes Frederic Sowa struct ifla_vf_trust vf_trust; 1179b22b941bSHannes Frederic Sowa struct ifla_vf_vlan vf_vlan; 1180b22b941bSHannes Frederic Sowa struct ifla_vf_rate vf_rate; 1181b22b941bSHannes Frederic Sowa struct ifla_vf_mac vf_mac; 1182b22b941bSHannes Frederic Sowa struct ifla_vf_info ivi; 1183b22b941bSHannes Frederic Sowa 11840eed9cf5SMintz, Yuval memset(&ivi, 0, sizeof(ivi)); 11850eed9cf5SMintz, Yuval 1186b22b941bSHannes Frederic Sowa /* Not all SR-IOV capable drivers support the 1187b22b941bSHannes Frederic Sowa * spoofcheck and "RSS query enable" query. Preset to 1188b22b941bSHannes Frederic Sowa * -1 so the user space tool can detect that the driver 1189b22b941bSHannes Frederic Sowa * didn't report anything. 1190b22b941bSHannes Frederic Sowa */ 1191b22b941bSHannes Frederic Sowa ivi.spoofchk = -1; 1192b22b941bSHannes Frederic Sowa ivi.rss_query_en = -1; 1193b22b941bSHannes Frederic Sowa ivi.trusted = -1; 1194b22b941bSHannes Frederic Sowa /* The default value for VF link state is "auto" 1195b22b941bSHannes Frederic Sowa * IFLA_VF_LINK_STATE_AUTO which equals zero 1196b22b941bSHannes Frederic Sowa */ 1197b22b941bSHannes Frederic Sowa ivi.linkstate = 0; 119879aab093SMoshe Shemesh /* VLAN Protocol by default is 802.1Q */ 119979aab093SMoshe Shemesh ivi.vlan_proto = htons(ETH_P_8021Q); 1200b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1201b22b941bSHannes Frederic Sowa return 0; 1202b22b941bSHannes Frederic Sowa 1203775f4f05SDan Carpenter memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1204775f4f05SDan Carpenter 1205b22b941bSHannes Frederic Sowa vf_mac.vf = 1206b22b941bSHannes Frederic Sowa vf_vlan.vf = 120779aab093SMoshe Shemesh vf_vlan_info.vf = 1208b22b941bSHannes Frederic Sowa vf_rate.vf = 1209b22b941bSHannes Frederic Sowa vf_tx_rate.vf = 1210b22b941bSHannes Frederic Sowa vf_spoofchk.vf = 1211b22b941bSHannes Frederic Sowa vf_linkstate.vf = 1212b22b941bSHannes Frederic Sowa vf_rss_query_en.vf = 1213b22b941bSHannes Frederic Sowa vf_trust.vf = ivi.vf; 1214b22b941bSHannes Frederic Sowa 1215b22b941bSHannes Frederic Sowa memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1216b22b941bSHannes Frederic Sowa vf_vlan.vlan = ivi.vlan; 1217b22b941bSHannes Frederic Sowa vf_vlan.qos = ivi.qos; 121879aab093SMoshe Shemesh vf_vlan_info.vlan = ivi.vlan; 121979aab093SMoshe Shemesh vf_vlan_info.qos = ivi.qos; 122079aab093SMoshe Shemesh vf_vlan_info.vlan_proto = ivi.vlan_proto; 1221b22b941bSHannes Frederic Sowa vf_tx_rate.rate = ivi.max_tx_rate; 1222b22b941bSHannes Frederic Sowa vf_rate.min_tx_rate = ivi.min_tx_rate; 1223b22b941bSHannes Frederic Sowa vf_rate.max_tx_rate = ivi.max_tx_rate; 1224b22b941bSHannes Frederic Sowa vf_spoofchk.setting = ivi.spoofchk; 1225b22b941bSHannes Frederic Sowa vf_linkstate.link_state = ivi.linkstate; 1226b22b941bSHannes Frederic Sowa vf_rss_query_en.setting = ivi.rss_query_en; 1227b22b941bSHannes Frederic Sowa vf_trust.setting = ivi.trusted; 1228b22b941bSHannes Frederic Sowa vf = nla_nest_start(skb, IFLA_VF_INFO); 122979aab093SMoshe Shemesh if (!vf) 123079aab093SMoshe Shemesh goto nla_put_vfinfo_failure; 1231b22b941bSHannes Frederic Sowa if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1232b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1233b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1234b22b941bSHannes Frederic Sowa &vf_rate) || 1235b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1236b22b941bSHannes Frederic Sowa &vf_tx_rate) || 1237b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1238b22b941bSHannes Frederic Sowa &vf_spoofchk) || 1239b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1240b22b941bSHannes Frederic Sowa &vf_linkstate) || 1241b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1242b22b941bSHannes Frederic Sowa sizeof(vf_rss_query_en), 1243b22b941bSHannes Frederic Sowa &vf_rss_query_en) || 1244b22b941bSHannes Frederic Sowa nla_put(skb, IFLA_VF_TRUST, 1245b22b941bSHannes Frederic Sowa sizeof(vf_trust), &vf_trust)) 124679aab093SMoshe Shemesh goto nla_put_vf_failure; 124779aab093SMoshe Shemesh vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 124879aab093SMoshe Shemesh if (!vfvlanlist) 124979aab093SMoshe Shemesh goto nla_put_vf_failure; 125079aab093SMoshe Shemesh if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 125179aab093SMoshe Shemesh &vf_vlan_info)) { 125279aab093SMoshe Shemesh nla_nest_cancel(skb, vfvlanlist); 125379aab093SMoshe Shemesh goto nla_put_vf_failure; 125479aab093SMoshe Shemesh } 125579aab093SMoshe Shemesh nla_nest_end(skb, vfvlanlist); 1256b22b941bSHannes Frederic Sowa memset(&vf_stats, 0, sizeof(vf_stats)); 1257b22b941bSHannes Frederic Sowa if (dev->netdev_ops->ndo_get_vf_stats) 1258b22b941bSHannes Frederic Sowa dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1259b22b941bSHannes Frederic Sowa &vf_stats); 1260b22b941bSHannes Frederic Sowa vfstats = nla_nest_start(skb, IFLA_VF_STATS); 126179aab093SMoshe Shemesh if (!vfstats) 126279aab093SMoshe Shemesh goto nla_put_vf_failure; 1263343a6d8eSNicolas Dichtel if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1264343a6d8eSNicolas Dichtel vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1265343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1266343a6d8eSNicolas Dichtel vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1267343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1268343a6d8eSNicolas Dichtel vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1269343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1270343a6d8eSNicolas Dichtel vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1271343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1272343a6d8eSNicolas Dichtel vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1273343a6d8eSNicolas Dichtel nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1274c5a9f6f0SEugenia Emantayev vf_stats.multicast, IFLA_VF_STATS_PAD) || 1275c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1276c5a9f6f0SEugenia Emantayev vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1277c5a9f6f0SEugenia Emantayev nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1278c5a9f6f0SEugenia Emantayev vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 127979aab093SMoshe Shemesh nla_nest_cancel(skb, vfstats); 128079aab093SMoshe Shemesh goto nla_put_vf_failure; 128179aab093SMoshe Shemesh } 1282b22b941bSHannes Frederic Sowa nla_nest_end(skb, vfstats); 1283b22b941bSHannes Frederic Sowa nla_nest_end(skb, vf); 1284b22b941bSHannes Frederic Sowa return 0; 128579aab093SMoshe Shemesh 128679aab093SMoshe Shemesh nla_put_vf_failure: 128779aab093SMoshe Shemesh nla_nest_cancel(skb, vf); 128879aab093SMoshe Shemesh nla_put_vfinfo_failure: 128979aab093SMoshe Shemesh nla_nest_cancel(skb, vfinfo); 129079aab093SMoshe Shemesh return -EMSGSIZE; 1291b22b941bSHannes Frederic Sowa } 1292b22b941bSHannes Frederic Sowa 1293250fc3dfSFlorian Westphal static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1294250fc3dfSFlorian Westphal struct net_device *dev, 1295250fc3dfSFlorian Westphal u32 ext_filter_mask) 1296250fc3dfSFlorian Westphal { 1297250fc3dfSFlorian Westphal struct nlattr *vfinfo; 1298250fc3dfSFlorian Westphal int i, num_vfs; 1299250fc3dfSFlorian Westphal 1300250fc3dfSFlorian Westphal if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1301250fc3dfSFlorian Westphal return 0; 1302250fc3dfSFlorian Westphal 1303250fc3dfSFlorian Westphal num_vfs = dev_num_vf(dev->dev.parent); 1304250fc3dfSFlorian Westphal if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1305250fc3dfSFlorian Westphal return -EMSGSIZE; 1306250fc3dfSFlorian Westphal 1307250fc3dfSFlorian Westphal if (!dev->netdev_ops->ndo_get_vf_config) 1308250fc3dfSFlorian Westphal return 0; 1309250fc3dfSFlorian Westphal 1310250fc3dfSFlorian Westphal vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1311250fc3dfSFlorian Westphal if (!vfinfo) 1312250fc3dfSFlorian Westphal return -EMSGSIZE; 1313250fc3dfSFlorian Westphal 1314250fc3dfSFlorian Westphal for (i = 0; i < num_vfs; i++) { 1315250fc3dfSFlorian Westphal if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1316250fc3dfSFlorian Westphal return -EMSGSIZE; 1317250fc3dfSFlorian Westphal } 1318250fc3dfSFlorian Westphal 1319250fc3dfSFlorian Westphal nla_nest_end(skb, vfinfo); 1320250fc3dfSFlorian Westphal return 0; 1321250fc3dfSFlorian Westphal } 1322250fc3dfSFlorian Westphal 1323b22b941bSHannes Frederic Sowa static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1324b22b941bSHannes Frederic Sowa { 13255f8e4474SKangjie Lu struct rtnl_link_ifmap map; 13265f8e4474SKangjie Lu 13275f8e4474SKangjie Lu memset(&map, 0, sizeof(map)); 13285f8e4474SKangjie Lu map.mem_start = dev->mem_start; 13295f8e4474SKangjie Lu map.mem_end = dev->mem_end; 13305f8e4474SKangjie Lu map.base_addr = dev->base_addr; 13315f8e4474SKangjie Lu map.irq = dev->irq; 13325f8e4474SKangjie Lu map.dma = dev->dma; 13335f8e4474SKangjie Lu map.port = dev->if_port; 13345f8e4474SKangjie Lu 1335270cb4d0SNicolas Dichtel if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1336b22b941bSHannes Frederic Sowa return -EMSGSIZE; 1337b22b941bSHannes Frederic Sowa 1338b22b941bSHannes Frederic Sowa return 0; 1339b22b941bSHannes Frederic Sowa } 1340b22b941bSHannes Frederic Sowa 134158038695SMartin KaFai Lau static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id) 1342d67b9cd2SDaniel Borkmann { 1343d67b9cd2SDaniel Borkmann const struct net_device_ops *ops = dev->netdev_ops; 134458038695SMartin KaFai Lau const struct bpf_prog *generic_xdp_prog; 1345118b4aa2SJakub Kicinski struct netdev_bpf xdp; 1346d67b9cd2SDaniel Borkmann 1347d67b9cd2SDaniel Borkmann ASSERT_RTNL(); 1348d67b9cd2SDaniel Borkmann 134958038695SMartin KaFai Lau *prog_id = 0; 135058038695SMartin KaFai Lau generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 135158038695SMartin KaFai Lau if (generic_xdp_prog) { 135258038695SMartin KaFai Lau *prog_id = generic_xdp_prog->aux->id; 1353d67b9cd2SDaniel Borkmann return XDP_ATTACHED_SKB; 135458038695SMartin KaFai Lau } 1355f4e63525SJakub Kicinski if (!ops->ndo_bpf) 1356d67b9cd2SDaniel Borkmann return XDP_ATTACHED_NONE; 1357ce158e58SJakub Kicinski 1358118b4aa2SJakub Kicinski __dev_xdp_query(dev, ops->ndo_bpf, &xdp); 1359118b4aa2SJakub Kicinski *prog_id = xdp.prog_id; 1360118b4aa2SJakub Kicinski 1361118b4aa2SJakub Kicinski return xdp.prog_attached; 1362d67b9cd2SDaniel Borkmann } 1363d67b9cd2SDaniel Borkmann 1364d1fdd913SBrenden Blanco static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1365d1fdd913SBrenden Blanco { 1366d1fdd913SBrenden Blanco struct nlattr *xdp; 136758038695SMartin KaFai Lau u32 prog_id; 1368d1fdd913SBrenden Blanco int err; 1369d1fdd913SBrenden Blanco 1370d1fdd913SBrenden Blanco xdp = nla_nest_start(skb, IFLA_XDP); 1371d1fdd913SBrenden Blanco if (!xdp) 1372d1fdd913SBrenden Blanco return -EMSGSIZE; 1373b5cdae32SDavid S. Miller 1374d67b9cd2SDaniel Borkmann err = nla_put_u8(skb, IFLA_XDP_ATTACHED, 137558038695SMartin KaFai Lau rtnl_xdp_attached_mode(dev, &prog_id)); 1376d1fdd913SBrenden Blanco if (err) 1377d1fdd913SBrenden Blanco goto err_cancel; 1378d1fdd913SBrenden Blanco 137958038695SMartin KaFai Lau if (prog_id) { 138058038695SMartin KaFai Lau err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 138158038695SMartin KaFai Lau if (err) 138258038695SMartin KaFai Lau goto err_cancel; 138358038695SMartin KaFai Lau } 138458038695SMartin KaFai Lau 1385d1fdd913SBrenden Blanco nla_nest_end(skb, xdp); 1386d1fdd913SBrenden Blanco return 0; 1387d1fdd913SBrenden Blanco 1388d1fdd913SBrenden Blanco err_cancel: 1389d1fdd913SBrenden Blanco nla_nest_cancel(skb, xdp); 1390d1fdd913SBrenden Blanco return err; 1391d1fdd913SBrenden Blanco } 1392d1fdd913SBrenden Blanco 13933d3ea5afSVlad Yasevich static u32 rtnl_get_event(unsigned long event) 13943d3ea5afSVlad Yasevich { 13953d3ea5afSVlad Yasevich u32 rtnl_event_type = IFLA_EVENT_NONE; 13963d3ea5afSVlad Yasevich 13973d3ea5afSVlad Yasevich switch (event) { 13983d3ea5afSVlad Yasevich case NETDEV_REBOOT: 13993d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_REBOOT; 14003d3ea5afSVlad Yasevich break; 14013d3ea5afSVlad Yasevich case NETDEV_FEAT_CHANGE: 14023d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_FEATURES; 14033d3ea5afSVlad Yasevich break; 14043d3ea5afSVlad Yasevich case NETDEV_BONDING_FAILOVER: 14053d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 14063d3ea5afSVlad Yasevich break; 14073d3ea5afSVlad Yasevich case NETDEV_NOTIFY_PEERS: 14083d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 14093d3ea5afSVlad Yasevich break; 14103d3ea5afSVlad Yasevich case NETDEV_RESEND_IGMP: 14113d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 14123d3ea5afSVlad Yasevich break; 14133d3ea5afSVlad Yasevich case NETDEV_CHANGEINFODATA: 14143d3ea5afSVlad Yasevich rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 14153d3ea5afSVlad Yasevich break; 14163d3ea5afSVlad Yasevich default: 14173d3ea5afSVlad Yasevich break; 14183d3ea5afSVlad Yasevich } 14193d3ea5afSVlad Yasevich 14203d3ea5afSVlad Yasevich return rtnl_event_type; 14213d3ea5afSVlad Yasevich } 14223d3ea5afSVlad Yasevich 142379110a04SFlorian Westphal static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 142479110a04SFlorian Westphal { 142579110a04SFlorian Westphal const struct net_device *upper_dev; 142679110a04SFlorian Westphal int ret = 0; 142779110a04SFlorian Westphal 142879110a04SFlorian Westphal rcu_read_lock(); 142979110a04SFlorian Westphal 143079110a04SFlorian Westphal upper_dev = netdev_master_upper_dev_get_rcu(dev); 143179110a04SFlorian Westphal if (upper_dev) 143279110a04SFlorian Westphal ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 143379110a04SFlorian Westphal 143479110a04SFlorian Westphal rcu_read_unlock(); 143579110a04SFlorian Westphal return ret; 143679110a04SFlorian Westphal } 143779110a04SFlorian Westphal 143879110a04SFlorian Westphal static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 143979110a04SFlorian Westphal { 144079110a04SFlorian Westphal int ifindex = dev_get_iflink(dev); 144179110a04SFlorian Westphal 144279110a04SFlorian Westphal if (dev->ifindex == ifindex) 144379110a04SFlorian Westphal return 0; 144479110a04SFlorian Westphal 144579110a04SFlorian Westphal return nla_put_u32(skb, IFLA_LINK, ifindex); 144679110a04SFlorian Westphal } 144779110a04SFlorian Westphal 14486c557001SFlorian Westphal static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 14496c557001SFlorian Westphal struct net_device *dev) 14506c557001SFlorian Westphal { 14516c557001SFlorian Westphal char buf[IFALIASZ]; 14526c557001SFlorian Westphal int ret; 14536c557001SFlorian Westphal 14546c557001SFlorian Westphal ret = dev_get_alias(dev, buf, sizeof(buf)); 14556c557001SFlorian Westphal return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 14566c557001SFlorian Westphal } 14576c557001SFlorian Westphal 1458b1e66b9aSFlorian Westphal static int rtnl_fill_link_netnsid(struct sk_buff *skb, 145979e1ad14SJiri Benc const struct net_device *dev, 146079e1ad14SJiri Benc struct net *src_net) 1461b1e66b9aSFlorian Westphal { 1462b1e66b9aSFlorian Westphal if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1463b1e66b9aSFlorian Westphal struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1464b1e66b9aSFlorian Westphal 1465b1e66b9aSFlorian Westphal if (!net_eq(dev_net(dev), link_net)) { 146679e1ad14SJiri Benc int id = peernet2id_alloc(src_net, link_net); 1467b1e66b9aSFlorian Westphal 1468b1e66b9aSFlorian Westphal if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1469b1e66b9aSFlorian Westphal return -EMSGSIZE; 1470b1e66b9aSFlorian Westphal } 1471b1e66b9aSFlorian Westphal } 1472b1e66b9aSFlorian Westphal 1473b1e66b9aSFlorian Westphal return 0; 1474b1e66b9aSFlorian Westphal } 1475b1e66b9aSFlorian Westphal 1476070cbf5bSFlorian Westphal static int rtnl_fill_link_af(struct sk_buff *skb, 1477070cbf5bSFlorian Westphal const struct net_device *dev, 1478070cbf5bSFlorian Westphal u32 ext_filter_mask) 1479070cbf5bSFlorian Westphal { 1480070cbf5bSFlorian Westphal const struct rtnl_af_ops *af_ops; 1481070cbf5bSFlorian Westphal struct nlattr *af_spec; 1482070cbf5bSFlorian Westphal 1483070cbf5bSFlorian Westphal af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1484070cbf5bSFlorian Westphal if (!af_spec) 1485070cbf5bSFlorian Westphal return -EMSGSIZE; 1486070cbf5bSFlorian Westphal 14875fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1488070cbf5bSFlorian Westphal struct nlattr *af; 1489070cbf5bSFlorian Westphal int err; 1490070cbf5bSFlorian Westphal 1491070cbf5bSFlorian Westphal if (!af_ops->fill_link_af) 1492070cbf5bSFlorian Westphal continue; 1493070cbf5bSFlorian Westphal 1494070cbf5bSFlorian Westphal af = nla_nest_start(skb, af_ops->family); 1495070cbf5bSFlorian Westphal if (!af) 1496070cbf5bSFlorian Westphal return -EMSGSIZE; 1497070cbf5bSFlorian Westphal 1498070cbf5bSFlorian Westphal err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1499070cbf5bSFlorian Westphal /* 1500070cbf5bSFlorian Westphal * Caller may return ENODATA to indicate that there 1501070cbf5bSFlorian Westphal * was no data to be dumped. This is not an error, it 1502070cbf5bSFlorian Westphal * means we should trim the attribute header and 1503070cbf5bSFlorian Westphal * continue. 1504070cbf5bSFlorian Westphal */ 1505070cbf5bSFlorian Westphal if (err == -ENODATA) 1506070cbf5bSFlorian Westphal nla_nest_cancel(skb, af); 1507070cbf5bSFlorian Westphal else if (err < 0) 1508070cbf5bSFlorian Westphal return -EMSGSIZE; 1509070cbf5bSFlorian Westphal 1510070cbf5bSFlorian Westphal nla_nest_end(skb, af); 1511070cbf5bSFlorian Westphal } 1512070cbf5bSFlorian Westphal 1513070cbf5bSFlorian Westphal nla_nest_end(skb, af_spec); 1514070cbf5bSFlorian Westphal return 0; 1515070cbf5bSFlorian Westphal } 1516070cbf5bSFlorian Westphal 151779e1ad14SJiri Benc static int rtnl_fill_ifinfo(struct sk_buff *skb, 151879e1ad14SJiri Benc struct net_device *dev, struct net *src_net, 1519575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 15203d3ea5afSVlad Yasevich unsigned int flags, u32 ext_filter_mask, 152138e01b30SNicolas Dichtel u32 event, int *new_nsid, int new_ifindex, 152238e01b30SNicolas Dichtel int tgt_netnsid) 1523b60c5115SThomas Graf { 1524b60c5115SThomas Graf struct ifinfomsg *ifm; 15251da177e4SLinus Torvalds struct nlmsghdr *nlh; 15261da177e4SLinus Torvalds 15272907c35fSEric Dumazet ASSERT_RTNL(); 1528b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1529b60c5115SThomas Graf if (nlh == NULL) 153026932566SPatrick McHardy return -EMSGSIZE; 15311da177e4SLinus Torvalds 1532b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1533b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1534b60c5115SThomas Graf ifm->__ifi_pad = 0; 1535b60c5115SThomas Graf ifm->ifi_type = dev->type; 1536b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1537b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1538b60c5115SThomas Graf ifm->ifi_change = change; 15391da177e4SLinus Torvalds 154079e1ad14SJiri Benc if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 154179e1ad14SJiri Benc goto nla_put_failure; 154279e1ad14SJiri Benc 1543a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1544a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1545a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1546a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1547a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1548a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1549a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1550edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 155176ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1552c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1553c70ce028SEric Dumazet nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 15541d69c2b3SMark A. Greer #ifdef CONFIG_RPS 155576ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 15561d69c2b3SMark A. Greer #endif 155779110a04SFlorian Westphal nla_put_iflink(skb, dev) || 155879110a04SFlorian Westphal put_master_ifindex(skb, dev) || 15599a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1560a6574349SDavid S. Miller (dev->qdisc && 1561a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 15626c557001SFlorian Westphal nla_put_ifalias(skb, dev) || 15632d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1564b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count) + 1565b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count)) || 1566b2d3bcfaSDavid Decotigny nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1567b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1568b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_up_count)) || 1569b2d3bcfaSDavid Decotigny nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1570b2d3bcfaSDavid Decotigny atomic_read(&dev->carrier_down_count))) 1571a6574349SDavid S. Miller goto nla_put_failure; 15720b815a1aSStephen Hemminger 15733d3ea5afSVlad Yasevich if (event != IFLA_EVENT_NONE) { 15743d3ea5afSVlad Yasevich if (nla_put_u32(skb, IFLA_EVENT, event)) 15753d3ea5afSVlad Yasevich goto nla_put_failure; 15763d3ea5afSVlad Yasevich } 15773d3ea5afSVlad Yasevich 1578b22b941bSHannes Frederic Sowa if (rtnl_fill_link_ifmap(skb, dev)) 1579a6574349SDavid S. Miller goto nla_put_failure; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds if (dev->addr_len) { 1582a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1583a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1584a6574349SDavid S. Miller goto nla_put_failure; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 158766cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 158866cae9edSJiri Pirko goto nla_put_failure; 158966cae9edSJiri Pirko 1590db24a904SDavid Ahern if (rtnl_phys_port_name_fill(skb, dev)) 1591db24a904SDavid Ahern goto nla_put_failure; 1592db24a904SDavid Ahern 159382f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 159482f28412SJiri Pirko goto nla_put_failure; 159582f28412SJiri Pirko 1596b22b941bSHannes Frederic Sowa if (rtnl_fill_stats(skb, dev)) 1597b60c5115SThomas Graf goto nla_put_failure; 1598b60c5115SThomas Graf 1599250fc3dfSFlorian Westphal if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1600a6574349SDavid S. Miller goto nla_put_failure; 160157b61080SScott Feldman 1602c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 160357b61080SScott Feldman goto nla_put_failure; 160457b61080SScott Feldman 1605d1fdd913SBrenden Blanco if (rtnl_xdp_fill(skb, dev)) 1606d1fdd913SBrenden Blanco goto nla_put_failure; 1607d1fdd913SBrenden Blanco 1608ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 160938f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 161038f7b870SPatrick McHardy goto nla_put_failure; 161138f7b870SPatrick McHardy } 161238f7b870SPatrick McHardy 161379e1ad14SJiri Benc if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1614d37512a2SNicolas Dichtel goto nla_put_failure; 1615d37512a2SNicolas Dichtel 16166621dd29SNicolas Dichtel if (new_nsid && 16176621dd29SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 16186621dd29SNicolas Dichtel goto nla_put_failure; 161938e01b30SNicolas Dichtel if (new_ifindex && 162038e01b30SNicolas Dichtel nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 162138e01b30SNicolas Dichtel goto nla_put_failure; 162238e01b30SNicolas Dichtel 16236621dd29SNicolas Dichtel 16245fa85a09SFlorian Westphal rcu_read_lock(); 1625070cbf5bSFlorian Westphal if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 16265fa85a09SFlorian Westphal goto nla_put_failure_rcu; 16275fa85a09SFlorian Westphal rcu_read_unlock(); 1628f8ff182cSThomas Graf 1629053c095aSJohannes Berg nlmsg_end(skb, nlh); 1630053c095aSJohannes Berg return 0; 1631b60c5115SThomas Graf 16325fa85a09SFlorian Westphal nla_put_failure_rcu: 16335fa85a09SFlorian Westphal rcu_read_unlock(); 1634b60c5115SThomas Graf nla_put_failure: 163526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 163626932566SPatrick McHardy return -EMSGSIZE; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 1639f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1640f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1641f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1642f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1643f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1644f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1645f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1646f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1647f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1648f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1649f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1650f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1651f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1652f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1653f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1654f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 16552459b4c6SNicolas Dichtel /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 16562459b4c6SNicolas Dichtel * allow 0-length string (needed to remove an alias). 16572459b4c6SNicolas Dichtel */ 16582459b4c6SNicolas Dichtel [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1659f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1660f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1661f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1662f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1663f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1664f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1665f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1666f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 166746e6b992SStephen Hemminger [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 166846e6b992SStephen Hemminger [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 166902637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 16702d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 167182f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1672317f4810SNicolas Dichtel [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 167388d6378bSAnuradha Karuppiah [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1674d1fdd913SBrenden Blanco [IFLA_XDP] = { .type = NLA_NESTED }, 16753d3ea5afSVlad Yasevich [IFLA_EVENT] = { .type = NLA_U32 }, 1676db833d40SSerhey Popovych [IFLA_GROUP] = { .type = NLA_U32 }, 167779e1ad14SJiri Benc [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1678b2d3bcfaSDavid Decotigny [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1679b2d3bcfaSDavid Decotigny [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 1680f7b12606SJiri Pirko }; 1681f7b12606SJiri Pirko 1682f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1683f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1684f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1685f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1686f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1687f7b12606SJiri Pirko }; 1688f7b12606SJiri Pirko 1689f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1690364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1691364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 169279aab093SMoshe Shemesh [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1693364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1694364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1695364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1696364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 169701a3d796SVlad Zolotarov [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 16983b766cd8SEran Ben Elisha [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1699dd461d6aSHiroshi Shimamoto [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1700cc8e27ccSEli Cohen [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1701cc8e27ccSEli Cohen [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 17023b766cd8SEran Ben Elisha }; 17033b766cd8SEran Ben Elisha 1704f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1705f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1706f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1707f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1708f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1709f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1710f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1711f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1712f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1713f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1714025331dfSDaniel Borkmann 1715025331dfSDaniel Borkmann /* Unused, but we need to keep it here since user space could 1716025331dfSDaniel Borkmann * fill it. It's also broken with regard to NLA_BINARY use in 1717025331dfSDaniel Borkmann * combination with structs. 1718025331dfSDaniel Borkmann */ 1719025331dfSDaniel Borkmann [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1720025331dfSDaniel Borkmann .len = sizeof(struct ifla_port_vsi) }, 1721f7b12606SJiri Pirko }; 1722f7b12606SJiri Pirko 1723d1fdd913SBrenden Blanco static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1724d1fdd913SBrenden Blanco [IFLA_XDP_FD] = { .type = NLA_S32 }, 1725d1fdd913SBrenden Blanco [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 172685de8576SDaniel Borkmann [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 172758038695SMartin KaFai Lau [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1728d1fdd913SBrenden Blanco }; 1729d1fdd913SBrenden Blanco 1730dc599f76SDavid Ahern static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1731dc599f76SDavid Ahern { 1732dc599f76SDavid Ahern const struct rtnl_link_ops *ops = NULL; 1733dc599f76SDavid Ahern struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1734dc599f76SDavid Ahern 1735fceb6435SJohannes Berg if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1736fceb6435SJohannes Berg ifla_info_policy, NULL) < 0) 1737dc599f76SDavid Ahern return NULL; 1738dc599f76SDavid Ahern 1739dc599f76SDavid Ahern if (linfo[IFLA_INFO_KIND]) { 1740dc599f76SDavid Ahern char kind[MODULE_NAME_LEN]; 1741dc599f76SDavid Ahern 1742dc599f76SDavid Ahern nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1743dc599f76SDavid Ahern ops = rtnl_link_ops_get(kind); 1744dc599f76SDavid Ahern } 1745dc599f76SDavid Ahern 1746dc599f76SDavid Ahern return ops; 1747dc599f76SDavid Ahern } 1748dc599f76SDavid Ahern 1749dc599f76SDavid Ahern static bool link_master_filtered(struct net_device *dev, int master_idx) 1750dc599f76SDavid Ahern { 1751dc599f76SDavid Ahern struct net_device *master; 1752dc599f76SDavid Ahern 1753dc599f76SDavid Ahern if (!master_idx) 1754dc599f76SDavid Ahern return false; 1755dc599f76SDavid Ahern 1756dc599f76SDavid Ahern master = netdev_master_upper_dev_get(dev); 1757dc599f76SDavid Ahern if (!master || master->ifindex != master_idx) 1758dc599f76SDavid Ahern return true; 1759dc599f76SDavid Ahern 1760dc599f76SDavid Ahern return false; 1761dc599f76SDavid Ahern } 1762dc599f76SDavid Ahern 1763dc599f76SDavid Ahern static bool link_kind_filtered(const struct net_device *dev, 1764dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1765dc599f76SDavid Ahern { 1766dc599f76SDavid Ahern if (kind_ops && dev->rtnl_link_ops != kind_ops) 1767dc599f76SDavid Ahern return true; 1768dc599f76SDavid Ahern 1769dc599f76SDavid Ahern return false; 1770dc599f76SDavid Ahern } 1771dc599f76SDavid Ahern 1772dc599f76SDavid Ahern static bool link_dump_filtered(struct net_device *dev, 1773dc599f76SDavid Ahern int master_idx, 1774dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops) 1775dc599f76SDavid Ahern { 1776dc599f76SDavid Ahern if (link_master_filtered(dev, master_idx) || 1777dc599f76SDavid Ahern link_kind_filtered(dev, kind_ops)) 1778dc599f76SDavid Ahern return true; 1779dc599f76SDavid Ahern 1780dc599f76SDavid Ahern return false; 1781dc599f76SDavid Ahern } 1782dc599f76SDavid Ahern 1783f428fe4aSAndrei Vagin static struct net *get_target_net(struct sock *sk, int netnsid) 178479e1ad14SJiri Benc { 178579e1ad14SJiri Benc struct net *net; 178679e1ad14SJiri Benc 1787f428fe4aSAndrei Vagin net = get_net_ns_by_id(sock_net(sk), netnsid); 178879e1ad14SJiri Benc if (!net) 178979e1ad14SJiri Benc return ERR_PTR(-EINVAL); 179079e1ad14SJiri Benc 179179e1ad14SJiri Benc /* For now, the caller is required to have CAP_NET_ADMIN in 179279e1ad14SJiri Benc * the user namespace owning the target net ns. 179379e1ad14SJiri Benc */ 1794f428fe4aSAndrei Vagin if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 179579e1ad14SJiri Benc put_net(net); 179679e1ad14SJiri Benc return ERR_PTR(-EACCES); 179779e1ad14SJiri Benc } 179879e1ad14SJiri Benc return net; 179979e1ad14SJiri Benc } 180079e1ad14SJiri Benc 1801b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 18021da177e4SLinus Torvalds { 18033b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 180479e1ad14SJiri Benc struct net *tgt_net = net; 18057c28bd0bSEric Dumazet int h, s_h; 18067c28bd0bSEric Dumazet int idx = 0, s_idx; 18071da177e4SLinus Torvalds struct net_device *dev; 18087c28bd0bSEric Dumazet struct hlist_head *head; 1809115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1810115c9b81SGreg Rose u32 ext_filter_mask = 0; 1811dc599f76SDavid Ahern const struct rtnl_link_ops *kind_ops = NULL; 1812dc599f76SDavid Ahern unsigned int flags = NLM_F_MULTI; 1813dc599f76SDavid Ahern int master_idx = 0; 181479e1ad14SJiri Benc int netnsid = -1; 1815973462bbSDavid Gibson int err; 1816e5eca6d4SMichal Schmidt int hdrlen; 18171da177e4SLinus Torvalds 18187c28bd0bSEric Dumazet s_h = cb->args[0]; 18197c28bd0bSEric Dumazet s_idx = cb->args[1]; 18207c28bd0bSEric Dumazet 1821e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1822e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1823e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1824e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1825e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1826e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1827e5eca6d4SMichal Schmidt */ 1828e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1829e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1830e5eca6d4SMichal Schmidt 1831fceb6435SJohannes Berg if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1832fceb6435SJohannes Berg ifla_policy, NULL) >= 0) { 183379e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 183479e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1835f428fe4aSAndrei Vagin tgt_net = get_target_net(skb->sk, netnsid); 183679e1ad14SJiri Benc if (IS_ERR(tgt_net)) { 183779e1ad14SJiri Benc tgt_net = net; 183879e1ad14SJiri Benc netnsid = -1; 183979e1ad14SJiri Benc } 184079e1ad14SJiri Benc } 184179e1ad14SJiri Benc 1842115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1843115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1844dc599f76SDavid Ahern 1845dc599f76SDavid Ahern if (tb[IFLA_MASTER]) 1846dc599f76SDavid Ahern master_idx = nla_get_u32(tb[IFLA_MASTER]); 1847dc599f76SDavid Ahern 1848dc599f76SDavid Ahern if (tb[IFLA_LINKINFO]) 1849dc599f76SDavid Ahern kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1850dc599f76SDavid Ahern 1851dc599f76SDavid Ahern if (master_idx || kind_ops) 1852dc599f76SDavid Ahern flags |= NLM_F_DUMP_FILTERED; 1853a4b64fbeSEric Dumazet } 1854115c9b81SGreg Rose 18557c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 18567562f876SPavel Emelianov idx = 0; 185779e1ad14SJiri Benc head = &tgt_net->dev_index_head[h]; 1858cac5e65eSEric Dumazet hlist_for_each_entry(dev, head, index_hlist) { 1859dc599f76SDavid Ahern if (link_dump_filtered(dev, master_idx, kind_ops)) 18603f0ae05dSZhang Shengju goto cont; 18611da177e4SLinus Torvalds if (idx < s_idx) 18627562f876SPavel Emelianov goto cont; 186379e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, net, 186479e1ad14SJiri Benc RTM_NEWLINK, 186515e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 18667c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1867dc599f76SDavid Ahern flags, 186838e01b30SNicolas Dichtel ext_filter_mask, 0, NULL, 0, 186979e1ad14SJiri Benc netnsid); 1870973462bbSDavid Gibson 1871f6c5775fSDavid Ahern if (err < 0) { 1872f6c5775fSDavid Ahern if (likely(skb->len)) 18737c28bd0bSEric Dumazet goto out; 18744e985adaSThomas Graf 1875f6c5775fSDavid Ahern goto out_err; 1876f6c5775fSDavid Ahern } 18777562f876SPavel Emelianov cont: 18787562f876SPavel Emelianov idx++; 18791da177e4SLinus Torvalds } 18807c28bd0bSEric Dumazet } 18817c28bd0bSEric Dumazet out: 1882f6c5775fSDavid Ahern err = skb->len; 1883f6c5775fSDavid Ahern out_err: 18847c28bd0bSEric Dumazet cb->args[1] = idx; 18857c28bd0bSEric Dumazet cb->args[0] = h; 1886d0225784SJakub Sitnicki cb->seq = net->dev_base_seq; 1887d0225784SJakub Sitnicki nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 188879e1ad14SJiri Benc if (netnsid >= 0) 188979e1ad14SJiri Benc put_net(tgt_net); 18901da177e4SLinus Torvalds 1891f6c5775fSDavid Ahern return err; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 1894fceb6435SJohannes Berg int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1895fceb6435SJohannes Berg struct netlink_ext_ack *exterr) 1896f7b12606SJiri Pirko { 1897fceb6435SJohannes Berg return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1898f7b12606SJiri Pirko } 1899f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 190057b61080SScott Feldman 190181adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 190281adee47SEric W. Biederman { 190381adee47SEric W. Biederman struct net *net; 190481adee47SEric W. Biederman /* Examine the link attributes and figure out which 190581adee47SEric W. Biederman * network namespace we are talking about. 190681adee47SEric W. Biederman */ 190781adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 190881adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1909f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1910f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 191181adee47SEric W. Biederman else 191281adee47SEric W. Biederman net = get_net(src_net); 191381adee47SEric W. Biederman return net; 191481adee47SEric W. Biederman } 191581adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 191681adee47SEric W. Biederman 19177c4f63baSChristian Brauner /* Figure out which network namespace we are talking about by 19187c4f63baSChristian Brauner * examining the link attributes in the following order: 19197c4f63baSChristian Brauner * 19207c4f63baSChristian Brauner * 1. IFLA_NET_NS_PID 19217c4f63baSChristian Brauner * 2. IFLA_NET_NS_FD 19227c4f63baSChristian Brauner * 3. IFLA_IF_NETNSID 19237c4f63baSChristian Brauner */ 19247c4f63baSChristian Brauner static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 19257c4f63baSChristian Brauner struct nlattr *tb[]) 19267c4f63baSChristian Brauner { 19277c4f63baSChristian Brauner struct net *net; 19287c4f63baSChristian Brauner 19297c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 19307c4f63baSChristian Brauner return rtnl_link_get_net(src_net, tb); 19317c4f63baSChristian Brauner 19327c4f63baSChristian Brauner if (!tb[IFLA_IF_NETNSID]) 19337c4f63baSChristian Brauner return get_net(src_net); 19347c4f63baSChristian Brauner 19357c4f63baSChristian Brauner net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 19367c4f63baSChristian Brauner if (!net) 19377c4f63baSChristian Brauner return ERR_PTR(-EINVAL); 19387c4f63baSChristian Brauner 19397c4f63baSChristian Brauner return net; 19407c4f63baSChristian Brauner } 19417c4f63baSChristian Brauner 19427c4f63baSChristian Brauner static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 19437c4f63baSChristian Brauner struct net *src_net, 19447c4f63baSChristian Brauner struct nlattr *tb[], int cap) 19457c4f63baSChristian Brauner { 19467c4f63baSChristian Brauner struct net *net; 19477c4f63baSChristian Brauner 19487c4f63baSChristian Brauner net = rtnl_link_get_net_by_nlattr(src_net, tb); 19497c4f63baSChristian Brauner if (IS_ERR(net)) 19507c4f63baSChristian Brauner return net; 19517c4f63baSChristian Brauner 19527c4f63baSChristian Brauner if (!netlink_ns_capable(skb, net->user_ns, cap)) { 19537c4f63baSChristian Brauner put_net(net); 19547c4f63baSChristian Brauner return ERR_PTR(-EPERM); 19557c4f63baSChristian Brauner } 19567c4f63baSChristian Brauner 19577c4f63baSChristian Brauner return net; 19587c4f63baSChristian Brauner } 19597c4f63baSChristian Brauner 19604ff66caeSChristian Brauner /* Verify that rtnetlink requests do not pass additional properties 19614ff66caeSChristian Brauner * potentially referring to different network namespaces. 19624ff66caeSChristian Brauner */ 19634ff66caeSChristian Brauner static int rtnl_ensure_unique_netns(struct nlattr *tb[], 19644ff66caeSChristian Brauner struct netlink_ext_ack *extack, 19654ff66caeSChristian Brauner bool netns_id_only) 19664ff66caeSChristian Brauner { 19674ff66caeSChristian Brauner 19684ff66caeSChristian Brauner if (netns_id_only) { 19694ff66caeSChristian Brauner if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 19704ff66caeSChristian Brauner return 0; 19714ff66caeSChristian Brauner 19724ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 19734ff66caeSChristian Brauner return -EOPNOTSUPP; 19744ff66caeSChristian Brauner } 19754ff66caeSChristian Brauner 19764ff66caeSChristian Brauner if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 19774ff66caeSChristian Brauner goto invalid_attr; 19784ff66caeSChristian Brauner 19794ff66caeSChristian Brauner if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 19804ff66caeSChristian Brauner goto invalid_attr; 19814ff66caeSChristian Brauner 19824ff66caeSChristian Brauner if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 19834ff66caeSChristian Brauner goto invalid_attr; 19844ff66caeSChristian Brauner 19854ff66caeSChristian Brauner return 0; 19864ff66caeSChristian Brauner 19874ff66caeSChristian Brauner invalid_attr: 19884ff66caeSChristian Brauner NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 19894ff66caeSChristian Brauner return -EINVAL; 19904ff66caeSChristian Brauner } 19914ff66caeSChristian Brauner 19921840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 19931840bb13SThomas Graf { 19941840bb13SThomas Graf if (dev) { 19951840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 19961840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 19971840bb13SThomas Graf return -EINVAL; 19981840bb13SThomas Graf 19991840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 20001840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 20011840bb13SThomas Graf return -EINVAL; 20021840bb13SThomas Graf } 20031840bb13SThomas Graf 2004cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 2005cf7afbfeSThomas Graf struct nlattr *af; 2006cf7afbfeSThomas Graf int rem, err; 2007cf7afbfeSThomas Graf 2008cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2009cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 2010cf7afbfeSThomas Graf 20115fa85a09SFlorian Westphal rcu_read_lock(); 20125fa85a09SFlorian Westphal af_ops = rtnl_af_lookup(nla_type(af)); 20135fa85a09SFlorian Westphal if (!af_ops) { 20145fa85a09SFlorian Westphal rcu_read_unlock(); 2015cf7afbfeSThomas Graf return -EAFNOSUPPORT; 20165fa85a09SFlorian Westphal } 2017cf7afbfeSThomas Graf 20185fa85a09SFlorian Westphal if (!af_ops->set_link_af) { 20195fa85a09SFlorian Westphal rcu_read_unlock(); 2020cf7afbfeSThomas Graf return -EOPNOTSUPP; 20215fa85a09SFlorian Westphal } 2022cf7afbfeSThomas Graf 2023cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 20246d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 20255fa85a09SFlorian Westphal if (err < 0) { 20265fa85a09SFlorian Westphal rcu_read_unlock(); 2027cf7afbfeSThomas Graf return err; 2028cf7afbfeSThomas Graf } 2029cf7afbfeSThomas Graf } 20305fa85a09SFlorian Westphal 20315fa85a09SFlorian Westphal rcu_read_unlock(); 20325fa85a09SFlorian Westphal } 2033cf7afbfeSThomas Graf } 2034cf7afbfeSThomas Graf 20351840bb13SThomas Graf return 0; 20361840bb13SThomas Graf } 20371840bb13SThomas Graf 2038cc8e27ccSEli Cohen static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2039cc8e27ccSEli Cohen int guid_type) 2040cc8e27ccSEli Cohen { 2041cc8e27ccSEli Cohen const struct net_device_ops *ops = dev->netdev_ops; 2042cc8e27ccSEli Cohen 2043cc8e27ccSEli Cohen return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2044cc8e27ccSEli Cohen } 2045cc8e27ccSEli Cohen 2046cc8e27ccSEli Cohen static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2047cc8e27ccSEli Cohen { 2048cc8e27ccSEli Cohen if (dev->type != ARPHRD_INFINIBAND) 2049cc8e27ccSEli Cohen return -EOPNOTSUPP; 2050cc8e27ccSEli Cohen 2051cc8e27ccSEli Cohen return handle_infiniband_guid(dev, ivt, guid_type); 2052cc8e27ccSEli Cohen } 2053cc8e27ccSEli Cohen 20544f7d2cdfSDaniel Borkmann static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2055c02db8c6SChris Wright { 2056c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 20574f7d2cdfSDaniel Borkmann int err = -EINVAL; 2058c02db8c6SChris Wright 20594f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_MAC]) { 20604f7d2cdfSDaniel Borkmann struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 20614f7d2cdfSDaniel Borkmann 2062c02db8c6SChris Wright err = -EOPNOTSUPP; 2063c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 2064c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 2065c02db8c6SChris Wright ivm->mac); 20664f7d2cdfSDaniel Borkmann if (err < 0) 20674f7d2cdfSDaniel Borkmann return err; 2068c02db8c6SChris Wright } 20694f7d2cdfSDaniel Borkmann 20704f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_VLAN]) { 20714f7d2cdfSDaniel Borkmann struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 20724f7d2cdfSDaniel Borkmann 2073c02db8c6SChris Wright err = -EOPNOTSUPP; 2074c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 20754f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 207679aab093SMoshe Shemesh ivv->qos, 207779aab093SMoshe Shemesh htons(ETH_P_8021Q)); 207879aab093SMoshe Shemesh if (err < 0) 207979aab093SMoshe Shemesh return err; 208079aab093SMoshe Shemesh } 208179aab093SMoshe Shemesh 208279aab093SMoshe Shemesh if (tb[IFLA_VF_VLAN_LIST]) { 208379aab093SMoshe Shemesh struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 208479aab093SMoshe Shemesh struct nlattr *attr; 208579aab093SMoshe Shemesh int rem, len = 0; 208679aab093SMoshe Shemesh 208779aab093SMoshe Shemesh err = -EOPNOTSUPP; 208879aab093SMoshe Shemesh if (!ops->ndo_set_vf_vlan) 208979aab093SMoshe Shemesh return err; 209079aab093SMoshe Shemesh 209179aab093SMoshe Shemesh nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 209279aab093SMoshe Shemesh if (nla_type(attr) != IFLA_VF_VLAN_INFO || 209379aab093SMoshe Shemesh nla_len(attr) < NLA_HDRLEN) { 209479aab093SMoshe Shemesh return -EINVAL; 209579aab093SMoshe Shemesh } 209679aab093SMoshe Shemesh if (len >= MAX_VLAN_LIST_LEN) 209779aab093SMoshe Shemesh return -EOPNOTSUPP; 209879aab093SMoshe Shemesh ivvl[len] = nla_data(attr); 209979aab093SMoshe Shemesh 210079aab093SMoshe Shemesh len++; 210179aab093SMoshe Shemesh } 2102fa34cd94SArnd Bergmann if (len == 0) 2103fa34cd94SArnd Bergmann return -EINVAL; 2104fa34cd94SArnd Bergmann 210579aab093SMoshe Shemesh err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 210679aab093SMoshe Shemesh ivvl[0]->qos, ivvl[0]->vlan_proto); 21074f7d2cdfSDaniel Borkmann if (err < 0) 21084f7d2cdfSDaniel Borkmann return err; 2109c02db8c6SChris Wright } 21104f7d2cdfSDaniel Borkmann 21114f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_TX_RATE]) { 21124f7d2cdfSDaniel Borkmann struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2113ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 21144f7d2cdfSDaniel Borkmann 2115c02db8c6SChris Wright err = -EOPNOTSUPP; 2116ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 21174f7d2cdfSDaniel Borkmann err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 21184f7d2cdfSDaniel Borkmann if (err < 0) 21194f7d2cdfSDaniel Borkmann return err; 21204f7d2cdfSDaniel Borkmann 2121ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2122ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2123ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2124ed616689SSucheta Chakraborty ivf.min_tx_rate, 2125c02db8c6SChris Wright ivt->rate); 21264f7d2cdfSDaniel Borkmann if (err < 0) 21274f7d2cdfSDaniel Borkmann return err; 2128c02db8c6SChris Wright } 21294f7d2cdfSDaniel Borkmann 21304f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RATE]) { 21314f7d2cdfSDaniel Borkmann struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 21324f7d2cdfSDaniel Borkmann 2133ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 2134ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 2135ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 2136ed616689SSucheta Chakraborty ivt->min_tx_rate, 2137ed616689SSucheta Chakraborty ivt->max_tx_rate); 21384f7d2cdfSDaniel Borkmann if (err < 0) 21394f7d2cdfSDaniel Borkmann return err; 2140ed616689SSucheta Chakraborty } 21414f7d2cdfSDaniel Borkmann 21424f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_SPOOFCHK]) { 21434f7d2cdfSDaniel Borkmann struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 21444f7d2cdfSDaniel Borkmann 21455f8444a3SGreg Rose err = -EOPNOTSUPP; 21465f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 21475f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 21485f8444a3SGreg Rose ivs->setting); 21494f7d2cdfSDaniel Borkmann if (err < 0) 21504f7d2cdfSDaniel Borkmann return err; 21515f8444a3SGreg Rose } 21524f7d2cdfSDaniel Borkmann 21534f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_LINK_STATE]) { 21544f7d2cdfSDaniel Borkmann struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 21554f7d2cdfSDaniel Borkmann 21561d8faf48SRony Efraim err = -EOPNOTSUPP; 21571d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 21581d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 21591d8faf48SRony Efraim ivl->link_state); 21604f7d2cdfSDaniel Borkmann if (err < 0) 21614f7d2cdfSDaniel Borkmann return err; 21621d8faf48SRony Efraim } 21634f7d2cdfSDaniel Borkmann 21644f7d2cdfSDaniel Borkmann if (tb[IFLA_VF_RSS_QUERY_EN]) { 216501a3d796SVlad Zolotarov struct ifla_vf_rss_query_en *ivrssq_en; 216601a3d796SVlad Zolotarov 216701a3d796SVlad Zolotarov err = -EOPNOTSUPP; 21684f7d2cdfSDaniel Borkmann ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 216901a3d796SVlad Zolotarov if (ops->ndo_set_vf_rss_query_en) 21704f7d2cdfSDaniel Borkmann err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 217101a3d796SVlad Zolotarov ivrssq_en->setting); 21724f7d2cdfSDaniel Borkmann if (err < 0) 21734f7d2cdfSDaniel Borkmann return err; 217401a3d796SVlad Zolotarov } 21754f7d2cdfSDaniel Borkmann 2176dd461d6aSHiroshi Shimamoto if (tb[IFLA_VF_TRUST]) { 2177dd461d6aSHiroshi Shimamoto struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2178dd461d6aSHiroshi Shimamoto 2179dd461d6aSHiroshi Shimamoto err = -EOPNOTSUPP; 2180dd461d6aSHiroshi Shimamoto if (ops->ndo_set_vf_trust) 2181dd461d6aSHiroshi Shimamoto err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2182dd461d6aSHiroshi Shimamoto if (err < 0) 2183dd461d6aSHiroshi Shimamoto return err; 2184dd461d6aSHiroshi Shimamoto } 2185dd461d6aSHiroshi Shimamoto 2186cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_NODE_GUID]) { 2187cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2188cc8e27ccSEli Cohen 2189cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2190cc8e27ccSEli Cohen return -EOPNOTSUPP; 2191cc8e27ccSEli Cohen 2192cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2193cc8e27ccSEli Cohen } 2194cc8e27ccSEli Cohen 2195cc8e27ccSEli Cohen if (tb[IFLA_VF_IB_PORT_GUID]) { 2196cc8e27ccSEli Cohen struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2197cc8e27ccSEli Cohen 2198cc8e27ccSEli Cohen if (!ops->ndo_set_vf_guid) 2199cc8e27ccSEli Cohen return -EOPNOTSUPP; 2200cc8e27ccSEli Cohen 2201cc8e27ccSEli Cohen return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2202cc8e27ccSEli Cohen } 2203cc8e27ccSEli Cohen 2204c02db8c6SChris Wright return err; 2205c02db8c6SChris Wright } 2206c02db8c6SChris Wright 220733eaf2a6SDavid Ahern static int do_set_master(struct net_device *dev, int ifindex, 220833eaf2a6SDavid Ahern struct netlink_ext_ack *extack) 2209fbaec0eaSJiri Pirko { 2210898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2211fbaec0eaSJiri Pirko const struct net_device_ops *ops; 2212fbaec0eaSJiri Pirko int err; 2213fbaec0eaSJiri Pirko 2214898e5061SJiri Pirko if (upper_dev) { 2215898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 2216fbaec0eaSJiri Pirko return 0; 2217898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2218fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 2219898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 2220fbaec0eaSJiri Pirko if (err) 2221fbaec0eaSJiri Pirko return err; 2222fbaec0eaSJiri Pirko } else { 2223fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2224fbaec0eaSJiri Pirko } 2225fbaec0eaSJiri Pirko } 2226fbaec0eaSJiri Pirko 2227fbaec0eaSJiri Pirko if (ifindex) { 2228898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2229898e5061SJiri Pirko if (!upper_dev) 2230fbaec0eaSJiri Pirko return -EINVAL; 2231898e5061SJiri Pirko ops = upper_dev->netdev_ops; 2232fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 223333eaf2a6SDavid Ahern err = ops->ndo_add_slave(upper_dev, dev, extack); 2234fbaec0eaSJiri Pirko if (err) 2235fbaec0eaSJiri Pirko return err; 2236fbaec0eaSJiri Pirko } else { 2237fbaec0eaSJiri Pirko return -EOPNOTSUPP; 2238fbaec0eaSJiri Pirko } 2239fbaec0eaSJiri Pirko } 2240fbaec0eaSJiri Pirko return 0; 2241fbaec0eaSJiri Pirko } 2242fbaec0eaSJiri Pirko 224390c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 2244ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 2245ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 224690f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 224790f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 2248ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 224990c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 22500157f60cSPatrick McHardy { 2251d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 22520157f60cSPatrick McHardy int err; 22530157f60cSPatrick McHardy 22547c4f63baSChristian Brauner if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 22557c4f63baSChristian Brauner struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 22567c4f63baSChristian Brauner tb, CAP_NET_ADMIN); 2257d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 2258d8a5ec67SEric W. Biederman err = PTR_ERR(net); 2259d8a5ec67SEric W. Biederman goto errout; 2260d8a5ec67SEric W. Biederman } 22617c4f63baSChristian Brauner 2262d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 2263d8a5ec67SEric W. Biederman put_net(net); 2264d8a5ec67SEric W. Biederman if (err) 2265d8a5ec67SEric W. Biederman goto errout; 226690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2267d8a5ec67SEric W. Biederman } 2268d8a5ec67SEric W. Biederman 22690157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 22700157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 22710157f60cSPatrick McHardy struct ifmap k_map; 22720157f60cSPatrick McHardy 2273d314774cSStephen Hemminger if (!ops->ndo_set_config) { 22740157f60cSPatrick McHardy err = -EOPNOTSUPP; 22750157f60cSPatrick McHardy goto errout; 22760157f60cSPatrick McHardy } 22770157f60cSPatrick McHardy 22780157f60cSPatrick McHardy if (!netif_device_present(dev)) { 22790157f60cSPatrick McHardy err = -ENODEV; 22800157f60cSPatrick McHardy goto errout; 22810157f60cSPatrick McHardy } 22820157f60cSPatrick McHardy 22830157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 22840157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 22850157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 22860157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 22870157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 22880157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 22890157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 22900157f60cSPatrick McHardy 2291d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 22920157f60cSPatrick McHardy if (err < 0) 22930157f60cSPatrick McHardy goto errout; 22940157f60cSPatrick McHardy 2295ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 22960157f60cSPatrick McHardy } 22970157f60cSPatrick McHardy 22980157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 22990157f60cSPatrick McHardy struct sockaddr *sa; 23000157f60cSPatrick McHardy int len; 23010157f60cSPatrick McHardy 2302153711f9SWANG Cong len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2303153711f9SWANG Cong sizeof(*sa)); 23040157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 23050157f60cSPatrick McHardy if (!sa) { 23060157f60cSPatrick McHardy err = -ENOMEM; 23070157f60cSPatrick McHardy goto errout; 23080157f60cSPatrick McHardy } 23090157f60cSPatrick McHardy sa->sa_family = dev->type; 23100157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 23110157f60cSPatrick McHardy dev->addr_len); 2312e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 23130157f60cSPatrick McHardy kfree(sa); 23140157f60cSPatrick McHardy if (err) 23150157f60cSPatrick McHardy goto errout; 231690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23170157f60cSPatrick McHardy } 23180157f60cSPatrick McHardy 23190157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 23200157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 23210157f60cSPatrick McHardy if (err < 0) 23220157f60cSPatrick McHardy goto errout; 232390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23240157f60cSPatrick McHardy } 23250157f60cSPatrick McHardy 2326cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 2327cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2328ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2329cbda10faSVlad Dogaru } 2330cbda10faSVlad Dogaru 23310157f60cSPatrick McHardy /* 23320157f60cSPatrick McHardy * Interface selected by interface index but interface 23330157f60cSPatrick McHardy * name provided implies that a name change has been 23340157f60cSPatrick McHardy * requested. 23350157f60cSPatrick McHardy */ 23360157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 23370157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 23380157f60cSPatrick McHardy if (err < 0) 23390157f60cSPatrick McHardy goto errout; 234090c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23410157f60cSPatrick McHardy } 23420157f60cSPatrick McHardy 23430b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 23440b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 23450b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 23460b815a1aSStephen Hemminger if (err < 0) 23470b815a1aSStephen Hemminger goto errout; 2348ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 23490b815a1aSStephen Hemminger } 23500b815a1aSStephen Hemminger 23510157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 23520157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2353e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 23540157f60cSPatrick McHardy } 23550157f60cSPatrick McHardy 23560157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 23573729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 23585f9021cfSJohannes Berg if (err < 0) 23595f9021cfSJohannes Berg goto errout; 23600157f60cSPatrick McHardy } 23610157f60cSPatrick McHardy 2362fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 236333eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2364fbaec0eaSJiri Pirko if (err) 2365fbaec0eaSJiri Pirko goto errout; 236690c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 2367fbaec0eaSJiri Pirko } 2368fbaec0eaSJiri Pirko 23699a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 23709a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 23719a57247fSJiri Pirko if (err) 23729a57247fSJiri Pirko goto errout; 237390c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 23749a57247fSJiri Pirko } 23759a57247fSJiri Pirko 23765d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 23770cd29503SAlexey Dobriyan unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 23785d1180fcSNicolas Dichtel 23796a643ddbSCong Wang err = dev_change_tx_queue_len(dev, value); 23806a643ddbSCong Wang if (err) 238108294a26SJason Wang goto errout; 23822d7f669bSXin Long status |= DO_SETLINK_MODIFIED; 238308294a26SJason Wang } 23840157f60cSPatrick McHardy 238546e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) { 238646e6b992SStephen Hemminger u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 238746e6b992SStephen Hemminger 238846e6b992SStephen Hemminger if (max_size > GSO_MAX_SIZE) { 238946e6b992SStephen Hemminger err = -EINVAL; 239046e6b992SStephen Hemminger goto errout; 239146e6b992SStephen Hemminger } 239246e6b992SStephen Hemminger 239346e6b992SStephen Hemminger if (dev->gso_max_size ^ max_size) { 239446e6b992SStephen Hemminger netif_set_gso_max_size(dev, max_size); 239546e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 239646e6b992SStephen Hemminger } 239746e6b992SStephen Hemminger } 239846e6b992SStephen Hemminger 239946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) { 240046e6b992SStephen Hemminger u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 240146e6b992SStephen Hemminger 240246e6b992SStephen Hemminger if (max_segs > GSO_MAX_SEGS) { 240346e6b992SStephen Hemminger err = -EINVAL; 240446e6b992SStephen Hemminger goto errout; 240546e6b992SStephen Hemminger } 240646e6b992SStephen Hemminger 240746e6b992SStephen Hemminger if (dev->gso_max_segs ^ max_segs) { 240846e6b992SStephen Hemminger dev->gso_max_segs = max_segs; 240946e6b992SStephen Hemminger status |= DO_SETLINK_MODIFIED; 241046e6b992SStephen Hemminger } 241146e6b992SStephen Hemminger } 241246e6b992SStephen Hemminger 24130157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 241493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 24150157f60cSPatrick McHardy 24160157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 24171889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 24181889b0e7SNicolas Dichtel 24190157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 24201889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 2421ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 24221889b0e7SNicolas Dichtel dev->link_mode = value; 242393b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 24240157f60cSPatrick McHardy } 24250157f60cSPatrick McHardy 2426c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 24274f7d2cdfSDaniel Borkmann struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2428c02db8c6SChris Wright struct nlattr *attr; 2429c02db8c6SChris Wright int rem; 24304f7d2cdfSDaniel Borkmann 2431c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 24324f7d2cdfSDaniel Borkmann if (nla_type(attr) != IFLA_VF_INFO || 24334f7d2cdfSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2434253683bbSDavid Howells err = -EINVAL; 2435c02db8c6SChris Wright goto errout; 2436253683bbSDavid Howells } 24374f7d2cdfSDaniel Borkmann err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2438fceb6435SJohannes Berg ifla_vf_policy, NULL); 24394f7d2cdfSDaniel Borkmann if (err < 0) 24404f7d2cdfSDaniel Borkmann goto errout; 24414f7d2cdfSDaniel Borkmann err = do_setvfinfo(dev, vfinfo); 2442ebc08a6fSWilliams, Mitch A if (err < 0) 2443ebc08a6fSWilliams, Mitch A goto errout; 2444ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2445ebc08a6fSWilliams, Mitch A } 2446ebc08a6fSWilliams, Mitch A } 24470157f60cSPatrick McHardy err = 0; 24480157f60cSPatrick McHardy 244957b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 245057b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 245157b61080SScott Feldman struct nlattr *attr; 245257b61080SScott Feldman int vf; 245357b61080SScott Feldman int rem; 245457b61080SScott Feldman 245557b61080SScott Feldman err = -EOPNOTSUPP; 245657b61080SScott Feldman if (!ops->ndo_set_vf_port) 245757b61080SScott Feldman goto errout; 245857b61080SScott Feldman 245957b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2460035d210fSDaniel Borkmann if (nla_type(attr) != IFLA_VF_PORT || 2461035d210fSDaniel Borkmann nla_len(attr) < NLA_HDRLEN) { 2462035d210fSDaniel Borkmann err = -EINVAL; 2463035d210fSDaniel Borkmann goto errout; 2464035d210fSDaniel Borkmann } 2465035d210fSDaniel Borkmann err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2466fceb6435SJohannes Berg ifla_port_policy, NULL); 246757b61080SScott Feldman if (err < 0) 246857b61080SScott Feldman goto errout; 246957b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 247057b61080SScott Feldman err = -EOPNOTSUPP; 247157b61080SScott Feldman goto errout; 247257b61080SScott Feldman } 247357b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 247457b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 247557b61080SScott Feldman if (err < 0) 247657b61080SScott Feldman goto errout; 2477ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 247857b61080SScott Feldman } 247957b61080SScott Feldman } 248057b61080SScott Feldman err = 0; 248157b61080SScott Feldman 248257b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 248357b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 248457b61080SScott Feldman 248557b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 2486fceb6435SJohannes Berg tb[IFLA_PORT_SELF], ifla_port_policy, 2487fceb6435SJohannes Berg NULL); 248857b61080SScott Feldman if (err < 0) 248957b61080SScott Feldman goto errout; 249057b61080SScott Feldman 249157b61080SScott Feldman err = -EOPNOTSUPP; 249257b61080SScott Feldman if (ops->ndo_set_vf_port) 249357b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 249457b61080SScott Feldman if (err < 0) 249557b61080SScott Feldman goto errout; 2496ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 249757b61080SScott Feldman } 2498f8ff182cSThomas Graf 2499f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 2500f8ff182cSThomas Graf struct nlattr *af; 2501f8ff182cSThomas Graf int rem; 2502f8ff182cSThomas Graf 2503f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2504f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 2505f8ff182cSThomas Graf 25065fa85a09SFlorian Westphal rcu_read_lock(); 25075fa85a09SFlorian Westphal 2508058c8d59SGustavo A. R. Silva BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2509f8ff182cSThomas Graf 2510cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 25115fa85a09SFlorian Westphal if (err < 0) { 25125fa85a09SFlorian Westphal rcu_read_unlock(); 2513f8ff182cSThomas Graf goto errout; 25145fa85a09SFlorian Westphal } 2515f8ff182cSThomas Graf 25165fa85a09SFlorian Westphal rcu_read_unlock(); 2517ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2518f8ff182cSThomas Graf } 2519f8ff182cSThomas Graf } 252057b61080SScott Feldman err = 0; 252157b61080SScott Feldman 252288d6378bSAnuradha Karuppiah if (tb[IFLA_PROTO_DOWN]) { 252388d6378bSAnuradha Karuppiah err = dev_change_proto_down(dev, 252488d6378bSAnuradha Karuppiah nla_get_u8(tb[IFLA_PROTO_DOWN])); 252588d6378bSAnuradha Karuppiah if (err) 252688d6378bSAnuradha Karuppiah goto errout; 252788d6378bSAnuradha Karuppiah status |= DO_SETLINK_NOTIFY; 252888d6378bSAnuradha Karuppiah } 252988d6378bSAnuradha Karuppiah 2530d1fdd913SBrenden Blanco if (tb[IFLA_XDP]) { 2531d1fdd913SBrenden Blanco struct nlattr *xdp[IFLA_XDP_MAX + 1]; 253285de8576SDaniel Borkmann u32 xdp_flags = 0; 2533d1fdd913SBrenden Blanco 2534d1fdd913SBrenden Blanco err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2535fceb6435SJohannes Berg ifla_xdp_policy, NULL); 2536d1fdd913SBrenden Blanco if (err < 0) 2537d1fdd913SBrenden Blanco goto errout; 2538d1fdd913SBrenden Blanco 253958038695SMartin KaFai Lau if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2540262d8625SBrenden Blanco err = -EINVAL; 2541262d8625SBrenden Blanco goto errout; 2542262d8625SBrenden Blanco } 254385de8576SDaniel Borkmann 254485de8576SDaniel Borkmann if (xdp[IFLA_XDP_FLAGS]) { 254585de8576SDaniel Borkmann xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 254685de8576SDaniel Borkmann if (xdp_flags & ~XDP_FLAGS_MASK) { 254785de8576SDaniel Borkmann err = -EINVAL; 254885de8576SDaniel Borkmann goto errout; 254985de8576SDaniel Borkmann } 2550ee5d032fSJakub Kicinski if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 25510489df9aSDaniel Borkmann err = -EINVAL; 25520489df9aSDaniel Borkmann goto errout; 25530489df9aSDaniel Borkmann } 255485de8576SDaniel Borkmann } 255585de8576SDaniel Borkmann 2556d1fdd913SBrenden Blanco if (xdp[IFLA_XDP_FD]) { 2557ddf9f970SJakub Kicinski err = dev_change_xdp_fd(dev, extack, 255885de8576SDaniel Borkmann nla_get_s32(xdp[IFLA_XDP_FD]), 255985de8576SDaniel Borkmann xdp_flags); 2560d1fdd913SBrenden Blanco if (err) 2561d1fdd913SBrenden Blanco goto errout; 2562d1fdd913SBrenden Blanco status |= DO_SETLINK_NOTIFY; 2563d1fdd913SBrenden Blanco } 2564d1fdd913SBrenden Blanco } 2565d1fdd913SBrenden Blanco 25660157f60cSPatrick McHardy errout: 2567ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 256864ff90ccSXin Long if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2569ba998906SNicolas Dichtel netdev_state_change(dev); 2570ba998906SNicolas Dichtel 2571ba998906SNicolas Dichtel if (err < 0) 2572e87cc472SJoe 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", 2573e87cc472SJoe Perches dev->name); 2574ba998906SNicolas Dichtel } 25750157f60cSPatrick McHardy 25760157f60cSPatrick McHardy return err; 25770157f60cSPatrick McHardy } 25780157f60cSPatrick McHardy 2579c21ef3e3SDavid Ahern static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2580c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 2581da5e0494SThomas Graf { 25823b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2583da5e0494SThomas Graf struct ifinfomsg *ifm; 2584da5e0494SThomas Graf struct net_device *dev; 25850157f60cSPatrick McHardy int err; 2586da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 25871da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 25881da177e4SLinus Torvalds 2589c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2590c21ef3e3SDavid Ahern extack); 2591da5e0494SThomas Graf if (err < 0) 2592da5e0494SThomas Graf goto errout; 25931da177e4SLinus Torvalds 25944ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 25954ff66caeSChristian Brauner if (err < 0) 25964ff66caeSChristian Brauner goto errout; 25974ff66caeSChristian Brauner 25985176f91eSThomas Graf if (tb[IFLA_IFNAME]) 25995176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 260078e5b891SPatrick McHardy else 260178e5b891SPatrick McHardy ifname[0] = '\0'; 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds err = -EINVAL; 2604da5e0494SThomas Graf ifm = nlmsg_data(nlh); 260551055be8SPatrick McHardy if (ifm->ifi_index > 0) 2606a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2607da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 2608a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2609da5e0494SThomas Graf else 2610da5e0494SThomas Graf goto errout; 26111da177e4SLinus Torvalds 2612da5e0494SThomas Graf if (dev == NULL) { 2613da5e0494SThomas Graf err = -ENODEV; 2614da5e0494SThomas Graf goto errout; 2615da5e0494SThomas Graf } 26161da177e4SLinus Torvalds 2617e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2618e0d087afSEric Dumazet if (err < 0) 2619a3d12891SEric Dumazet goto errout; 2620da5e0494SThomas Graf 2621ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2622da5e0494SThomas Graf errout: 26231da177e4SLinus Torvalds return err; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 262666400d54SWANG Cong static int rtnl_group_dellink(const struct net *net, int group) 262766400d54SWANG Cong { 262866400d54SWANG Cong struct net_device *dev, *aux; 262966400d54SWANG Cong LIST_HEAD(list_kill); 263066400d54SWANG Cong bool found = false; 263166400d54SWANG Cong 263266400d54SWANG Cong if (!group) 263366400d54SWANG Cong return -EPERM; 263466400d54SWANG Cong 263566400d54SWANG Cong for_each_netdev(net, dev) { 263666400d54SWANG Cong if (dev->group == group) { 263766400d54SWANG Cong const struct rtnl_link_ops *ops; 263866400d54SWANG Cong 263966400d54SWANG Cong found = true; 264066400d54SWANG Cong ops = dev->rtnl_link_ops; 264166400d54SWANG Cong if (!ops || !ops->dellink) 264266400d54SWANG Cong return -EOPNOTSUPP; 264366400d54SWANG Cong } 264466400d54SWANG Cong } 264566400d54SWANG Cong 264666400d54SWANG Cong if (!found) 264766400d54SWANG Cong return -ENODEV; 264866400d54SWANG Cong 264966400d54SWANG Cong for_each_netdev_safe(net, dev, aux) { 265066400d54SWANG Cong if (dev->group == group) { 265166400d54SWANG Cong const struct rtnl_link_ops *ops; 265266400d54SWANG Cong 265366400d54SWANG Cong ops = dev->rtnl_link_ops; 265466400d54SWANG Cong ops->dellink(dev, &list_kill); 265566400d54SWANG Cong } 265666400d54SWANG Cong } 265766400d54SWANG Cong unregister_netdevice_many(&list_kill); 265866400d54SWANG Cong 265966400d54SWANG Cong return 0; 266066400d54SWANG Cong } 266166400d54SWANG Cong 2662614732eaSThomas Graf int rtnl_delete_link(struct net_device *dev) 2663614732eaSThomas Graf { 2664614732eaSThomas Graf const struct rtnl_link_ops *ops; 2665614732eaSThomas Graf LIST_HEAD(list_kill); 2666614732eaSThomas Graf 2667614732eaSThomas Graf ops = dev->rtnl_link_ops; 2668614732eaSThomas Graf if (!ops || !ops->dellink) 2669614732eaSThomas Graf return -EOPNOTSUPP; 2670614732eaSThomas Graf 2671614732eaSThomas Graf ops->dellink(dev, &list_kill); 2672614732eaSThomas Graf unregister_netdevice_many(&list_kill); 2673614732eaSThomas Graf 2674614732eaSThomas Graf return 0; 2675614732eaSThomas Graf } 2676614732eaSThomas Graf EXPORT_SYMBOL_GPL(rtnl_delete_link); 2677614732eaSThomas Graf 2678c21ef3e3SDavid Ahern static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2679c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 268038f7b870SPatrick McHardy { 26813b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2682b61ad68aSChristian Brauner struct net *tgt_net = net; 2683b61ad68aSChristian Brauner struct net_device *dev = NULL; 268438f7b870SPatrick McHardy struct ifinfomsg *ifm; 268538f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 268638f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 268738f7b870SPatrick McHardy int err; 2688b61ad68aSChristian Brauner int netnsid = -1; 268938f7b870SPatrick McHardy 2690c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 269138f7b870SPatrick McHardy if (err < 0) 269238f7b870SPatrick McHardy return err; 269338f7b870SPatrick McHardy 26944ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 26954ff66caeSChristian Brauner if (err < 0) 26964ff66caeSChristian Brauner return err; 26974ff66caeSChristian Brauner 269838f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 269938f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 270038f7b870SPatrick McHardy 2701b61ad68aSChristian Brauner if (tb[IFLA_IF_NETNSID]) { 2702b61ad68aSChristian Brauner netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2703b61ad68aSChristian Brauner tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2704b61ad68aSChristian Brauner if (IS_ERR(tgt_net)) 2705b61ad68aSChristian Brauner return PTR_ERR(tgt_net); 2706b61ad68aSChristian Brauner } 2707b61ad68aSChristian Brauner 2708b61ad68aSChristian Brauner err = -EINVAL; 270938f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 271038f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2711b61ad68aSChristian Brauner dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 271238f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 2713b61ad68aSChristian Brauner dev = __dev_get_by_name(tgt_net, ifname); 271466400d54SWANG Cong else if (tb[IFLA_GROUP]) 2715b61ad68aSChristian Brauner err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 271638f7b870SPatrick McHardy else 2717b61ad68aSChristian Brauner goto out; 271838f7b870SPatrick McHardy 2719b61ad68aSChristian Brauner if (!dev) { 2720b61ad68aSChristian Brauner if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2721b61ad68aSChristian Brauner err = -ENODEV; 272238f7b870SPatrick McHardy 2723b61ad68aSChristian Brauner goto out; 2724b61ad68aSChristian Brauner } 2725b61ad68aSChristian Brauner 2726b61ad68aSChristian Brauner err = rtnl_delete_link(dev); 2727b61ad68aSChristian Brauner 2728b61ad68aSChristian Brauner out: 2729b61ad68aSChristian Brauner if (netnsid >= 0) 2730b61ad68aSChristian Brauner put_net(tgt_net); 2731b61ad68aSChristian Brauner 2732b61ad68aSChristian Brauner return err; 273338f7b870SPatrick McHardy } 273438f7b870SPatrick McHardy 27353729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 27363729d502SPatrick McHardy { 27373729d502SPatrick McHardy unsigned int old_flags; 27383729d502SPatrick McHardy int err; 27393729d502SPatrick McHardy 27403729d502SPatrick McHardy old_flags = dev->flags; 27413729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 27423729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 27433729d502SPatrick McHardy if (err < 0) 27443729d502SPatrick McHardy return err; 27453729d502SPatrick McHardy } 27463729d502SPatrick McHardy 27473729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 27483729d502SPatrick McHardy 2749a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 27503729d502SPatrick McHardy return 0; 27513729d502SPatrick McHardy } 27523729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 27533729d502SPatrick McHardy 2754c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 275578ebb0d0SThomas Graf const char *ifname, unsigned char name_assign_type, 27565517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2757e7199288SPavel Emelianov { 2758e7199288SPavel Emelianov struct net_device *dev; 2759d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 2760d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 2761e7199288SPavel Emelianov 276276ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 276376ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 276476ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 2765d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 276676ff5cc9SJiri Pirko 276776ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 276876ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 276976ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 2770d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 2771efacb309Sstephen hemminger 27725517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2773c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 2774e7199288SPavel Emelianov if (!dev) 2775d1892e4eSTobias Klauser return ERR_PTR(-ENOMEM); 2776e7199288SPavel Emelianov 277781adee47SEric W. Biederman dev_net_set(dev, net); 277881adee47SEric W. Biederman dev->rtnl_link_ops = ops; 27793729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 278081adee47SEric W. Biederman 2781e7199288SPavel Emelianov if (tb[IFLA_MTU]) 2782e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 27832afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 2784e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2785e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 27862afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 27872afb9b53SJiri Pirko } 2788e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 2789e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2790e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 2791e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 2792e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2793e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 279493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2795e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 2796e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2797ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 2798ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 279946e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SIZE]) 280046e6b992SStephen Hemminger netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 280146e6b992SStephen Hemminger if (tb[IFLA_GSO_MAX_SEGS]) 2802a0b586faSStephen Hemminger dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2803e7199288SPavel Emelianov 2804e7199288SPavel Emelianov return dev; 2805e7199288SPavel Emelianov } 2806e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 2807e7199288SPavel Emelianov 280890f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 280990f62cf3SEric W. Biederman struct net *net, int group, 2810e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 2811ddf9f970SJakub Kicinski struct netlink_ext_ack *extack, 2812e7ed828fSVlad Dogaru struct nlattr **tb) 2813e7ed828fSVlad Dogaru { 2814d079535dSWANG Cong struct net_device *dev, *aux; 2815e7ed828fSVlad Dogaru int err; 2816e7ed828fSVlad Dogaru 2817d079535dSWANG Cong for_each_netdev_safe(net, dev, aux) { 2818e7ed828fSVlad Dogaru if (dev->group == group) { 2819ddf9f970SJakub Kicinski err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2820e7ed828fSVlad Dogaru if (err < 0) 2821e7ed828fSVlad Dogaru return err; 2822e7ed828fSVlad Dogaru } 2823e7ed828fSVlad Dogaru } 2824e7ed828fSVlad Dogaru 2825e7ed828fSVlad Dogaru return 0; 2826e7ed828fSVlad Dogaru } 2827e7ed828fSVlad Dogaru 2828c21ef3e3SDavid Ahern static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2829c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 283038f7b870SPatrick McHardy { 28313b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 283238f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 2833ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 283438f7b870SPatrick McHardy struct net_device *dev; 2835ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 283638f7b870SPatrick McHardy struct ifinfomsg *ifm; 283738f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 283838f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 283938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 284038f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 28415517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 284238f7b870SPatrick McHardy int err; 284338f7b870SPatrick McHardy 284495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 284538f7b870SPatrick McHardy replay: 28468072f085SThomas Graf #endif 2847c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 284838f7b870SPatrick McHardy if (err < 0) 284938f7b870SPatrick McHardy return err; 285038f7b870SPatrick McHardy 28514ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, false); 28524ff66caeSChristian Brauner if (err < 0) 28534ff66caeSChristian Brauner return err; 28544ff66caeSChristian Brauner 285538f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 285638f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 285738f7b870SPatrick McHardy else 285838f7b870SPatrick McHardy ifname[0] = '\0'; 285938f7b870SPatrick McHardy 286038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 286138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 2862881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 2863e7ed828fSVlad Dogaru else { 2864e7ed828fSVlad Dogaru if (ifname[0]) 2865881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 286638f7b870SPatrick McHardy else 286738f7b870SPatrick McHardy dev = NULL; 2868e7ed828fSVlad Dogaru } 286938f7b870SPatrick McHardy 2870ba7d49b1SJiri Pirko if (dev) { 2871ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 2872ba7d49b1SJiri Pirko if (master_dev) 2873ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 2874ba7d49b1SJiri Pirko } 2875ba7d49b1SJiri Pirko 2876e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 2877e0d087afSEric Dumazet if (err < 0) 28781840bb13SThomas Graf return err; 28791840bb13SThomas Graf 288038f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 288138f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2882fceb6435SJohannes Berg tb[IFLA_LINKINFO], ifla_info_policy, 2883fceb6435SJohannes Berg NULL); 288438f7b870SPatrick McHardy if (err < 0) 288538f7b870SPatrick McHardy return err; 288638f7b870SPatrick McHardy } else 288738f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 288838f7b870SPatrick McHardy 288938f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 289038f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 289138f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 289238f7b870SPatrick McHardy } else { 289338f7b870SPatrick McHardy kind[0] = '\0'; 289438f7b870SPatrick McHardy ops = NULL; 289538f7b870SPatrick McHardy } 289638f7b870SPatrick McHardy 289738f7b870SPatrick McHardy if (1) { 28984e10fd5bSSasha Levin struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; 28994e10fd5bSSasha Levin struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; 2900ba7d49b1SJiri Pirko struct nlattr **data = NULL; 2901ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 2902317f4810SNicolas Dichtel struct net *dest_net, *link_net = NULL; 290338f7b870SPatrick McHardy 290438f7b870SPatrick McHardy if (ops) { 290538f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 290638f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 290738f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 2908fceb6435SJohannes Berg ops->policy, NULL); 290938f7b870SPatrick McHardy if (err < 0) 291038f7b870SPatrick McHardy return err; 291138f7b870SPatrick McHardy data = attr; 291238f7b870SPatrick McHardy } 291338f7b870SPatrick McHardy if (ops->validate) { 2914a8b8a889SMatthias Schiffer err = ops->validate(tb, data, extack); 291538f7b870SPatrick McHardy if (err < 0) 291638f7b870SPatrick McHardy return err; 291738f7b870SPatrick McHardy } 291838f7b870SPatrick McHardy } 291938f7b870SPatrick McHardy 2920ba7d49b1SJiri Pirko if (m_ops) { 2921ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 2922ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 2923ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 2924ba7d49b1SJiri Pirko m_ops->slave_maxtype, 2925ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 2926fceb6435SJohannes Berg m_ops->slave_policy, 2927fceb6435SJohannes Berg NULL); 2928ba7d49b1SJiri Pirko if (err < 0) 2929ba7d49b1SJiri Pirko return err; 2930ba7d49b1SJiri Pirko slave_data = slave_attr; 2931ba7d49b1SJiri Pirko } 2932ba7d49b1SJiri Pirko } 2933ba7d49b1SJiri Pirko 293438f7b870SPatrick McHardy if (dev) { 293590c325e3SNicolas Dichtel int status = 0; 293638f7b870SPatrick McHardy 293738f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 293838f7b870SPatrick McHardy return -EEXIST; 293938f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 294038f7b870SPatrick McHardy return -EOPNOTSUPP; 294138f7b870SPatrick McHardy 294238f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 294338f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 294438f7b870SPatrick McHardy !ops->changelink) 294538f7b870SPatrick McHardy return -EOPNOTSUPP; 294638f7b870SPatrick McHardy 2947ad744b22SMatthias Schiffer err = ops->changelink(dev, tb, data, extack); 294838f7b870SPatrick McHardy if (err < 0) 294938f7b870SPatrick McHardy return err; 2950ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 295138f7b870SPatrick McHardy } 295238f7b870SPatrick McHardy 2953ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2954ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 2955ba7d49b1SJiri Pirko return -EOPNOTSUPP; 2956ba7d49b1SJiri Pirko 2957ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 295817dd0ec4SMatthias Schiffer tb, slave_data, 295917dd0ec4SMatthias Schiffer extack); 2960ba7d49b1SJiri Pirko if (err < 0) 2961ba7d49b1SJiri Pirko return err; 2962ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2963ba7d49b1SJiri Pirko } 2964ba7d49b1SJiri Pirko 2965ddf9f970SJakub Kicinski return do_setlink(skb, dev, ifm, extack, tb, ifname, 2966ddf9f970SJakub Kicinski status); 296738f7b870SPatrick McHardy } 296838f7b870SPatrick McHardy 2969ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2970ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 297190f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 2972ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 2973ddf9f970SJakub Kicinski ifm, extack, tb); 297438f7b870SPatrick McHardy return -ENODEV; 2975ffa934f1SPatrick McHardy } 297638f7b870SPatrick McHardy 2977160ca014STheuns Verwoerd if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 297838f7b870SPatrick McHardy return -EOPNOTSUPP; 297938f7b870SPatrick McHardy 298038f7b870SPatrick McHardy if (!ops) { 298195a5afcaSJohannes Berg #ifdef CONFIG_MODULES 298238f7b870SPatrick McHardy if (kind[0]) { 298338f7b870SPatrick McHardy __rtnl_unlock(); 298438f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 298538f7b870SPatrick McHardy rtnl_lock(); 298638f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 298738f7b870SPatrick McHardy if (ops) 298838f7b870SPatrick McHardy goto replay; 298938f7b870SPatrick McHardy } 299038f7b870SPatrick McHardy #endif 299138f7b870SPatrick McHardy return -EOPNOTSUPP; 299238f7b870SPatrick McHardy } 299338f7b870SPatrick McHardy 2994b0ab2fabSJiri Pirko if (!ops->setup) 2995b0ab2fabSJiri Pirko return -EOPNOTSUPP; 2996b0ab2fabSJiri Pirko 29975517750fSTom Gundersen if (!ifname[0]) { 299838f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 29995517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 30005517750fSTom Gundersen } 300138f7b870SPatrick McHardy 30025bb8ed07SChristian Brauner dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 300313ad1774SEric W. Biederman if (IS_ERR(dest_net)) 300413ad1774SEric W. Biederman return PTR_ERR(dest_net); 300513ad1774SEric W. Biederman 3006317f4810SNicolas Dichtel if (tb[IFLA_LINK_NETNSID]) { 3007317f4810SNicolas Dichtel int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3008317f4810SNicolas Dichtel 3009317f4810SNicolas Dichtel link_net = get_net_ns_by_id(dest_net, id); 3010317f4810SNicolas Dichtel if (!link_net) { 3011317f4810SNicolas Dichtel err = -EINVAL; 3012317f4810SNicolas Dichtel goto out; 3013317f4810SNicolas Dichtel } 301406615bedSEric W. Biederman err = -EPERM; 301506615bedSEric W. Biederman if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 301606615bedSEric W. Biederman goto out; 3017317f4810SNicolas Dichtel } 3018317f4810SNicolas Dichtel 3019317f4810SNicolas Dichtel dev = rtnl_create_link(link_net ? : dest_net, ifname, 3020317f4810SNicolas Dichtel name_assign_type, ops, tb); 30219c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 3022e7199288SPavel Emelianov err = PTR_ERR(dev); 30239c7dafbfSPavel Emelyanov goto out; 30249c7dafbfSPavel Emelyanov } 30259c7dafbfSPavel Emelyanov 30269c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 30279c7dafbfSPavel Emelyanov 30280e0eee24SCong Wang if (ops->newlink) { 30297a3f4a18SMatthias Schiffer err = ops->newlink(link_net ? : net, dev, tb, data, 30307a3f4a18SMatthias Schiffer extack); 30310e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 3032e51fb152SCong Wang * and unregister it on failure after registration 3033e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 30340e0eee24SCong Wang */ 3035e51fb152SCong Wang if (err < 0) { 3036e51fb152SCong Wang /* If device is not registered at all, free it now */ 3037e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 3038e51fb152SCong Wang free_netdev(dev); 30390e0eee24SCong Wang goto out; 3040e51fb152SCong Wang } 30410e0eee24SCong Wang } else { 30422d85cba2SPatrick McHardy err = register_netdevice(dev); 3043fce9b9beSDan Carpenter if (err < 0) { 304438f7b870SPatrick McHardy free_netdev(dev); 30453729d502SPatrick McHardy goto out; 3046fce9b9beSDan Carpenter } 30470e0eee24SCong Wang } 30483729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 304943638900SDavid S. Miller if (err < 0) 305043638900SDavid S. Miller goto out_unregister; 305143638900SDavid S. Miller if (link_net) { 305243638900SDavid S. Miller err = dev_change_net_namespace(dev, dest_net, ifname); 305343638900SDavid S. Miller if (err < 0) 305443638900SDavid S. Miller goto out_unregister; 305543638900SDavid S. Miller } 3056160ca014STheuns Verwoerd if (tb[IFLA_MASTER]) { 305733eaf2a6SDavid Ahern err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 305833eaf2a6SDavid Ahern extack); 3059160ca014STheuns Verwoerd if (err) 3060160ca014STheuns Verwoerd goto out_unregister; 3061160ca014STheuns Verwoerd } 306243638900SDavid S. Miller out: 306343638900SDavid S. Miller if (link_net) 306443638900SDavid S. Miller put_net(link_net); 306543638900SDavid S. Miller put_net(dest_net); 306643638900SDavid S. Miller return err; 306743638900SDavid S. Miller out_unregister: 30687afb8886SWANG Cong if (ops->newlink) { 30697afb8886SWANG Cong LIST_HEAD(list_kill); 30707afb8886SWANG Cong 30717afb8886SWANG Cong ops->dellink(dev, &list_kill); 30727afb8886SWANG Cong unregister_netdevice_many(&list_kill); 30737afb8886SWANG Cong } else { 30743729d502SPatrick McHardy unregister_netdevice(dev); 30757afb8886SWANG Cong } 3076317f4810SNicolas Dichtel goto out; 3077317f4810SNicolas Dichtel } 307838f7b870SPatrick McHardy } 307938f7b870SPatrick McHardy 3080c21ef3e3SDavid Ahern static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3081c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3082711e2c33SJean Tourrilhes { 30833b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 308479e1ad14SJiri Benc struct net *tgt_net = net; 3085b60c5115SThomas Graf struct ifinfomsg *ifm; 3086a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 3087b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 3088b60c5115SThomas Graf struct net_device *dev = NULL; 3089b60c5115SThomas Graf struct sk_buff *nskb; 309079e1ad14SJiri Benc int netnsid = -1; 3091339bf98fSThomas Graf int err; 3092115c9b81SGreg Rose u32 ext_filter_mask = 0; 3093711e2c33SJean Tourrilhes 3094c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3095b60c5115SThomas Graf if (err < 0) 30969918f230SEric Sesterhenn return err; 3097b60c5115SThomas Graf 30984ff66caeSChristian Brauner err = rtnl_ensure_unique_netns(tb, extack, true); 30994ff66caeSChristian Brauner if (err < 0) 31004ff66caeSChristian Brauner return err; 31014ff66caeSChristian Brauner 310279e1ad14SJiri Benc if (tb[IFLA_IF_NETNSID]) { 310379e1ad14SJiri Benc netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3104f428fe4aSAndrei Vagin tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 310579e1ad14SJiri Benc if (IS_ERR(tgt_net)) 310679e1ad14SJiri Benc return PTR_ERR(tgt_net); 310779e1ad14SJiri Benc } 310879e1ad14SJiri Benc 3109a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 3110a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3111a3d12891SEric Dumazet 3112115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3113115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3114115c9b81SGreg Rose 311579e1ad14SJiri Benc err = -EINVAL; 3116b60c5115SThomas Graf ifm = nlmsg_data(nlh); 3117a3d12891SEric Dumazet if (ifm->ifi_index > 0) 311879e1ad14SJiri Benc dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3119a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 312079e1ad14SJiri Benc dev = __dev_get_by_name(tgt_net, ifname); 3121a3d12891SEric Dumazet else 312279e1ad14SJiri Benc goto out; 3123b60c5115SThomas Graf 312479e1ad14SJiri Benc err = -ENODEV; 3125a3d12891SEric Dumazet if (dev == NULL) 312679e1ad14SJiri Benc goto out; 3127a3d12891SEric Dumazet 312879e1ad14SJiri Benc err = -ENOBUFS; 3129115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3130a3d12891SEric Dumazet if (nskb == NULL) 313179e1ad14SJiri Benc goto out; 3132711e2c33SJean Tourrilhes 313379e1ad14SJiri Benc err = rtnl_fill_ifinfo(nskb, dev, net, 313479e1ad14SJiri Benc RTM_NEWLINK, NETLINK_CB(skb).portid, 313579e1ad14SJiri Benc nlh->nlmsg_seq, 0, 0, ext_filter_mask, 313638e01b30SNicolas Dichtel 0, NULL, 0, netnsid); 313726932566SPatrick McHardy if (err < 0) { 313826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 313926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 314026932566SPatrick McHardy kfree_skb(nskb); 3141a3d12891SEric Dumazet } else 314215e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 314379e1ad14SJiri Benc out: 314479e1ad14SJiri Benc if (netnsid >= 0) 314579e1ad14SJiri Benc put_net(tgt_net); 3146b60c5115SThomas Graf 3147711e2c33SJean Tourrilhes return err; 3148711e2c33SJean Tourrilhes } 3149711e2c33SJean Tourrilhes 3150115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3151c7ac8679SGreg Rose { 3152115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 3153115c9b81SGreg Rose struct net_device *dev; 3154115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 3155115c9b81SGreg Rose u32 ext_filter_mask = 0; 3156115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 3157e5eca6d4SMichal Schmidt int hdrlen; 3158115c9b81SGreg Rose 3159e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3160e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3161e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3162e5eca6d4SMichal Schmidt 3163fceb6435SJohannes Berg if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3164115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 3165115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3166a4b64fbeSEric Dumazet } 3167115c9b81SGreg Rose 3168115c9b81SGreg Rose if (!ext_filter_mask) 3169115c9b81SGreg Rose return NLMSG_GOODSIZE; 3170115c9b81SGreg Rose /* 3171115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 3172115c9b81SGreg Rose * buffer size based upon the filter mask. 3173115c9b81SGreg Rose */ 31746853dd48SFlorian Westphal rcu_read_lock(); 31756853dd48SFlorian Westphal for_each_netdev_rcu(net, dev) { 3176115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3177115c9b81SGreg Rose if_nlmsg_size(dev, 3178115c9b81SGreg Rose ext_filter_mask)); 3179115c9b81SGreg Rose } 31806853dd48SFlorian Westphal rcu_read_unlock(); 3181115c9b81SGreg Rose 318293af2056SZhang Shengju return nlmsg_total_size(min_ifinfo_dump_size); 3183c7ac8679SGreg Rose } 3184c7ac8679SGreg Rose 318542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 31861da177e4SLinus Torvalds { 31871da177e4SLinus Torvalds int idx; 31881da177e4SLinus Torvalds int s_idx = cb->family; 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds if (s_idx == 0) 31911da177e4SLinus Torvalds s_idx = 1; 31926853dd48SFlorian Westphal 319325239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3194addf9b90SFlorian Westphal struct rtnl_link **tab; 31951da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 3196addf9b90SFlorian Westphal struct rtnl_link *link; 31976853dd48SFlorian Westphal rtnl_dumpit_func dumpit; 31986853dd48SFlorian Westphal 31991da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 32001da177e4SLinus Torvalds continue; 32016853dd48SFlorian Westphal 3202addf9b90SFlorian Westphal if (type < 0 || type >= RTM_NR_MSGTYPES) 32031da177e4SLinus Torvalds continue; 32046853dd48SFlorian Westphal 3205addf9b90SFlorian Westphal tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3206addf9b90SFlorian Westphal if (!tab) 3207addf9b90SFlorian Westphal continue; 3208addf9b90SFlorian Westphal 3209addf9b90SFlorian Westphal link = tab[type]; 3210addf9b90SFlorian Westphal if (!link) 3211addf9b90SFlorian Westphal continue; 3212addf9b90SFlorian Westphal 3213addf9b90SFlorian Westphal dumpit = link->dumpit; 32146853dd48SFlorian Westphal if (!dumpit) 32156853dd48SFlorian Westphal continue; 32166853dd48SFlorian Westphal 32170465277fSNicolas Dichtel if (idx > s_idx) { 32181da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 32190465277fSNicolas Dichtel cb->prev_seq = 0; 32200465277fSNicolas Dichtel cb->seq = 0; 32210465277fSNicolas Dichtel } 32226853dd48SFlorian Westphal if (dumpit(skb, cb)) 32231da177e4SLinus Torvalds break; 32241da177e4SLinus Torvalds } 32251da177e4SLinus Torvalds cb->family = idx; 32261da177e4SLinus Torvalds 32271da177e4SLinus Torvalds return skb->len; 32281da177e4SLinus Torvalds } 32291da177e4SLinus Torvalds 3230395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 32313d3ea5afSVlad Yasevich unsigned int change, 323238e01b30SNicolas Dichtel u32 event, gfp_t flags, int *new_nsid, 323338e01b30SNicolas Dichtel int new_ifindex) 32341da177e4SLinus Torvalds { 3235c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 32361da177e4SLinus Torvalds struct sk_buff *skb; 32370ec6d3f4SThomas Graf int err = -ENOBUFS; 3238c7ac8679SGreg Rose size_t if_info_size; 32391da177e4SLinus Torvalds 32407f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 32410ec6d3f4SThomas Graf if (skb == NULL) 32420ec6d3f4SThomas Graf goto errout; 32431da177e4SLinus Torvalds 324479e1ad14SJiri Benc err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 324579e1ad14SJiri Benc type, 0, 0, change, 0, 0, event, 324638e01b30SNicolas Dichtel new_nsid, new_ifindex, -1); 324726932566SPatrick McHardy if (err < 0) { 324826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 324926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 325026932566SPatrick McHardy kfree_skb(skb); 325126932566SPatrick McHardy goto errout; 325226932566SPatrick McHardy } 3253395eea6cSMahesh Bandewar return skb; 32540ec6d3f4SThomas Graf errout: 32550ec6d3f4SThomas Graf if (err < 0) 32564b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3257395eea6cSMahesh Bandewar return NULL; 3258395eea6cSMahesh Bandewar } 3259395eea6cSMahesh Bandewar 3260395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3261395eea6cSMahesh Bandewar { 3262395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 3263395eea6cSMahesh Bandewar 3264395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3265395eea6cSMahesh Bandewar } 3266395eea6cSMahesh Bandewar 32673d3ea5afSVlad Yasevich static void rtmsg_ifinfo_event(int type, struct net_device *dev, 32683d3ea5afSVlad Yasevich unsigned int change, u32 event, 326938e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 3270395eea6cSMahesh Bandewar { 3271395eea6cSMahesh Bandewar struct sk_buff *skb; 3272395eea6cSMahesh Bandewar 3273ed2a80abSNicolas Dichtel if (dev->reg_state != NETREG_REGISTERED) 3274ed2a80abSNicolas Dichtel return; 3275ed2a80abSNicolas Dichtel 327638e01b30SNicolas Dichtel skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 327738e01b30SNicolas Dichtel new_ifindex); 3278395eea6cSMahesh Bandewar if (skb) 3279395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 32801da177e4SLinus Torvalds } 32813d3ea5afSVlad Yasevich 32823d3ea5afSVlad Yasevich void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 32833d3ea5afSVlad Yasevich gfp_t flags) 32843d3ea5afSVlad Yasevich { 328538e01b30SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 328638e01b30SNicolas Dichtel NULL, 0); 32873d3ea5afSVlad Yasevich } 32881da177e4SLinus Torvalds 32896621dd29SNicolas Dichtel void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 329038e01b30SNicolas Dichtel gfp_t flags, int *new_nsid, int new_ifindex) 32916621dd29SNicolas Dichtel { 32926621dd29SNicolas Dichtel rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 329338e01b30SNicolas Dichtel new_nsid, new_ifindex); 32946621dd29SNicolas Dichtel } 32956621dd29SNicolas Dichtel 3296d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3297d83b0603SJohn Fastabend struct net_device *dev, 32981e53d5bbSHubert Sokolowski u8 *addr, u16 vid, u32 pid, u32 seq, 32991c104a6bSNicolas Dichtel int type, unsigned int flags, 3300b3379041SHubert Sokolowski int nlflags, u16 ndm_state) 3301d83b0603SJohn Fastabend { 3302d83b0603SJohn Fastabend struct nlmsghdr *nlh; 3303d83b0603SJohn Fastabend struct ndmsg *ndm; 3304d83b0603SJohn Fastabend 33051c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3306d83b0603SJohn Fastabend if (!nlh) 3307d83b0603SJohn Fastabend return -EMSGSIZE; 3308d83b0603SJohn Fastabend 3309d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 3310d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 3311d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 3312d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 3313d83b0603SJohn Fastabend ndm->ndm_flags = flags; 3314d83b0603SJohn Fastabend ndm->ndm_type = 0; 3315d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 3316b3379041SHubert Sokolowski ndm->ndm_state = ndm_state; 3317d83b0603SJohn Fastabend 3318d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3319d83b0603SJohn Fastabend goto nla_put_failure; 33201e53d5bbSHubert Sokolowski if (vid) 33211e53d5bbSHubert Sokolowski if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 33221e53d5bbSHubert Sokolowski goto nla_put_failure; 3323d83b0603SJohn Fastabend 3324053c095aSJohannes Berg nlmsg_end(skb, nlh); 3325053c095aSJohannes Berg return 0; 3326d83b0603SJohn Fastabend 3327d83b0603SJohn Fastabend nla_put_failure: 3328d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 3329d83b0603SJohn Fastabend return -EMSGSIZE; 3330d83b0603SJohn Fastabend } 3331d83b0603SJohn Fastabend 33323ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 33333ff661c3SJohn Fastabend { 3334f82ef3e1SSabrina Dubroca return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3335f82ef3e1SSabrina Dubroca nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3336f82ef3e1SSabrina Dubroca nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3337f82ef3e1SSabrina Dubroca 0; 33383ff661c3SJohn Fastabend } 33393ff661c3SJohn Fastabend 3340b3379041SHubert Sokolowski static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3341b3379041SHubert Sokolowski u16 ndm_state) 33423ff661c3SJohn Fastabend { 33433ff661c3SJohn Fastabend struct net *net = dev_net(dev); 33443ff661c3SJohn Fastabend struct sk_buff *skb; 33453ff661c3SJohn Fastabend int err = -ENOBUFS; 33463ff661c3SJohn Fastabend 33473ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 33483ff661c3SJohn Fastabend if (!skb) 33493ff661c3SJohn Fastabend goto errout; 33503ff661c3SJohn Fastabend 33511e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3352b3379041SHubert Sokolowski 0, 0, type, NTF_SELF, 0, ndm_state); 33533ff661c3SJohn Fastabend if (err < 0) { 33543ff661c3SJohn Fastabend kfree_skb(skb); 33553ff661c3SJohn Fastabend goto errout; 33563ff661c3SJohn Fastabend } 33573ff661c3SJohn Fastabend 33583ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 33593ff661c3SJohn Fastabend return; 33603ff661c3SJohn Fastabend errout: 33613ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 33623ff661c3SJohn Fastabend } 33633ff661c3SJohn Fastabend 3364090096bfSVlad Yasevich /** 3365090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3366090096bfSVlad Yasevich */ 3367090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 3368090096bfSVlad Yasevich struct nlattr *tb[], 3369090096bfSVlad Yasevich struct net_device *dev, 3370f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 3371090096bfSVlad Yasevich u16 flags) 3372090096bfSVlad Yasevich { 3373090096bfSVlad Yasevich int err = -EINVAL; 3374090096bfSVlad Yasevich 3375090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3376090096bfSVlad Yasevich * implement its own handler for this. 3377090096bfSVlad Yasevich */ 3378090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3379090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3380090096bfSVlad Yasevich return err; 3381090096bfSVlad Yasevich } 3382090096bfSVlad Yasevich 338365891feaSOr Gerlitz if (vid) { 338465891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 338565891feaSOr Gerlitz return err; 338665891feaSOr Gerlitz } 338765891feaSOr Gerlitz 3388090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3389090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 3390090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3391090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 3392090096bfSVlad Yasevich 3393090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 3394090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3395090096bfSVlad Yasevich err = 0; 3396090096bfSVlad Yasevich 3397090096bfSVlad Yasevich return err; 3398090096bfSVlad Yasevich } 3399090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 3400090096bfSVlad Yasevich 3401b88d12e4SFlorian Westphal static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3402b88d12e4SFlorian Westphal struct netlink_ext_ack *extack) 3403f6f6424bSJiri Pirko { 3404f6f6424bSJiri Pirko u16 vid = 0; 3405f6f6424bSJiri Pirko 3406f6f6424bSJiri Pirko if (vlan_attr) { 3407f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 3408b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3409f6f6424bSJiri Pirko return -EINVAL; 3410f6f6424bSJiri Pirko } 3411f6f6424bSJiri Pirko 3412f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 3413f6f6424bSJiri Pirko 3414f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 3415b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid vlan id"); 3416f6f6424bSJiri Pirko return -EINVAL; 3417f6f6424bSJiri Pirko } 3418f6f6424bSJiri Pirko } 3419f6f6424bSJiri Pirko *p_vid = vid; 3420f6f6424bSJiri Pirko return 0; 3421f6f6424bSJiri Pirko } 3422f6f6424bSJiri Pirko 3423c21ef3e3SDavid Ahern static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3424c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 342577162022SJohn Fastabend { 342677162022SJohn Fastabend struct net *net = sock_net(skb->sk); 342777162022SJohn Fastabend struct ndmsg *ndm; 342877162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 342977162022SJohn Fastabend struct net_device *dev; 343077162022SJohn Fastabend u8 *addr; 3431f6f6424bSJiri Pirko u16 vid; 343277162022SJohn Fastabend int err; 343377162022SJohn Fastabend 3434c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 343577162022SJohn Fastabend if (err < 0) 343677162022SJohn Fastabend return err; 343777162022SJohn Fastabend 343877162022SJohn Fastabend ndm = nlmsg_data(nlh); 343977162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3440b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 344177162022SJohn Fastabend return -EINVAL; 344277162022SJohn Fastabend } 344377162022SJohn Fastabend 344477162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 344577162022SJohn Fastabend if (dev == NULL) { 3446b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 344777162022SJohn Fastabend return -ENODEV; 344877162022SJohn Fastabend } 344977162022SJohn Fastabend 345077162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3451b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 345277162022SJohn Fastabend return -EINVAL; 345377162022SJohn Fastabend } 345477162022SJohn Fastabend 345577162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 345677162022SJohn Fastabend 3457b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3458f6f6424bSJiri Pirko if (err) 3459f6f6424bSJiri Pirko return err; 3460f6f6424bSJiri Pirko 346177162022SJohn Fastabend err = -EOPNOTSUPP; 346277162022SJohn Fastabend 346377162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 346477162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 346577162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3466898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3467898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 3468898e5061SJiri Pirko 3469f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3470f6f6424bSJiri Pirko nlh->nlmsg_flags); 347177162022SJohn Fastabend if (err) 347277162022SJohn Fastabend goto out; 347377162022SJohn Fastabend else 347477162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 347577162022SJohn Fastabend } 347677162022SJohn Fastabend 347777162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3478090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 3479090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 3480090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3481f6f6424bSJiri Pirko vid, 3482090096bfSVlad Yasevich nlh->nlmsg_flags); 3483090096bfSVlad Yasevich else 3484f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 348577162022SJohn Fastabend nlh->nlmsg_flags); 348677162022SJohn Fastabend 34873ff661c3SJohn Fastabend if (!err) { 3488b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3489b3379041SHubert Sokolowski ndm->ndm_state); 349077162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 349177162022SJohn Fastabend } 34923ff661c3SJohn Fastabend } 349377162022SJohn Fastabend out: 349477162022SJohn Fastabend return err; 349577162022SJohn Fastabend } 349677162022SJohn Fastabend 3497090096bfSVlad Yasevich /** 3498090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3499090096bfSVlad Yasevich */ 3500090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 3501090096bfSVlad Yasevich struct nlattr *tb[], 3502090096bfSVlad Yasevich struct net_device *dev, 3503f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 3504090096bfSVlad Yasevich { 3505c8a89c4aSAlexander Duyck int err = -EINVAL; 3506090096bfSVlad Yasevich 3507090096bfSVlad Yasevich /* If aging addresses are supported device will need to 3508090096bfSVlad Yasevich * implement its own handler for this. 3509090096bfSVlad Yasevich */ 351064535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 3511090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 3512c8a89c4aSAlexander Duyck return err; 3513090096bfSVlad Yasevich } 3514090096bfSVlad Yasevich 3515090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3516090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 3517090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 3518090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 3519090096bfSVlad Yasevich 3520090096bfSVlad Yasevich return err; 3521090096bfSVlad Yasevich } 3522090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 3523090096bfSVlad Yasevich 3524c21ef3e3SDavid Ahern static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3525c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 352677162022SJohn Fastabend { 352777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 352877162022SJohn Fastabend struct ndmsg *ndm; 35291690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 353077162022SJohn Fastabend struct net_device *dev; 353177162022SJohn Fastabend int err = -EINVAL; 353277162022SJohn Fastabend __u8 *addr; 3533f6f6424bSJiri Pirko u16 vid; 353477162022SJohn Fastabend 353590f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 35361690be63SVlad Yasevich return -EPERM; 35371690be63SVlad Yasevich 3538c21ef3e3SDavid Ahern err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 35391690be63SVlad Yasevich if (err < 0) 35401690be63SVlad Yasevich return err; 354177162022SJohn Fastabend 354277162022SJohn Fastabend ndm = nlmsg_data(nlh); 354377162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 3544b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid ifindex"); 354577162022SJohn Fastabend return -EINVAL; 354677162022SJohn Fastabend } 354777162022SJohn Fastabend 354877162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 354977162022SJohn Fastabend if (dev == NULL) { 3550b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 355177162022SJohn Fastabend return -ENODEV; 355277162022SJohn Fastabend } 355377162022SJohn Fastabend 35541690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3555b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "invalid address"); 355677162022SJohn Fastabend return -EINVAL; 355777162022SJohn Fastabend } 355877162022SJohn Fastabend 35591690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 35601690be63SVlad Yasevich 3561b88d12e4SFlorian Westphal err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3562f6f6424bSJiri Pirko if (err) 3563f6f6424bSJiri Pirko return err; 3564f6f6424bSJiri Pirko 356577162022SJohn Fastabend err = -EOPNOTSUPP; 356677162022SJohn Fastabend 356777162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 356877162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 356977162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 3570898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3571898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 357277162022SJohn Fastabend 3573898e5061SJiri Pirko if (ops->ndo_fdb_del) 3574f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 357577162022SJohn Fastabend 357677162022SJohn Fastabend if (err) 357777162022SJohn Fastabend goto out; 357877162022SJohn Fastabend else 357977162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 358077162022SJohn Fastabend } 358177162022SJohn Fastabend 358277162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 3583090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 3584090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 3585f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3586f6f6424bSJiri Pirko vid); 3587090096bfSVlad Yasevich else 3588f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 358977162022SJohn Fastabend 35903ff661c3SJohn Fastabend if (!err) { 3591b3379041SHubert Sokolowski rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3592b3379041SHubert Sokolowski ndm->ndm_state); 359377162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 359477162022SJohn Fastabend } 35953ff661c3SJohn Fastabend } 359677162022SJohn Fastabend out: 359777162022SJohn Fastabend return err; 359877162022SJohn Fastabend } 359977162022SJohn Fastabend 3600d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 3601d83b0603SJohn Fastabend struct netlink_callback *cb, 3602d83b0603SJohn Fastabend struct net_device *dev, 3603d83b0603SJohn Fastabend int *idx, 3604d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 3605d83b0603SJohn Fastabend { 3606d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 3607d83b0603SJohn Fastabend int err; 360815e47304SEric W. Biederman u32 portid, seq; 3609d83b0603SJohn Fastabend 361015e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 3611d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 3612d83b0603SJohn Fastabend 3613d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 3614d297653dSRoopa Prabhu if (*idx < cb->args[2]) 3615d83b0603SJohn Fastabend goto skip; 3616d83b0603SJohn Fastabend 36171e53d5bbSHubert Sokolowski err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3618a7a558feSJohn Fastabend portid, seq, 36191c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 3620b3379041SHubert Sokolowski NLM_F_MULTI, NUD_PERMANENT); 3621d83b0603SJohn Fastabend if (err < 0) 3622d83b0603SJohn Fastabend return err; 3623d83b0603SJohn Fastabend skip: 3624d83b0603SJohn Fastabend *idx += 1; 3625d83b0603SJohn Fastabend } 3626d83b0603SJohn Fastabend return 0; 3627d83b0603SJohn Fastabend } 3628d83b0603SJohn Fastabend 3629d83b0603SJohn Fastabend /** 36302c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3631d83b0603SJohn Fastabend * @nlh: netlink message header 3632d83b0603SJohn Fastabend * @dev: netdevice 3633d83b0603SJohn Fastabend * 3634d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 363591f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 3636d83b0603SJohn Fastabend */ 3637d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 3638d83b0603SJohn Fastabend struct netlink_callback *cb, 3639d83b0603SJohn Fastabend struct net_device *dev, 36405d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 3641d297653dSRoopa Prabhu int *idx) 3642d83b0603SJohn Fastabend { 3643d83b0603SJohn Fastabend int err; 3644d83b0603SJohn Fastabend 3645d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 3646d297653dSRoopa Prabhu err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3647d83b0603SJohn Fastabend if (err) 3648d83b0603SJohn Fastabend goto out; 36492934c9dbSZhang Shengju err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3650d83b0603SJohn Fastabend out: 3651d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 3652d297653dSRoopa Prabhu return err; 3653d83b0603SJohn Fastabend } 3654d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3655d83b0603SJohn Fastabend 365677162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 365777162022SJohn Fastabend { 365877162022SJohn Fastabend struct net_device *dev; 36595e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 36605e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 36615e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 36625e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 36635e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 36645e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 3665d297653dSRoopa Prabhu struct hlist_head *head; 36665e6d2435SJamal Hadi Salim int brport_idx = 0; 36675e6d2435SJamal Hadi Salim int br_idx = 0; 3668d297653dSRoopa Prabhu int h, s_h; 3669d297653dSRoopa Prabhu int idx = 0, s_idx; 3670d297653dSRoopa Prabhu int err = 0; 3671d297653dSRoopa Prabhu int fidx = 0; 367277162022SJohn Fastabend 36730ff50e83SAlexander Potapenko err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 36740ff50e83SAlexander Potapenko IFLA_MAX, ifla_policy, NULL); 36750ff50e83SAlexander Potapenko if (err < 0) { 36760ff50e83SAlexander Potapenko return -EINVAL; 36770ff50e83SAlexander Potapenko } else if (err == 0) { 36785e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 36795e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 36805e6d2435SJamal Hadi Salim } 368177162022SJohn Fastabend 36825e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 36835e6d2435SJamal Hadi Salim 36845e6d2435SJamal Hadi Salim if (br_idx) { 36855e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 36865e6d2435SJamal Hadi Salim if (!br_dev) 36875e6d2435SJamal Hadi Salim return -ENODEV; 36885e6d2435SJamal Hadi Salim 3689898e5061SJiri Pirko ops = br_dev->netdev_ops; 36905e6d2435SJamal Hadi Salim } 36915e6d2435SJamal Hadi Salim 3692d297653dSRoopa Prabhu s_h = cb->args[0]; 3693d297653dSRoopa Prabhu s_idx = cb->args[1]; 3694d297653dSRoopa Prabhu 3695d297653dSRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3696d297653dSRoopa Prabhu idx = 0; 3697d297653dSRoopa Prabhu head = &net->dev_index_head[h]; 3698d297653dSRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 3699d297653dSRoopa Prabhu 37005e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 37015e6d2435SJamal Hadi Salim continue; 37025e6d2435SJamal Hadi Salim 37035e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 37045e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 37055e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 37065e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 37075e6d2435SJamal Hadi Salim } 37085e6d2435SJamal Hadi Salim } else { 37095e6d2435SJamal Hadi Salim if (dev != br_dev && 37105e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 37115e6d2435SJamal Hadi Salim continue; 37125e6d2435SJamal Hadi Salim 37135e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 37145e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 37155e6d2435SJamal Hadi Salim continue; 37165e6d2435SJamal Hadi Salim cops = ops; 37175e6d2435SJamal Hadi Salim } 37185e6d2435SJamal Hadi Salim 3719d297653dSRoopa Prabhu if (idx < s_idx) 3720d297653dSRoopa Prabhu goto cont; 3721d297653dSRoopa Prabhu 37225e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 3723d297653dSRoopa Prabhu if (cops && cops->ndo_fdb_dump) { 3724d297653dSRoopa Prabhu err = cops->ndo_fdb_dump(skb, cb, 3725d297653dSRoopa Prabhu br_dev, dev, 3726d297653dSRoopa Prabhu &fidx); 3727d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3728d297653dSRoopa Prabhu goto out; 372977162022SJohn Fastabend } 3730d297653dSRoopa Prabhu } 373177162022SJohn Fastabend 37325e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 3733d297653dSRoopa Prabhu err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3734d297653dSRoopa Prabhu dev, NULL, 3735d297653dSRoopa Prabhu &fidx); 37366cb69742SHubert Sokolowski else 3737d297653dSRoopa Prabhu err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3738d297653dSRoopa Prabhu &fidx); 3739d297653dSRoopa Prabhu if (err == -EMSGSIZE) 3740d297653dSRoopa Prabhu goto out; 37415e6d2435SJamal Hadi Salim 37425e6d2435SJamal Hadi Salim cops = NULL; 3743d297653dSRoopa Prabhu 3744d297653dSRoopa Prabhu /* reset fdb offset to 0 for rest of the interfaces */ 3745d297653dSRoopa Prabhu cb->args[2] = 0; 3746d297653dSRoopa Prabhu fidx = 0; 3747d297653dSRoopa Prabhu cont: 3748d297653dSRoopa Prabhu idx++; 3749d297653dSRoopa Prabhu } 375077162022SJohn Fastabend } 375177162022SJohn Fastabend 3752d297653dSRoopa Prabhu out: 3753d297653dSRoopa Prabhu cb->args[0] = h; 3754d297653dSRoopa Prabhu cb->args[1] = idx; 3755d297653dSRoopa Prabhu cb->args[2] = fidx; 3756d297653dSRoopa Prabhu 375777162022SJohn Fastabend return skb->len; 375877162022SJohn Fastabend } 375977162022SJohn Fastabend 37602c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 37612c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 37622c3c031cSScott Feldman { 37632c3c031cSScott Feldman if (mask & flag) 37642c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 37652c3c031cSScott Feldman return 0; 37662c3c031cSScott Feldman } 37672c3c031cSScott Feldman 3768815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 37692c3c031cSScott Feldman struct net_device *dev, u16 mode, 37707d4f8d87SScott Feldman u32 flags, u32 mask, int nlflags, 37717d4f8d87SScott Feldman u32 filter_mask, 37727d4f8d87SScott Feldman int (*vlan_fill)(struct sk_buff *skb, 37737d4f8d87SScott Feldman struct net_device *dev, 37747d4f8d87SScott Feldman u32 filter_mask)) 3775815cccbfSJohn Fastabend { 3776815cccbfSJohn Fastabend struct nlmsghdr *nlh; 3777815cccbfSJohn Fastabend struct ifinfomsg *ifm; 3778815cccbfSJohn Fastabend struct nlattr *br_afspec; 37792c3c031cSScott Feldman struct nlattr *protinfo; 3780815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3781898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 37827d4f8d87SScott Feldman int err = 0; 3783815cccbfSJohn Fastabend 378446c264daSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3785815cccbfSJohn Fastabend if (nlh == NULL) 3786815cccbfSJohn Fastabend return -EMSGSIZE; 3787815cccbfSJohn Fastabend 3788815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 3789815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 3790815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 3791815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 3792815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 3793815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 3794815cccbfSJohn Fastabend ifm->ifi_change = 0; 3795815cccbfSJohn Fastabend 3796815cccbfSJohn Fastabend 3797815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3798815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3799815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3800898e5061SJiri Pirko (br_dev && 3801898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3802815cccbfSJohn Fastabend (dev->addr_len && 3803815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3804a54acb3aSNicolas Dichtel (dev->ifindex != dev_get_iflink(dev) && 3805a54acb3aSNicolas Dichtel nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3806815cccbfSJohn Fastabend goto nla_put_failure; 3807815cccbfSJohn Fastabend 3808815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3809815cccbfSJohn Fastabend if (!br_afspec) 3810815cccbfSJohn Fastabend goto nla_put_failure; 3811815cccbfSJohn Fastabend 38121d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3813815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 3814815cccbfSJohn Fastabend goto nla_put_failure; 3815815cccbfSJohn Fastabend } 38161d460b98SRoopa Prabhu 38171d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 38181d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 38191d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 38201d460b98SRoopa Prabhu goto nla_put_failure; 38211d460b98SRoopa Prabhu } 38221d460b98SRoopa Prabhu } 38237d4f8d87SScott Feldman if (vlan_fill) { 38247d4f8d87SScott Feldman err = vlan_fill(skb, dev, filter_mask); 38257d4f8d87SScott Feldman if (err) { 38267d4f8d87SScott Feldman nla_nest_cancel(skb, br_afspec); 38277d4f8d87SScott Feldman goto nla_put_failure; 38287d4f8d87SScott Feldman } 38297d4f8d87SScott Feldman } 3830815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 3831815cccbfSJohn Fastabend 38322c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 38332c3c031cSScott Feldman if (!protinfo) 38342c3c031cSScott Feldman goto nla_put_failure; 38352c3c031cSScott Feldman 38362c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 38372c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 38382c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38392c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 38402c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38412c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 38422c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 38432c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38442c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 38452c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38462c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 38472c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38482c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 38492c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38502c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 38512c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 38522c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 38532c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 38542c3c031cSScott Feldman goto nla_put_failure; 38552c3c031cSScott Feldman } 38562c3c031cSScott Feldman 38572c3c031cSScott Feldman nla_nest_end(skb, protinfo); 38582c3c031cSScott Feldman 3859053c095aSJohannes Berg nlmsg_end(skb, nlh); 3860053c095aSJohannes Berg return 0; 3861815cccbfSJohn Fastabend nla_put_failure: 3862815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 38637d4f8d87SScott Feldman return err ? err : -EMSGSIZE; 3864815cccbfSJohn Fastabend } 38657d4f8d87SScott Feldman EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3866815cccbfSJohn Fastabend 3867e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3868e5a55a89SJohn Fastabend { 3869e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3870e5a55a89SJohn Fastabend struct net_device *dev; 3871e5a55a89SJohn Fastabend int idx = 0; 3872e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 3873e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 38746cbdceebSVlad Yasevich u32 filter_mask = 0; 3875d64f69b0SRoopa Prabhu int err; 38766cbdceebSVlad Yasevich 3877aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3878aa68c20fSThomas Graf struct nlattr *extfilt; 3879aa68c20fSThomas Graf 38803e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 38816cbdceebSVlad Yasevich IFLA_EXT_MASK); 3882aa68c20fSThomas Graf if (extfilt) { 3883aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 3884aa68c20fSThomas Graf return -EINVAL; 3885aa68c20fSThomas Graf 38866cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 3887aa68c20fSThomas Graf } 3888aa68c20fSThomas Graf } 3889e5a55a89SJohn Fastabend 3890e5a55a89SJohn Fastabend rcu_read_lock(); 3891e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 3892e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 3893898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3894e5a55a89SJohn Fastabend 3895898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3896d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3897d64f69b0SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_getlink( 3898d64f69b0SRoopa Prabhu skb, portid, seq, dev, 3899d64f69b0SRoopa Prabhu filter_mask, NLM_F_MULTI); 3900f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3901f6c5775fSDavid Ahern if (likely(skb->len)) 3902e5a55a89SJohn Fastabend break; 3903f6c5775fSDavid Ahern 3904f6c5775fSDavid Ahern goto out_err; 3905f6c5775fSDavid Ahern } 3906d64f69b0SRoopa Prabhu } 3907e5a55a89SJohn Fastabend idx++; 3908e5a55a89SJohn Fastabend } 3909e5a55a89SJohn Fastabend 3910e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 3911d64f69b0SRoopa Prabhu if (idx >= cb->args[0]) { 3912d64f69b0SRoopa Prabhu err = ops->ndo_bridge_getlink(skb, portid, 3913d64f69b0SRoopa Prabhu seq, dev, 391446c264daSNicolas Dichtel filter_mask, 3915d64f69b0SRoopa Prabhu NLM_F_MULTI); 3916f6c5775fSDavid Ahern if (err < 0 && err != -EOPNOTSUPP) { 3917f6c5775fSDavid Ahern if (likely(skb->len)) 3918e5a55a89SJohn Fastabend break; 3919f6c5775fSDavid Ahern 3920f6c5775fSDavid Ahern goto out_err; 3921f6c5775fSDavid Ahern } 3922d64f69b0SRoopa Prabhu } 3923e5a55a89SJohn Fastabend idx++; 3924e5a55a89SJohn Fastabend } 3925e5a55a89SJohn Fastabend } 3926f6c5775fSDavid Ahern err = skb->len; 3927f6c5775fSDavid Ahern out_err: 3928e5a55a89SJohn Fastabend rcu_read_unlock(); 3929e5a55a89SJohn Fastabend cb->args[0] = idx; 3930e5a55a89SJohn Fastabend 3931f6c5775fSDavid Ahern return err; 3932e5a55a89SJohn Fastabend } 3933e5a55a89SJohn Fastabend 39342469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 39352469ffd7SJohn Fastabend { 39362469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 39372469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 39382469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 39392469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 39402469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 39412469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 39422469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 39432469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 39442469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 39452469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 39462469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 39472469ffd7SJohn Fastabend } 39482469ffd7SJohn Fastabend 394902dba438SRoopa Prabhu static int rtnl_bridge_notify(struct net_device *dev) 39502469ffd7SJohn Fastabend { 39512469ffd7SJohn Fastabend struct net *net = dev_net(dev); 39522469ffd7SJohn Fastabend struct sk_buff *skb; 39532469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 39542469ffd7SJohn Fastabend 395502dba438SRoopa Prabhu if (!dev->netdev_ops->ndo_bridge_getlink) 395602dba438SRoopa Prabhu return 0; 395702dba438SRoopa Prabhu 39582469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 39592469ffd7SJohn Fastabend if (!skb) { 39602469ffd7SJohn Fastabend err = -ENOMEM; 39612469ffd7SJohn Fastabend goto errout; 39622469ffd7SJohn Fastabend } 39632469ffd7SJohn Fastabend 396446c264daSNicolas Dichtel err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 3965c38e01b8SJohn Fastabend if (err < 0) 3966c38e01b8SJohn Fastabend goto errout; 39672469ffd7SJohn Fastabend 396859ccaaaaSRoopa Prabhu if (!skb->len) 396959ccaaaaSRoopa Prabhu goto errout; 397059ccaaaaSRoopa Prabhu 39712469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 39722469ffd7SJohn Fastabend return 0; 39732469ffd7SJohn Fastabend errout: 39742469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 39752469ffd7SJohn Fastabend kfree_skb(skb); 397659ccaaaaSRoopa Prabhu if (err) 39772469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 39782469ffd7SJohn Fastabend return err; 39792469ffd7SJohn Fastabend } 39802469ffd7SJohn Fastabend 3981c21ef3e3SDavid Ahern static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3982c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 3983e5a55a89SJohn Fastabend { 3984e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 3985e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 3986e5a55a89SJohn Fastabend struct net_device *dev; 39872469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 39882469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 39894de8b413SRosen, Rami u16 flags = 0; 3990c38e01b8SJohn Fastabend bool have_flags = false; 3991e5a55a89SJohn Fastabend 3992e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 3993e5a55a89SJohn Fastabend return -EINVAL; 3994e5a55a89SJohn Fastabend 3995e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 3996e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 3997e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 3998e5a55a89SJohn Fastabend 3999e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 4000e5a55a89SJohn Fastabend if (!dev) { 4001b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4002e5a55a89SJohn Fastabend return -ENODEV; 4003e5a55a89SJohn Fastabend } 4004e5a55a89SJohn Fastabend 40052469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 40062469ffd7SJohn Fastabend if (br_spec) { 40072469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 40082469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40096e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40106e8d1c55SThomas Graf return -EINVAL; 40116e8d1c55SThomas Graf 4012c38e01b8SJohn Fastabend have_flags = true; 40132469ffd7SJohn Fastabend flags = nla_get_u16(attr); 40142469ffd7SJohn Fastabend break; 40152469ffd7SJohn Fastabend } 40162469ffd7SJohn Fastabend } 40172469ffd7SJohn Fastabend } 40182469ffd7SJohn Fastabend 40192469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4020898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4021898e5061SJiri Pirko 4022898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 40232469ffd7SJohn Fastabend err = -EOPNOTSUPP; 4024e5a55a89SJohn Fastabend goto out; 4025e5a55a89SJohn Fastabend } 4026e5a55a89SJohn Fastabend 4027add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 40282469ffd7SJohn Fastabend if (err) 40292469ffd7SJohn Fastabend goto out; 40302469ffd7SJohn Fastabend 40312469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 40322469ffd7SJohn Fastabend } 40332469ffd7SJohn Fastabend 40342469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 40352469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 40362469ffd7SJohn Fastabend err = -EOPNOTSUPP; 40372469ffd7SJohn Fastabend else 4038add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4039add511b3SRoopa Prabhu flags); 404002dba438SRoopa Prabhu if (!err) { 40412469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 404202dba438SRoopa Prabhu 404302dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 404402dba438SRoopa Prabhu * change 404502dba438SRoopa Prabhu */ 404602dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 404702dba438SRoopa Prabhu } 40482469ffd7SJohn Fastabend } 40492469ffd7SJohn Fastabend 4050c38e01b8SJohn Fastabend if (have_flags) 40512469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 4052e5a55a89SJohn Fastabend out: 4053e5a55a89SJohn Fastabend return err; 4054e5a55a89SJohn Fastabend } 4055e5a55a89SJohn Fastabend 4056c21ef3e3SDavid Ahern static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4057c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 4058407af329SVlad Yasevich { 4059407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 4060407af329SVlad Yasevich struct ifinfomsg *ifm; 4061407af329SVlad Yasevich struct net_device *dev; 4062407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 4063407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 40644de8b413SRosen, Rami u16 flags = 0; 4065407af329SVlad Yasevich bool have_flags = false; 4066407af329SVlad Yasevich 4067407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 4068407af329SVlad Yasevich return -EINVAL; 4069407af329SVlad Yasevich 4070407af329SVlad Yasevich ifm = nlmsg_data(nlh); 4071407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 4072407af329SVlad Yasevich return -EPFNOSUPPORT; 4073407af329SVlad Yasevich 4074407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 4075407af329SVlad Yasevich if (!dev) { 4076b88d12e4SFlorian Westphal NL_SET_ERR_MSG(extack, "unknown ifindex"); 4077407af329SVlad Yasevich return -ENODEV; 4078407af329SVlad Yasevich } 4079407af329SVlad Yasevich 4080407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4081407af329SVlad Yasevich if (br_spec) { 4082407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 4083407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 40846e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 40856e8d1c55SThomas Graf return -EINVAL; 40866e8d1c55SThomas Graf 4087407af329SVlad Yasevich have_flags = true; 4088407af329SVlad Yasevich flags = nla_get_u16(attr); 4089407af329SVlad Yasevich break; 4090407af329SVlad Yasevich } 4091407af329SVlad Yasevich } 4092407af329SVlad Yasevich } 4093407af329SVlad Yasevich 4094407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4095407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4096407af329SVlad Yasevich 4097407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4098407af329SVlad Yasevich err = -EOPNOTSUPP; 4099407af329SVlad Yasevich goto out; 4100407af329SVlad Yasevich } 4101407af329SVlad Yasevich 4102add511b3SRoopa Prabhu err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4103407af329SVlad Yasevich if (err) 4104407af329SVlad Yasevich goto out; 4105407af329SVlad Yasevich 4106407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 4107407af329SVlad Yasevich } 4108407af329SVlad Yasevich 4109407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 4110407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 4111407af329SVlad Yasevich err = -EOPNOTSUPP; 4112407af329SVlad Yasevich else 4113add511b3SRoopa Prabhu err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4114add511b3SRoopa Prabhu flags); 4115407af329SVlad Yasevich 411602dba438SRoopa Prabhu if (!err) { 4117407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 411802dba438SRoopa Prabhu 411902dba438SRoopa Prabhu /* Generate event to notify upper layer of bridge 412002dba438SRoopa Prabhu * change 412102dba438SRoopa Prabhu */ 412202dba438SRoopa Prabhu err = rtnl_bridge_notify(dev); 412302dba438SRoopa Prabhu } 4124407af329SVlad Yasevich } 4125407af329SVlad Yasevich 4126407af329SVlad Yasevich if (have_flags) 4127407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 4128407af329SVlad Yasevich out: 4129407af329SVlad Yasevich return err; 4130407af329SVlad Yasevich } 4131407af329SVlad Yasevich 4132e8872a25SNikolay Aleksandrov static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4133e8872a25SNikolay Aleksandrov { 4134e8872a25SNikolay Aleksandrov return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4135e8872a25SNikolay Aleksandrov (!idxattr || idxattr == attrid); 4136e8872a25SNikolay Aleksandrov } 4137e8872a25SNikolay Aleksandrov 413869ae6ad2SNogah Frankel #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 413969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_attr_size(int attr_id) 414069ae6ad2SNogah Frankel { 414169ae6ad2SNogah Frankel switch (attr_id) { 414269ae6ad2SNogah Frankel case IFLA_OFFLOAD_XSTATS_CPU_HIT: 414369ae6ad2SNogah Frankel return sizeof(struct rtnl_link_stats64); 414469ae6ad2SNogah Frankel } 414569ae6ad2SNogah Frankel 414669ae6ad2SNogah Frankel return 0; 414769ae6ad2SNogah Frankel } 414869ae6ad2SNogah Frankel 414969ae6ad2SNogah Frankel static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 415069ae6ad2SNogah Frankel int *prividx) 415169ae6ad2SNogah Frankel { 415269ae6ad2SNogah Frankel struct nlattr *attr = NULL; 415369ae6ad2SNogah Frankel int attr_id, size; 415469ae6ad2SNogah Frankel void *attr_data; 415569ae6ad2SNogah Frankel int err; 415669ae6ad2SNogah Frankel 415769ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 415869ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 415969ae6ad2SNogah Frankel return -ENODATA; 416069ae6ad2SNogah Frankel 416169ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 416269ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 416369ae6ad2SNogah Frankel if (attr_id < *prividx) 416469ae6ad2SNogah Frankel continue; 416569ae6ad2SNogah Frankel 416669ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 416769ae6ad2SNogah Frankel if (!size) 416869ae6ad2SNogah Frankel continue; 416969ae6ad2SNogah Frankel 41703df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 417169ae6ad2SNogah Frankel continue; 417269ae6ad2SNogah Frankel 417369ae6ad2SNogah Frankel attr = nla_reserve_64bit(skb, attr_id, size, 417469ae6ad2SNogah Frankel IFLA_OFFLOAD_XSTATS_UNSPEC); 417569ae6ad2SNogah Frankel if (!attr) 417669ae6ad2SNogah Frankel goto nla_put_failure; 417769ae6ad2SNogah Frankel 417869ae6ad2SNogah Frankel attr_data = nla_data(attr); 417969ae6ad2SNogah Frankel memset(attr_data, 0, size); 418069ae6ad2SNogah Frankel err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 418169ae6ad2SNogah Frankel attr_data); 418269ae6ad2SNogah Frankel if (err) 418369ae6ad2SNogah Frankel goto get_offload_stats_failure; 418469ae6ad2SNogah Frankel } 418569ae6ad2SNogah Frankel 418669ae6ad2SNogah Frankel if (!attr) 418769ae6ad2SNogah Frankel return -ENODATA; 418869ae6ad2SNogah Frankel 418969ae6ad2SNogah Frankel *prividx = 0; 419069ae6ad2SNogah Frankel return 0; 419169ae6ad2SNogah Frankel 419269ae6ad2SNogah Frankel nla_put_failure: 419369ae6ad2SNogah Frankel err = -EMSGSIZE; 419469ae6ad2SNogah Frankel get_offload_stats_failure: 419569ae6ad2SNogah Frankel *prividx = attr_id; 419669ae6ad2SNogah Frankel return err; 419769ae6ad2SNogah Frankel } 419869ae6ad2SNogah Frankel 419969ae6ad2SNogah Frankel static int rtnl_get_offload_stats_size(const struct net_device *dev) 420069ae6ad2SNogah Frankel { 420169ae6ad2SNogah Frankel int nla_size = 0; 420269ae6ad2SNogah Frankel int attr_id; 420369ae6ad2SNogah Frankel int size; 420469ae6ad2SNogah Frankel 420569ae6ad2SNogah Frankel if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 420669ae6ad2SNogah Frankel dev->netdev_ops->ndo_get_offload_stats)) 420769ae6ad2SNogah Frankel return 0; 420869ae6ad2SNogah Frankel 420969ae6ad2SNogah Frankel for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 421069ae6ad2SNogah Frankel attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 42113df5b3c6SOr Gerlitz if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 421269ae6ad2SNogah Frankel continue; 421369ae6ad2SNogah Frankel size = rtnl_get_offload_stats_attr_size(attr_id); 421469ae6ad2SNogah Frankel nla_size += nla_total_size_64bit(size); 421569ae6ad2SNogah Frankel } 421669ae6ad2SNogah Frankel 421769ae6ad2SNogah Frankel if (nla_size != 0) 421869ae6ad2SNogah Frankel nla_size += nla_total_size(0); 421969ae6ad2SNogah Frankel 422069ae6ad2SNogah Frankel return nla_size; 422169ae6ad2SNogah Frankel } 422269ae6ad2SNogah Frankel 422310c9ead9SRoopa Prabhu static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 422410c9ead9SRoopa Prabhu int type, u32 pid, u32 seq, u32 change, 4225e8872a25SNikolay Aleksandrov unsigned int flags, unsigned int filter_mask, 4226e8872a25SNikolay Aleksandrov int *idxattr, int *prividx) 422710c9ead9SRoopa Prabhu { 422810c9ead9SRoopa Prabhu struct if_stats_msg *ifsm; 422910c9ead9SRoopa Prabhu struct nlmsghdr *nlh; 423010c9ead9SRoopa Prabhu struct nlattr *attr; 4231e8872a25SNikolay Aleksandrov int s_prividx = *prividx; 423269ae6ad2SNogah Frankel int err; 423310c9ead9SRoopa Prabhu 423410c9ead9SRoopa Prabhu ASSERT_RTNL(); 423510c9ead9SRoopa Prabhu 423610c9ead9SRoopa Prabhu nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 423710c9ead9SRoopa Prabhu if (!nlh) 423810c9ead9SRoopa Prabhu return -EMSGSIZE; 423910c9ead9SRoopa Prabhu 424010c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 4241ce024f42SNikolay Aleksandrov ifsm->family = PF_UNSPEC; 4242ce024f42SNikolay Aleksandrov ifsm->pad1 = 0; 4243ce024f42SNikolay Aleksandrov ifsm->pad2 = 0; 424410c9ead9SRoopa Prabhu ifsm->ifindex = dev->ifindex; 424510c9ead9SRoopa Prabhu ifsm->filter_mask = filter_mask; 424610c9ead9SRoopa Prabhu 4247e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 424810c9ead9SRoopa Prabhu struct rtnl_link_stats64 *sp; 424910c9ead9SRoopa Prabhu 425058414d32SNicolas Dichtel attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 425158414d32SNicolas Dichtel sizeof(struct rtnl_link_stats64), 425258414d32SNicolas Dichtel IFLA_STATS_UNSPEC); 425310c9ead9SRoopa Prabhu if (!attr) 425410c9ead9SRoopa Prabhu goto nla_put_failure; 425510c9ead9SRoopa Prabhu 425610c9ead9SRoopa Prabhu sp = nla_data(attr); 425710c9ead9SRoopa Prabhu dev_get_stats(dev, sp); 425810c9ead9SRoopa Prabhu } 425910c9ead9SRoopa Prabhu 426097a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 426197a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 426297a47facSNikolay Aleksandrov 426397a47facSNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 426497a47facSNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS; 426597a47facSNikolay Aleksandrov attr = nla_nest_start(skb, 426697a47facSNikolay Aleksandrov IFLA_STATS_LINK_XSTATS); 426797a47facSNikolay Aleksandrov if (!attr) 426897a47facSNikolay Aleksandrov goto nla_put_failure; 426997a47facSNikolay Aleksandrov 427080e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 427180e73cc5SNikolay Aleksandrov nla_nest_end(skb, attr); 427280e73cc5SNikolay Aleksandrov if (err) 427380e73cc5SNikolay Aleksandrov goto nla_put_failure; 427480e73cc5SNikolay Aleksandrov *idxattr = 0; 427580e73cc5SNikolay Aleksandrov } 427680e73cc5SNikolay Aleksandrov } 427780e73cc5SNikolay Aleksandrov 427880e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 427980e73cc5SNikolay Aleksandrov *idxattr)) { 428080e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 428180e73cc5SNikolay Aleksandrov const struct net_device *master; 428280e73cc5SNikolay Aleksandrov 428380e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(dev); 428480e73cc5SNikolay Aleksandrov if (master) 428580e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 428680e73cc5SNikolay Aleksandrov if (ops && ops->fill_linkxstats) { 428780e73cc5SNikolay Aleksandrov *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 428880e73cc5SNikolay Aleksandrov attr = nla_nest_start(skb, 428980e73cc5SNikolay Aleksandrov IFLA_STATS_LINK_XSTATS_SLAVE); 429080e73cc5SNikolay Aleksandrov if (!attr) 429180e73cc5SNikolay Aleksandrov goto nla_put_failure; 429280e73cc5SNikolay Aleksandrov 429380e73cc5SNikolay Aleksandrov err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 429497a47facSNikolay Aleksandrov nla_nest_end(skb, attr); 429597a47facSNikolay Aleksandrov if (err) 429697a47facSNikolay Aleksandrov goto nla_put_failure; 429797a47facSNikolay Aleksandrov *idxattr = 0; 429897a47facSNikolay Aleksandrov } 429997a47facSNikolay Aleksandrov } 430097a47facSNikolay Aleksandrov 430169ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 430269ae6ad2SNogah Frankel *idxattr)) { 430369ae6ad2SNogah Frankel *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 430469ae6ad2SNogah Frankel attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 430569ae6ad2SNogah Frankel if (!attr) 430669ae6ad2SNogah Frankel goto nla_put_failure; 430769ae6ad2SNogah Frankel 430869ae6ad2SNogah Frankel err = rtnl_get_offload_stats(skb, dev, prividx); 430969ae6ad2SNogah Frankel if (err == -ENODATA) 431069ae6ad2SNogah Frankel nla_nest_cancel(skb, attr); 431169ae6ad2SNogah Frankel else 431269ae6ad2SNogah Frankel nla_nest_end(skb, attr); 431369ae6ad2SNogah Frankel 431469ae6ad2SNogah Frankel if (err && err != -ENODATA) 431569ae6ad2SNogah Frankel goto nla_put_failure; 431669ae6ad2SNogah Frankel *idxattr = 0; 431769ae6ad2SNogah Frankel } 431869ae6ad2SNogah Frankel 4319aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4320aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4321aefb4d4aSRobert Shearman 4322aefb4d4aSRobert Shearman *idxattr = IFLA_STATS_AF_SPEC; 4323aefb4d4aSRobert Shearman attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4324aefb4d4aSRobert Shearman if (!attr) 4325aefb4d4aSRobert Shearman goto nla_put_failure; 4326aefb4d4aSRobert Shearman 43275fa85a09SFlorian Westphal rcu_read_lock(); 43285fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4329aefb4d4aSRobert Shearman if (af_ops->fill_stats_af) { 4330aefb4d4aSRobert Shearman struct nlattr *af; 4331aefb4d4aSRobert Shearman int err; 4332aefb4d4aSRobert Shearman 4333aefb4d4aSRobert Shearman af = nla_nest_start(skb, af_ops->family); 43345fa85a09SFlorian Westphal if (!af) { 43355fa85a09SFlorian Westphal rcu_read_unlock(); 4336aefb4d4aSRobert Shearman goto nla_put_failure; 43375fa85a09SFlorian Westphal } 4338aefb4d4aSRobert Shearman err = af_ops->fill_stats_af(skb, dev); 4339aefb4d4aSRobert Shearman 43405fa85a09SFlorian Westphal if (err == -ENODATA) { 4341aefb4d4aSRobert Shearman nla_nest_cancel(skb, af); 43425fa85a09SFlorian Westphal } else if (err < 0) { 43435fa85a09SFlorian Westphal rcu_read_unlock(); 4344aefb4d4aSRobert Shearman goto nla_put_failure; 43455fa85a09SFlorian Westphal } 4346aefb4d4aSRobert Shearman 4347aefb4d4aSRobert Shearman nla_nest_end(skb, af); 4348aefb4d4aSRobert Shearman } 4349aefb4d4aSRobert Shearman } 43505fa85a09SFlorian Westphal rcu_read_unlock(); 4351aefb4d4aSRobert Shearman 4352aefb4d4aSRobert Shearman nla_nest_end(skb, attr); 4353aefb4d4aSRobert Shearman 4354aefb4d4aSRobert Shearman *idxattr = 0; 4355aefb4d4aSRobert Shearman } 4356aefb4d4aSRobert Shearman 435710c9ead9SRoopa Prabhu nlmsg_end(skb, nlh); 435810c9ead9SRoopa Prabhu 435910c9ead9SRoopa Prabhu return 0; 436010c9ead9SRoopa Prabhu 436110c9ead9SRoopa Prabhu nla_put_failure: 4362e8872a25SNikolay Aleksandrov /* not a multi message or no progress mean a real error */ 4363e8872a25SNikolay Aleksandrov if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 436410c9ead9SRoopa Prabhu nlmsg_cancel(skb, nlh); 4365e8872a25SNikolay Aleksandrov else 4366e8872a25SNikolay Aleksandrov nlmsg_end(skb, nlh); 436710c9ead9SRoopa Prabhu 436810c9ead9SRoopa Prabhu return -EMSGSIZE; 436910c9ead9SRoopa Prabhu } 437010c9ead9SRoopa Prabhu 437110c9ead9SRoopa Prabhu static size_t if_nlmsg_stats_size(const struct net_device *dev, 437210c9ead9SRoopa Prabhu u32 filter_mask) 437310c9ead9SRoopa Prabhu { 437410c9ead9SRoopa Prabhu size_t size = 0; 437510c9ead9SRoopa Prabhu 4376e8872a25SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 437710c9ead9SRoopa Prabhu size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 437810c9ead9SRoopa Prabhu 437997a47facSNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 438097a47facSNikolay Aleksandrov const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 438180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS; 438297a47facSNikolay Aleksandrov 438397a47facSNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 438480e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 438580e73cc5SNikolay Aleksandrov attr)); 438697a47facSNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS */ 438797a47facSNikolay Aleksandrov size += nla_total_size(0); 438897a47facSNikolay Aleksandrov } 438997a47facSNikolay Aleksandrov } 439097a47facSNikolay Aleksandrov 439180e73cc5SNikolay Aleksandrov if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 439280e73cc5SNikolay Aleksandrov struct net_device *_dev = (struct net_device *)dev; 439380e73cc5SNikolay Aleksandrov const struct rtnl_link_ops *ops = NULL; 439480e73cc5SNikolay Aleksandrov const struct net_device *master; 439580e73cc5SNikolay Aleksandrov 439680e73cc5SNikolay Aleksandrov /* netdev_master_upper_dev_get can't take const */ 439780e73cc5SNikolay Aleksandrov master = netdev_master_upper_dev_get(_dev); 439880e73cc5SNikolay Aleksandrov if (master) 439980e73cc5SNikolay Aleksandrov ops = master->rtnl_link_ops; 440080e73cc5SNikolay Aleksandrov if (ops && ops->get_linkxstats_size) { 440180e73cc5SNikolay Aleksandrov int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 440280e73cc5SNikolay Aleksandrov 440380e73cc5SNikolay Aleksandrov size += nla_total_size(ops->get_linkxstats_size(dev, 440480e73cc5SNikolay Aleksandrov attr)); 440580e73cc5SNikolay Aleksandrov /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 440680e73cc5SNikolay Aleksandrov size += nla_total_size(0); 440780e73cc5SNikolay Aleksandrov } 440880e73cc5SNikolay Aleksandrov } 440980e73cc5SNikolay Aleksandrov 441069ae6ad2SNogah Frankel if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 441169ae6ad2SNogah Frankel size += rtnl_get_offload_stats_size(dev); 441269ae6ad2SNogah Frankel 4413aefb4d4aSRobert Shearman if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4414aefb4d4aSRobert Shearman struct rtnl_af_ops *af_ops; 4415aefb4d4aSRobert Shearman 4416aefb4d4aSRobert Shearman /* for IFLA_STATS_AF_SPEC */ 4417aefb4d4aSRobert Shearman size += nla_total_size(0); 4418aefb4d4aSRobert Shearman 44195fa85a09SFlorian Westphal rcu_read_lock(); 44205fa85a09SFlorian Westphal list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4421aefb4d4aSRobert Shearman if (af_ops->get_stats_af_size) { 4422aefb4d4aSRobert Shearman size += nla_total_size( 4423aefb4d4aSRobert Shearman af_ops->get_stats_af_size(dev)); 4424aefb4d4aSRobert Shearman 4425aefb4d4aSRobert Shearman /* for AF_* */ 4426aefb4d4aSRobert Shearman size += nla_total_size(0); 4427aefb4d4aSRobert Shearman } 4428aefb4d4aSRobert Shearman } 44295fa85a09SFlorian Westphal rcu_read_unlock(); 4430aefb4d4aSRobert Shearman } 4431aefb4d4aSRobert Shearman 443210c9ead9SRoopa Prabhu return size; 443310c9ead9SRoopa Prabhu } 443410c9ead9SRoopa Prabhu 4435c21ef3e3SDavid Ahern static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4436c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 443710c9ead9SRoopa Prabhu { 443810c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 443910c9ead9SRoopa Prabhu struct net_device *dev = NULL; 4440e8872a25SNikolay Aleksandrov int idxattr = 0, prividx = 0; 4441e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 444210c9ead9SRoopa Prabhu struct sk_buff *nskb; 444310c9ead9SRoopa Prabhu u32 filter_mask; 444410c9ead9SRoopa Prabhu int err; 444510c9ead9SRoopa Prabhu 44464775cc1fSMathias Krause if (nlmsg_len(nlh) < sizeof(*ifsm)) 44474775cc1fSMathias Krause return -EINVAL; 44484775cc1fSMathias Krause 444910c9ead9SRoopa Prabhu ifsm = nlmsg_data(nlh); 445010c9ead9SRoopa Prabhu if (ifsm->ifindex > 0) 445110c9ead9SRoopa Prabhu dev = __dev_get_by_index(net, ifsm->ifindex); 445210c9ead9SRoopa Prabhu else 445310c9ead9SRoopa Prabhu return -EINVAL; 445410c9ead9SRoopa Prabhu 445510c9ead9SRoopa Prabhu if (!dev) 445610c9ead9SRoopa Prabhu return -ENODEV; 445710c9ead9SRoopa Prabhu 445810c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 445910c9ead9SRoopa Prabhu if (!filter_mask) 446010c9ead9SRoopa Prabhu return -EINVAL; 446110c9ead9SRoopa Prabhu 446210c9ead9SRoopa Prabhu nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 446310c9ead9SRoopa Prabhu if (!nskb) 446410c9ead9SRoopa Prabhu return -ENOBUFS; 446510c9ead9SRoopa Prabhu 446610c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 446710c9ead9SRoopa Prabhu NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4468e8872a25SNikolay Aleksandrov 0, filter_mask, &idxattr, &prividx); 446910c9ead9SRoopa Prabhu if (err < 0) { 447010c9ead9SRoopa Prabhu /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 447110c9ead9SRoopa Prabhu WARN_ON(err == -EMSGSIZE); 447210c9ead9SRoopa Prabhu kfree_skb(nskb); 447310c9ead9SRoopa Prabhu } else { 447410c9ead9SRoopa Prabhu err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 447510c9ead9SRoopa Prabhu } 447610c9ead9SRoopa Prabhu 447710c9ead9SRoopa Prabhu return err; 447810c9ead9SRoopa Prabhu } 447910c9ead9SRoopa Prabhu 448010c9ead9SRoopa Prabhu static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 448110c9ead9SRoopa Prabhu { 4482e8872a25SNikolay Aleksandrov int h, s_h, err, s_idx, s_idxattr, s_prividx; 448310c9ead9SRoopa Prabhu struct net *net = sock_net(skb->sk); 448410c9ead9SRoopa Prabhu unsigned int flags = NLM_F_MULTI; 4485e8872a25SNikolay Aleksandrov struct if_stats_msg *ifsm; 4486e8872a25SNikolay Aleksandrov struct hlist_head *head; 4487e8872a25SNikolay Aleksandrov struct net_device *dev; 448810c9ead9SRoopa Prabhu u32 filter_mask = 0; 4489e8872a25SNikolay Aleksandrov int idx = 0; 449010c9ead9SRoopa Prabhu 449110c9ead9SRoopa Prabhu s_h = cb->args[0]; 449210c9ead9SRoopa Prabhu s_idx = cb->args[1]; 4493e8872a25SNikolay Aleksandrov s_idxattr = cb->args[2]; 4494e8872a25SNikolay Aleksandrov s_prividx = cb->args[3]; 449510c9ead9SRoopa Prabhu 449610c9ead9SRoopa Prabhu cb->seq = net->dev_base_seq; 449710c9ead9SRoopa Prabhu 44984775cc1fSMathias Krause if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 44994775cc1fSMathias Krause return -EINVAL; 45004775cc1fSMathias Krause 450110c9ead9SRoopa Prabhu ifsm = nlmsg_data(cb->nlh); 450210c9ead9SRoopa Prabhu filter_mask = ifsm->filter_mask; 450310c9ead9SRoopa Prabhu if (!filter_mask) 450410c9ead9SRoopa Prabhu return -EINVAL; 450510c9ead9SRoopa Prabhu 450610c9ead9SRoopa Prabhu for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 450710c9ead9SRoopa Prabhu idx = 0; 450810c9ead9SRoopa Prabhu head = &net->dev_index_head[h]; 450910c9ead9SRoopa Prabhu hlist_for_each_entry(dev, head, index_hlist) { 451010c9ead9SRoopa Prabhu if (idx < s_idx) 451110c9ead9SRoopa Prabhu goto cont; 451210c9ead9SRoopa Prabhu err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 451310c9ead9SRoopa Prabhu NETLINK_CB(cb->skb).portid, 451410c9ead9SRoopa Prabhu cb->nlh->nlmsg_seq, 0, 4515e8872a25SNikolay Aleksandrov flags, filter_mask, 4516e8872a25SNikolay Aleksandrov &s_idxattr, &s_prividx); 451710c9ead9SRoopa Prabhu /* If we ran out of room on the first message, 451810c9ead9SRoopa Prabhu * we're in trouble 451910c9ead9SRoopa Prabhu */ 452010c9ead9SRoopa Prabhu WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 452110c9ead9SRoopa Prabhu 452210c9ead9SRoopa Prabhu if (err < 0) 452310c9ead9SRoopa Prabhu goto out; 4524e8872a25SNikolay Aleksandrov s_prividx = 0; 4525e8872a25SNikolay Aleksandrov s_idxattr = 0; 452610c9ead9SRoopa Prabhu nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 452710c9ead9SRoopa Prabhu cont: 452810c9ead9SRoopa Prabhu idx++; 452910c9ead9SRoopa Prabhu } 453010c9ead9SRoopa Prabhu } 453110c9ead9SRoopa Prabhu out: 4532e8872a25SNikolay Aleksandrov cb->args[3] = s_prividx; 4533e8872a25SNikolay Aleksandrov cb->args[2] = s_idxattr; 453410c9ead9SRoopa Prabhu cb->args[1] = idx; 453510c9ead9SRoopa Prabhu cb->args[0] = h; 453610c9ead9SRoopa Prabhu 453710c9ead9SRoopa Prabhu return skb->len; 453810c9ead9SRoopa Prabhu } 453910c9ead9SRoopa Prabhu 45401da177e4SLinus Torvalds /* Process one rtnetlink message. */ 45411da177e4SLinus Torvalds 45422d4bc933SJohannes Berg static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 45432d4bc933SJohannes Berg struct netlink_ext_ack *extack) 45441da177e4SLinus Torvalds { 45453b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 4546addf9b90SFlorian Westphal struct rtnl_link *link; 4547e4202511SFlorian Westphal struct module *owner; 45486853dd48SFlorian Westphal int err = -EOPNOTSUPP; 4549e2849863SThomas Graf rtnl_doit_func doit; 455062256f98SFlorian Westphal unsigned int flags; 4551617cfc75SAlexander Kuleshov int kind; 45521da177e4SLinus Torvalds int family; 45531da177e4SLinus Torvalds int type; 45541da177e4SLinus Torvalds 45551da177e4SLinus Torvalds type = nlh->nlmsg_type; 45561da177e4SLinus Torvalds if (type > RTM_MAX) 4557038890feSThomas Graf return -EOPNOTSUPP; 45581da177e4SLinus Torvalds 45591da177e4SLinus Torvalds type -= RTM_BASE; 45601da177e4SLinus Torvalds 45611da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 4562573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 45631da177e4SLinus Torvalds return 0; 45641da177e4SLinus Torvalds 4565573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 45661da177e4SLinus Torvalds kind = type&3; 45671da177e4SLinus Torvalds 456890f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 45691d00a4ebSThomas Graf return -EPERM; 45701da177e4SLinus Torvalds 45716853dd48SFlorian Westphal rcu_read_lock(); 4572b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 457397c53cacSDenis V. Lunev struct sock *rtnl; 4574e2849863SThomas Graf rtnl_dumpit_func dumpit; 4575c7ac8679SGreg Rose u16 min_dump_alloc = 0; 45761da177e4SLinus Torvalds 4577addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4578addf9b90SFlorian Westphal if (!link || !link->dumpit) { 45796853dd48SFlorian Westphal family = PF_UNSPEC; 4580addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4581addf9b90SFlorian Westphal if (!link || !link->dumpit) 45826853dd48SFlorian Westphal goto err_unlock; 45836853dd48SFlorian Westphal } 4584e4202511SFlorian Westphal owner = link->owner; 4585addf9b90SFlorian Westphal dumpit = link->dumpit; 45866853dd48SFlorian Westphal 45875c2bb9b6SFlorian Westphal if (type == RTM_GETLINK - RTM_BASE) 4588e1fa6d21SFlorian Westphal min_dump_alloc = rtnl_calcit(skb, nlh); 45891da177e4SLinus Torvalds 4590e4202511SFlorian Westphal err = 0; 4591e4202511SFlorian Westphal /* need to do this before rcu_read_unlock() */ 4592e4202511SFlorian Westphal if (!try_module_get(owner)) 4593e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4594e4202511SFlorian Westphal 45956853dd48SFlorian Westphal rcu_read_unlock(); 45966853dd48SFlorian Westphal 459797c53cacSDenis V. Lunev rtnl = net->rtnl; 4598e4202511SFlorian Westphal if (err == 0) { 459980d326faSPablo Neira Ayuso struct netlink_dump_control c = { 460080d326faSPablo Neira Ayuso .dump = dumpit, 460180d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 4602e4202511SFlorian Westphal .module = owner, 460380d326faSPablo Neira Ayuso }; 460480d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 4605e4202511SFlorian Westphal /* netlink_dump_start() will keep a reference on 4606e4202511SFlorian Westphal * module if dump is still in progress. 4607e4202511SFlorian Westphal */ 4608e4202511SFlorian Westphal module_put(owner); 460980d326faSPablo Neira Ayuso } 46102907c35fSEric Dumazet return err; 46111da177e4SLinus Torvalds } 46121da177e4SLinus Torvalds 4613addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4614addf9b90SFlorian Westphal if (!link || !link->doit) { 46158caa38b5SFlorian Westphal family = PF_UNSPEC; 4616addf9b90SFlorian Westphal link = rtnl_get_link(PF_UNSPEC, type); 4617addf9b90SFlorian Westphal if (!link || !link->doit) 4618addf9b90SFlorian Westphal goto out_unlock; 46198caa38b5SFlorian Westphal } 46208caa38b5SFlorian Westphal 4621e4202511SFlorian Westphal owner = link->owner; 4622e4202511SFlorian Westphal if (!try_module_get(owner)) { 4623e4202511SFlorian Westphal err = -EPROTONOSUPPORT; 4624e4202511SFlorian Westphal goto out_unlock; 4625e4202511SFlorian Westphal } 4626e4202511SFlorian Westphal 4627addf9b90SFlorian Westphal flags = link->flags; 462862256f98SFlorian Westphal if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4629addf9b90SFlorian Westphal doit = link->doit; 463062256f98SFlorian Westphal rcu_read_unlock(); 463162256f98SFlorian Westphal if (doit) 463262256f98SFlorian Westphal err = doit(skb, nlh, extack); 4633e4202511SFlorian Westphal module_put(owner); 463462256f98SFlorian Westphal return err; 463562256f98SFlorian Westphal } 46366853dd48SFlorian Westphal rcu_read_unlock(); 46376853dd48SFlorian Westphal 46380cc09020SFlorian Westphal rtnl_lock(); 4639addf9b90SFlorian Westphal link = rtnl_get_link(family, type); 4640addf9b90SFlorian Westphal if (link && link->doit) 4641addf9b90SFlorian Westphal err = link->doit(skb, nlh, extack); 46420cc09020SFlorian Westphal rtnl_unlock(); 4643addf9b90SFlorian Westphal 4644e4202511SFlorian Westphal module_put(owner); 4645e4202511SFlorian Westphal 4646addf9b90SFlorian Westphal return err; 4647addf9b90SFlorian Westphal 4648addf9b90SFlorian Westphal out_unlock: 4649addf9b90SFlorian Westphal rcu_read_unlock(); 46500cc09020SFlorian Westphal return err; 46510cc09020SFlorian Westphal 46520cc09020SFlorian Westphal err_unlock: 46536853dd48SFlorian Westphal rcu_read_unlock(); 46540cc09020SFlorian Westphal return -EOPNOTSUPP; 46551da177e4SLinus Torvalds } 46561da177e4SLinus Torvalds 4657cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 46581da177e4SLinus Torvalds { 4659cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds 46625f729eaaSJulien Gomes static int rtnetlink_bind(struct net *net, int group) 46635f729eaaSJulien Gomes { 46645f729eaaSJulien Gomes switch (group) { 46655f729eaaSJulien Gomes case RTNLGRP_IPV4_MROUTE_R: 46665f729eaaSJulien Gomes case RTNLGRP_IPV6_MROUTE_R: 46675f729eaaSJulien Gomes if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 46685f729eaaSJulien Gomes return -EPERM; 46695f729eaaSJulien Gomes break; 46705f729eaaSJulien Gomes } 46715f729eaaSJulien Gomes return 0; 46725f729eaaSJulien Gomes } 46735f729eaaSJulien Gomes 46741da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 46751da177e4SLinus Torvalds { 4676351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4677e9dc8653SEric W. Biederman 46781da177e4SLinus Torvalds switch (event) { 46795138e86fSVlad Yasevich case NETDEV_REBOOT: 46808a212589SXin Long case NETDEV_CHANGEMTU: 46813753654eSDavid Ahern case NETDEV_CHANGEADDR: 46825138e86fSVlad Yasevich case NETDEV_CHANGENAME: 46835138e86fSVlad Yasevich case NETDEV_FEAT_CHANGE: 46845138e86fSVlad Yasevich case NETDEV_BONDING_FAILOVER: 4685e6e66594SXin Long case NETDEV_POST_TYPE_CHANGE: 46865138e86fSVlad Yasevich case NETDEV_NOTIFY_PEERS: 4687dc709f37SXin Long case NETDEV_CHANGEUPPER: 46885138e86fSVlad Yasevich case NETDEV_RESEND_IGMP: 46895138e86fSVlad Yasevich case NETDEV_CHANGEINFODATA: 4690eeda3fb9SXin Long case NETDEV_CHANGELOWERSTATE: 4691ebdcf045SXin Long case NETDEV_CHANGE_TX_QUEUE_LEN: 46923d3ea5afSVlad Yasevich rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 469338e01b30SNicolas Dichtel GFP_KERNEL, NULL, 0); 46941da177e4SLinus Torvalds break; 46951da177e4SLinus Torvalds default: 46961da177e4SLinus Torvalds break; 46971da177e4SLinus Torvalds } 46981da177e4SLinus Torvalds return NOTIFY_DONE; 46991da177e4SLinus Torvalds } 47001da177e4SLinus Torvalds 47011da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 47021da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 47031da177e4SLinus Torvalds }; 47041da177e4SLinus Torvalds 470597c53cacSDenis V. Lunev 47062c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 470797c53cacSDenis V. Lunev { 470897c53cacSDenis V. Lunev struct sock *sk; 4709a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 4710a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 4711a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 4712a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 47139785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 47145f729eaaSJulien Gomes .bind = rtnetlink_bind, 4715a31f2d17SPablo Neira Ayuso }; 4716a31f2d17SPablo Neira Ayuso 47179f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 471897c53cacSDenis V. Lunev if (!sk) 471997c53cacSDenis V. Lunev return -ENOMEM; 472097c53cacSDenis V. Lunev net->rtnl = sk; 472197c53cacSDenis V. Lunev return 0; 472297c53cacSDenis V. Lunev } 472397c53cacSDenis V. Lunev 47242c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 472597c53cacSDenis V. Lunev { 4726b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 472797c53cacSDenis V. Lunev net->rtnl = NULL; 472897c53cacSDenis V. Lunev } 472997c53cacSDenis V. Lunev 473097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 473197c53cacSDenis V. Lunev .init = rtnetlink_net_init, 473297c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 473346456675SKirill Tkhai .async = true, 473497c53cacSDenis V. Lunev }; 473597c53cacSDenis V. Lunev 47361da177e4SLinus Torvalds void __init rtnetlink_init(void) 47371da177e4SLinus Torvalds { 473897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 47391da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 474097c53cacSDenis V. Lunev 47411da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 4742340d17fcSThomas Graf 4743c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4744b97bac64SFlorian Westphal rtnl_dump_ifinfo, 0); 4745b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4746b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4747b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4748687ad8ccSThomas Graf 4749b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4750b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4751b97bac64SFlorian Westphal rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 475277162022SJohn Fastabend 4753b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4754b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4755b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4756e5a55a89SJohn Fastabend 4757b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4758b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4759b97bac64SFlorian Westphal rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 476010c9ead9SRoopa Prabhu 476110c9ead9SRoopa Prabhu rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4762b97bac64SFlorian Westphal 0); 47631da177e4SLinus Torvalds } 4764