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 191da177e4SLinus Torvalds #include <linux/errno.h> 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/socket.h> 231da177e4SLinus Torvalds #include <linux/kernel.h> 241da177e4SLinus Torvalds #include <linux/timer.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/sockios.h> 271da177e4SLinus Torvalds #include <linux/net.h> 281da177e4SLinus Torvalds #include <linux/fcntl.h> 291da177e4SLinus Torvalds #include <linux/mm.h> 301da177e4SLinus Torvalds #include <linux/slab.h> 311da177e4SLinus Torvalds #include <linux/interrupt.h> 321da177e4SLinus Torvalds #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/skbuff.h> 341da177e4SLinus Torvalds #include <linux/init.h> 351da177e4SLinus Torvalds #include <linux/security.h> 366756ae4bSStephen Hemminger #include <linux/mutex.h> 371823730fSThomas Graf #include <linux/if_addr.h> 3877162022SJohn Fastabend #include <linux/if_bridge.h> 39f6f6424bSJiri Pirko #include <linux/if_vlan.h> 40ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 4177162022SJohn Fastabend #include <linux/etherdevice.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <asm/uaccess.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <linux/inet.h> 461da177e4SLinus Torvalds #include <linux/netdevice.h> 4782f28412SJiri Pirko #include <net/switchdev.h> 481da177e4SLinus Torvalds #include <net/ip.h> 491da177e4SLinus Torvalds #include <net/protocol.h> 501da177e4SLinus Torvalds #include <net/arp.h> 511da177e4SLinus Torvalds #include <net/route.h> 521da177e4SLinus Torvalds #include <net/udp.h> 531da177e4SLinus Torvalds #include <net/sock.h> 541da177e4SLinus Torvalds #include <net/pkt_sched.h> 5514c0b97dSThomas Graf #include <net/fib_rules.h> 56e2849863SThomas Graf #include <net/rtnetlink.h> 5730ffee84SJohannes Berg #include <net/net_namespace.h> 581da177e4SLinus Torvalds 59e0d087afSEric Dumazet struct rtnl_link { 60e2849863SThomas Graf rtnl_doit_func doit; 61e2849863SThomas Graf rtnl_dumpit_func dumpit; 62c7ac8679SGreg Rose rtnl_calcit_func calcit; 63e2849863SThomas Graf }; 64e2849863SThomas Graf 656756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds void rtnl_lock(void) 681da177e4SLinus Torvalds { 696756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 701da177e4SLinus Torvalds } 71e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 721da177e4SLinus Torvalds 736756ae4bSStephen Hemminger void __rtnl_unlock(void) 741da177e4SLinus Torvalds { 756756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds void rtnl_unlock(void) 791da177e4SLinus Torvalds { 8058ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 811da177e4SLinus Torvalds netdev_run_todo(); 821da177e4SLinus Torvalds } 83e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 841da177e4SLinus Torvalds 856756ae4bSStephen Hemminger int rtnl_trylock(void) 866756ae4bSStephen Hemminger { 876756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 886756ae4bSStephen Hemminger } 89e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 906756ae4bSStephen Hemminger 91c9c1014bSPatrick McHardy int rtnl_is_locked(void) 92c9c1014bSPatrick McHardy { 93c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 94c9c1014bSPatrick McHardy } 95e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 96c9c1014bSPatrick McHardy 97a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 98a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void) 99a898def2SPaul E. McKenney { 100a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 101a898def2SPaul E. McKenney } 102a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 103a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 104a898def2SPaul E. McKenney 10525239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 106e2849863SThomas Graf 107e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 108e2849863SThomas Graf { 109e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 110e2849863SThomas Graf 111e2849863SThomas Graf /* 112e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 113e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 114e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 115e2849863SThomas Graf */ 116e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 117e2849863SThomas Graf 118e2849863SThomas Graf return msgindex; 119e2849863SThomas Graf } 120e2849863SThomas Graf 121e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 122e2849863SThomas Graf { 123e2849863SThomas Graf struct rtnl_link *tab; 124e2849863SThomas Graf 12525239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 126e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1270f87b1ddSPatrick McHardy else 1280f87b1ddSPatrick McHardy tab = NULL; 1290f87b1ddSPatrick McHardy 13051057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 131e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 132e2849863SThomas Graf 133c80bbeaeSHans Zhang return tab[msgindex].doit; 134e2849863SThomas Graf } 135e2849863SThomas Graf 136e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 137e2849863SThomas Graf { 138e2849863SThomas Graf struct rtnl_link *tab; 139e2849863SThomas Graf 14025239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 141e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1420f87b1ddSPatrick McHardy else 1430f87b1ddSPatrick McHardy tab = NULL; 1440f87b1ddSPatrick McHardy 14551057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 146e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 147e2849863SThomas Graf 148c80bbeaeSHans Zhang return tab[msgindex].dumpit; 149e2849863SThomas Graf } 150e2849863SThomas Graf 151c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) 152c7ac8679SGreg Rose { 153c7ac8679SGreg Rose struct rtnl_link *tab; 154c7ac8679SGreg Rose 155c7ac8679SGreg Rose if (protocol <= RTNL_FAMILY_MAX) 156c7ac8679SGreg Rose tab = rtnl_msg_handlers[protocol]; 157c7ac8679SGreg Rose else 158c7ac8679SGreg Rose tab = NULL; 159c7ac8679SGreg Rose 160c7ac8679SGreg Rose if (tab == NULL || tab[msgindex].calcit == NULL) 161c7ac8679SGreg Rose tab = rtnl_msg_handlers[PF_UNSPEC]; 162c7ac8679SGreg Rose 163c80bbeaeSHans Zhang return tab[msgindex].calcit; 164c7ac8679SGreg Rose } 165c7ac8679SGreg Rose 166e2849863SThomas Graf /** 167e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 168e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 169e2849863SThomas Graf * @msgtype: rtnetlink message type 170e2849863SThomas Graf * @doit: Function pointer called for each request message 171e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 172c7ac8679SGreg Rose * @calcit: Function pointer to calc size of dump message 173e2849863SThomas Graf * 174e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 175e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 176e2849863SThomas Graf * specified protocol family and message type is received. 177e2849863SThomas Graf * 178e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 179e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 180e2849863SThomas Graf * family exists. 181e2849863SThomas Graf * 182e2849863SThomas Graf * Returns 0 on success or a negative error code. 183e2849863SThomas Graf */ 184e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 185c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 186c7ac8679SGreg Rose rtnl_calcit_func calcit) 187e2849863SThomas Graf { 188e2849863SThomas Graf struct rtnl_link *tab; 189e2849863SThomas Graf int msgindex; 190e2849863SThomas Graf 19125239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 192e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 193e2849863SThomas Graf 194e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 195e2849863SThomas Graf if (tab == NULL) { 196e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 197e2849863SThomas Graf if (tab == NULL) 198e2849863SThomas Graf return -ENOBUFS; 199e2849863SThomas Graf 200e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 201e2849863SThomas Graf } 202e2849863SThomas Graf 203e2849863SThomas Graf if (doit) 204e2849863SThomas Graf tab[msgindex].doit = doit; 205e2849863SThomas Graf 206e2849863SThomas Graf if (dumpit) 207e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 208e2849863SThomas Graf 209c7ac8679SGreg Rose if (calcit) 210c7ac8679SGreg Rose tab[msgindex].calcit = calcit; 211c7ac8679SGreg Rose 212e2849863SThomas Graf return 0; 213e2849863SThomas Graf } 214e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 215e2849863SThomas Graf 216e2849863SThomas Graf /** 217e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 218e2849863SThomas Graf * 219e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 220e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 221e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 222e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 22325985edcSLucas De Marchi * of memory implies no sense in continuing. 224e2849863SThomas Graf */ 225e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 226c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 227c7ac8679SGreg Rose rtnl_calcit_func calcit) 228e2849863SThomas Graf { 229c7ac8679SGreg Rose if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) 230e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 231e2849863SThomas Graf "protocol = %d, message type = %d\n", 232e2849863SThomas Graf protocol, msgtype); 233e2849863SThomas Graf } 234e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 235e2849863SThomas Graf 236e2849863SThomas Graf /** 237e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 238e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 239e2849863SThomas Graf * @msgtype: rtnetlink message type 240e2849863SThomas Graf * 241e2849863SThomas Graf * Returns 0 on success or a negative error code. 242e2849863SThomas Graf */ 243e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 244e2849863SThomas Graf { 245e2849863SThomas Graf int msgindex; 246e2849863SThomas Graf 24725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 248e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 249e2849863SThomas Graf 250e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 251e2849863SThomas Graf return -ENOENT; 252e2849863SThomas Graf 253e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 254e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 255e2849863SThomas Graf 256e2849863SThomas Graf return 0; 257e2849863SThomas Graf } 258e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 259e2849863SThomas Graf 260e2849863SThomas Graf /** 261e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 262e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 263e2849863SThomas Graf * 264e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 265e2849863SThomas Graf * of a certain protocol family. 266e2849863SThomas Graf */ 267e2849863SThomas Graf void rtnl_unregister_all(int protocol) 268e2849863SThomas Graf { 26925239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 270e2849863SThomas Graf 271e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 272e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 273e2849863SThomas Graf } 274e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2751da177e4SLinus Torvalds 27638f7b870SPatrick McHardy static LIST_HEAD(link_ops); 27738f7b870SPatrick McHardy 278c63044f0SEric Dumazet static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 279c63044f0SEric Dumazet { 280c63044f0SEric Dumazet const struct rtnl_link_ops *ops; 281c63044f0SEric Dumazet 282c63044f0SEric Dumazet list_for_each_entry(ops, &link_ops, list) { 283c63044f0SEric Dumazet if (!strcmp(ops->kind, kind)) 284c63044f0SEric Dumazet return ops; 285c63044f0SEric Dumazet } 286c63044f0SEric Dumazet return NULL; 287c63044f0SEric Dumazet } 288c63044f0SEric Dumazet 28938f7b870SPatrick McHardy /** 29038f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 29138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 29238f7b870SPatrick McHardy * 29338f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 29438f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 29538f7b870SPatrick McHardy * must be called before registering the devices. 29638f7b870SPatrick McHardy * 29738f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 29838f7b870SPatrick McHardy */ 29938f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 30038f7b870SPatrick McHardy { 301c63044f0SEric Dumazet if (rtnl_link_ops_get(ops->kind)) 302c63044f0SEric Dumazet return -EEXIST; 303c63044f0SEric Dumazet 304b0ab2fabSJiri Pirko /* The check for setup is here because if ops 305b0ab2fabSJiri Pirko * does not have that filled up, it is not possible 306b0ab2fabSJiri Pirko * to use the ops for creating device. So do not 307b0ab2fabSJiri Pirko * fill up dellink as well. That disables rtnl_dellink. 308b0ab2fabSJiri Pirko */ 309b0ab2fabSJiri Pirko if (ops->setup && !ops->dellink) 31023289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 3112d85cba2SPatrick McHardy 31238f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 31338f7b870SPatrick McHardy return 0; 31438f7b870SPatrick McHardy } 31538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 31638f7b870SPatrick McHardy 31738f7b870SPatrick McHardy /** 31838f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 31938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 32038f7b870SPatrick McHardy * 32138f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 32238f7b870SPatrick McHardy */ 32338f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 32438f7b870SPatrick McHardy { 32538f7b870SPatrick McHardy int err; 32638f7b870SPatrick McHardy 32738f7b870SPatrick McHardy rtnl_lock(); 32838f7b870SPatrick McHardy err = __rtnl_link_register(ops); 32938f7b870SPatrick McHardy rtnl_unlock(); 33038f7b870SPatrick McHardy return err; 33138f7b870SPatrick McHardy } 33238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 33338f7b870SPatrick McHardy 334669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 335669f87baSPavel Emelyanov { 336669f87baSPavel Emelyanov struct net_device *dev; 33723289a37SEric Dumazet LIST_HEAD(list_kill); 33823289a37SEric Dumazet 339669f87baSPavel Emelyanov for_each_netdev(net, dev) { 34023289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 34123289a37SEric Dumazet ops->dellink(dev, &list_kill); 342669f87baSPavel Emelyanov } 34323289a37SEric Dumazet unregister_netdevice_many(&list_kill); 344669f87baSPavel Emelyanov } 345669f87baSPavel Emelyanov 34638f7b870SPatrick McHardy /** 34738f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 34838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 34938f7b870SPatrick McHardy * 3502d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 35138f7b870SPatrick McHardy */ 35238f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 35338f7b870SPatrick McHardy { 354881d966bSEric W. Biederman struct net *net; 3552d85cba2SPatrick McHardy 356881d966bSEric W. Biederman for_each_net(net) { 357669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 358881d966bSEric W. Biederman } 35938f7b870SPatrick McHardy list_del(&ops->list); 36038f7b870SPatrick McHardy } 36138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 36238f7b870SPatrick McHardy 363200b916fSCong Wang /* Return with the rtnl_lock held when there are no network 364200b916fSCong Wang * devices unregistering in any network namespace. 365200b916fSCong Wang */ 366200b916fSCong Wang static void rtnl_lock_unregistering_all(void) 367200b916fSCong Wang { 368200b916fSCong Wang struct net *net; 369200b916fSCong Wang bool unregistering; 370ff960a73SPeter Zijlstra DEFINE_WAIT_FUNC(wait, woken_wake_function); 371200b916fSCong Wang 372ff960a73SPeter Zijlstra add_wait_queue(&netdev_unregistering_wq, &wait); 373200b916fSCong Wang for (;;) { 374200b916fSCong Wang unregistering = false; 375200b916fSCong Wang rtnl_lock(); 376200b916fSCong Wang for_each_net(net) { 377200b916fSCong Wang if (net->dev_unreg_count > 0) { 378200b916fSCong Wang unregistering = true; 379200b916fSCong Wang break; 380200b916fSCong Wang } 381200b916fSCong Wang } 382200b916fSCong Wang if (!unregistering) 383200b916fSCong Wang break; 384200b916fSCong Wang __rtnl_unlock(); 385ff960a73SPeter Zijlstra 386ff960a73SPeter Zijlstra wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 387200b916fSCong Wang } 388ff960a73SPeter Zijlstra remove_wait_queue(&netdev_unregistering_wq, &wait); 389200b916fSCong Wang } 390200b916fSCong Wang 39138f7b870SPatrick McHardy /** 39238f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 39338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 39438f7b870SPatrick McHardy */ 39538f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 39638f7b870SPatrick McHardy { 397200b916fSCong Wang /* Close the race with cleanup_net() */ 398200b916fSCong Wang mutex_lock(&net_mutex); 399200b916fSCong Wang rtnl_lock_unregistering_all(); 40038f7b870SPatrick McHardy __rtnl_link_unregister(ops); 40138f7b870SPatrick McHardy rtnl_unlock(); 402200b916fSCong Wang mutex_unlock(&net_mutex); 40338f7b870SPatrick McHardy } 40438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 40538f7b870SPatrick McHardy 406ba7d49b1SJiri Pirko static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 407ba7d49b1SJiri Pirko { 408ba7d49b1SJiri Pirko struct net_device *master_dev; 409ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 410ba7d49b1SJiri Pirko 411ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 412ba7d49b1SJiri Pirko if (!master_dev) 413ba7d49b1SJiri Pirko return 0; 414ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 4156049f253SFernando Luis Vazquez Cao if (!ops || !ops->get_slave_size) 416ba7d49b1SJiri Pirko return 0; 417ba7d49b1SJiri Pirko /* IFLA_INFO_SLAVE_DATA + nested data */ 418ba7d49b1SJiri Pirko return nla_total_size(sizeof(struct nlattr)) + 419ba7d49b1SJiri Pirko ops->get_slave_size(master_dev, dev); 420ba7d49b1SJiri Pirko } 421ba7d49b1SJiri Pirko 42238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 42338f7b870SPatrick McHardy { 42438f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 42538f7b870SPatrick McHardy size_t size; 42638f7b870SPatrick McHardy 42738f7b870SPatrick McHardy if (!ops) 42838f7b870SPatrick McHardy return 0; 42938f7b870SPatrick McHardy 430369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 431369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 43238f7b870SPatrick McHardy 43338f7b870SPatrick McHardy if (ops->get_size) 43438f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 435369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 43638f7b870SPatrick McHardy ops->get_size(dev); 43738f7b870SPatrick McHardy 43838f7b870SPatrick McHardy if (ops->get_xstats_size) 439369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 440369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 44138f7b870SPatrick McHardy 442ba7d49b1SJiri Pirko size += rtnl_link_get_slave_info_data_size(dev); 443ba7d49b1SJiri Pirko 44438f7b870SPatrick McHardy return size; 44538f7b870SPatrick McHardy } 44638f7b870SPatrick McHardy 447f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 448f8ff182cSThomas Graf 449f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 450f8ff182cSThomas Graf { 451f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 452f8ff182cSThomas Graf 453f8ff182cSThomas Graf list_for_each_entry(ops, &rtnl_af_ops, list) { 454f8ff182cSThomas Graf if (ops->family == family) 455f8ff182cSThomas Graf return ops; 456f8ff182cSThomas Graf } 457f8ff182cSThomas Graf 458f8ff182cSThomas Graf return NULL; 459f8ff182cSThomas Graf } 460f8ff182cSThomas Graf 461f8ff182cSThomas Graf /** 462f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 463f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 464f8ff182cSThomas Graf * 465f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 466f8ff182cSThomas Graf */ 4673678a9d8Sstephen hemminger void rtnl_af_register(struct rtnl_af_ops *ops) 468f8ff182cSThomas Graf { 469f8ff182cSThomas Graf rtnl_lock(); 4703678a9d8Sstephen hemminger list_add_tail(&ops->list, &rtnl_af_ops); 471f8ff182cSThomas Graf rtnl_unlock(); 472f8ff182cSThomas Graf } 473f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 474f8ff182cSThomas Graf 475f8ff182cSThomas Graf /** 476f8ff182cSThomas Graf * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 477f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 478f8ff182cSThomas Graf * 479f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 480f8ff182cSThomas Graf */ 481f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops) 482f8ff182cSThomas Graf { 483f8ff182cSThomas Graf list_del(&ops->list); 484f8ff182cSThomas Graf } 485f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 486f8ff182cSThomas Graf 487f8ff182cSThomas Graf /** 488f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 489f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 490f8ff182cSThomas Graf */ 491f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 492f8ff182cSThomas Graf { 493f8ff182cSThomas Graf rtnl_lock(); 494f8ff182cSThomas Graf __rtnl_af_unregister(ops); 495f8ff182cSThomas Graf rtnl_unlock(); 496f8ff182cSThomas Graf } 497f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 498f8ff182cSThomas Graf 499f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev) 500f8ff182cSThomas Graf { 501f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 502f8ff182cSThomas Graf size_t size; 503f8ff182cSThomas Graf 504f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 505f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 506f8ff182cSThomas Graf 507f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 508f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 509f8ff182cSThomas Graf /* AF_* + nested data */ 510f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 511f8ff182cSThomas Graf af_ops->get_link_af_size(dev); 512f8ff182cSThomas Graf } 513f8ff182cSThomas Graf } 514f8ff182cSThomas Graf 515f8ff182cSThomas Graf return size; 516f8ff182cSThomas Graf } 517f8ff182cSThomas Graf 518ba7d49b1SJiri Pirko static bool rtnl_have_link_slave_info(const struct net_device *dev) 519ba7d49b1SJiri Pirko { 520ba7d49b1SJiri Pirko struct net_device *master_dev; 521ba7d49b1SJiri Pirko 522ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 523813f020cSJiri Pirko if (master_dev && master_dev->rtnl_link_ops) 524ba7d49b1SJiri Pirko return true; 525ba7d49b1SJiri Pirko return false; 526ba7d49b1SJiri Pirko } 527ba7d49b1SJiri Pirko 528ba7d49b1SJiri Pirko static int rtnl_link_slave_info_fill(struct sk_buff *skb, 529ba7d49b1SJiri Pirko const struct net_device *dev) 530ba7d49b1SJiri Pirko { 531ba7d49b1SJiri Pirko struct net_device *master_dev; 532ba7d49b1SJiri Pirko const struct rtnl_link_ops *ops; 533ba7d49b1SJiri Pirko struct nlattr *slave_data; 534ba7d49b1SJiri Pirko int err; 535ba7d49b1SJiri Pirko 536ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 537ba7d49b1SJiri Pirko if (!master_dev) 538ba7d49b1SJiri Pirko return 0; 539ba7d49b1SJiri Pirko ops = master_dev->rtnl_link_ops; 540ba7d49b1SJiri Pirko if (!ops) 541ba7d49b1SJiri Pirko return 0; 542ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 543ba7d49b1SJiri Pirko return -EMSGSIZE; 544ba7d49b1SJiri Pirko if (ops->fill_slave_info) { 545ba7d49b1SJiri Pirko slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 546ba7d49b1SJiri Pirko if (!slave_data) 547ba7d49b1SJiri Pirko return -EMSGSIZE; 548ba7d49b1SJiri Pirko err = ops->fill_slave_info(skb, master_dev, dev); 549ba7d49b1SJiri Pirko if (err < 0) 550ba7d49b1SJiri Pirko goto err_cancel_slave_data; 551ba7d49b1SJiri Pirko nla_nest_end(skb, slave_data); 552ba7d49b1SJiri Pirko } 553ba7d49b1SJiri Pirko return 0; 554ba7d49b1SJiri Pirko 555ba7d49b1SJiri Pirko err_cancel_slave_data: 556ba7d49b1SJiri Pirko nla_nest_cancel(skb, slave_data); 557ba7d49b1SJiri Pirko return err; 558ba7d49b1SJiri Pirko } 559ba7d49b1SJiri Pirko 560ba7d49b1SJiri Pirko static int rtnl_link_info_fill(struct sk_buff *skb, 561ba7d49b1SJiri Pirko const struct net_device *dev) 56238f7b870SPatrick McHardy { 56338f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 564ba7d49b1SJiri Pirko struct nlattr *data; 565ba7d49b1SJiri Pirko int err; 566ba7d49b1SJiri Pirko 567ba7d49b1SJiri Pirko if (!ops) 568ba7d49b1SJiri Pirko return 0; 569ba7d49b1SJiri Pirko if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 570ba7d49b1SJiri Pirko return -EMSGSIZE; 571ba7d49b1SJiri Pirko if (ops->fill_xstats) { 572ba7d49b1SJiri Pirko err = ops->fill_xstats(skb, dev); 573ba7d49b1SJiri Pirko if (err < 0) 574ba7d49b1SJiri Pirko return err; 575ba7d49b1SJiri Pirko } 576ba7d49b1SJiri Pirko if (ops->fill_info) { 577ba7d49b1SJiri Pirko data = nla_nest_start(skb, IFLA_INFO_DATA); 578ba7d49b1SJiri Pirko if (data == NULL) 579ba7d49b1SJiri Pirko return -EMSGSIZE; 580ba7d49b1SJiri Pirko err = ops->fill_info(skb, dev); 581ba7d49b1SJiri Pirko if (err < 0) 582ba7d49b1SJiri Pirko goto err_cancel_data; 583ba7d49b1SJiri Pirko nla_nest_end(skb, data); 584ba7d49b1SJiri Pirko } 585ba7d49b1SJiri Pirko return 0; 586ba7d49b1SJiri Pirko 587ba7d49b1SJiri Pirko err_cancel_data: 588ba7d49b1SJiri Pirko nla_nest_cancel(skb, data); 589ba7d49b1SJiri Pirko return err; 590ba7d49b1SJiri Pirko } 591ba7d49b1SJiri Pirko 592ba7d49b1SJiri Pirko static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 593ba7d49b1SJiri Pirko { 594ba7d49b1SJiri Pirko struct nlattr *linkinfo; 59538f7b870SPatrick McHardy int err = -EMSGSIZE; 59638f7b870SPatrick McHardy 59738f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 59838f7b870SPatrick McHardy if (linkinfo == NULL) 59938f7b870SPatrick McHardy goto out; 60038f7b870SPatrick McHardy 601ba7d49b1SJiri Pirko err = rtnl_link_info_fill(skb, dev); 60238f7b870SPatrick McHardy if (err < 0) 60338f7b870SPatrick McHardy goto err_cancel_link; 604ba7d49b1SJiri Pirko 605ba7d49b1SJiri Pirko err = rtnl_link_slave_info_fill(skb, dev); 60638f7b870SPatrick McHardy if (err < 0) 607ba7d49b1SJiri Pirko goto err_cancel_link; 60838f7b870SPatrick McHardy 60938f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 61038f7b870SPatrick McHardy return 0; 61138f7b870SPatrick McHardy 61238f7b870SPatrick McHardy err_cancel_link: 61338f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 61438f7b870SPatrick McHardy out: 61538f7b870SPatrick McHardy return err; 61638f7b870SPatrick McHardy } 61738f7b870SPatrick McHardy 61895c96174SEric Dumazet int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 6191da177e4SLinus Torvalds { 62097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 6211da177e4SLinus Torvalds int err = 0; 6221da177e4SLinus Torvalds 623ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 6241da177e4SLinus Torvalds if (echo) 6251da177e4SLinus Torvalds atomic_inc(&skb->users); 6261da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 6271da177e4SLinus Torvalds if (echo) 6281da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 6291da177e4SLinus Torvalds return err; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 63297c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 6332942e900SThomas Graf { 63497c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 63597c53cacSDenis V. Lunev 6362942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 6372942e900SThomas Graf } 638e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 6392942e900SThomas Graf 6401ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 64197676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 64297676b6bSThomas Graf { 64397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 64497676b6bSThomas Graf int report = 0; 64597676b6bSThomas Graf 64697676b6bSThomas Graf if (nlh) 64797676b6bSThomas Graf report = nlmsg_report(nlh); 64897676b6bSThomas Graf 6491ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 65097676b6bSThomas Graf } 651e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 65297676b6bSThomas Graf 65397c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 65497676b6bSThomas Graf { 65597c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 65697c53cacSDenis V. Lunev 65797676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 65897676b6bSThomas Graf } 659e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 66097676b6bSThomas Graf 6611da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 6621da177e4SLinus Torvalds { 6632d7202bfSThomas Graf struct nlattr *mx; 6642d7202bfSThomas Graf int i, valid = 0; 6651da177e4SLinus Torvalds 6662d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 6672d7202bfSThomas Graf if (mx == NULL) 6682d7202bfSThomas Graf return -ENOBUFS; 6692d7202bfSThomas Graf 6701da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 6712d7202bfSThomas Graf if (metrics[i]) { 6722d7202bfSThomas Graf valid++; 673a6574349SDavid S. Miller if (nla_put_u32(skb, i+1, metrics[i])) 674a6574349SDavid S. Miller goto nla_put_failure; 6751da177e4SLinus Torvalds } 6762d7202bfSThomas Graf } 6771da177e4SLinus Torvalds 678a57d27fcSDavid S. Miller if (!valid) { 679a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 680a57d27fcSDavid S. Miller return 0; 681a57d27fcSDavid S. Miller } 6822d7202bfSThomas Graf 6832d7202bfSThomas Graf return nla_nest_end(skb, mx); 6842d7202bfSThomas Graf 6852d7202bfSThomas Graf nla_put_failure: 686bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 687bc3ed28cSThomas Graf return -EMSGSIZE; 6881da177e4SLinus Torvalds } 689e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 6901da177e4SLinus Torvalds 691e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 69287a50699SDavid S. Miller long expires, u32 error) 693e3703b3dSThomas Graf { 694e3703b3dSThomas Graf struct rta_cacheinfo ci = { 695a399a805SEric Dumazet .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), 696e3703b3dSThomas Graf .rta_used = dst->__use, 697e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 698e3703b3dSThomas Graf .rta_error = error, 699e3703b3dSThomas Graf .rta_id = id, 700e3703b3dSThomas Graf }; 701e3703b3dSThomas Graf 7028253947eSLi Wei if (expires) { 7038253947eSLi Wei unsigned long clock; 704e3703b3dSThomas Graf 7058253947eSLi Wei clock = jiffies_to_clock_t(abs(expires)); 7068253947eSLi Wei clock = min_t(unsigned long, clock, INT_MAX); 7078253947eSLi Wei ci.rta_expires = (expires > 0) ? clock : -clock; 7088253947eSLi Wei } 709e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 710e3703b3dSThomas Graf } 711e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 7121da177e4SLinus Torvalds 71393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 714b00055aaSStefan Rompf { 715b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 716b00055aaSStefan Rompf 717b00055aaSStefan Rompf switch (transition) { 718b00055aaSStefan Rompf case IF_OPER_UP: 719b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 720b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 721b00055aaSStefan Rompf !netif_dormant(dev)) 722b00055aaSStefan Rompf operstate = IF_OPER_UP; 723b00055aaSStefan Rompf break; 724b00055aaSStefan Rompf 725b00055aaSStefan Rompf case IF_OPER_DORMANT: 726b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 727b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 728b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 729b00055aaSStefan Rompf break; 7303ff50b79SStephen Hemminger } 731b00055aaSStefan Rompf 732b00055aaSStefan Rompf if (dev->operstate != operstate) { 733b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 734b00055aaSStefan Rompf dev->operstate = operstate; 735b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 736b00055aaSStefan Rompf netdev_state_change(dev); 73793b2d4a2SDavid S. Miller } 738b00055aaSStefan Rompf } 739b00055aaSStefan Rompf 740b1beb681SJiri Benc static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 741b1beb681SJiri Benc { 742b1beb681SJiri Benc return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 743b1beb681SJiri Benc (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 744b1beb681SJiri Benc } 745b1beb681SJiri Benc 7463729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 7473729d502SPatrick McHardy const struct ifinfomsg *ifm) 7483729d502SPatrick McHardy { 7493729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 7503729d502SPatrick McHardy 7513729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 7523729d502SPatrick McHardy if (ifm->ifi_change) 7533729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 754b1beb681SJiri Benc (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 7553729d502SPatrick McHardy 7563729d502SPatrick McHardy return flags; 7573729d502SPatrick McHardy } 7583729d502SPatrick McHardy 759b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 760be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 7611da177e4SLinus Torvalds { 762b60c5115SThomas Graf a->rx_packets = b->rx_packets; 763b60c5115SThomas Graf a->tx_packets = b->tx_packets; 764b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 765b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 766b60c5115SThomas Graf a->rx_errors = b->rx_errors; 767b60c5115SThomas Graf a->tx_errors = b->tx_errors; 768b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 769b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 770b60c5115SThomas Graf 771b60c5115SThomas Graf a->multicast = b->multicast; 772b60c5115SThomas Graf a->collisions = b->collisions; 773b60c5115SThomas Graf 774b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 775b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 776b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 777b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 778b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 779b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 780b60c5115SThomas Graf 781b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 782b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 783b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 784b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 785b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 786b60c5115SThomas Graf 787b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 788b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 78910708f37SJan Engelhardt } 79010708f37SJan Engelhardt 791be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) 79210708f37SJan Engelhardt { 793afdcba37SEric Dumazet memcpy(v, b, sizeof(*b)); 79410708f37SJan Engelhardt } 795b60c5115SThomas Graf 796c02db8c6SChris Wright /* All VF info */ 797115c9b81SGreg Rose static inline int rtnl_vfinfo_size(const struct net_device *dev, 798115c9b81SGreg Rose u32 ext_filter_mask) 799ebc08a6fSWilliams, Mitch A { 800115c9b81SGreg Rose if (dev->dev.parent && dev_is_pci(dev->dev.parent) && 801115c9b81SGreg Rose (ext_filter_mask & RTEXT_FILTER_VF)) { 802c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 803045de01aSScott Feldman size_t size = nla_total_size(sizeof(struct nlattr)); 804045de01aSScott Feldman size += nla_total_size(num_vfs * sizeof(struct nlattr)); 805045de01aSScott Feldman size += num_vfs * 806045de01aSScott Feldman (nla_total_size(sizeof(struct ifla_vf_mac)) + 807045de01aSScott Feldman nla_total_size(sizeof(struct ifla_vf_vlan)) + 808ed616689SSucheta Chakraborty nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 809945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_rate)) + 810945a3676SJiri Benc nla_total_size(sizeof(struct ifla_vf_link_state))); 811c02db8c6SChris Wright return size; 812c02db8c6SChris Wright } else 813ebc08a6fSWilliams, Mitch A return 0; 814ebc08a6fSWilliams, Mitch A } 815ebc08a6fSWilliams, Mitch A 816c53864fdSDavid Gibson static size_t rtnl_port_size(const struct net_device *dev, 817c53864fdSDavid Gibson u32 ext_filter_mask) 81857b61080SScott Feldman { 81957b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 82057b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 82157b61080SScott Feldman + nla_total_size(sizeof(struct ifla_port_vsi)) 82257b61080SScott Feldman /* PORT_VSI_TYPE */ 82357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 82457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 82557b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 82657b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 82757b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 82857b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 82957b61080SScott Feldman + port_size; 83057b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 83157b61080SScott Feldman + port_size; 83257b61080SScott Feldman 833c53864fdSDavid Gibson if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 834c53864fdSDavid Gibson !(ext_filter_mask & RTEXT_FILTER_VF)) 83557b61080SScott Feldman return 0; 83657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 83757b61080SScott Feldman return port_self_size + vf_ports_size + 83857b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 83957b61080SScott Feldman else 84057b61080SScott Feldman return port_self_size; 84157b61080SScott Feldman } 84257b61080SScott Feldman 843115c9b81SGreg Rose static noinline size_t if_nlmsg_size(const struct net_device *dev, 844115c9b81SGreg Rose u32 ext_filter_mask) 845339bf98fSThomas Graf { 846339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 847339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 8480b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 849339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 850339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 851339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 852adcfe196SJan Engelhardt + nla_total_size(sizeof(struct rtnl_link_stats64)) 853339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 854339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 855339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 856339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 857339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 858339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 859339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 8609a57247fSJiri Pirko + nla_total_size(1) /* IFLA_CARRIER */ 861edbc0bb3SBen Greear + nla_total_size(4) /* IFLA_PROMISCUITY */ 86276ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 86376ff5cc9SJiri Pirko + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 864339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 86538f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 8662d3b479dSdavid decotigny + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 867115c9b81SGreg Rose + nla_total_size(ext_filter_mask 868115c9b81SGreg Rose & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 869115c9b81SGreg Rose + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 870c53864fdSDavid Gibson + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 871f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 87266cae9edSJiri Pirko + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */ 87382f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 87482f28412SJiri Pirko + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */ 875339bf98fSThomas Graf } 876339bf98fSThomas Graf 87757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 87857b61080SScott Feldman { 87957b61080SScott Feldman struct nlattr *vf_ports; 88057b61080SScott Feldman struct nlattr *vf_port; 88157b61080SScott Feldman int vf; 88257b61080SScott Feldman int err; 88357b61080SScott Feldman 88457b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 88557b61080SScott Feldman if (!vf_ports) 88657b61080SScott Feldman return -EMSGSIZE; 88757b61080SScott Feldman 88857b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 88957b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 8908ca94183SScott Feldman if (!vf_port) 8918ca94183SScott Feldman goto nla_put_failure; 892a6574349SDavid S. Miller if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 893a6574349SDavid S. Miller goto nla_put_failure; 89457b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 8958ca94183SScott Feldman if (err == -EMSGSIZE) 8968ca94183SScott Feldman goto nla_put_failure; 89757b61080SScott Feldman if (err) { 89857b61080SScott Feldman nla_nest_cancel(skb, vf_port); 89957b61080SScott Feldman continue; 90057b61080SScott Feldman } 90157b61080SScott Feldman nla_nest_end(skb, vf_port); 90257b61080SScott Feldman } 90357b61080SScott Feldman 90457b61080SScott Feldman nla_nest_end(skb, vf_ports); 90557b61080SScott Feldman 90657b61080SScott Feldman return 0; 9078ca94183SScott Feldman 9088ca94183SScott Feldman nla_put_failure: 9098ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 9108ca94183SScott Feldman return -EMSGSIZE; 91157b61080SScott Feldman } 91257b61080SScott Feldman 91357b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 91457b61080SScott Feldman { 91557b61080SScott Feldman struct nlattr *port_self; 91657b61080SScott Feldman int err; 91757b61080SScott Feldman 91857b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 91957b61080SScott Feldman if (!port_self) 92057b61080SScott Feldman return -EMSGSIZE; 92157b61080SScott Feldman 92257b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 92357b61080SScott Feldman if (err) { 92457b61080SScott Feldman nla_nest_cancel(skb, port_self); 9258ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 92657b61080SScott Feldman } 92757b61080SScott Feldman 92857b61080SScott Feldman nla_nest_end(skb, port_self); 92957b61080SScott Feldman 93057b61080SScott Feldman return 0; 93157b61080SScott Feldman } 93257b61080SScott Feldman 933c53864fdSDavid Gibson static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 934c53864fdSDavid Gibson u32 ext_filter_mask) 93557b61080SScott Feldman { 93657b61080SScott Feldman int err; 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 94257b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 94357b61080SScott Feldman if (err) 94457b61080SScott Feldman return err; 94557b61080SScott Feldman 94657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 94757b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 94857b61080SScott Feldman if (err) 94957b61080SScott Feldman return err; 95057b61080SScott Feldman } 95157b61080SScott Feldman 95257b61080SScott Feldman return 0; 95357b61080SScott Feldman } 95457b61080SScott Feldman 95566cae9edSJiri Pirko static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 95666cae9edSJiri Pirko { 95766cae9edSJiri Pirko int err; 95802637fceSJiri Pirko struct netdev_phys_item_id ppid; 95966cae9edSJiri Pirko 96066cae9edSJiri Pirko err = dev_get_phys_port_id(dev, &ppid); 96166cae9edSJiri Pirko if (err) { 96266cae9edSJiri Pirko if (err == -EOPNOTSUPP) 96366cae9edSJiri Pirko return 0; 96466cae9edSJiri Pirko return err; 96566cae9edSJiri Pirko } 96666cae9edSJiri Pirko 96766cae9edSJiri Pirko if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 96866cae9edSJiri Pirko return -EMSGSIZE; 96966cae9edSJiri Pirko 97066cae9edSJiri Pirko return 0; 97166cae9edSJiri Pirko } 97266cae9edSJiri Pirko 97382f28412SJiri Pirko static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 97482f28412SJiri Pirko { 97582f28412SJiri Pirko int err; 97682f28412SJiri Pirko struct netdev_phys_item_id psid; 97782f28412SJiri Pirko 97882f28412SJiri Pirko err = netdev_switch_parent_id_get(dev, &psid); 97982f28412SJiri Pirko if (err) { 98082f28412SJiri Pirko if (err == -EOPNOTSUPP) 98182f28412SJiri Pirko return 0; 98282f28412SJiri Pirko return err; 98382f28412SJiri Pirko } 98482f28412SJiri Pirko 98582f28412SJiri Pirko if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id)) 98682f28412SJiri Pirko return -EMSGSIZE; 98782f28412SJiri Pirko 98882f28412SJiri Pirko return 0; 98982f28412SJiri Pirko } 99082f28412SJiri Pirko 991b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 992575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 993115c9b81SGreg Rose unsigned int flags, u32 ext_filter_mask) 994b60c5115SThomas Graf { 995b60c5115SThomas Graf struct ifinfomsg *ifm; 9961da177e4SLinus Torvalds struct nlmsghdr *nlh; 99728172739SEric Dumazet struct rtnl_link_stats64 temp; 998be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *stats; 999f8ff182cSThomas Graf struct nlattr *attr, *af_spec; 1000f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 1001898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 10021da177e4SLinus Torvalds 10032907c35fSEric Dumazet ASSERT_RTNL(); 1004b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1005b60c5115SThomas Graf if (nlh == NULL) 100626932566SPatrick McHardy return -EMSGSIZE; 10071da177e4SLinus Torvalds 1008b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1009b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 1010b60c5115SThomas Graf ifm->__ifi_pad = 0; 1011b60c5115SThomas Graf ifm->ifi_type = dev->type; 1012b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 1013b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 1014b60c5115SThomas Graf ifm->ifi_change = change; 10151da177e4SLinus Torvalds 1016a6574349SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1017a6574349SDavid S. Miller nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1018a6574349SDavid S. Miller nla_put_u8(skb, IFLA_OPERSTATE, 1019a6574349SDavid S. Miller netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1020a6574349SDavid S. Miller nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1021a6574349SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1022a6574349SDavid S. Miller nla_put_u32(skb, IFLA_GROUP, dev->group) || 1023edbc0bb3SBen Greear nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 102476ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 10251d69c2b3SMark A. Greer #ifdef CONFIG_RPS 102676ff5cc9SJiri Pirko nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 10271d69c2b3SMark A. Greer #endif 1028a6574349SDavid S. Miller (dev->ifindex != dev->iflink && 1029a6574349SDavid S. Miller nla_put_u32(skb, IFLA_LINK, dev->iflink)) || 1030898e5061SJiri Pirko (upper_dev && 1031898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || 10329a57247fSJiri Pirko nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1033a6574349SDavid S. Miller (dev->qdisc && 1034a6574349SDavid S. Miller nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 1035a6574349SDavid S. Miller (dev->ifalias && 10362d3b479dSdavid decotigny nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || 10372d3b479dSdavid decotigny nla_put_u32(skb, IFLA_CARRIER_CHANGES, 10382d3b479dSdavid decotigny atomic_read(&dev->carrier_changes))) 1039a6574349SDavid S. Miller goto nla_put_failure; 10400b815a1aSStephen Hemminger 1041b00055aaSStefan Rompf if (1) { 10421da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 10431da177e4SLinus Torvalds .mem_start = dev->mem_start, 10441da177e4SLinus Torvalds .mem_end = dev->mem_end, 10451da177e4SLinus Torvalds .base_addr = dev->base_addr, 10461da177e4SLinus Torvalds .irq = dev->irq, 10471da177e4SLinus Torvalds .dma = dev->dma, 10481da177e4SLinus Torvalds .port = dev->if_port, 10491da177e4SLinus Torvalds }; 1050a6574349SDavid S. Miller if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) 1051a6574349SDavid S. Miller goto nla_put_failure; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if (dev->addr_len) { 1055a6574349SDavid S. Miller if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1056a6574349SDavid S. Miller nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1057a6574349SDavid S. Miller goto nla_put_failure; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 106066cae9edSJiri Pirko if (rtnl_phys_port_id_fill(skb, dev)) 106166cae9edSJiri Pirko goto nla_put_failure; 106266cae9edSJiri Pirko 106382f28412SJiri Pirko if (rtnl_phys_switch_id_fill(skb, dev)) 106482f28412SJiri Pirko goto nla_put_failure; 106582f28412SJiri Pirko 1066b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 1067b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 1068b60c5115SThomas Graf if (attr == NULL) 1069b60c5115SThomas Graf goto nla_put_failure; 1070b60c5115SThomas Graf 107128172739SEric Dumazet stats = dev_get_stats(dev, &temp); 1072b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 10731da177e4SLinus Torvalds 107410708f37SJan Engelhardt attr = nla_reserve(skb, IFLA_STATS64, 107510708f37SJan Engelhardt sizeof(struct rtnl_link_stats64)); 107610708f37SJan Engelhardt if (attr == NULL) 107710708f37SJan Engelhardt goto nla_put_failure; 107810708f37SJan Engelhardt copy_rtnl_link_stats64(nla_data(attr), stats); 107910708f37SJan Engelhardt 1080a6574349SDavid S. Miller if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && 1081a6574349SDavid S. Miller nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) 1082a6574349SDavid S. Miller goto nla_put_failure; 108357b61080SScott Feldman 1084115c9b81SGreg Rose if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent 1085115c9b81SGreg Rose && (ext_filter_mask & RTEXT_FILTER_VF)) { 1086ebc08a6fSWilliams, Mitch A int i; 1087ebc08a6fSWilliams, Mitch A 1088c02db8c6SChris Wright struct nlattr *vfinfo, *vf; 1089c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 1090c02db8c6SChris Wright 1091c02db8c6SChris Wright vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1092c02db8c6SChris Wright if (!vfinfo) 1093c02db8c6SChris Wright goto nla_put_failure; 1094c02db8c6SChris Wright for (i = 0; i < num_vfs; i++) { 1095c02db8c6SChris Wright struct ifla_vf_info ivi; 1096c02db8c6SChris Wright struct ifla_vf_mac vf_mac; 1097c02db8c6SChris Wright struct ifla_vf_vlan vf_vlan; 1098ed616689SSucheta Chakraborty struct ifla_vf_rate vf_rate; 1099c02db8c6SChris Wright struct ifla_vf_tx_rate vf_tx_rate; 11005f8444a3SGreg Rose struct ifla_vf_spoofchk vf_spoofchk; 11011d8faf48SRony Efraim struct ifla_vf_link_state vf_linkstate; 11025f8444a3SGreg Rose 11035f8444a3SGreg Rose /* 11045f8444a3SGreg Rose * Not all SR-IOV capable drivers support the 11055f8444a3SGreg Rose * spoofcheck query. Preset to -1 so the user 11065f8444a3SGreg Rose * space tool can detect that the driver didn't 11075f8444a3SGreg Rose * report anything. 11085f8444a3SGreg Rose */ 11095f8444a3SGreg Rose ivi.spoofchk = -1; 111084d73cd3SMathias Krause memset(ivi.mac, 0, sizeof(ivi.mac)); 11111d8faf48SRony Efraim /* The default value for VF link state is "auto" 11121d8faf48SRony Efraim * IFLA_VF_LINK_STATE_AUTO which equals zero 11131d8faf48SRony Efraim */ 11141d8faf48SRony Efraim ivi.linkstate = 0; 1115ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 1116ebc08a6fSWilliams, Mitch A break; 11175f8444a3SGreg Rose vf_mac.vf = 11185f8444a3SGreg Rose vf_vlan.vf = 1119ed616689SSucheta Chakraborty vf_rate.vf = 11205f8444a3SGreg Rose vf_tx_rate.vf = 11211d8faf48SRony Efraim vf_spoofchk.vf = 11221d8faf48SRony Efraim vf_linkstate.vf = ivi.vf; 11235f8444a3SGreg Rose 1124c02db8c6SChris Wright memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1125c02db8c6SChris Wright vf_vlan.vlan = ivi.vlan; 1126c02db8c6SChris Wright vf_vlan.qos = ivi.qos; 1127ed616689SSucheta Chakraborty vf_tx_rate.rate = ivi.max_tx_rate; 1128ed616689SSucheta Chakraborty vf_rate.min_tx_rate = ivi.min_tx_rate; 1129ed616689SSucheta Chakraborty vf_rate.max_tx_rate = ivi.max_tx_rate; 11305f8444a3SGreg Rose vf_spoofchk.setting = ivi.spoofchk; 11311d8faf48SRony Efraim vf_linkstate.link_state = ivi.linkstate; 1132c02db8c6SChris Wright vf = nla_nest_start(skb, IFLA_VF_INFO); 1133c02db8c6SChris Wright if (!vf) { 1134c02db8c6SChris Wright nla_nest_cancel(skb, vfinfo); 1135c02db8c6SChris Wright goto nla_put_failure; 1136ebc08a6fSWilliams, Mitch A } 1137a6574349SDavid S. Miller if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1138a6574349SDavid S. Miller nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1139ed616689SSucheta Chakraborty nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1140ed616689SSucheta Chakraborty &vf_rate) || 1141a6574349SDavid S. Miller nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1142a6574349SDavid S. Miller &vf_tx_rate) || 1143a6574349SDavid S. Miller nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 11441d8faf48SRony Efraim &vf_spoofchk) || 11451d8faf48SRony Efraim nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 11461d8faf48SRony Efraim &vf_linkstate)) 1147a6574349SDavid S. Miller goto nla_put_failure; 1148c02db8c6SChris Wright nla_nest_end(skb, vf); 1149c02db8c6SChris Wright } 1150c02db8c6SChris Wright nla_nest_end(skb, vfinfo); 1151ebc08a6fSWilliams, Mitch A } 115257b61080SScott Feldman 1153c53864fdSDavid Gibson if (rtnl_port_fill(skb, dev, ext_filter_mask)) 115457b61080SScott Feldman goto nla_put_failure; 115557b61080SScott Feldman 1156ba7d49b1SJiri Pirko if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 115738f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 115838f7b870SPatrick McHardy goto nla_put_failure; 115938f7b870SPatrick McHardy } 116038f7b870SPatrick McHardy 1161f8ff182cSThomas Graf if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 1162f8ff182cSThomas Graf goto nla_put_failure; 1163f8ff182cSThomas Graf 1164f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 1165f8ff182cSThomas Graf if (af_ops->fill_link_af) { 1166f8ff182cSThomas Graf struct nlattr *af; 1167f8ff182cSThomas Graf int err; 1168f8ff182cSThomas Graf 1169f8ff182cSThomas Graf if (!(af = nla_nest_start(skb, af_ops->family))) 1170f8ff182cSThomas Graf goto nla_put_failure; 1171f8ff182cSThomas Graf 1172f8ff182cSThomas Graf err = af_ops->fill_link_af(skb, dev); 1173f8ff182cSThomas Graf 1174f8ff182cSThomas Graf /* 1175f8ff182cSThomas Graf * Caller may return ENODATA to indicate that there 1176f8ff182cSThomas Graf * was no data to be dumped. This is not an error, it 1177f8ff182cSThomas Graf * means we should trim the attribute header and 1178f8ff182cSThomas Graf * continue. 1179f8ff182cSThomas Graf */ 1180f8ff182cSThomas Graf if (err == -ENODATA) 1181f8ff182cSThomas Graf nla_nest_cancel(skb, af); 1182f8ff182cSThomas Graf else if (err < 0) 1183f8ff182cSThomas Graf goto nla_put_failure; 1184f8ff182cSThomas Graf 1185f8ff182cSThomas Graf nla_nest_end(skb, af); 1186f8ff182cSThomas Graf } 1187f8ff182cSThomas Graf } 1188f8ff182cSThomas Graf 1189f8ff182cSThomas Graf nla_nest_end(skb, af_spec); 1190f8ff182cSThomas Graf 1191b60c5115SThomas Graf return nlmsg_end(skb, nlh); 1192b60c5115SThomas Graf 1193b60c5115SThomas Graf nla_put_failure: 119426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 119526932566SPatrick McHardy return -EMSGSIZE; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 1198f7b12606SJiri Pirko static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1199f7b12606SJiri Pirko [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1200f7b12606SJiri Pirko [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1201f7b12606SJiri Pirko [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1202f7b12606SJiri Pirko [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1203f7b12606SJiri Pirko [IFLA_MTU] = { .type = NLA_U32 }, 1204f7b12606SJiri Pirko [IFLA_LINK] = { .type = NLA_U32 }, 1205f7b12606SJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1206f7b12606SJiri Pirko [IFLA_CARRIER] = { .type = NLA_U8 }, 1207f7b12606SJiri Pirko [IFLA_TXQLEN] = { .type = NLA_U32 }, 1208f7b12606SJiri Pirko [IFLA_WEIGHT] = { .type = NLA_U32 }, 1209f7b12606SJiri Pirko [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1210f7b12606SJiri Pirko [IFLA_LINKMODE] = { .type = NLA_U8 }, 1211f7b12606SJiri Pirko [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1212f7b12606SJiri Pirko [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1213f7b12606SJiri Pirko [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1214f7b12606SJiri Pirko [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1215f7b12606SJiri Pirko [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1216f7b12606SJiri Pirko [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1217f7b12606SJiri Pirko [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1218f7b12606SJiri Pirko [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1219f7b12606SJiri Pirko [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1220f7b12606SJiri Pirko [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1221f7b12606SJiri Pirko [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1222f7b12606SJiri Pirko [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 122302637fceSJiri Pirko [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 12242d3b479dSdavid decotigny [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 122582f28412SJiri Pirko [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1226f7b12606SJiri Pirko }; 1227f7b12606SJiri Pirko 1228f7b12606SJiri Pirko static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1229f7b12606SJiri Pirko [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1230f7b12606SJiri Pirko [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1231f7b12606SJiri Pirko [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1232f7b12606SJiri Pirko [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1233f7b12606SJiri Pirko }; 1234f7b12606SJiri Pirko 1235f7b12606SJiri Pirko static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 1236f7b12606SJiri Pirko [IFLA_VF_INFO] = { .type = NLA_NESTED }, 1237f7b12606SJiri Pirko }; 1238f7b12606SJiri Pirko 1239f7b12606SJiri Pirko static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1240*364d5716SDaniel Borkmann [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1241*364d5716SDaniel Borkmann [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 1242*364d5716SDaniel Borkmann [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1243*364d5716SDaniel Borkmann [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1244*364d5716SDaniel Borkmann [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1245*364d5716SDaniel Borkmann [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1246f7b12606SJiri Pirko }; 1247f7b12606SJiri Pirko 1248f7b12606SJiri Pirko static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1249f7b12606SJiri Pirko [IFLA_PORT_VF] = { .type = NLA_U32 }, 1250f7b12606SJiri Pirko [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1251f7b12606SJiri Pirko .len = PORT_PROFILE_MAX }, 1252f7b12606SJiri Pirko [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1253f7b12606SJiri Pirko .len = sizeof(struct ifla_port_vsi)}, 1254f7b12606SJiri Pirko [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1255f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1256f7b12606SJiri Pirko [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1257f7b12606SJiri Pirko .len = PORT_UUID_MAX }, 1258f7b12606SJiri Pirko [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1259f7b12606SJiri Pirko [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1260f7b12606SJiri Pirko }; 1261f7b12606SJiri Pirko 1262b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 12631da177e4SLinus Torvalds { 12643b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 12657c28bd0bSEric Dumazet int h, s_h; 12667c28bd0bSEric Dumazet int idx = 0, s_idx; 12671da177e4SLinus Torvalds struct net_device *dev; 12687c28bd0bSEric Dumazet struct hlist_head *head; 1269115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 1270115c9b81SGreg Rose u32 ext_filter_mask = 0; 1271973462bbSDavid Gibson int err; 1272e5eca6d4SMichal Schmidt int hdrlen; 12731da177e4SLinus Torvalds 12747c28bd0bSEric Dumazet s_h = cb->args[0]; 12757c28bd0bSEric Dumazet s_idx = cb->args[1]; 12767c28bd0bSEric Dumazet 1277e67f88ddSEric Dumazet rcu_read_lock(); 12784e985adaSThomas Graf cb->seq = net->dev_base_seq; 12794e985adaSThomas Graf 1280e5eca6d4SMichal Schmidt /* A hack to preserve kernel<->userspace interface. 1281e5eca6d4SMichal Schmidt * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1282e5eca6d4SMichal Schmidt * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1283e5eca6d4SMichal Schmidt * what iproute2 < v3.9.0 used. 1284e5eca6d4SMichal Schmidt * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1285e5eca6d4SMichal Schmidt * attribute, its netlink message is shorter than struct ifinfomsg. 1286e5eca6d4SMichal Schmidt */ 1287e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1288e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1289e5eca6d4SMichal Schmidt 1290e5eca6d4SMichal Schmidt if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { 1291115c9b81SGreg Rose 1292115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 1293115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1294a4b64fbeSEric Dumazet } 1295115c9b81SGreg Rose 12967c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 12977562f876SPavel Emelianov idx = 0; 12987c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 1299b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 13001da177e4SLinus Torvalds if (idx < s_idx) 13017562f876SPavel Emelianov goto cont; 1302973462bbSDavid Gibson err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 130315e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 13047c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 1305115c9b81SGreg Rose NLM_F_MULTI, 1306973462bbSDavid Gibson ext_filter_mask); 1307973462bbSDavid Gibson /* If we ran out of room on the first message, 1308973462bbSDavid Gibson * we're in trouble 1309973462bbSDavid Gibson */ 1310973462bbSDavid Gibson WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 1311973462bbSDavid Gibson 1312973462bbSDavid Gibson if (err <= 0) 13137c28bd0bSEric Dumazet goto out; 13144e985adaSThomas Graf 13154e985adaSThomas Graf nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 13167562f876SPavel Emelianov cont: 13177562f876SPavel Emelianov idx++; 13181da177e4SLinus Torvalds } 13197c28bd0bSEric Dumazet } 13207c28bd0bSEric Dumazet out: 1321e67f88ddSEric Dumazet rcu_read_unlock(); 13227c28bd0bSEric Dumazet cb->args[1] = idx; 13237c28bd0bSEric Dumazet cb->args[0] = h; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds return skb->len; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 1328f7b12606SJiri Pirko int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) 1329f7b12606SJiri Pirko { 1330f7b12606SJiri Pirko return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); 1331f7b12606SJiri Pirko } 1332f7b12606SJiri Pirko EXPORT_SYMBOL(rtnl_nla_parse_ifla); 133357b61080SScott Feldman 133481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 133581adee47SEric W. Biederman { 133681adee47SEric W. Biederman struct net *net; 133781adee47SEric W. Biederman /* Examine the link attributes and figure out which 133881adee47SEric W. Biederman * network namespace we are talking about. 133981adee47SEric W. Biederman */ 134081adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 134181adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1342f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1343f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 134481adee47SEric W. Biederman else 134581adee47SEric W. Biederman net = get_net(src_net); 134681adee47SEric W. Biederman return net; 134781adee47SEric W. Biederman } 134881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 134981adee47SEric W. Biederman 13501840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 13511840bb13SThomas Graf { 13521840bb13SThomas Graf if (dev) { 13531840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 13541840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 13551840bb13SThomas Graf return -EINVAL; 13561840bb13SThomas Graf 13571840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 13581840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 13591840bb13SThomas Graf return -EINVAL; 13601840bb13SThomas Graf } 13611840bb13SThomas Graf 1362cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 1363cf7afbfeSThomas Graf struct nlattr *af; 1364cf7afbfeSThomas Graf int rem, err; 1365cf7afbfeSThomas Graf 1366cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1367cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 1368cf7afbfeSThomas Graf 1369cf7afbfeSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1370cf7afbfeSThomas Graf return -EAFNOSUPPORT; 1371cf7afbfeSThomas Graf 1372cf7afbfeSThomas Graf if (!af_ops->set_link_af) 1373cf7afbfeSThomas Graf return -EOPNOTSUPP; 1374cf7afbfeSThomas Graf 1375cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 13766d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 1377cf7afbfeSThomas Graf if (err < 0) 1378cf7afbfeSThomas Graf return err; 1379cf7afbfeSThomas Graf } 1380cf7afbfeSThomas Graf } 1381cf7afbfeSThomas Graf } 1382cf7afbfeSThomas Graf 13831840bb13SThomas Graf return 0; 13841840bb13SThomas Graf } 13851840bb13SThomas Graf 1386c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1387c02db8c6SChris Wright { 1388c02db8c6SChris Wright int rem, err = -EINVAL; 1389c02db8c6SChris Wright struct nlattr *vf; 1390c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 1391c02db8c6SChris Wright 1392c02db8c6SChris Wright nla_for_each_nested(vf, attr, rem) { 1393c02db8c6SChris Wright switch (nla_type(vf)) { 1394c02db8c6SChris Wright case IFLA_VF_MAC: { 1395c02db8c6SChris Wright struct ifla_vf_mac *ivm; 1396c02db8c6SChris Wright ivm = nla_data(vf); 1397c02db8c6SChris Wright err = -EOPNOTSUPP; 1398c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 1399c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 1400c02db8c6SChris Wright ivm->mac); 1401c02db8c6SChris Wright break; 1402c02db8c6SChris Wright } 1403c02db8c6SChris Wright case IFLA_VF_VLAN: { 1404c02db8c6SChris Wright struct ifla_vf_vlan *ivv; 1405c02db8c6SChris Wright ivv = nla_data(vf); 1406c02db8c6SChris Wright err = -EOPNOTSUPP; 1407c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 1408c02db8c6SChris Wright err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1409c02db8c6SChris Wright ivv->vlan, 1410c02db8c6SChris Wright ivv->qos); 1411c02db8c6SChris Wright break; 1412c02db8c6SChris Wright } 1413c02db8c6SChris Wright case IFLA_VF_TX_RATE: { 1414c02db8c6SChris Wright struct ifla_vf_tx_rate *ivt; 1415ed616689SSucheta Chakraborty struct ifla_vf_info ivf; 1416c02db8c6SChris Wright ivt = nla_data(vf); 1417c02db8c6SChris Wright err = -EOPNOTSUPP; 1418ed616689SSucheta Chakraborty if (ops->ndo_get_vf_config) 1419ed616689SSucheta Chakraborty err = ops->ndo_get_vf_config(dev, ivt->vf, 1420ed616689SSucheta Chakraborty &ivf); 1421ed616689SSucheta Chakraborty if (err) 1422ed616689SSucheta Chakraborty break; 1423ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 1424ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 1425ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 1426ed616689SSucheta Chakraborty ivf.min_tx_rate, 1427c02db8c6SChris Wright ivt->rate); 1428c02db8c6SChris Wright break; 1429c02db8c6SChris Wright } 1430ed616689SSucheta Chakraborty case IFLA_VF_RATE: { 1431ed616689SSucheta Chakraborty struct ifla_vf_rate *ivt; 1432ed616689SSucheta Chakraborty ivt = nla_data(vf); 1433ed616689SSucheta Chakraborty err = -EOPNOTSUPP; 1434ed616689SSucheta Chakraborty if (ops->ndo_set_vf_rate) 1435ed616689SSucheta Chakraborty err = ops->ndo_set_vf_rate(dev, ivt->vf, 1436ed616689SSucheta Chakraborty ivt->min_tx_rate, 1437ed616689SSucheta Chakraborty ivt->max_tx_rate); 1438ed616689SSucheta Chakraborty break; 1439ed616689SSucheta Chakraborty } 14405f8444a3SGreg Rose case IFLA_VF_SPOOFCHK: { 14415f8444a3SGreg Rose struct ifla_vf_spoofchk *ivs; 14425f8444a3SGreg Rose ivs = nla_data(vf); 14435f8444a3SGreg Rose err = -EOPNOTSUPP; 14445f8444a3SGreg Rose if (ops->ndo_set_vf_spoofchk) 14455f8444a3SGreg Rose err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 14465f8444a3SGreg Rose ivs->setting); 14475f8444a3SGreg Rose break; 14485f8444a3SGreg Rose } 14491d8faf48SRony Efraim case IFLA_VF_LINK_STATE: { 14501d8faf48SRony Efraim struct ifla_vf_link_state *ivl; 14511d8faf48SRony Efraim ivl = nla_data(vf); 14521d8faf48SRony Efraim err = -EOPNOTSUPP; 14531d8faf48SRony Efraim if (ops->ndo_set_vf_link_state) 14541d8faf48SRony Efraim err = ops->ndo_set_vf_link_state(dev, ivl->vf, 14551d8faf48SRony Efraim ivl->link_state); 14561d8faf48SRony Efraim break; 14571d8faf48SRony Efraim } 1458c02db8c6SChris Wright default: 1459c02db8c6SChris Wright err = -EINVAL; 1460c02db8c6SChris Wright break; 1461c02db8c6SChris Wright } 1462c02db8c6SChris Wright if (err) 1463c02db8c6SChris Wright break; 1464c02db8c6SChris Wright } 1465c02db8c6SChris Wright return err; 1466c02db8c6SChris Wright } 1467c02db8c6SChris Wright 1468fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex) 1469fbaec0eaSJiri Pirko { 1470898e5061SJiri Pirko struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 1471fbaec0eaSJiri Pirko const struct net_device_ops *ops; 1472fbaec0eaSJiri Pirko int err; 1473fbaec0eaSJiri Pirko 1474898e5061SJiri Pirko if (upper_dev) { 1475898e5061SJiri Pirko if (upper_dev->ifindex == ifindex) 1476fbaec0eaSJiri Pirko return 0; 1477898e5061SJiri Pirko ops = upper_dev->netdev_ops; 1478fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 1479898e5061SJiri Pirko err = ops->ndo_del_slave(upper_dev, dev); 1480fbaec0eaSJiri Pirko if (err) 1481fbaec0eaSJiri Pirko return err; 1482fbaec0eaSJiri Pirko } else { 1483fbaec0eaSJiri Pirko return -EOPNOTSUPP; 1484fbaec0eaSJiri Pirko } 1485fbaec0eaSJiri Pirko } 1486fbaec0eaSJiri Pirko 1487fbaec0eaSJiri Pirko if (ifindex) { 1488898e5061SJiri Pirko upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 1489898e5061SJiri Pirko if (!upper_dev) 1490fbaec0eaSJiri Pirko return -EINVAL; 1491898e5061SJiri Pirko ops = upper_dev->netdev_ops; 1492fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 1493898e5061SJiri Pirko err = ops->ndo_add_slave(upper_dev, dev); 1494fbaec0eaSJiri Pirko if (err) 1495fbaec0eaSJiri Pirko return err; 1496fbaec0eaSJiri Pirko } else { 1497fbaec0eaSJiri Pirko return -EOPNOTSUPP; 1498fbaec0eaSJiri Pirko } 1499fbaec0eaSJiri Pirko } 1500fbaec0eaSJiri Pirko return 0; 1501fbaec0eaSJiri Pirko } 1502fbaec0eaSJiri Pirko 150390c325e3SNicolas Dichtel #define DO_SETLINK_MODIFIED 0x01 1504ba998906SNicolas Dichtel /* notify flag means notify + modified. */ 1505ba998906SNicolas Dichtel #define DO_SETLINK_NOTIFY 0x03 150690f62cf3SEric W. Biederman static int do_setlink(const struct sk_buff *skb, 150790f62cf3SEric W. Biederman struct net_device *dev, struct ifinfomsg *ifm, 150890c325e3SNicolas Dichtel struct nlattr **tb, char *ifname, int status) 15090157f60cSPatrick McHardy { 1510d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 15110157f60cSPatrick McHardy int err; 15120157f60cSPatrick McHardy 1513f0630529SEric W. Biederman if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { 151481adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1515d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 1516d8a5ec67SEric W. Biederman err = PTR_ERR(net); 1517d8a5ec67SEric W. Biederman goto errout; 1518d8a5ec67SEric W. Biederman } 151990f62cf3SEric W. Biederman if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { 1520e0ebde0eSNicolas Dichtel put_net(net); 1521b51642f6SEric W. Biederman err = -EPERM; 1522b51642f6SEric W. Biederman goto errout; 1523b51642f6SEric W. Biederman } 1524d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 1525d8a5ec67SEric W. Biederman put_net(net); 1526d8a5ec67SEric W. Biederman if (err) 1527d8a5ec67SEric W. Biederman goto errout; 152890c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 1529d8a5ec67SEric W. Biederman } 1530d8a5ec67SEric W. Biederman 15310157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 15320157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 15330157f60cSPatrick McHardy struct ifmap k_map; 15340157f60cSPatrick McHardy 1535d314774cSStephen Hemminger if (!ops->ndo_set_config) { 15360157f60cSPatrick McHardy err = -EOPNOTSUPP; 15370157f60cSPatrick McHardy goto errout; 15380157f60cSPatrick McHardy } 15390157f60cSPatrick McHardy 15400157f60cSPatrick McHardy if (!netif_device_present(dev)) { 15410157f60cSPatrick McHardy err = -ENODEV; 15420157f60cSPatrick McHardy goto errout; 15430157f60cSPatrick McHardy } 15440157f60cSPatrick McHardy 15450157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 15460157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 15470157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 15480157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 15490157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 15500157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 15510157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 15520157f60cSPatrick McHardy 1553d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 15540157f60cSPatrick McHardy if (err < 0) 15550157f60cSPatrick McHardy goto errout; 15560157f60cSPatrick McHardy 1557ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 15580157f60cSPatrick McHardy } 15590157f60cSPatrick McHardy 15600157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 15610157f60cSPatrick McHardy struct sockaddr *sa; 15620157f60cSPatrick McHardy int len; 15630157f60cSPatrick McHardy 15640157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 15650157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 15660157f60cSPatrick McHardy if (!sa) { 15670157f60cSPatrick McHardy err = -ENOMEM; 15680157f60cSPatrick McHardy goto errout; 15690157f60cSPatrick McHardy } 15700157f60cSPatrick McHardy sa->sa_family = dev->type; 15710157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 15720157f60cSPatrick McHardy dev->addr_len); 1573e7c3273eSJiri Pirko err = dev_set_mac_address(dev, sa); 15740157f60cSPatrick McHardy kfree(sa); 15750157f60cSPatrick McHardy if (err) 15760157f60cSPatrick McHardy goto errout; 157790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 15780157f60cSPatrick McHardy } 15790157f60cSPatrick McHardy 15800157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 15810157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 15820157f60cSPatrick McHardy if (err < 0) 15830157f60cSPatrick McHardy goto errout; 158490c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 15850157f60cSPatrick McHardy } 15860157f60cSPatrick McHardy 1587cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 1588cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1589ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 1590cbda10faSVlad Dogaru } 1591cbda10faSVlad Dogaru 15920157f60cSPatrick McHardy /* 15930157f60cSPatrick McHardy * Interface selected by interface index but interface 15940157f60cSPatrick McHardy * name provided implies that a name change has been 15950157f60cSPatrick McHardy * requested. 15960157f60cSPatrick McHardy */ 15970157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 15980157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 15990157f60cSPatrick McHardy if (err < 0) 16000157f60cSPatrick McHardy goto errout; 160190c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 16020157f60cSPatrick McHardy } 16030157f60cSPatrick McHardy 16040b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 16050b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 16060b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 16070b815a1aSStephen Hemminger if (err < 0) 16080b815a1aSStephen Hemminger goto errout; 1609ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 16100b815a1aSStephen Hemminger } 16110b815a1aSStephen Hemminger 16120157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 16130157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 1614e7c3273eSJiri Pirko call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 16150157f60cSPatrick McHardy } 16160157f60cSPatrick McHardy 16170157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 16183729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 16195f9021cfSJohannes Berg if (err < 0) 16205f9021cfSJohannes Berg goto errout; 16210157f60cSPatrick McHardy } 16220157f60cSPatrick McHardy 1623fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 1624fbaec0eaSJiri Pirko err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 1625fbaec0eaSJiri Pirko if (err) 1626fbaec0eaSJiri Pirko goto errout; 162790c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 1628fbaec0eaSJiri Pirko } 1629fbaec0eaSJiri Pirko 16309a57247fSJiri Pirko if (tb[IFLA_CARRIER]) { 16319a57247fSJiri Pirko err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 16329a57247fSJiri Pirko if (err) 16339a57247fSJiri Pirko goto errout; 163490c325e3SNicolas Dichtel status |= DO_SETLINK_MODIFIED; 16359a57247fSJiri Pirko } 16369a57247fSJiri Pirko 16375d1180fcSNicolas Dichtel if (tb[IFLA_TXQLEN]) { 16385d1180fcSNicolas Dichtel unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); 16395d1180fcSNicolas Dichtel 16405d1180fcSNicolas Dichtel if (dev->tx_queue_len ^ value) 1641ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 16425d1180fcSNicolas Dichtel 16435d1180fcSNicolas Dichtel dev->tx_queue_len = value; 16445d1180fcSNicolas Dichtel } 16450157f60cSPatrick McHardy 16460157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 164793b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 16480157f60cSPatrick McHardy 16490157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 16501889b0e7SNicolas Dichtel unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 16511889b0e7SNicolas Dichtel 16520157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 16531889b0e7SNicolas Dichtel if (dev->link_mode ^ value) 1654ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 16551889b0e7SNicolas Dichtel dev->link_mode = value; 165693b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 16570157f60cSPatrick McHardy } 16580157f60cSPatrick McHardy 1659c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 1660c02db8c6SChris Wright struct nlattr *attr; 1661c02db8c6SChris Wright int rem; 1662c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1663253683bbSDavid Howells if (nla_type(attr) != IFLA_VF_INFO) { 1664253683bbSDavid Howells err = -EINVAL; 1665c02db8c6SChris Wright goto errout; 1666253683bbSDavid Howells } 1667c02db8c6SChris Wright err = do_setvfinfo(dev, attr); 1668ebc08a6fSWilliams, Mitch A if (err < 0) 1669ebc08a6fSWilliams, Mitch A goto errout; 1670ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 1671ebc08a6fSWilliams, Mitch A } 1672ebc08a6fSWilliams, Mitch A } 16730157f60cSPatrick McHardy err = 0; 16740157f60cSPatrick McHardy 167557b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 167657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 167757b61080SScott Feldman struct nlattr *attr; 167857b61080SScott Feldman int vf; 167957b61080SScott Feldman int rem; 168057b61080SScott Feldman 168157b61080SScott Feldman err = -EOPNOTSUPP; 168257b61080SScott Feldman if (!ops->ndo_set_vf_port) 168357b61080SScott Feldman goto errout; 168457b61080SScott Feldman 168557b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 168657b61080SScott Feldman if (nla_type(attr) != IFLA_VF_PORT) 168757b61080SScott Feldman continue; 168857b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 168957b61080SScott Feldman attr, ifla_port_policy); 169057b61080SScott Feldman if (err < 0) 169157b61080SScott Feldman goto errout; 169257b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 169357b61080SScott Feldman err = -EOPNOTSUPP; 169457b61080SScott Feldman goto errout; 169557b61080SScott Feldman } 169657b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 169757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 169857b61080SScott Feldman if (err < 0) 169957b61080SScott Feldman goto errout; 1700ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 170157b61080SScott Feldman } 170257b61080SScott Feldman } 170357b61080SScott Feldman err = 0; 170457b61080SScott Feldman 170557b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 170657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 170757b61080SScott Feldman 170857b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 170957b61080SScott Feldman tb[IFLA_PORT_SELF], ifla_port_policy); 171057b61080SScott Feldman if (err < 0) 171157b61080SScott Feldman goto errout; 171257b61080SScott Feldman 171357b61080SScott Feldman err = -EOPNOTSUPP; 171457b61080SScott Feldman if (ops->ndo_set_vf_port) 171557b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 171657b61080SScott Feldman if (err < 0) 171757b61080SScott Feldman goto errout; 1718ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 171957b61080SScott Feldman } 1720f8ff182cSThomas Graf 1721f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 1722f8ff182cSThomas Graf struct nlattr *af; 1723f8ff182cSThomas Graf int rem; 1724f8ff182cSThomas Graf 1725f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1726f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 1727f8ff182cSThomas Graf 1728f8ff182cSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1729cf7afbfeSThomas Graf BUG(); 1730f8ff182cSThomas Graf 1731cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 1732f8ff182cSThomas Graf if (err < 0) 1733f8ff182cSThomas Graf goto errout; 1734f8ff182cSThomas Graf 1735ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 1736f8ff182cSThomas Graf } 1737f8ff182cSThomas Graf } 173857b61080SScott Feldman err = 0; 173957b61080SScott Feldman 17400157f60cSPatrick McHardy errout: 1741ba998906SNicolas Dichtel if (status & DO_SETLINK_MODIFIED) { 1742ba998906SNicolas Dichtel if (status & DO_SETLINK_NOTIFY) 1743ba998906SNicolas Dichtel netdev_state_change(dev); 1744ba998906SNicolas Dichtel 1745ba998906SNicolas Dichtel if (err < 0) 1746e87cc472SJoe 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", 1747e87cc472SJoe Perches dev->name); 1748ba998906SNicolas Dichtel } 17490157f60cSPatrick McHardy 17500157f60cSPatrick McHardy return err; 17510157f60cSPatrick McHardy } 17520157f60cSPatrick McHardy 1753661d2967SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) 1754da5e0494SThomas Graf { 17553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1756da5e0494SThomas Graf struct ifinfomsg *ifm; 1757da5e0494SThomas Graf struct net_device *dev; 17580157f60cSPatrick McHardy int err; 1759da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 17601da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 17611da177e4SLinus Torvalds 1762da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1763da5e0494SThomas Graf if (err < 0) 1764da5e0494SThomas Graf goto errout; 17651da177e4SLinus Torvalds 17665176f91eSThomas Graf if (tb[IFLA_IFNAME]) 17675176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 176878e5b891SPatrick McHardy else 176978e5b891SPatrick McHardy ifname[0] = '\0'; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds err = -EINVAL; 1772da5e0494SThomas Graf ifm = nlmsg_data(nlh); 177351055be8SPatrick McHardy if (ifm->ifi_index > 0) 1774a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1775da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1776a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1777da5e0494SThomas Graf else 1778da5e0494SThomas Graf goto errout; 17791da177e4SLinus Torvalds 1780da5e0494SThomas Graf if (dev == NULL) { 1781da5e0494SThomas Graf err = -ENODEV; 1782da5e0494SThomas Graf goto errout; 1783da5e0494SThomas Graf } 17841da177e4SLinus Torvalds 1785e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1786e0d087afSEric Dumazet if (err < 0) 1787a3d12891SEric Dumazet goto errout; 1788da5e0494SThomas Graf 178990f62cf3SEric W. Biederman err = do_setlink(skb, dev, ifm, tb, ifname, 0); 1790da5e0494SThomas Graf errout: 17911da177e4SLinus Torvalds return err; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 1794661d2967SThomas Graf static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) 179538f7b870SPatrick McHardy { 17963b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 179738f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 179838f7b870SPatrick McHardy struct net_device *dev; 179938f7b870SPatrick McHardy struct ifinfomsg *ifm; 180038f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 180138f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 180238f7b870SPatrick McHardy int err; 1803226bd341SEric Dumazet LIST_HEAD(list_kill); 180438f7b870SPatrick McHardy 180538f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 180638f7b870SPatrick McHardy if (err < 0) 180738f7b870SPatrick McHardy return err; 180838f7b870SPatrick McHardy 180938f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 181038f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 181138f7b870SPatrick McHardy 181238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 181338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1814881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 181538f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1816881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 181738f7b870SPatrick McHardy else 181838f7b870SPatrick McHardy return -EINVAL; 181938f7b870SPatrick McHardy 182038f7b870SPatrick McHardy if (!dev) 182138f7b870SPatrick McHardy return -ENODEV; 182238f7b870SPatrick McHardy 182338f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 1824b0ab2fabSJiri Pirko if (!ops || !ops->dellink) 182538f7b870SPatrick McHardy return -EOPNOTSUPP; 182638f7b870SPatrick McHardy 1827226bd341SEric Dumazet ops->dellink(dev, &list_kill); 1828226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 182938f7b870SPatrick McHardy return 0; 183038f7b870SPatrick McHardy } 183138f7b870SPatrick McHardy 18323729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 18333729d502SPatrick McHardy { 18343729d502SPatrick McHardy unsigned int old_flags; 18353729d502SPatrick McHardy int err; 18363729d502SPatrick McHardy 18373729d502SPatrick McHardy old_flags = dev->flags; 18383729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 18393729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 18403729d502SPatrick McHardy if (err < 0) 18413729d502SPatrick McHardy return err; 18423729d502SPatrick McHardy } 18433729d502SPatrick McHardy 18443729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 18453729d502SPatrick McHardy 1846a528c219SNicolas Dichtel __dev_notify_flags(dev, old_flags, ~0U); 18473729d502SPatrick McHardy return 0; 18483729d502SPatrick McHardy } 18493729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 18503729d502SPatrick McHardy 1851c0713563SRami Rosen struct net_device *rtnl_create_link(struct net *net, 18525517750fSTom Gundersen char *ifname, unsigned char name_assign_type, 18535517750fSTom Gundersen const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1854e7199288SPavel Emelianov { 1855e7199288SPavel Emelianov int err; 1856e7199288SPavel Emelianov struct net_device *dev; 1857d40156aaSJiri Pirko unsigned int num_tx_queues = 1; 1858d40156aaSJiri Pirko unsigned int num_rx_queues = 1; 1859e7199288SPavel Emelianov 186076ff5cc9SJiri Pirko if (tb[IFLA_NUM_TX_QUEUES]) 186176ff5cc9SJiri Pirko num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 186276ff5cc9SJiri Pirko else if (ops->get_num_tx_queues) 1863d40156aaSJiri Pirko num_tx_queues = ops->get_num_tx_queues(); 186476ff5cc9SJiri Pirko 186576ff5cc9SJiri Pirko if (tb[IFLA_NUM_RX_QUEUES]) 186676ff5cc9SJiri Pirko num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 186776ff5cc9SJiri Pirko else if (ops->get_num_rx_queues) 1868d40156aaSJiri Pirko num_rx_queues = ops->get_num_rx_queues(); 1869efacb309Sstephen hemminger 1870e7199288SPavel Emelianov err = -ENOMEM; 18715517750fSTom Gundersen dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 1872c835a677STom Gundersen ops->setup, num_tx_queues, num_rx_queues); 1873e7199288SPavel Emelianov if (!dev) 1874e7199288SPavel Emelianov goto err; 1875e7199288SPavel Emelianov 187681adee47SEric W. Biederman dev_net_set(dev, net); 187781adee47SEric W. Biederman dev->rtnl_link_ops = ops; 18783729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 187981adee47SEric W. Biederman 1880e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1881e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 18822afb9b53SJiri Pirko if (tb[IFLA_ADDRESS]) { 1883e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1884e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 18852afb9b53SJiri Pirko dev->addr_assign_type = NET_ADDR_SET; 18862afb9b53SJiri Pirko } 1887e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1888e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1889e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1890e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1891e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1892e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 189393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1894e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1895e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1896ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 1897ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1898e7199288SPavel Emelianov 1899e7199288SPavel Emelianov return dev; 1900e7199288SPavel Emelianov 1901e7199288SPavel Emelianov err: 1902e7199288SPavel Emelianov return ERR_PTR(err); 1903e7199288SPavel Emelianov } 1904e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1905e7199288SPavel Emelianov 190690f62cf3SEric W. Biederman static int rtnl_group_changelink(const struct sk_buff *skb, 190790f62cf3SEric W. Biederman struct net *net, int group, 1908e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 1909e7ed828fSVlad Dogaru struct nlattr **tb) 1910e7ed828fSVlad Dogaru { 1911e7ed828fSVlad Dogaru struct net_device *dev; 1912e7ed828fSVlad Dogaru int err; 1913e7ed828fSVlad Dogaru 1914e7ed828fSVlad Dogaru for_each_netdev(net, dev) { 1915e7ed828fSVlad Dogaru if (dev->group == group) { 191690f62cf3SEric W. Biederman err = do_setlink(skb, dev, ifm, tb, NULL, 0); 1917e7ed828fSVlad Dogaru if (err < 0) 1918e7ed828fSVlad Dogaru return err; 1919e7ed828fSVlad Dogaru } 1920e7ed828fSVlad Dogaru } 1921e7ed828fSVlad Dogaru 1922e7ed828fSVlad Dogaru return 0; 1923e7ed828fSVlad Dogaru } 1924e7ed828fSVlad Dogaru 1925661d2967SThomas Graf static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) 192638f7b870SPatrick McHardy { 19273b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 192838f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 1929ba7d49b1SJiri Pirko const struct rtnl_link_ops *m_ops = NULL; 193038f7b870SPatrick McHardy struct net_device *dev; 1931ba7d49b1SJiri Pirko struct net_device *master_dev = NULL; 193238f7b870SPatrick McHardy struct ifinfomsg *ifm; 193338f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 193438f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 193538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 193638f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 19375517750fSTom Gundersen unsigned char name_assign_type = NET_NAME_USER; 193838f7b870SPatrick McHardy int err; 193938f7b870SPatrick McHardy 194095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 194138f7b870SPatrick McHardy replay: 19428072f085SThomas Graf #endif 194338f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 194438f7b870SPatrick McHardy if (err < 0) 194538f7b870SPatrick McHardy return err; 194638f7b870SPatrick McHardy 194738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 194838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 194938f7b870SPatrick McHardy else 195038f7b870SPatrick McHardy ifname[0] = '\0'; 195138f7b870SPatrick McHardy 195238f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 195338f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1954881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 1955e7ed828fSVlad Dogaru else { 1956e7ed828fSVlad Dogaru if (ifname[0]) 1957881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 195838f7b870SPatrick McHardy else 195938f7b870SPatrick McHardy dev = NULL; 1960e7ed828fSVlad Dogaru } 196138f7b870SPatrick McHardy 1962ba7d49b1SJiri Pirko if (dev) { 1963ba7d49b1SJiri Pirko master_dev = netdev_master_upper_dev_get(dev); 1964ba7d49b1SJiri Pirko if (master_dev) 1965ba7d49b1SJiri Pirko m_ops = master_dev->rtnl_link_ops; 1966ba7d49b1SJiri Pirko } 1967ba7d49b1SJiri Pirko 1968e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1969e0d087afSEric Dumazet if (err < 0) 19701840bb13SThomas Graf return err; 19711840bb13SThomas Graf 197238f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 197338f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 197438f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 197538f7b870SPatrick McHardy if (err < 0) 197638f7b870SPatrick McHardy return err; 197738f7b870SPatrick McHardy } else 197838f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 197938f7b870SPatrick McHardy 198038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 198138f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 198238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 198338f7b870SPatrick McHardy } else { 198438f7b870SPatrick McHardy kind[0] = '\0'; 198538f7b870SPatrick McHardy ops = NULL; 198638f7b870SPatrick McHardy } 198738f7b870SPatrick McHardy 198838f7b870SPatrick McHardy if (1) { 1989ba7d49b1SJiri Pirko struct nlattr *attr[ops ? ops->maxtype + 1 : 0]; 1990ba7d49b1SJiri Pirko struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0]; 1991ba7d49b1SJiri Pirko struct nlattr **data = NULL; 1992ba7d49b1SJiri Pirko struct nlattr **slave_data = NULL; 199381adee47SEric W. Biederman struct net *dest_net; 199438f7b870SPatrick McHardy 199538f7b870SPatrick McHardy if (ops) { 199638f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 199738f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 199838f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 199938f7b870SPatrick McHardy ops->policy); 200038f7b870SPatrick McHardy if (err < 0) 200138f7b870SPatrick McHardy return err; 200238f7b870SPatrick McHardy data = attr; 200338f7b870SPatrick McHardy } 200438f7b870SPatrick McHardy if (ops->validate) { 200538f7b870SPatrick McHardy err = ops->validate(tb, data); 200638f7b870SPatrick McHardy if (err < 0) 200738f7b870SPatrick McHardy return err; 200838f7b870SPatrick McHardy } 200938f7b870SPatrick McHardy } 201038f7b870SPatrick McHardy 2011ba7d49b1SJiri Pirko if (m_ops) { 2012ba7d49b1SJiri Pirko if (m_ops->slave_maxtype && 2013ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA]) { 2014ba7d49b1SJiri Pirko err = nla_parse_nested(slave_attr, 2015ba7d49b1SJiri Pirko m_ops->slave_maxtype, 2016ba7d49b1SJiri Pirko linkinfo[IFLA_INFO_SLAVE_DATA], 2017ba7d49b1SJiri Pirko m_ops->slave_policy); 2018ba7d49b1SJiri Pirko if (err < 0) 2019ba7d49b1SJiri Pirko return err; 2020ba7d49b1SJiri Pirko slave_data = slave_attr; 2021ba7d49b1SJiri Pirko } 2022ba7d49b1SJiri Pirko if (m_ops->slave_validate) { 2023ba7d49b1SJiri Pirko err = m_ops->slave_validate(tb, slave_data); 2024ba7d49b1SJiri Pirko if (err < 0) 2025ba7d49b1SJiri Pirko return err; 2026ba7d49b1SJiri Pirko } 2027ba7d49b1SJiri Pirko } 2028ba7d49b1SJiri Pirko 202938f7b870SPatrick McHardy if (dev) { 203090c325e3SNicolas Dichtel int status = 0; 203138f7b870SPatrick McHardy 203238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 203338f7b870SPatrick McHardy return -EEXIST; 203438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 203538f7b870SPatrick McHardy return -EOPNOTSUPP; 203638f7b870SPatrick McHardy 203738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 203838f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 203938f7b870SPatrick McHardy !ops->changelink) 204038f7b870SPatrick McHardy return -EOPNOTSUPP; 204138f7b870SPatrick McHardy 204238f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 204338f7b870SPatrick McHardy if (err < 0) 204438f7b870SPatrick McHardy return err; 2045ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 204638f7b870SPatrick McHardy } 204738f7b870SPatrick McHardy 2048ba7d49b1SJiri Pirko if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2049ba7d49b1SJiri Pirko if (!m_ops || !m_ops->slave_changelink) 2050ba7d49b1SJiri Pirko return -EOPNOTSUPP; 2051ba7d49b1SJiri Pirko 2052ba7d49b1SJiri Pirko err = m_ops->slave_changelink(master_dev, dev, 2053ba7d49b1SJiri Pirko tb, slave_data); 2054ba7d49b1SJiri Pirko if (err < 0) 2055ba7d49b1SJiri Pirko return err; 2056ba998906SNicolas Dichtel status |= DO_SETLINK_NOTIFY; 2057ba7d49b1SJiri Pirko } 2058ba7d49b1SJiri Pirko 205990c325e3SNicolas Dichtel return do_setlink(skb, dev, ifm, tb, ifname, status); 206038f7b870SPatrick McHardy } 206138f7b870SPatrick McHardy 2062ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2063ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 206490f62cf3SEric W. Biederman return rtnl_group_changelink(skb, net, 2065ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 2066ffa934f1SPatrick McHardy ifm, tb); 206738f7b870SPatrick McHardy return -ENODEV; 2068ffa934f1SPatrick McHardy } 206938f7b870SPatrick McHardy 20700e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 207138f7b870SPatrick McHardy return -EOPNOTSUPP; 207238f7b870SPatrick McHardy 207338f7b870SPatrick McHardy if (!ops) { 207495a5afcaSJohannes Berg #ifdef CONFIG_MODULES 207538f7b870SPatrick McHardy if (kind[0]) { 207638f7b870SPatrick McHardy __rtnl_unlock(); 207738f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 207838f7b870SPatrick McHardy rtnl_lock(); 207938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 208038f7b870SPatrick McHardy if (ops) 208138f7b870SPatrick McHardy goto replay; 208238f7b870SPatrick McHardy } 208338f7b870SPatrick McHardy #endif 208438f7b870SPatrick McHardy return -EOPNOTSUPP; 208538f7b870SPatrick McHardy } 208638f7b870SPatrick McHardy 2087b0ab2fabSJiri Pirko if (!ops->setup) 2088b0ab2fabSJiri Pirko return -EOPNOTSUPP; 2089b0ab2fabSJiri Pirko 20905517750fSTom Gundersen if (!ifname[0]) { 209138f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 20925517750fSTom Gundersen name_assign_type = NET_NAME_ENUM; 20935517750fSTom Gundersen } 209438f7b870SPatrick McHardy 209581adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 209613ad1774SEric W. Biederman if (IS_ERR(dest_net)) 209713ad1774SEric W. Biederman return PTR_ERR(dest_net); 209813ad1774SEric W. Biederman 20995517750fSTom Gundersen dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb); 21009c7dafbfSPavel Emelyanov if (IS_ERR(dev)) { 2101e7199288SPavel Emelianov err = PTR_ERR(dev); 21029c7dafbfSPavel Emelyanov goto out; 21039c7dafbfSPavel Emelyanov } 21049c7dafbfSPavel Emelyanov 21059c7dafbfSPavel Emelyanov dev->ifindex = ifm->ifi_index; 21069c7dafbfSPavel Emelyanov 21070e0eee24SCong Wang if (ops->newlink) { 210881adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 21090e0eee24SCong Wang /* Drivers should call free_netdev() in ->destructor 2110e51fb152SCong Wang * and unregister it on failure after registration 2111e51fb152SCong Wang * so that device could be finally freed in rtnl_unlock. 21120e0eee24SCong Wang */ 2113e51fb152SCong Wang if (err < 0) { 2114e51fb152SCong Wang /* If device is not registered at all, free it now */ 2115e51fb152SCong Wang if (dev->reg_state == NETREG_UNINITIALIZED) 2116e51fb152SCong Wang free_netdev(dev); 21170e0eee24SCong Wang goto out; 2118e51fb152SCong Wang } 21190e0eee24SCong Wang } else { 21202d85cba2SPatrick McHardy err = register_netdevice(dev); 2121fce9b9beSDan Carpenter if (err < 0) { 212238f7b870SPatrick McHardy free_netdev(dev); 21233729d502SPatrick McHardy goto out; 2124fce9b9beSDan Carpenter } 21250e0eee24SCong Wang } 21263729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 21273729d502SPatrick McHardy if (err < 0) 21283729d502SPatrick McHardy unregister_netdevice(dev); 21293729d502SPatrick McHardy out: 213081adee47SEric W. Biederman put_net(dest_net); 213138f7b870SPatrick McHardy return err; 213238f7b870SPatrick McHardy } 213338f7b870SPatrick McHardy } 213438f7b870SPatrick McHardy 2135661d2967SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) 2136711e2c33SJean Tourrilhes { 21373b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 2138b60c5115SThomas Graf struct ifinfomsg *ifm; 2139a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 2140b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 2141b60c5115SThomas Graf struct net_device *dev = NULL; 2142b60c5115SThomas Graf struct sk_buff *nskb; 2143339bf98fSThomas Graf int err; 2144115c9b81SGreg Rose u32 ext_filter_mask = 0; 2145711e2c33SJean Tourrilhes 2146b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 2147b60c5115SThomas Graf if (err < 0) 21489918f230SEric Sesterhenn return err; 2149b60c5115SThomas Graf 2150a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 2151a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2152a3d12891SEric Dumazet 2153115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 2154115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2155115c9b81SGreg Rose 2156b60c5115SThomas Graf ifm = nlmsg_data(nlh); 2157a3d12891SEric Dumazet if (ifm->ifi_index > 0) 2158a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 2159a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 2160a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 2161a3d12891SEric Dumazet else 2162b60c5115SThomas Graf return -EINVAL; 2163b60c5115SThomas Graf 2164a3d12891SEric Dumazet if (dev == NULL) 2165a3d12891SEric Dumazet return -ENODEV; 2166a3d12891SEric Dumazet 2167115c9b81SGreg Rose nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 2168a3d12891SEric Dumazet if (nskb == NULL) 2169a3d12891SEric Dumazet return -ENOBUFS; 2170711e2c33SJean Tourrilhes 217115e47304SEric W. Biederman err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, 2172115c9b81SGreg Rose nlh->nlmsg_seq, 0, 0, ext_filter_mask); 217326932566SPatrick McHardy if (err < 0) { 217426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 217526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 217626932566SPatrick McHardy kfree_skb(nskb); 2177a3d12891SEric Dumazet } else 217815e47304SEric W. Biederman err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 2179b60c5115SThomas Graf 2180711e2c33SJean Tourrilhes return err; 2181711e2c33SJean Tourrilhes } 2182711e2c33SJean Tourrilhes 2183115c9b81SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 2184c7ac8679SGreg Rose { 2185115c9b81SGreg Rose struct net *net = sock_net(skb->sk); 2186115c9b81SGreg Rose struct net_device *dev; 2187115c9b81SGreg Rose struct nlattr *tb[IFLA_MAX+1]; 2188115c9b81SGreg Rose u32 ext_filter_mask = 0; 2189115c9b81SGreg Rose u16 min_ifinfo_dump_size = 0; 2190e5eca6d4SMichal Schmidt int hdrlen; 2191115c9b81SGreg Rose 2192e5eca6d4SMichal Schmidt /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 2193e5eca6d4SMichal Schmidt hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2194e5eca6d4SMichal Schmidt sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2195e5eca6d4SMichal Schmidt 2196e5eca6d4SMichal Schmidt if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { 2197115c9b81SGreg Rose if (tb[IFLA_EXT_MASK]) 2198115c9b81SGreg Rose ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2199a4b64fbeSEric Dumazet } 2200115c9b81SGreg Rose 2201115c9b81SGreg Rose if (!ext_filter_mask) 2202115c9b81SGreg Rose return NLMSG_GOODSIZE; 2203115c9b81SGreg Rose /* 2204115c9b81SGreg Rose * traverse the list of net devices and compute the minimum 2205115c9b81SGreg Rose * buffer size based upon the filter mask. 2206115c9b81SGreg Rose */ 2207115c9b81SGreg Rose list_for_each_entry(dev, &net->dev_base_head, dev_list) { 2208115c9b81SGreg Rose min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 2209115c9b81SGreg Rose if_nlmsg_size(dev, 2210115c9b81SGreg Rose ext_filter_mask)); 2211115c9b81SGreg Rose } 2212115c9b81SGreg Rose 2213c7ac8679SGreg Rose return min_ifinfo_dump_size; 2214c7ac8679SGreg Rose } 2215c7ac8679SGreg Rose 221642bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 22171da177e4SLinus Torvalds { 22181da177e4SLinus Torvalds int idx; 22191da177e4SLinus Torvalds int s_idx = cb->family; 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds if (s_idx == 0) 22221da177e4SLinus Torvalds s_idx = 1; 222325239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 22241da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 22251da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 22261da177e4SLinus Torvalds continue; 2227e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 2228e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 22291da177e4SLinus Torvalds continue; 22300465277fSNicolas Dichtel if (idx > s_idx) { 22311da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 22320465277fSNicolas Dichtel cb->prev_seq = 0; 22330465277fSNicolas Dichtel cb->seq = 0; 22340465277fSNicolas Dichtel } 2235e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 22361da177e4SLinus Torvalds break; 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds cb->family = idx; 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds return skb->len; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 2243395eea6cSMahesh Bandewar struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 2244395eea6cSMahesh Bandewar unsigned int change, gfp_t flags) 22451da177e4SLinus Torvalds { 2246c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 22471da177e4SLinus Torvalds struct sk_buff *skb; 22480ec6d3f4SThomas Graf int err = -ENOBUFS; 2249c7ac8679SGreg Rose size_t if_info_size; 22501da177e4SLinus Torvalds 22517f294054SAlexei Starovoitov skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 22520ec6d3f4SThomas Graf if (skb == NULL) 22530ec6d3f4SThomas Graf goto errout; 22541da177e4SLinus Torvalds 2255115c9b81SGreg Rose err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); 225626932566SPatrick McHardy if (err < 0) { 225726932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 225826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 225926932566SPatrick McHardy kfree_skb(skb); 226026932566SPatrick McHardy goto errout; 226126932566SPatrick McHardy } 2262395eea6cSMahesh Bandewar return skb; 22630ec6d3f4SThomas Graf errout: 22640ec6d3f4SThomas Graf if (err < 0) 22654b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 2266395eea6cSMahesh Bandewar return NULL; 2267395eea6cSMahesh Bandewar } 2268395eea6cSMahesh Bandewar 2269395eea6cSMahesh Bandewar void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 2270395eea6cSMahesh Bandewar { 2271395eea6cSMahesh Bandewar struct net *net = dev_net(dev); 2272395eea6cSMahesh Bandewar 2273395eea6cSMahesh Bandewar rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 2274395eea6cSMahesh Bandewar } 2275395eea6cSMahesh Bandewar 2276395eea6cSMahesh Bandewar void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 2277395eea6cSMahesh Bandewar gfp_t flags) 2278395eea6cSMahesh Bandewar { 2279395eea6cSMahesh Bandewar struct sk_buff *skb; 2280395eea6cSMahesh Bandewar 2281395eea6cSMahesh Bandewar skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); 2282395eea6cSMahesh Bandewar if (skb) 2283395eea6cSMahesh Bandewar rtmsg_ifinfo_send(skb, dev, flags); 22841da177e4SLinus Torvalds } 2285471cb5a3SJiri Pirko EXPORT_SYMBOL(rtmsg_ifinfo); 22861da177e4SLinus Torvalds 2287d83b0603SJohn Fastabend static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 2288d83b0603SJohn Fastabend struct net_device *dev, 2289d83b0603SJohn Fastabend u8 *addr, u32 pid, u32 seq, 22901c104a6bSNicolas Dichtel int type, unsigned int flags, 22911c104a6bSNicolas Dichtel int nlflags) 2292d83b0603SJohn Fastabend { 2293d83b0603SJohn Fastabend struct nlmsghdr *nlh; 2294d83b0603SJohn Fastabend struct ndmsg *ndm; 2295d83b0603SJohn Fastabend 22961c104a6bSNicolas Dichtel nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 2297d83b0603SJohn Fastabend if (!nlh) 2298d83b0603SJohn Fastabend return -EMSGSIZE; 2299d83b0603SJohn Fastabend 2300d83b0603SJohn Fastabend ndm = nlmsg_data(nlh); 2301d83b0603SJohn Fastabend ndm->ndm_family = AF_BRIDGE; 2302d83b0603SJohn Fastabend ndm->ndm_pad1 = 0; 2303d83b0603SJohn Fastabend ndm->ndm_pad2 = 0; 2304d83b0603SJohn Fastabend ndm->ndm_flags = flags; 2305d83b0603SJohn Fastabend ndm->ndm_type = 0; 2306d83b0603SJohn Fastabend ndm->ndm_ifindex = dev->ifindex; 2307d83b0603SJohn Fastabend ndm->ndm_state = NUD_PERMANENT; 2308d83b0603SJohn Fastabend 2309d83b0603SJohn Fastabend if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 2310d83b0603SJohn Fastabend goto nla_put_failure; 2311d83b0603SJohn Fastabend 2312d83b0603SJohn Fastabend return nlmsg_end(skb, nlh); 2313d83b0603SJohn Fastabend 2314d83b0603SJohn Fastabend nla_put_failure: 2315d83b0603SJohn Fastabend nlmsg_cancel(skb, nlh); 2316d83b0603SJohn Fastabend return -EMSGSIZE; 2317d83b0603SJohn Fastabend } 2318d83b0603SJohn Fastabend 23193ff661c3SJohn Fastabend static inline size_t rtnl_fdb_nlmsg_size(void) 23203ff661c3SJohn Fastabend { 23213ff661c3SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); 23223ff661c3SJohn Fastabend } 23233ff661c3SJohn Fastabend 23243ff661c3SJohn Fastabend static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type) 23253ff661c3SJohn Fastabend { 23263ff661c3SJohn Fastabend struct net *net = dev_net(dev); 23273ff661c3SJohn Fastabend struct sk_buff *skb; 23283ff661c3SJohn Fastabend int err = -ENOBUFS; 23293ff661c3SJohn Fastabend 23303ff661c3SJohn Fastabend skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 23313ff661c3SJohn Fastabend if (!skb) 23323ff661c3SJohn Fastabend goto errout; 23333ff661c3SJohn Fastabend 23341c104a6bSNicolas Dichtel err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0); 23353ff661c3SJohn Fastabend if (err < 0) { 23363ff661c3SJohn Fastabend kfree_skb(skb); 23373ff661c3SJohn Fastabend goto errout; 23383ff661c3SJohn Fastabend } 23393ff661c3SJohn Fastabend 23403ff661c3SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 23413ff661c3SJohn Fastabend return; 23423ff661c3SJohn Fastabend errout: 23433ff661c3SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 23443ff661c3SJohn Fastabend } 23453ff661c3SJohn Fastabend 2346090096bfSVlad Yasevich /** 2347090096bfSVlad Yasevich * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 2348090096bfSVlad Yasevich */ 2349090096bfSVlad Yasevich int ndo_dflt_fdb_add(struct ndmsg *ndm, 2350090096bfSVlad Yasevich struct nlattr *tb[], 2351090096bfSVlad Yasevich struct net_device *dev, 2352f6f6424bSJiri Pirko const unsigned char *addr, u16 vid, 2353090096bfSVlad Yasevich u16 flags) 2354090096bfSVlad Yasevich { 2355090096bfSVlad Yasevich int err = -EINVAL; 2356090096bfSVlad Yasevich 2357090096bfSVlad Yasevich /* If aging addresses are supported device will need to 2358090096bfSVlad Yasevich * implement its own handler for this. 2359090096bfSVlad Yasevich */ 2360090096bfSVlad Yasevich if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 2361090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 2362090096bfSVlad Yasevich return err; 2363090096bfSVlad Yasevich } 2364090096bfSVlad Yasevich 236565891feaSOr Gerlitz if (vid) { 236665891feaSOr Gerlitz pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 236765891feaSOr Gerlitz return err; 236865891feaSOr Gerlitz } 236965891feaSOr Gerlitz 2370090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 2371090096bfSVlad Yasevich err = dev_uc_add_excl(dev, addr); 2372090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 2373090096bfSVlad Yasevich err = dev_mc_add_excl(dev, addr); 2374090096bfSVlad Yasevich 2375090096bfSVlad Yasevich /* Only return duplicate errors if NLM_F_EXCL is set */ 2376090096bfSVlad Yasevich if (err == -EEXIST && !(flags & NLM_F_EXCL)) 2377090096bfSVlad Yasevich err = 0; 2378090096bfSVlad Yasevich 2379090096bfSVlad Yasevich return err; 2380090096bfSVlad Yasevich } 2381090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_add); 2382090096bfSVlad Yasevich 2383f6f6424bSJiri Pirko static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) 2384f6f6424bSJiri Pirko { 2385f6f6424bSJiri Pirko u16 vid = 0; 2386f6f6424bSJiri Pirko 2387f6f6424bSJiri Pirko if (vlan_attr) { 2388f6f6424bSJiri Pirko if (nla_len(vlan_attr) != sizeof(u16)) { 2389f6f6424bSJiri Pirko pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); 2390f6f6424bSJiri Pirko return -EINVAL; 2391f6f6424bSJiri Pirko } 2392f6f6424bSJiri Pirko 2393f6f6424bSJiri Pirko vid = nla_get_u16(vlan_attr); 2394f6f6424bSJiri Pirko 2395f6f6424bSJiri Pirko if (!vid || vid >= VLAN_VID_MASK) { 2396f6f6424bSJiri Pirko pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", 2397f6f6424bSJiri Pirko vid); 2398f6f6424bSJiri Pirko return -EINVAL; 2399f6f6424bSJiri Pirko } 2400f6f6424bSJiri Pirko } 2401f6f6424bSJiri Pirko *p_vid = vid; 2402f6f6424bSJiri Pirko return 0; 2403f6f6424bSJiri Pirko } 2404f6f6424bSJiri Pirko 2405661d2967SThomas Graf static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) 240677162022SJohn Fastabend { 240777162022SJohn Fastabend struct net *net = sock_net(skb->sk); 240877162022SJohn Fastabend struct ndmsg *ndm; 240977162022SJohn Fastabend struct nlattr *tb[NDA_MAX+1]; 241077162022SJohn Fastabend struct net_device *dev; 241177162022SJohn Fastabend u8 *addr; 2412f6f6424bSJiri Pirko u16 vid; 241377162022SJohn Fastabend int err; 241477162022SJohn Fastabend 241577162022SJohn Fastabend err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 241677162022SJohn Fastabend if (err < 0) 241777162022SJohn Fastabend return err; 241877162022SJohn Fastabend 241977162022SJohn Fastabend ndm = nlmsg_data(nlh); 242077162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 242177162022SJohn Fastabend pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); 242277162022SJohn Fastabend return -EINVAL; 242377162022SJohn Fastabend } 242477162022SJohn Fastabend 242577162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 242677162022SJohn Fastabend if (dev == NULL) { 242777162022SJohn Fastabend pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); 242877162022SJohn Fastabend return -ENODEV; 242977162022SJohn Fastabend } 243077162022SJohn Fastabend 243177162022SJohn Fastabend if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 243277162022SJohn Fastabend pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); 243377162022SJohn Fastabend return -EINVAL; 243477162022SJohn Fastabend } 243577162022SJohn Fastabend 243677162022SJohn Fastabend addr = nla_data(tb[NDA_LLADDR]); 243777162022SJohn Fastabend 2438f6f6424bSJiri Pirko err = fdb_vid_parse(tb[NDA_VLAN], &vid); 2439f6f6424bSJiri Pirko if (err) 2440f6f6424bSJiri Pirko return err; 2441f6f6424bSJiri Pirko 244277162022SJohn Fastabend err = -EOPNOTSUPP; 244377162022SJohn Fastabend 244477162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 244577162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 244677162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 2447898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2448898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 2449898e5061SJiri Pirko 2450f6f6424bSJiri Pirko err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 2451f6f6424bSJiri Pirko nlh->nlmsg_flags); 245277162022SJohn Fastabend if (err) 245377162022SJohn Fastabend goto out; 245477162022SJohn Fastabend else 245577162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 245677162022SJohn Fastabend } 245777162022SJohn Fastabend 245877162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 2459090096bfSVlad Yasevich if ((ndm->ndm_flags & NTF_SELF)) { 2460090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_add) 2461090096bfSVlad Yasevich err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 2462f6f6424bSJiri Pirko vid, 2463090096bfSVlad Yasevich nlh->nlmsg_flags); 2464090096bfSVlad Yasevich else 2465f6f6424bSJiri Pirko err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 246677162022SJohn Fastabend nlh->nlmsg_flags); 246777162022SJohn Fastabend 24683ff661c3SJohn Fastabend if (!err) { 24693ff661c3SJohn Fastabend rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH); 247077162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 247177162022SJohn Fastabend } 24723ff661c3SJohn Fastabend } 247377162022SJohn Fastabend out: 247477162022SJohn Fastabend return err; 247577162022SJohn Fastabend } 247677162022SJohn Fastabend 2477090096bfSVlad Yasevich /** 2478090096bfSVlad Yasevich * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 2479090096bfSVlad Yasevich */ 2480090096bfSVlad Yasevich int ndo_dflt_fdb_del(struct ndmsg *ndm, 2481090096bfSVlad Yasevich struct nlattr *tb[], 2482090096bfSVlad Yasevich struct net_device *dev, 2483f6f6424bSJiri Pirko const unsigned char *addr, u16 vid) 2484090096bfSVlad Yasevich { 2485c8a89c4aSAlexander Duyck int err = -EINVAL; 2486090096bfSVlad Yasevich 2487090096bfSVlad Yasevich /* If aging addresses are supported device will need to 2488090096bfSVlad Yasevich * implement its own handler for this. 2489090096bfSVlad Yasevich */ 249064535993SSridhar Samudrala if (!(ndm->ndm_state & NUD_PERMANENT)) { 2491090096bfSVlad Yasevich pr_info("%s: FDB only supports static addresses\n", dev->name); 2492c8a89c4aSAlexander Duyck return err; 2493090096bfSVlad Yasevich } 2494090096bfSVlad Yasevich 2495090096bfSVlad Yasevich if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 2496090096bfSVlad Yasevich err = dev_uc_del(dev, addr); 2497090096bfSVlad Yasevich else if (is_multicast_ether_addr(addr)) 2498090096bfSVlad Yasevich err = dev_mc_del(dev, addr); 2499090096bfSVlad Yasevich 2500090096bfSVlad Yasevich return err; 2501090096bfSVlad Yasevich } 2502090096bfSVlad Yasevich EXPORT_SYMBOL(ndo_dflt_fdb_del); 2503090096bfSVlad Yasevich 2504661d2967SThomas Graf static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) 250577162022SJohn Fastabend { 250677162022SJohn Fastabend struct net *net = sock_net(skb->sk); 250777162022SJohn Fastabend struct ndmsg *ndm; 25081690be63SVlad Yasevich struct nlattr *tb[NDA_MAX+1]; 250977162022SJohn Fastabend struct net_device *dev; 251077162022SJohn Fastabend int err = -EINVAL; 251177162022SJohn Fastabend __u8 *addr; 2512f6f6424bSJiri Pirko u16 vid; 251377162022SJohn Fastabend 251490f62cf3SEric W. Biederman if (!netlink_capable(skb, CAP_NET_ADMIN)) 25151690be63SVlad Yasevich return -EPERM; 25161690be63SVlad Yasevich 25171690be63SVlad Yasevich err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 25181690be63SVlad Yasevich if (err < 0) 25191690be63SVlad Yasevich return err; 252077162022SJohn Fastabend 252177162022SJohn Fastabend ndm = nlmsg_data(nlh); 252277162022SJohn Fastabend if (ndm->ndm_ifindex == 0) { 252377162022SJohn Fastabend pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); 252477162022SJohn Fastabend return -EINVAL; 252577162022SJohn Fastabend } 252677162022SJohn Fastabend 252777162022SJohn Fastabend dev = __dev_get_by_index(net, ndm->ndm_ifindex); 252877162022SJohn Fastabend if (dev == NULL) { 252977162022SJohn Fastabend pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); 253077162022SJohn Fastabend return -ENODEV; 253177162022SJohn Fastabend } 253277162022SJohn Fastabend 25331690be63SVlad Yasevich if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 25341690be63SVlad Yasevich pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); 253577162022SJohn Fastabend return -EINVAL; 253677162022SJohn Fastabend } 253777162022SJohn Fastabend 25381690be63SVlad Yasevich addr = nla_data(tb[NDA_LLADDR]); 25391690be63SVlad Yasevich 2540f6f6424bSJiri Pirko err = fdb_vid_parse(tb[NDA_VLAN], &vid); 2541f6f6424bSJiri Pirko if (err) 2542f6f6424bSJiri Pirko return err; 2543f6f6424bSJiri Pirko 254477162022SJohn Fastabend err = -EOPNOTSUPP; 254577162022SJohn Fastabend 254677162022SJohn Fastabend /* Support fdb on master device the net/bridge default case */ 254777162022SJohn Fastabend if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 254877162022SJohn Fastabend (dev->priv_flags & IFF_BRIDGE_PORT)) { 2549898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2550898e5061SJiri Pirko const struct net_device_ops *ops = br_dev->netdev_ops; 255177162022SJohn Fastabend 2552898e5061SJiri Pirko if (ops->ndo_fdb_del) 2553f6f6424bSJiri Pirko err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 255477162022SJohn Fastabend 255577162022SJohn Fastabend if (err) 255677162022SJohn Fastabend goto out; 255777162022SJohn Fastabend else 255877162022SJohn Fastabend ndm->ndm_flags &= ~NTF_MASTER; 255977162022SJohn Fastabend } 256077162022SJohn Fastabend 256177162022SJohn Fastabend /* Embedded bridge, macvlan, and any other device support */ 2562090096bfSVlad Yasevich if (ndm->ndm_flags & NTF_SELF) { 2563090096bfSVlad Yasevich if (dev->netdev_ops->ndo_fdb_del) 2564f6f6424bSJiri Pirko err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 2565f6f6424bSJiri Pirko vid); 2566090096bfSVlad Yasevich else 2567f6f6424bSJiri Pirko err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 256877162022SJohn Fastabend 25693ff661c3SJohn Fastabend if (!err) { 25703ff661c3SJohn Fastabend rtnl_fdb_notify(dev, addr, RTM_DELNEIGH); 257177162022SJohn Fastabend ndm->ndm_flags &= ~NTF_SELF; 257277162022SJohn Fastabend } 25733ff661c3SJohn Fastabend } 257477162022SJohn Fastabend out: 257577162022SJohn Fastabend return err; 257677162022SJohn Fastabend } 257777162022SJohn Fastabend 2578d83b0603SJohn Fastabend static int nlmsg_populate_fdb(struct sk_buff *skb, 2579d83b0603SJohn Fastabend struct netlink_callback *cb, 2580d83b0603SJohn Fastabend struct net_device *dev, 2581d83b0603SJohn Fastabend int *idx, 2582d83b0603SJohn Fastabend struct netdev_hw_addr_list *list) 2583d83b0603SJohn Fastabend { 2584d83b0603SJohn Fastabend struct netdev_hw_addr *ha; 2585d83b0603SJohn Fastabend int err; 258615e47304SEric W. Biederman u32 portid, seq; 2587d83b0603SJohn Fastabend 258815e47304SEric W. Biederman portid = NETLINK_CB(cb->skb).portid; 2589d83b0603SJohn Fastabend seq = cb->nlh->nlmsg_seq; 2590d83b0603SJohn Fastabend 2591d83b0603SJohn Fastabend list_for_each_entry(ha, &list->list, list) { 2592d83b0603SJohn Fastabend if (*idx < cb->args[0]) 2593d83b0603SJohn Fastabend goto skip; 2594d83b0603SJohn Fastabend 2595d83b0603SJohn Fastabend err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 2596a7a558feSJohn Fastabend portid, seq, 25971c104a6bSNicolas Dichtel RTM_NEWNEIGH, NTF_SELF, 25981c104a6bSNicolas Dichtel NLM_F_MULTI); 2599d83b0603SJohn Fastabend if (err < 0) 2600d83b0603SJohn Fastabend return err; 2601d83b0603SJohn Fastabend skip: 2602d83b0603SJohn Fastabend *idx += 1; 2603d83b0603SJohn Fastabend } 2604d83b0603SJohn Fastabend return 0; 2605d83b0603SJohn Fastabend } 2606d83b0603SJohn Fastabend 2607d83b0603SJohn Fastabend /** 26082c53040fSBen Hutchings * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 2609d83b0603SJohn Fastabend * @nlh: netlink message header 2610d83b0603SJohn Fastabend * @dev: netdevice 2611d83b0603SJohn Fastabend * 2612d83b0603SJohn Fastabend * Default netdevice operation to dump the existing unicast address list. 261391f3e7b1SJohn Fastabend * Returns number of addresses from list put in skb. 2614d83b0603SJohn Fastabend */ 2615d83b0603SJohn Fastabend int ndo_dflt_fdb_dump(struct sk_buff *skb, 2616d83b0603SJohn Fastabend struct netlink_callback *cb, 2617d83b0603SJohn Fastabend struct net_device *dev, 26185d5eacb3SJamal Hadi Salim struct net_device *filter_dev, 2619d83b0603SJohn Fastabend int idx) 2620d83b0603SJohn Fastabend { 2621d83b0603SJohn Fastabend int err; 2622d83b0603SJohn Fastabend 2623d83b0603SJohn Fastabend netif_addr_lock_bh(dev); 2624d83b0603SJohn Fastabend err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); 2625d83b0603SJohn Fastabend if (err) 2626d83b0603SJohn Fastabend goto out; 2627d83b0603SJohn Fastabend nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); 2628d83b0603SJohn Fastabend out: 2629d83b0603SJohn Fastabend netif_addr_unlock_bh(dev); 2630d83b0603SJohn Fastabend return idx; 2631d83b0603SJohn Fastabend } 2632d83b0603SJohn Fastabend EXPORT_SYMBOL(ndo_dflt_fdb_dump); 2633d83b0603SJohn Fastabend 263477162022SJohn Fastabend static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 263577162022SJohn Fastabend { 263677162022SJohn Fastabend struct net_device *dev; 26375e6d2435SJamal Hadi Salim struct nlattr *tb[IFLA_MAX+1]; 26385e6d2435SJamal Hadi Salim struct net_device *bdev = NULL; 26395e6d2435SJamal Hadi Salim struct net_device *br_dev = NULL; 26405e6d2435SJamal Hadi Salim const struct net_device_ops *ops = NULL; 26415e6d2435SJamal Hadi Salim const struct net_device_ops *cops = NULL; 26425e6d2435SJamal Hadi Salim struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 26435e6d2435SJamal Hadi Salim struct net *net = sock_net(skb->sk); 26445e6d2435SJamal Hadi Salim int brport_idx = 0; 26455e6d2435SJamal Hadi Salim int br_idx = 0; 26465e6d2435SJamal Hadi Salim int idx = 0; 264777162022SJohn Fastabend 26485e6d2435SJamal Hadi Salim if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, 26495e6d2435SJamal Hadi Salim ifla_policy) == 0) { 26505e6d2435SJamal Hadi Salim if (tb[IFLA_MASTER]) 26515e6d2435SJamal Hadi Salim br_idx = nla_get_u32(tb[IFLA_MASTER]); 26525e6d2435SJamal Hadi Salim } 265377162022SJohn Fastabend 26545e6d2435SJamal Hadi Salim brport_idx = ifm->ifi_index; 26555e6d2435SJamal Hadi Salim 26565e6d2435SJamal Hadi Salim if (br_idx) { 26575e6d2435SJamal Hadi Salim br_dev = __dev_get_by_index(net, br_idx); 26585e6d2435SJamal Hadi Salim if (!br_dev) 26595e6d2435SJamal Hadi Salim return -ENODEV; 26605e6d2435SJamal Hadi Salim 2661898e5061SJiri Pirko ops = br_dev->netdev_ops; 26625e6d2435SJamal Hadi Salim bdev = br_dev; 26635e6d2435SJamal Hadi Salim } 26645e6d2435SJamal Hadi Salim 26655e6d2435SJamal Hadi Salim for_each_netdev(net, dev) { 26665e6d2435SJamal Hadi Salim if (brport_idx && (dev->ifindex != brport_idx)) 26675e6d2435SJamal Hadi Salim continue; 26685e6d2435SJamal Hadi Salim 26695e6d2435SJamal Hadi Salim if (!br_idx) { /* user did not specify a specific bridge */ 26705e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 26715e6d2435SJamal Hadi Salim br_dev = netdev_master_upper_dev_get(dev); 26725e6d2435SJamal Hadi Salim cops = br_dev->netdev_ops; 26735e6d2435SJamal Hadi Salim } 26745e6d2435SJamal Hadi Salim 26755e6d2435SJamal Hadi Salim bdev = dev; 26765e6d2435SJamal Hadi Salim } else { 26775e6d2435SJamal Hadi Salim if (dev != br_dev && 26785e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_BRIDGE_PORT)) 26795e6d2435SJamal Hadi Salim continue; 26805e6d2435SJamal Hadi Salim 26815e6d2435SJamal Hadi Salim if (br_dev != netdev_master_upper_dev_get(dev) && 26825e6d2435SJamal Hadi Salim !(dev->priv_flags & IFF_EBRIDGE)) 26835e6d2435SJamal Hadi Salim continue; 26845e6d2435SJamal Hadi Salim 26855e6d2435SJamal Hadi Salim bdev = br_dev; 26865e6d2435SJamal Hadi Salim cops = ops; 26875e6d2435SJamal Hadi Salim } 26885e6d2435SJamal Hadi Salim 26895e6d2435SJamal Hadi Salim if (dev->priv_flags & IFF_BRIDGE_PORT) { 26905e6d2435SJamal Hadi Salim if (cops && cops->ndo_fdb_dump) 26915e6d2435SJamal Hadi Salim idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, 26925d5eacb3SJamal Hadi Salim idx); 269377162022SJohn Fastabend } 269477162022SJohn Fastabend 26955d5eacb3SJamal Hadi Salim idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); 26965e6d2435SJamal Hadi Salim if (dev->netdev_ops->ndo_fdb_dump) 26975e6d2435SJamal Hadi Salim idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev, 26985e6d2435SJamal Hadi Salim idx); 26995e6d2435SJamal Hadi Salim 27005e6d2435SJamal Hadi Salim cops = NULL; 270177162022SJohn Fastabend } 270277162022SJohn Fastabend 270377162022SJohn Fastabend cb->args[0] = idx; 270477162022SJohn Fastabend return skb->len; 270577162022SJohn Fastabend } 270677162022SJohn Fastabend 27072c3c031cSScott Feldman static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 27082c3c031cSScott Feldman unsigned int attrnum, unsigned int flag) 27092c3c031cSScott Feldman { 27102c3c031cSScott Feldman if (mask & flag) 27112c3c031cSScott Feldman return nla_put_u8(skb, attrnum, !!(flags & flag)); 27122c3c031cSScott Feldman return 0; 27132c3c031cSScott Feldman } 27142c3c031cSScott Feldman 2715815cccbfSJohn Fastabend int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 27162c3c031cSScott Feldman struct net_device *dev, u16 mode, 27172c3c031cSScott Feldman u32 flags, u32 mask) 2718815cccbfSJohn Fastabend { 2719815cccbfSJohn Fastabend struct nlmsghdr *nlh; 2720815cccbfSJohn Fastabend struct ifinfomsg *ifm; 2721815cccbfSJohn Fastabend struct nlattr *br_afspec; 27222c3c031cSScott Feldman struct nlattr *protinfo; 2723815cccbfSJohn Fastabend u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 2724898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2725815cccbfSJohn Fastabend 2726815cccbfSJohn Fastabend nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI); 2727815cccbfSJohn Fastabend if (nlh == NULL) 2728815cccbfSJohn Fastabend return -EMSGSIZE; 2729815cccbfSJohn Fastabend 2730815cccbfSJohn Fastabend ifm = nlmsg_data(nlh); 2731815cccbfSJohn Fastabend ifm->ifi_family = AF_BRIDGE; 2732815cccbfSJohn Fastabend ifm->__ifi_pad = 0; 2733815cccbfSJohn Fastabend ifm->ifi_type = dev->type; 2734815cccbfSJohn Fastabend ifm->ifi_index = dev->ifindex; 2735815cccbfSJohn Fastabend ifm->ifi_flags = dev_get_flags(dev); 2736815cccbfSJohn Fastabend ifm->ifi_change = 0; 2737815cccbfSJohn Fastabend 2738815cccbfSJohn Fastabend 2739815cccbfSJohn Fastabend if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 2740815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_MTU, dev->mtu) || 2741815cccbfSJohn Fastabend nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 2742898e5061SJiri Pirko (br_dev && 2743898e5061SJiri Pirko nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 2744815cccbfSJohn Fastabend (dev->addr_len && 2745815cccbfSJohn Fastabend nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 2746815cccbfSJohn Fastabend (dev->ifindex != dev->iflink && 2747815cccbfSJohn Fastabend nla_put_u32(skb, IFLA_LINK, dev->iflink))) 2748815cccbfSJohn Fastabend goto nla_put_failure; 2749815cccbfSJohn Fastabend 2750815cccbfSJohn Fastabend br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 2751815cccbfSJohn Fastabend if (!br_afspec) 2752815cccbfSJohn Fastabend goto nla_put_failure; 2753815cccbfSJohn Fastabend 27541d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 2755815cccbfSJohn Fastabend nla_nest_cancel(skb, br_afspec); 2756815cccbfSJohn Fastabend goto nla_put_failure; 2757815cccbfSJohn Fastabend } 27581d460b98SRoopa Prabhu 27591d460b98SRoopa Prabhu if (mode != BRIDGE_MODE_UNDEF) { 27601d460b98SRoopa Prabhu if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 27611d460b98SRoopa Prabhu nla_nest_cancel(skb, br_afspec); 27621d460b98SRoopa Prabhu goto nla_put_failure; 27631d460b98SRoopa Prabhu } 27641d460b98SRoopa Prabhu } 2765815cccbfSJohn Fastabend nla_nest_end(skb, br_afspec); 2766815cccbfSJohn Fastabend 27672c3c031cSScott Feldman protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 27682c3c031cSScott Feldman if (!protinfo) 27692c3c031cSScott Feldman goto nla_put_failure; 27702c3c031cSScott Feldman 27712c3c031cSScott Feldman if (brport_nla_put_flag(skb, flags, mask, 27722c3c031cSScott Feldman IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 27732c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27742c3c031cSScott Feldman IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 27752c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27762c3c031cSScott Feldman IFLA_BRPORT_FAST_LEAVE, 27772c3c031cSScott Feldman BR_MULTICAST_FAST_LEAVE) || 27782c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27792c3c031cSScott Feldman IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 27802c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27812c3c031cSScott Feldman IFLA_BRPORT_LEARNING, BR_LEARNING) || 27822c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27832c3c031cSScott Feldman IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 27842c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27852c3c031cSScott Feldman IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 27862c3c031cSScott Feldman brport_nla_put_flag(skb, flags, mask, 27872c3c031cSScott Feldman IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 27882c3c031cSScott Feldman nla_nest_cancel(skb, protinfo); 27892c3c031cSScott Feldman goto nla_put_failure; 27902c3c031cSScott Feldman } 27912c3c031cSScott Feldman 27922c3c031cSScott Feldman nla_nest_end(skb, protinfo); 27932c3c031cSScott Feldman 2794815cccbfSJohn Fastabend return nlmsg_end(skb, nlh); 2795815cccbfSJohn Fastabend nla_put_failure: 2796815cccbfSJohn Fastabend nlmsg_cancel(skb, nlh); 2797815cccbfSJohn Fastabend return -EMSGSIZE; 2798815cccbfSJohn Fastabend } 2799815cccbfSJohn Fastabend EXPORT_SYMBOL(ndo_dflt_bridge_getlink); 2800815cccbfSJohn Fastabend 2801e5a55a89SJohn Fastabend static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 2802e5a55a89SJohn Fastabend { 2803e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 2804e5a55a89SJohn Fastabend struct net_device *dev; 2805e5a55a89SJohn Fastabend int idx = 0; 2806e5a55a89SJohn Fastabend u32 portid = NETLINK_CB(cb->skb).portid; 2807e5a55a89SJohn Fastabend u32 seq = cb->nlh->nlmsg_seq; 28086cbdceebSVlad Yasevich u32 filter_mask = 0; 28096cbdceebSVlad Yasevich 2810aa68c20fSThomas Graf if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 2811aa68c20fSThomas Graf struct nlattr *extfilt; 2812aa68c20fSThomas Graf 28133e805ad2SAsbjoern Sloth Toennesen extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 28146cbdceebSVlad Yasevich IFLA_EXT_MASK); 2815aa68c20fSThomas Graf if (extfilt) { 2816aa68c20fSThomas Graf if (nla_len(extfilt) < sizeof(filter_mask)) 2817aa68c20fSThomas Graf return -EINVAL; 2818aa68c20fSThomas Graf 28196cbdceebSVlad Yasevich filter_mask = nla_get_u32(extfilt); 2820aa68c20fSThomas Graf } 2821aa68c20fSThomas Graf } 2822e5a55a89SJohn Fastabend 2823e5a55a89SJohn Fastabend rcu_read_lock(); 2824e5a55a89SJohn Fastabend for_each_netdev_rcu(net, dev) { 2825e5a55a89SJohn Fastabend const struct net_device_ops *ops = dev->netdev_ops; 2826898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2827e5a55a89SJohn Fastabend 2828898e5061SJiri Pirko if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 282925b1e679SBen Hutchings if (idx >= cb->args[0] && 2830898e5061SJiri Pirko br_dev->netdev_ops->ndo_bridge_getlink( 28316cbdceebSVlad Yasevich skb, portid, seq, dev, filter_mask) < 0) 2832e5a55a89SJohn Fastabend break; 2833e5a55a89SJohn Fastabend idx++; 2834e5a55a89SJohn Fastabend } 2835e5a55a89SJohn Fastabend 2836e5a55a89SJohn Fastabend if (ops->ndo_bridge_getlink) { 283725b1e679SBen Hutchings if (idx >= cb->args[0] && 28386cbdceebSVlad Yasevich ops->ndo_bridge_getlink(skb, portid, seq, dev, 28396cbdceebSVlad Yasevich filter_mask) < 0) 2840e5a55a89SJohn Fastabend break; 2841e5a55a89SJohn Fastabend idx++; 2842e5a55a89SJohn Fastabend } 2843e5a55a89SJohn Fastabend } 2844e5a55a89SJohn Fastabend rcu_read_unlock(); 2845e5a55a89SJohn Fastabend cb->args[0] = idx; 2846e5a55a89SJohn Fastabend 2847e5a55a89SJohn Fastabend return skb->len; 2848e5a55a89SJohn Fastabend } 2849e5a55a89SJohn Fastabend 28502469ffd7SJohn Fastabend static inline size_t bridge_nlmsg_size(void) 28512469ffd7SJohn Fastabend { 28522469ffd7SJohn Fastabend return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 28532469ffd7SJohn Fastabend + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 28542469ffd7SJohn Fastabend + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 28552469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 28562469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 28572469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 28582469ffd7SJohn Fastabend + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 28592469ffd7SJohn Fastabend + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 28602469ffd7SJohn Fastabend + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 28612469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 28622469ffd7SJohn Fastabend + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 28632469ffd7SJohn Fastabend } 28642469ffd7SJohn Fastabend 28652469ffd7SJohn Fastabend static int rtnl_bridge_notify(struct net_device *dev, u16 flags) 28662469ffd7SJohn Fastabend { 28672469ffd7SJohn Fastabend struct net *net = dev_net(dev); 2868898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 28692469ffd7SJohn Fastabend struct sk_buff *skb; 28702469ffd7SJohn Fastabend int err = -EOPNOTSUPP; 28712469ffd7SJohn Fastabend 28722469ffd7SJohn Fastabend skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 28732469ffd7SJohn Fastabend if (!skb) { 28742469ffd7SJohn Fastabend err = -ENOMEM; 28752469ffd7SJohn Fastabend goto errout; 28762469ffd7SJohn Fastabend } 28772469ffd7SJohn Fastabend 2878c38e01b8SJohn Fastabend if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) && 2879898e5061SJiri Pirko br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 28806cbdceebSVlad Yasevich err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0); 28812469ffd7SJohn Fastabend if (err < 0) 28822469ffd7SJohn Fastabend goto errout; 2883c38e01b8SJohn Fastabend } 2884c38e01b8SJohn Fastabend 2885c38e01b8SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF) && 2886c38e01b8SJohn Fastabend dev->netdev_ops->ndo_bridge_getlink) { 28876cbdceebSVlad Yasevich err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0); 2888c38e01b8SJohn Fastabend if (err < 0) 2889c38e01b8SJohn Fastabend goto errout; 2890c38e01b8SJohn Fastabend } 28912469ffd7SJohn Fastabend 289259ccaaaaSRoopa Prabhu if (!skb->len) 289359ccaaaaSRoopa Prabhu goto errout; 289459ccaaaaSRoopa Prabhu 28952469ffd7SJohn Fastabend rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 28962469ffd7SJohn Fastabend return 0; 28972469ffd7SJohn Fastabend errout: 28982469ffd7SJohn Fastabend WARN_ON(err == -EMSGSIZE); 28992469ffd7SJohn Fastabend kfree_skb(skb); 290059ccaaaaSRoopa Prabhu if (err) 29012469ffd7SJohn Fastabend rtnl_set_sk_err(net, RTNLGRP_LINK, err); 29022469ffd7SJohn Fastabend return err; 29032469ffd7SJohn Fastabend } 29042469ffd7SJohn Fastabend 2905661d2967SThomas Graf static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) 2906e5a55a89SJohn Fastabend { 2907e5a55a89SJohn Fastabend struct net *net = sock_net(skb->sk); 2908e5a55a89SJohn Fastabend struct ifinfomsg *ifm; 2909e5a55a89SJohn Fastabend struct net_device *dev; 29102469ffd7SJohn Fastabend struct nlattr *br_spec, *attr = NULL; 29112469ffd7SJohn Fastabend int rem, err = -EOPNOTSUPP; 2912c38e01b8SJohn Fastabend u16 oflags, flags = 0; 2913c38e01b8SJohn Fastabend bool have_flags = false; 2914e5a55a89SJohn Fastabend 2915e5a55a89SJohn Fastabend if (nlmsg_len(nlh) < sizeof(*ifm)) 2916e5a55a89SJohn Fastabend return -EINVAL; 2917e5a55a89SJohn Fastabend 2918e5a55a89SJohn Fastabend ifm = nlmsg_data(nlh); 2919e5a55a89SJohn Fastabend if (ifm->ifi_family != AF_BRIDGE) 2920e5a55a89SJohn Fastabend return -EPFNOSUPPORT; 2921e5a55a89SJohn Fastabend 2922e5a55a89SJohn Fastabend dev = __dev_get_by_index(net, ifm->ifi_index); 2923e5a55a89SJohn Fastabend if (!dev) { 2924e5a55a89SJohn Fastabend pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 2925e5a55a89SJohn Fastabend return -ENODEV; 2926e5a55a89SJohn Fastabend } 2927e5a55a89SJohn Fastabend 29282469ffd7SJohn Fastabend br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 29292469ffd7SJohn Fastabend if (br_spec) { 29302469ffd7SJohn Fastabend nla_for_each_nested(attr, br_spec, rem) { 29312469ffd7SJohn Fastabend if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 29326e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 29336e8d1c55SThomas Graf return -EINVAL; 29346e8d1c55SThomas Graf 2935c38e01b8SJohn Fastabend have_flags = true; 29362469ffd7SJohn Fastabend flags = nla_get_u16(attr); 29372469ffd7SJohn Fastabend break; 29382469ffd7SJohn Fastabend } 29392469ffd7SJohn Fastabend } 29402469ffd7SJohn Fastabend } 29412469ffd7SJohn Fastabend 2942c38e01b8SJohn Fastabend oflags = flags; 2943c38e01b8SJohn Fastabend 29442469ffd7SJohn Fastabend if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 2945898e5061SJiri Pirko struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2946898e5061SJiri Pirko 2947898e5061SJiri Pirko if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 29482469ffd7SJohn Fastabend err = -EOPNOTSUPP; 2949e5a55a89SJohn Fastabend goto out; 2950e5a55a89SJohn Fastabend } 2951e5a55a89SJohn Fastabend 2952898e5061SJiri Pirko err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh); 29532469ffd7SJohn Fastabend if (err) 29542469ffd7SJohn Fastabend goto out; 29552469ffd7SJohn Fastabend 29562469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_MASTER; 29572469ffd7SJohn Fastabend } 29582469ffd7SJohn Fastabend 29592469ffd7SJohn Fastabend if ((flags & BRIDGE_FLAGS_SELF)) { 29602469ffd7SJohn Fastabend if (!dev->netdev_ops->ndo_bridge_setlink) 29612469ffd7SJohn Fastabend err = -EOPNOTSUPP; 29622469ffd7SJohn Fastabend else 2963e5a55a89SJohn Fastabend err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh); 2964e5a55a89SJohn Fastabend 29652469ffd7SJohn Fastabend if (!err) 29662469ffd7SJohn Fastabend flags &= ~BRIDGE_FLAGS_SELF; 29672469ffd7SJohn Fastabend } 29682469ffd7SJohn Fastabend 2969c38e01b8SJohn Fastabend if (have_flags) 29702469ffd7SJohn Fastabend memcpy(nla_data(attr), &flags, sizeof(flags)); 29712469ffd7SJohn Fastabend /* Generate event to notify upper layer of bridge change */ 29722469ffd7SJohn Fastabend if (!err) 2973c38e01b8SJohn Fastabend err = rtnl_bridge_notify(dev, oflags); 2974e5a55a89SJohn Fastabend out: 2975e5a55a89SJohn Fastabend return err; 2976e5a55a89SJohn Fastabend } 2977e5a55a89SJohn Fastabend 2978661d2967SThomas Graf static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) 2979407af329SVlad Yasevich { 2980407af329SVlad Yasevich struct net *net = sock_net(skb->sk); 2981407af329SVlad Yasevich struct ifinfomsg *ifm; 2982407af329SVlad Yasevich struct net_device *dev; 2983407af329SVlad Yasevich struct nlattr *br_spec, *attr = NULL; 2984407af329SVlad Yasevich int rem, err = -EOPNOTSUPP; 2985407af329SVlad Yasevich u16 oflags, flags = 0; 2986407af329SVlad Yasevich bool have_flags = false; 2987407af329SVlad Yasevich 2988407af329SVlad Yasevich if (nlmsg_len(nlh) < sizeof(*ifm)) 2989407af329SVlad Yasevich return -EINVAL; 2990407af329SVlad Yasevich 2991407af329SVlad Yasevich ifm = nlmsg_data(nlh); 2992407af329SVlad Yasevich if (ifm->ifi_family != AF_BRIDGE) 2993407af329SVlad Yasevich return -EPFNOSUPPORT; 2994407af329SVlad Yasevich 2995407af329SVlad Yasevich dev = __dev_get_by_index(net, ifm->ifi_index); 2996407af329SVlad Yasevich if (!dev) { 2997407af329SVlad Yasevich pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 2998407af329SVlad Yasevich return -ENODEV; 2999407af329SVlad Yasevich } 3000407af329SVlad Yasevich 3001407af329SVlad Yasevich br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 3002407af329SVlad Yasevich if (br_spec) { 3003407af329SVlad Yasevich nla_for_each_nested(attr, br_spec, rem) { 3004407af329SVlad Yasevich if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 30056e8d1c55SThomas Graf if (nla_len(attr) < sizeof(flags)) 30066e8d1c55SThomas Graf return -EINVAL; 30076e8d1c55SThomas Graf 3008407af329SVlad Yasevich have_flags = true; 3009407af329SVlad Yasevich flags = nla_get_u16(attr); 3010407af329SVlad Yasevich break; 3011407af329SVlad Yasevich } 3012407af329SVlad Yasevich } 3013407af329SVlad Yasevich } 3014407af329SVlad Yasevich 3015407af329SVlad Yasevich oflags = flags; 3016407af329SVlad Yasevich 3017407af329SVlad Yasevich if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 3018407af329SVlad Yasevich struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3019407af329SVlad Yasevich 3020407af329SVlad Yasevich if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 3021407af329SVlad Yasevich err = -EOPNOTSUPP; 3022407af329SVlad Yasevich goto out; 3023407af329SVlad Yasevich } 3024407af329SVlad Yasevich 3025407af329SVlad Yasevich err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh); 3026407af329SVlad Yasevich if (err) 3027407af329SVlad Yasevich goto out; 3028407af329SVlad Yasevich 3029407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_MASTER; 3030407af329SVlad Yasevich } 3031407af329SVlad Yasevich 3032407af329SVlad Yasevich if ((flags & BRIDGE_FLAGS_SELF)) { 3033407af329SVlad Yasevich if (!dev->netdev_ops->ndo_bridge_dellink) 3034407af329SVlad Yasevich err = -EOPNOTSUPP; 3035407af329SVlad Yasevich else 3036407af329SVlad Yasevich err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh); 3037407af329SVlad Yasevich 3038407af329SVlad Yasevich if (!err) 3039407af329SVlad Yasevich flags &= ~BRIDGE_FLAGS_SELF; 3040407af329SVlad Yasevich } 3041407af329SVlad Yasevich 3042407af329SVlad Yasevich if (have_flags) 3043407af329SVlad Yasevich memcpy(nla_data(attr), &flags, sizeof(flags)); 3044407af329SVlad Yasevich /* Generate event to notify upper layer of bridge change */ 3045407af329SVlad Yasevich if (!err) 3046407af329SVlad Yasevich err = rtnl_bridge_notify(dev, oflags); 3047407af329SVlad Yasevich out: 3048407af329SVlad Yasevich return err; 3049407af329SVlad Yasevich } 3050407af329SVlad Yasevich 30511da177e4SLinus Torvalds /* Process one rtnetlink message. */ 30521da177e4SLinus Torvalds 30531d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 30541da177e4SLinus Torvalds { 30553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3056e2849863SThomas Graf rtnl_doit_func doit; 30571da177e4SLinus Torvalds int sz_idx, kind; 30581da177e4SLinus Torvalds int family; 30591da177e4SLinus Torvalds int type; 30602907c35fSEric Dumazet int err; 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds type = nlh->nlmsg_type; 30631da177e4SLinus Torvalds if (type > RTM_MAX) 3064038890feSThomas Graf return -EOPNOTSUPP; 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds type -= RTM_BASE; 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 3069573ce260SHong zhi guo if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 30701da177e4SLinus Torvalds return 0; 30711da177e4SLinus Torvalds 3072573ce260SHong zhi guo family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 30731da177e4SLinus Torvalds sz_idx = type>>2; 30741da177e4SLinus Torvalds kind = type&3; 30751da177e4SLinus Torvalds 307690f62cf3SEric W. Biederman if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 30771d00a4ebSThomas Graf return -EPERM; 30781da177e4SLinus Torvalds 3079b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 308097c53cacSDenis V. Lunev struct sock *rtnl; 3081e2849863SThomas Graf rtnl_dumpit_func dumpit; 3082c7ac8679SGreg Rose rtnl_calcit_func calcit; 3083c7ac8679SGreg Rose u16 min_dump_alloc = 0; 30841da177e4SLinus Torvalds 3085e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 3086e2849863SThomas Graf if (dumpit == NULL) 3087038890feSThomas Graf return -EOPNOTSUPP; 3088c7ac8679SGreg Rose calcit = rtnl_get_calcit(family, type); 3089c7ac8679SGreg Rose if (calcit) 3090115c9b81SGreg Rose min_dump_alloc = calcit(skb, nlh); 30911da177e4SLinus Torvalds 30922907c35fSEric Dumazet __rtnl_unlock(); 309397c53cacSDenis V. Lunev rtnl = net->rtnl; 309480d326faSPablo Neira Ayuso { 309580d326faSPablo Neira Ayuso struct netlink_dump_control c = { 309680d326faSPablo Neira Ayuso .dump = dumpit, 309780d326faSPablo Neira Ayuso .min_dump_alloc = min_dump_alloc, 309880d326faSPablo Neira Ayuso }; 309980d326faSPablo Neira Ayuso err = netlink_dump_start(rtnl, skb, nlh, &c); 310080d326faSPablo Neira Ayuso } 31012907c35fSEric Dumazet rtnl_lock(); 31022907c35fSEric Dumazet return err; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 3105e2849863SThomas Graf doit = rtnl_get_doit(family, type); 3106e2849863SThomas Graf if (doit == NULL) 3107038890feSThomas Graf return -EOPNOTSUPP; 31081da177e4SLinus Torvalds 3109661d2967SThomas Graf return doit(skb, nlh); 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 3112cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 31131da177e4SLinus Torvalds { 31141536cc0dSDenis V. Lunev rtnl_lock(); 3115cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 31161536cc0dSDenis V. Lunev rtnl_unlock(); 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 31201da177e4SLinus Torvalds { 3121351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3122e9dc8653SEric W. Biederman 31231da177e4SLinus Torvalds switch (event) { 31241da177e4SLinus Torvalds case NETDEV_UP: 31251da177e4SLinus Torvalds case NETDEV_DOWN: 312610de05afSPatrick McHardy case NETDEV_PRE_UP: 3127d90a909eSEric W. Biederman case NETDEV_POST_INIT: 3128d90a909eSEric W. Biederman case NETDEV_REGISTER: 31291da177e4SLinus Torvalds case NETDEV_CHANGE: 3130755d0e77SPatrick McHardy case NETDEV_PRE_TYPE_CHANGE: 31311da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 3132a2835763SPatrick McHardy case NETDEV_UNREGISTER: 31330115e8e3SEric Dumazet case NETDEV_UNREGISTER_FINAL: 3134ac3d3f81SAmerigo Wang case NETDEV_RELEASE: 3135ac3d3f81SAmerigo Wang case NETDEV_JOIN: 31361da177e4SLinus Torvalds break; 31371da177e4SLinus Torvalds default: 31387f294054SAlexei Starovoitov rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); 31391da177e4SLinus Torvalds break; 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds return NOTIFY_DONE; 31421da177e4SLinus Torvalds } 31431da177e4SLinus Torvalds 31441da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 31451da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 31461da177e4SLinus Torvalds }; 31471da177e4SLinus Torvalds 314897c53cacSDenis V. Lunev 31492c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 315097c53cacSDenis V. Lunev { 315197c53cacSDenis V. Lunev struct sock *sk; 3152a31f2d17SPablo Neira Ayuso struct netlink_kernel_cfg cfg = { 3153a31f2d17SPablo Neira Ayuso .groups = RTNLGRP_MAX, 3154a31f2d17SPablo Neira Ayuso .input = rtnetlink_rcv, 3155a31f2d17SPablo Neira Ayuso .cb_mutex = &rtnl_mutex, 31569785e10aSPablo Neira Ayuso .flags = NL_CFG_F_NONROOT_RECV, 3157a31f2d17SPablo Neira Ayuso }; 3158a31f2d17SPablo Neira Ayuso 31599f00d977SPablo Neira Ayuso sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 316097c53cacSDenis V. Lunev if (!sk) 316197c53cacSDenis V. Lunev return -ENOMEM; 316297c53cacSDenis V. Lunev net->rtnl = sk; 316397c53cacSDenis V. Lunev return 0; 316497c53cacSDenis V. Lunev } 316597c53cacSDenis V. Lunev 31662c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 316797c53cacSDenis V. Lunev { 3168b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 316997c53cacSDenis V. Lunev net->rtnl = NULL; 317097c53cacSDenis V. Lunev } 317197c53cacSDenis V. Lunev 317297c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 317397c53cacSDenis V. Lunev .init = rtnetlink_net_init, 317497c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 317597c53cacSDenis V. Lunev }; 317697c53cacSDenis V. Lunev 31771da177e4SLinus Torvalds void __init rtnetlink_init(void) 31781da177e4SLinus Torvalds { 317997c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 31801da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 318197c53cacSDenis V. Lunev 31821da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 3183340d17fcSThomas Graf 3184c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 3185c7ac8679SGreg Rose rtnl_dump_ifinfo, rtnl_calcit); 3186c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); 3187c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); 3188c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); 3189687ad8ccSThomas Graf 3190c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); 3191c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); 319277162022SJohn Fastabend 319377162022SJohn Fastabend rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); 319477162022SJohn Fastabend rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); 319577162022SJohn Fastabend rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); 3196e5a55a89SJohn Fastabend 3197e5a55a89SJohn Fastabend rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); 3198407af329SVlad Yasevich rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); 3199e5a55a89SJohn Fastabend rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 3202