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> 38ebc08a6fSWilliams, Mitch A #include <linux/pci.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <asm/uaccess.h> 411da177e4SLinus Torvalds #include <asm/system.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/netdevice.h> 451da177e4SLinus Torvalds #include <net/ip.h> 461da177e4SLinus Torvalds #include <net/protocol.h> 471da177e4SLinus Torvalds #include <net/arp.h> 481da177e4SLinus Torvalds #include <net/route.h> 491da177e4SLinus Torvalds #include <net/udp.h> 501da177e4SLinus Torvalds #include <net/sock.h> 511da177e4SLinus Torvalds #include <net/pkt_sched.h> 5214c0b97dSThomas Graf #include <net/fib_rules.h> 53e2849863SThomas Graf #include <net/rtnetlink.h> 5430ffee84SJohannes Berg #include <net/net_namespace.h> 551da177e4SLinus Torvalds 56e0d087afSEric Dumazet struct rtnl_link { 57e2849863SThomas Graf rtnl_doit_func doit; 58e2849863SThomas Graf rtnl_dumpit_func dumpit; 59*c7ac8679SGreg Rose rtnl_calcit_func calcit; 60e2849863SThomas Graf }; 61e2849863SThomas Graf 626756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 63*c7ac8679SGreg Rose static u16 min_ifinfo_dump_size; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds void rtnl_lock(void) 661da177e4SLinus Torvalds { 676756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 681da177e4SLinus Torvalds } 69e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 701da177e4SLinus Torvalds 716756ae4bSStephen Hemminger void __rtnl_unlock(void) 721da177e4SLinus Torvalds { 736756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds void rtnl_unlock(void) 771da177e4SLinus Torvalds { 7858ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 791da177e4SLinus Torvalds netdev_run_todo(); 801da177e4SLinus Torvalds } 81e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 821da177e4SLinus Torvalds 836756ae4bSStephen Hemminger int rtnl_trylock(void) 846756ae4bSStephen Hemminger { 856756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 866756ae4bSStephen Hemminger } 87e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 886756ae4bSStephen Hemminger 89c9c1014bSPatrick McHardy int rtnl_is_locked(void) 90c9c1014bSPatrick McHardy { 91c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 92c9c1014bSPatrick McHardy } 93e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 94c9c1014bSPatrick McHardy 95a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 96a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void) 97a898def2SPaul E. McKenney { 98a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 99a898def2SPaul E. McKenney } 100a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 101a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 102a898def2SPaul E. McKenney 10325239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 104e2849863SThomas Graf 105e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 106e2849863SThomas Graf { 107e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 108e2849863SThomas Graf 109e2849863SThomas Graf /* 110e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 111e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 112e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 113e2849863SThomas Graf */ 114e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 115e2849863SThomas Graf 116e2849863SThomas Graf return msgindex; 117e2849863SThomas Graf } 118e2849863SThomas Graf 119e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 120e2849863SThomas Graf { 121e2849863SThomas Graf struct rtnl_link *tab; 122e2849863SThomas Graf 12325239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 124e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1250f87b1ddSPatrick McHardy else 1260f87b1ddSPatrick McHardy tab = NULL; 1270f87b1ddSPatrick McHardy 12851057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 129e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 130e2849863SThomas Graf 13151057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 132e2849863SThomas Graf } 133e2849863SThomas Graf 134e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 135e2849863SThomas Graf { 136e2849863SThomas Graf struct rtnl_link *tab; 137e2849863SThomas Graf 13825239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 139e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1400f87b1ddSPatrick McHardy else 1410f87b1ddSPatrick McHardy tab = NULL; 1420f87b1ddSPatrick McHardy 14351057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 144e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 145e2849863SThomas Graf 14651057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 147e2849863SThomas Graf } 148e2849863SThomas Graf 149*c7ac8679SGreg Rose static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) 150*c7ac8679SGreg Rose { 151*c7ac8679SGreg Rose struct rtnl_link *tab; 152*c7ac8679SGreg Rose 153*c7ac8679SGreg Rose if (protocol <= RTNL_FAMILY_MAX) 154*c7ac8679SGreg Rose tab = rtnl_msg_handlers[protocol]; 155*c7ac8679SGreg Rose else 156*c7ac8679SGreg Rose tab = NULL; 157*c7ac8679SGreg Rose 158*c7ac8679SGreg Rose if (tab == NULL || tab[msgindex].calcit == NULL) 159*c7ac8679SGreg Rose tab = rtnl_msg_handlers[PF_UNSPEC]; 160*c7ac8679SGreg Rose 161*c7ac8679SGreg Rose return tab ? tab[msgindex].calcit : NULL; 162*c7ac8679SGreg Rose } 163*c7ac8679SGreg Rose 164e2849863SThomas Graf /** 165e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 166e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 167e2849863SThomas Graf * @msgtype: rtnetlink message type 168e2849863SThomas Graf * @doit: Function pointer called for each request message 169e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 170*c7ac8679SGreg Rose * @calcit: Function pointer to calc size of dump message 171e2849863SThomas Graf * 172e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 173e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 174e2849863SThomas Graf * specified protocol family and message type is received. 175e2849863SThomas Graf * 176e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 177e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 178e2849863SThomas Graf * family exists. 179e2849863SThomas Graf * 180e2849863SThomas Graf * Returns 0 on success or a negative error code. 181e2849863SThomas Graf */ 182e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 183*c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 184*c7ac8679SGreg Rose rtnl_calcit_func calcit) 185e2849863SThomas Graf { 186e2849863SThomas Graf struct rtnl_link *tab; 187e2849863SThomas Graf int msgindex; 188e2849863SThomas Graf 18925239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 190e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 191e2849863SThomas Graf 192e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 193e2849863SThomas Graf if (tab == NULL) { 194e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 195e2849863SThomas Graf if (tab == NULL) 196e2849863SThomas Graf return -ENOBUFS; 197e2849863SThomas Graf 198e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 199e2849863SThomas Graf } 200e2849863SThomas Graf 201e2849863SThomas Graf if (doit) 202e2849863SThomas Graf tab[msgindex].doit = doit; 203e2849863SThomas Graf 204e2849863SThomas Graf if (dumpit) 205e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 206e2849863SThomas Graf 207*c7ac8679SGreg Rose if (calcit) 208*c7ac8679SGreg Rose tab[msgindex].calcit = calcit; 209*c7ac8679SGreg Rose 210e2849863SThomas Graf return 0; 211e2849863SThomas Graf } 212e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 213e2849863SThomas Graf 214e2849863SThomas Graf /** 215e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 216e2849863SThomas Graf * 217e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 218e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 219e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 220e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 22125985edcSLucas De Marchi * of memory implies no sense in continuing. 222e2849863SThomas Graf */ 223e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 224*c7ac8679SGreg Rose rtnl_doit_func doit, rtnl_dumpit_func dumpit, 225*c7ac8679SGreg Rose rtnl_calcit_func calcit) 226e2849863SThomas Graf { 227*c7ac8679SGreg Rose if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) 228e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 229e2849863SThomas Graf "protocol = %d, message type = %d\n", 230e2849863SThomas Graf protocol, msgtype); 231e2849863SThomas Graf } 232e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 233e2849863SThomas Graf 234e2849863SThomas Graf /** 235e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 236e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 237e2849863SThomas Graf * @msgtype: rtnetlink message type 238e2849863SThomas Graf * 239e2849863SThomas Graf * Returns 0 on success or a negative error code. 240e2849863SThomas Graf */ 241e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 242e2849863SThomas Graf { 243e2849863SThomas Graf int msgindex; 244e2849863SThomas Graf 24525239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 246e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 247e2849863SThomas Graf 248e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 249e2849863SThomas Graf return -ENOENT; 250e2849863SThomas Graf 251e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 252e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 253e2849863SThomas Graf 254e2849863SThomas Graf return 0; 255e2849863SThomas Graf } 256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 257e2849863SThomas Graf 258e2849863SThomas Graf /** 259e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 260e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 261e2849863SThomas Graf * 262e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 263e2849863SThomas Graf * of a certain protocol family. 264e2849863SThomas Graf */ 265e2849863SThomas Graf void rtnl_unregister_all(int protocol) 266e2849863SThomas Graf { 26725239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 268e2849863SThomas Graf 269e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 270e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 271e2849863SThomas Graf } 272e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2731da177e4SLinus Torvalds 27438f7b870SPatrick McHardy static LIST_HEAD(link_ops); 27538f7b870SPatrick McHardy 27638f7b870SPatrick McHardy /** 27738f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 27838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 27938f7b870SPatrick McHardy * 28038f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 28138f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 28238f7b870SPatrick McHardy * must be called before registering the devices. 28338f7b870SPatrick McHardy * 28438f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 28538f7b870SPatrick McHardy */ 28638f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 28738f7b870SPatrick McHardy { 2882d85cba2SPatrick McHardy if (!ops->dellink) 28923289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 2902d85cba2SPatrick McHardy 29138f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 29238f7b870SPatrick McHardy return 0; 29338f7b870SPatrick McHardy } 29438f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 29538f7b870SPatrick McHardy 29638f7b870SPatrick McHardy /** 29738f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 29838f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 29938f7b870SPatrick McHardy * 30038f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 30138f7b870SPatrick McHardy */ 30238f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 30338f7b870SPatrick McHardy { 30438f7b870SPatrick McHardy int err; 30538f7b870SPatrick McHardy 30638f7b870SPatrick McHardy rtnl_lock(); 30738f7b870SPatrick McHardy err = __rtnl_link_register(ops); 30838f7b870SPatrick McHardy rtnl_unlock(); 30938f7b870SPatrick McHardy return err; 31038f7b870SPatrick McHardy } 31138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 31238f7b870SPatrick McHardy 313669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 314669f87baSPavel Emelyanov { 315669f87baSPavel Emelyanov struct net_device *dev; 31623289a37SEric Dumazet LIST_HEAD(list_kill); 31723289a37SEric Dumazet 318669f87baSPavel Emelyanov for_each_netdev(net, dev) { 31923289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 32023289a37SEric Dumazet ops->dellink(dev, &list_kill); 321669f87baSPavel Emelyanov } 32223289a37SEric Dumazet unregister_netdevice_many(&list_kill); 323669f87baSPavel Emelyanov } 324669f87baSPavel Emelyanov 32538f7b870SPatrick McHardy /** 32638f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 32738f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 32838f7b870SPatrick McHardy * 3292d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 33038f7b870SPatrick McHardy */ 33138f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 33238f7b870SPatrick McHardy { 333881d966bSEric W. Biederman struct net *net; 3342d85cba2SPatrick McHardy 335881d966bSEric W. Biederman for_each_net(net) { 336669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 337881d966bSEric W. Biederman } 33838f7b870SPatrick McHardy list_del(&ops->list); 33938f7b870SPatrick McHardy } 34038f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 34138f7b870SPatrick McHardy 34238f7b870SPatrick McHardy /** 34338f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 34438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 34538f7b870SPatrick McHardy */ 34638f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 34738f7b870SPatrick McHardy { 34838f7b870SPatrick McHardy rtnl_lock(); 34938f7b870SPatrick McHardy __rtnl_link_unregister(ops); 35038f7b870SPatrick McHardy rtnl_unlock(); 35138f7b870SPatrick McHardy } 35238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 35338f7b870SPatrick McHardy 35438f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 35538f7b870SPatrick McHardy { 35638f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 35738f7b870SPatrick McHardy 35838f7b870SPatrick McHardy list_for_each_entry(ops, &link_ops, list) { 35938f7b870SPatrick McHardy if (!strcmp(ops->kind, kind)) 36038f7b870SPatrick McHardy return ops; 36138f7b870SPatrick McHardy } 36238f7b870SPatrick McHardy return NULL; 36338f7b870SPatrick McHardy } 36438f7b870SPatrick McHardy 36538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 36638f7b870SPatrick McHardy { 36738f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 36838f7b870SPatrick McHardy size_t size; 36938f7b870SPatrick McHardy 37038f7b870SPatrick McHardy if (!ops) 37138f7b870SPatrick McHardy return 0; 37238f7b870SPatrick McHardy 373369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 374369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 37538f7b870SPatrick McHardy 37638f7b870SPatrick McHardy if (ops->get_size) 37738f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 378369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 37938f7b870SPatrick McHardy ops->get_size(dev); 38038f7b870SPatrick McHardy 38138f7b870SPatrick McHardy if (ops->get_xstats_size) 382369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 383369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 38438f7b870SPatrick McHardy 38538f7b870SPatrick McHardy return size; 38638f7b870SPatrick McHardy } 38738f7b870SPatrick McHardy 388f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 389f8ff182cSThomas Graf 390f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 391f8ff182cSThomas Graf { 392f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 393f8ff182cSThomas Graf 394f8ff182cSThomas Graf list_for_each_entry(ops, &rtnl_af_ops, list) { 395f8ff182cSThomas Graf if (ops->family == family) 396f8ff182cSThomas Graf return ops; 397f8ff182cSThomas Graf } 398f8ff182cSThomas Graf 399f8ff182cSThomas Graf return NULL; 400f8ff182cSThomas Graf } 401f8ff182cSThomas Graf 402f8ff182cSThomas Graf /** 403f8ff182cSThomas Graf * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. 404f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 405f8ff182cSThomas Graf * 406f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 407f8ff182cSThomas Graf * 408f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 409f8ff182cSThomas Graf */ 410f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops) 411f8ff182cSThomas Graf { 412f8ff182cSThomas Graf list_add_tail(&ops->list, &rtnl_af_ops); 413f8ff182cSThomas Graf return 0; 414f8ff182cSThomas Graf } 415f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register); 416f8ff182cSThomas Graf 417f8ff182cSThomas Graf /** 418f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 419f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 420f8ff182cSThomas Graf * 421f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 422f8ff182cSThomas Graf */ 423f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops) 424f8ff182cSThomas Graf { 425f8ff182cSThomas Graf int err; 426f8ff182cSThomas Graf 427f8ff182cSThomas Graf rtnl_lock(); 428f8ff182cSThomas Graf err = __rtnl_af_register(ops); 429f8ff182cSThomas Graf rtnl_unlock(); 430f8ff182cSThomas Graf return err; 431f8ff182cSThomas Graf } 432f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 433f8ff182cSThomas Graf 434f8ff182cSThomas Graf /** 435f8ff182cSThomas Graf * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 436f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 437f8ff182cSThomas Graf * 438f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 439f8ff182cSThomas Graf */ 440f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops) 441f8ff182cSThomas Graf { 442f8ff182cSThomas Graf list_del(&ops->list); 443f8ff182cSThomas Graf } 444f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 445f8ff182cSThomas Graf 446f8ff182cSThomas Graf /** 447f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 448f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 449f8ff182cSThomas Graf */ 450f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 451f8ff182cSThomas Graf { 452f8ff182cSThomas Graf rtnl_lock(); 453f8ff182cSThomas Graf __rtnl_af_unregister(ops); 454f8ff182cSThomas Graf rtnl_unlock(); 455f8ff182cSThomas Graf } 456f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 457f8ff182cSThomas Graf 458f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev) 459f8ff182cSThomas Graf { 460f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 461f8ff182cSThomas Graf size_t size; 462f8ff182cSThomas Graf 463f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 464f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 465f8ff182cSThomas Graf 466f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 467f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 468f8ff182cSThomas Graf /* AF_* + nested data */ 469f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 470f8ff182cSThomas Graf af_ops->get_link_af_size(dev); 471f8ff182cSThomas Graf } 472f8ff182cSThomas Graf } 473f8ff182cSThomas Graf 474f8ff182cSThomas Graf return size; 475f8ff182cSThomas Graf } 476f8ff182cSThomas Graf 47738f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 47838f7b870SPatrick McHardy { 47938f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 48038f7b870SPatrick McHardy struct nlattr *linkinfo, *data; 48138f7b870SPatrick McHardy int err = -EMSGSIZE; 48238f7b870SPatrick McHardy 48338f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 48438f7b870SPatrick McHardy if (linkinfo == NULL) 48538f7b870SPatrick McHardy goto out; 48638f7b870SPatrick McHardy 48738f7b870SPatrick McHardy if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 48838f7b870SPatrick McHardy goto err_cancel_link; 48938f7b870SPatrick McHardy if (ops->fill_xstats) { 49038f7b870SPatrick McHardy err = ops->fill_xstats(skb, dev); 49138f7b870SPatrick McHardy if (err < 0) 49238f7b870SPatrick McHardy goto err_cancel_link; 49338f7b870SPatrick McHardy } 49438f7b870SPatrick McHardy if (ops->fill_info) { 49538f7b870SPatrick McHardy data = nla_nest_start(skb, IFLA_INFO_DATA); 49638f7b870SPatrick McHardy if (data == NULL) 49738f7b870SPatrick McHardy goto err_cancel_link; 49838f7b870SPatrick McHardy err = ops->fill_info(skb, dev); 49938f7b870SPatrick McHardy if (err < 0) 50038f7b870SPatrick McHardy goto err_cancel_data; 50138f7b870SPatrick McHardy nla_nest_end(skb, data); 50238f7b870SPatrick McHardy } 50338f7b870SPatrick McHardy 50438f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 50538f7b870SPatrick McHardy return 0; 50638f7b870SPatrick McHardy 50738f7b870SPatrick McHardy err_cancel_data: 50838f7b870SPatrick McHardy nla_nest_cancel(skb, data); 50938f7b870SPatrick McHardy err_cancel_link: 51038f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 51138f7b870SPatrick McHardy out: 51238f7b870SPatrick McHardy return err; 51338f7b870SPatrick McHardy } 51438f7b870SPatrick McHardy 515db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 5161da177e4SLinus Torvalds { 517f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 518f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 519f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 52014c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 521f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 522f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 523f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 524f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 525f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 526f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 5271da177e4SLinus Torvalds }; 5281da177e4SLinus Torvalds 529db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 5301da177e4SLinus Torvalds { 531f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 532f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 533f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 53414c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 535f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 536f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 537f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 538f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 5391da177e4SLinus Torvalds }; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 5421da177e4SLinus Torvalds { 5431da177e4SLinus Torvalds struct rtattr *rta; 5441da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); 5471da177e4SLinus Torvalds rta->rta_type = attrtype; 5481da177e4SLinus Torvalds rta->rta_len = size; 5491da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 550b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 5511da177e4SLinus Torvalds } 552e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill); 5531da177e4SLinus Torvalds 55497c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) 5551da177e4SLinus Torvalds { 55697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 5571da177e4SLinus Torvalds int err = 0; 5581da177e4SLinus Torvalds 559ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 5601da177e4SLinus Torvalds if (echo) 5611da177e4SLinus Torvalds atomic_inc(&skb->users); 5621da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 5631da177e4SLinus Torvalds if (echo) 5641da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 5651da177e4SLinus Torvalds return err; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 56897c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 5692942e900SThomas Graf { 57097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 57197c53cacSDenis V. Lunev 5722942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 5732942e900SThomas Graf } 574e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 5752942e900SThomas Graf 5761ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 57797676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 57897676b6bSThomas Graf { 57997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 58097676b6bSThomas Graf int report = 0; 58197676b6bSThomas Graf 58297676b6bSThomas Graf if (nlh) 58397676b6bSThomas Graf report = nlmsg_report(nlh); 58497676b6bSThomas Graf 5851ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 58697676b6bSThomas Graf } 587e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 58897676b6bSThomas Graf 58997c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 59097676b6bSThomas Graf { 59197c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 59297c53cacSDenis V. Lunev 59397676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 59497676b6bSThomas Graf } 595e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 59697676b6bSThomas Graf 5971da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 5981da177e4SLinus Torvalds { 5992d7202bfSThomas Graf struct nlattr *mx; 6002d7202bfSThomas Graf int i, valid = 0; 6011da177e4SLinus Torvalds 6022d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 6032d7202bfSThomas Graf if (mx == NULL) 6042d7202bfSThomas Graf return -ENOBUFS; 6052d7202bfSThomas Graf 6061da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 6072d7202bfSThomas Graf if (metrics[i]) { 6082d7202bfSThomas Graf valid++; 6092d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 6101da177e4SLinus Torvalds } 6112d7202bfSThomas Graf } 6121da177e4SLinus Torvalds 613a57d27fcSDavid S. Miller if (!valid) { 614a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 615a57d27fcSDavid S. Miller return 0; 616a57d27fcSDavid S. Miller } 6172d7202bfSThomas Graf 6182d7202bfSThomas Graf return nla_nest_end(skb, mx); 6192d7202bfSThomas Graf 6202d7202bfSThomas Graf nla_put_failure: 621bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 622bc3ed28cSThomas Graf return -EMSGSIZE; 6231da177e4SLinus Torvalds } 624e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 6251da177e4SLinus Torvalds 626e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 627e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 628e3703b3dSThomas Graf { 629e3703b3dSThomas Graf struct rta_cacheinfo ci = { 630e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 631e3703b3dSThomas Graf .rta_used = dst->__use, 632e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 633e3703b3dSThomas Graf .rta_error = error, 634e3703b3dSThomas Graf .rta_id = id, 635e3703b3dSThomas Graf .rta_ts = ts, 636e3703b3dSThomas Graf .rta_tsage = tsage, 637e3703b3dSThomas Graf }; 638e3703b3dSThomas Graf 639e3703b3dSThomas Graf if (expires) 640e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 641e3703b3dSThomas Graf 642e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 643e3703b3dSThomas Graf } 644e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 6451da177e4SLinus Torvalds 64693b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 647b00055aaSStefan Rompf { 648b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 649b00055aaSStefan Rompf 650b00055aaSStefan Rompf switch (transition) { 651b00055aaSStefan Rompf case IF_OPER_UP: 652b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 653b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 654b00055aaSStefan Rompf !netif_dormant(dev)) 655b00055aaSStefan Rompf operstate = IF_OPER_UP; 656b00055aaSStefan Rompf break; 657b00055aaSStefan Rompf 658b00055aaSStefan Rompf case IF_OPER_DORMANT: 659b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 660b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 661b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 662b00055aaSStefan Rompf break; 6633ff50b79SStephen Hemminger } 664b00055aaSStefan Rompf 665b00055aaSStefan Rompf if (dev->operstate != operstate) { 666b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 667b00055aaSStefan Rompf dev->operstate = operstate; 668b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 669b00055aaSStefan Rompf netdev_state_change(dev); 67093b2d4a2SDavid S. Miller } 671b00055aaSStefan Rompf } 672b00055aaSStefan Rompf 6733729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 6743729d502SPatrick McHardy const struct ifinfomsg *ifm) 6753729d502SPatrick McHardy { 6763729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 6773729d502SPatrick McHardy 6783729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 6793729d502SPatrick McHardy if (ifm->ifi_change) 6803729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 6813729d502SPatrick McHardy (dev->flags & ~ifm->ifi_change); 6823729d502SPatrick McHardy 6833729d502SPatrick McHardy return flags; 6843729d502SPatrick McHardy } 6853729d502SPatrick McHardy 686b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 687be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 6881da177e4SLinus Torvalds { 689b60c5115SThomas Graf a->rx_packets = b->rx_packets; 690b60c5115SThomas Graf a->tx_packets = b->tx_packets; 691b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 692b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 693b60c5115SThomas Graf a->rx_errors = b->rx_errors; 694b60c5115SThomas Graf a->tx_errors = b->tx_errors; 695b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 696b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 697b60c5115SThomas Graf 698b60c5115SThomas Graf a->multicast = b->multicast; 699b60c5115SThomas Graf a->collisions = b->collisions; 700b60c5115SThomas Graf 701b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 702b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 703b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 704b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 705b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 706b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 707b60c5115SThomas Graf 708b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 709b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 710b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 711b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 712b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 713b60c5115SThomas Graf 714b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 715b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 71610708f37SJan Engelhardt } 71710708f37SJan Engelhardt 718be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) 71910708f37SJan Engelhardt { 720afdcba37SEric Dumazet memcpy(v, b, sizeof(*b)); 72110708f37SJan Engelhardt } 722b60c5115SThomas Graf 723c02db8c6SChris Wright /* All VF info */ 724ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev) 725ebc08a6fSWilliams, Mitch A { 726c02db8c6SChris Wright if (dev->dev.parent && dev_is_pci(dev->dev.parent)) { 727c02db8c6SChris Wright 728c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 729045de01aSScott Feldman size_t size = nla_total_size(sizeof(struct nlattr)); 730045de01aSScott Feldman size += nla_total_size(num_vfs * sizeof(struct nlattr)); 731045de01aSScott Feldman size += num_vfs * 732045de01aSScott Feldman (nla_total_size(sizeof(struct ifla_vf_mac)) + 733045de01aSScott Feldman nla_total_size(sizeof(struct ifla_vf_vlan)) + 734045de01aSScott Feldman nla_total_size(sizeof(struct ifla_vf_tx_rate))); 735c02db8c6SChris Wright return size; 736c02db8c6SChris Wright } else 737ebc08a6fSWilliams, Mitch A return 0; 738ebc08a6fSWilliams, Mitch A } 739ebc08a6fSWilliams, Mitch A 74057b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev) 74157b61080SScott Feldman { 74257b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 74357b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 74457b61080SScott Feldman + nla_total_size(sizeof(struct ifla_port_vsi)) 74557b61080SScott Feldman /* PORT_VSI_TYPE */ 74657b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 74757b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 74857b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 74957b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 75057b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 75157b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 75257b61080SScott Feldman + port_size; 75357b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 75457b61080SScott Feldman + port_size; 75557b61080SScott Feldman 75657b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 75757b61080SScott Feldman return 0; 75857b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 75957b61080SScott Feldman return port_self_size + vf_ports_size + 76057b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 76157b61080SScott Feldman else 76257b61080SScott Feldman return port_self_size; 76357b61080SScott Feldman } 76457b61080SScott Feldman 7659e34a5b5SEric Dumazet static noinline size_t if_nlmsg_size(const struct net_device *dev) 766339bf98fSThomas Graf { 767339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 768339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 7690b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 770339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 771339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 772339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 773adcfe196SJan Engelhardt + nla_total_size(sizeof(struct rtnl_link_stats64)) 774339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 775339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 776339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 777339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 778339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 779339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 780339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 781339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 78238f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 783ebc08a6fSWilliams, Mitch A + nla_total_size(4) /* IFLA_NUM_VF */ 784c02db8c6SChris Wright + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */ 78557b61080SScott Feldman + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 786f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 787f8ff182cSThomas Graf + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */ 788339bf98fSThomas Graf } 789339bf98fSThomas Graf 79057b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 79157b61080SScott Feldman { 79257b61080SScott Feldman struct nlattr *vf_ports; 79357b61080SScott Feldman struct nlattr *vf_port; 79457b61080SScott Feldman int vf; 79557b61080SScott Feldman int err; 79657b61080SScott Feldman 79757b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 79857b61080SScott Feldman if (!vf_ports) 79957b61080SScott Feldman return -EMSGSIZE; 80057b61080SScott Feldman 80157b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 80257b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 8038ca94183SScott Feldman if (!vf_port) 8048ca94183SScott Feldman goto nla_put_failure; 80557b61080SScott Feldman NLA_PUT_U32(skb, IFLA_PORT_VF, vf); 80657b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 8078ca94183SScott Feldman if (err == -EMSGSIZE) 8088ca94183SScott Feldman goto nla_put_failure; 80957b61080SScott Feldman if (err) { 81057b61080SScott Feldman nla_nest_cancel(skb, vf_port); 81157b61080SScott Feldman continue; 81257b61080SScott Feldman } 81357b61080SScott Feldman nla_nest_end(skb, vf_port); 81457b61080SScott Feldman } 81557b61080SScott Feldman 81657b61080SScott Feldman nla_nest_end(skb, vf_ports); 81757b61080SScott Feldman 81857b61080SScott Feldman return 0; 8198ca94183SScott Feldman 8208ca94183SScott Feldman nla_put_failure: 8218ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 8228ca94183SScott Feldman return -EMSGSIZE; 82357b61080SScott Feldman } 82457b61080SScott Feldman 82557b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 82657b61080SScott Feldman { 82757b61080SScott Feldman struct nlattr *port_self; 82857b61080SScott Feldman int err; 82957b61080SScott Feldman 83057b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 83157b61080SScott Feldman if (!port_self) 83257b61080SScott Feldman return -EMSGSIZE; 83357b61080SScott Feldman 83457b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 83557b61080SScott Feldman if (err) { 83657b61080SScott Feldman nla_nest_cancel(skb, port_self); 8378ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 83857b61080SScott Feldman } 83957b61080SScott Feldman 84057b61080SScott Feldman nla_nest_end(skb, port_self); 84157b61080SScott Feldman 84257b61080SScott Feldman return 0; 84357b61080SScott Feldman } 84457b61080SScott Feldman 84557b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev) 84657b61080SScott Feldman { 84757b61080SScott Feldman int err; 84857b61080SScott Feldman 84957b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 85057b61080SScott Feldman return 0; 85157b61080SScott Feldman 85257b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 85357b61080SScott Feldman if (err) 85457b61080SScott Feldman return err; 85557b61080SScott Feldman 85657b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 85757b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 85857b61080SScott Feldman if (err) 85957b61080SScott Feldman return err; 86057b61080SScott Feldman } 86157b61080SScott Feldman 86257b61080SScott Feldman return 0; 86357b61080SScott Feldman } 86457b61080SScott Feldman 865b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 866575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 867575c3e2aSPatrick McHardy unsigned int flags) 868b60c5115SThomas Graf { 869b60c5115SThomas Graf struct ifinfomsg *ifm; 8701da177e4SLinus Torvalds struct nlmsghdr *nlh; 87128172739SEric Dumazet struct rtnl_link_stats64 temp; 872be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *stats; 873f8ff182cSThomas Graf struct nlattr *attr, *af_spec; 874f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 8751da177e4SLinus Torvalds 8762907c35fSEric Dumazet ASSERT_RTNL(); 877b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 878b60c5115SThomas Graf if (nlh == NULL) 87926932566SPatrick McHardy return -EMSGSIZE; 8801da177e4SLinus Torvalds 881b60c5115SThomas Graf ifm = nlmsg_data(nlh); 882b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 883b60c5115SThomas Graf ifm->__ifi_pad = 0; 884b60c5115SThomas Graf ifm->ifi_type = dev->type; 885b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 886b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 887b60c5115SThomas Graf ifm->ifi_change = change; 8881da177e4SLinus Torvalds 889b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 890b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 891b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 892b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 893b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 894b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 895cbda10faSVlad Dogaru NLA_PUT_U32(skb, IFLA_GROUP, dev->group); 8961da177e4SLinus Torvalds 897b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 898b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 8991da177e4SLinus Torvalds 900b60c5115SThomas Graf if (dev->master) 901b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 902b60c5115SThomas Graf 903af356afaSPatrick McHardy if (dev->qdisc) 904af356afaSPatrick McHardy NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 905b00055aaSStefan Rompf 9060b815a1aSStephen Hemminger if (dev->ifalias) 9070b815a1aSStephen Hemminger NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 9080b815a1aSStephen Hemminger 909b00055aaSStefan Rompf if (1) { 9101da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 9111da177e4SLinus Torvalds .mem_start = dev->mem_start, 9121da177e4SLinus Torvalds .mem_end = dev->mem_end, 9131da177e4SLinus Torvalds .base_addr = dev->base_addr, 9141da177e4SLinus Torvalds .irq = dev->irq, 9151da177e4SLinus Torvalds .dma = dev->dma, 9161da177e4SLinus Torvalds .port = dev->if_port, 9171da177e4SLinus Torvalds }; 918b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (dev->addr_len) { 922b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 923b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 926b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 927b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 928b60c5115SThomas Graf if (attr == NULL) 929b60c5115SThomas Graf goto nla_put_failure; 930b60c5115SThomas Graf 93128172739SEric Dumazet stats = dev_get_stats(dev, &temp); 932b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 9331da177e4SLinus Torvalds 93410708f37SJan Engelhardt attr = nla_reserve(skb, IFLA_STATS64, 93510708f37SJan Engelhardt sizeof(struct rtnl_link_stats64)); 93610708f37SJan Engelhardt if (attr == NULL) 93710708f37SJan Engelhardt goto nla_put_failure; 93810708f37SJan Engelhardt copy_rtnl_link_stats64(nla_data(attr), stats); 93910708f37SJan Engelhardt 94057b61080SScott Feldman if (dev->dev.parent) 94157b61080SScott Feldman NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 94257b61080SScott Feldman 943ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { 944ebc08a6fSWilliams, Mitch A int i; 945ebc08a6fSWilliams, Mitch A 946c02db8c6SChris Wright struct nlattr *vfinfo, *vf; 947c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 948c02db8c6SChris Wright 949c02db8c6SChris Wright vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 950c02db8c6SChris Wright if (!vfinfo) 951c02db8c6SChris Wright goto nla_put_failure; 952c02db8c6SChris Wright for (i = 0; i < num_vfs; i++) { 953c02db8c6SChris Wright struct ifla_vf_info ivi; 954c02db8c6SChris Wright struct ifla_vf_mac vf_mac; 955c02db8c6SChris Wright struct ifla_vf_vlan vf_vlan; 956c02db8c6SChris Wright struct ifla_vf_tx_rate vf_tx_rate; 957ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 958ebc08a6fSWilliams, Mitch A break; 959c02db8c6SChris Wright vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf; 960c02db8c6SChris Wright memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 961c02db8c6SChris Wright vf_vlan.vlan = ivi.vlan; 962c02db8c6SChris Wright vf_vlan.qos = ivi.qos; 963c02db8c6SChris Wright vf_tx_rate.rate = ivi.tx_rate; 964c02db8c6SChris Wright vf = nla_nest_start(skb, IFLA_VF_INFO); 965c02db8c6SChris Wright if (!vf) { 966c02db8c6SChris Wright nla_nest_cancel(skb, vfinfo); 967c02db8c6SChris Wright goto nla_put_failure; 968ebc08a6fSWilliams, Mitch A } 969c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); 970c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); 971c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate); 972c02db8c6SChris Wright nla_nest_end(skb, vf); 973c02db8c6SChris Wright } 974c02db8c6SChris Wright nla_nest_end(skb, vfinfo); 975ebc08a6fSWilliams, Mitch A } 97657b61080SScott Feldman 97757b61080SScott Feldman if (rtnl_port_fill(skb, dev)) 97857b61080SScott Feldman goto nla_put_failure; 97957b61080SScott Feldman 98038f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 98138f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 98238f7b870SPatrick McHardy goto nla_put_failure; 98338f7b870SPatrick McHardy } 98438f7b870SPatrick McHardy 985f8ff182cSThomas Graf if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 986f8ff182cSThomas Graf goto nla_put_failure; 987f8ff182cSThomas Graf 988f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 989f8ff182cSThomas Graf if (af_ops->fill_link_af) { 990f8ff182cSThomas Graf struct nlattr *af; 991f8ff182cSThomas Graf int err; 992f8ff182cSThomas Graf 993f8ff182cSThomas Graf if (!(af = nla_nest_start(skb, af_ops->family))) 994f8ff182cSThomas Graf goto nla_put_failure; 995f8ff182cSThomas Graf 996f8ff182cSThomas Graf err = af_ops->fill_link_af(skb, dev); 997f8ff182cSThomas Graf 998f8ff182cSThomas Graf /* 999f8ff182cSThomas Graf * Caller may return ENODATA to indicate that there 1000f8ff182cSThomas Graf * was no data to be dumped. This is not an error, it 1001f8ff182cSThomas Graf * means we should trim the attribute header and 1002f8ff182cSThomas Graf * continue. 1003f8ff182cSThomas Graf */ 1004f8ff182cSThomas Graf if (err == -ENODATA) 1005f8ff182cSThomas Graf nla_nest_cancel(skb, af); 1006f8ff182cSThomas Graf else if (err < 0) 1007f8ff182cSThomas Graf goto nla_put_failure; 1008f8ff182cSThomas Graf 1009f8ff182cSThomas Graf nla_nest_end(skb, af); 1010f8ff182cSThomas Graf } 1011f8ff182cSThomas Graf } 1012f8ff182cSThomas Graf 1013f8ff182cSThomas Graf nla_nest_end(skb, af_spec); 1014f8ff182cSThomas Graf 1015b60c5115SThomas Graf return nlmsg_end(skb, nlh); 1016b60c5115SThomas Graf 1017b60c5115SThomas Graf nla_put_failure: 101826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 101926932566SPatrick McHardy return -EMSGSIZE; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 1022b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 10231da177e4SLinus Torvalds { 10243b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 10257c28bd0bSEric Dumazet int h, s_h; 10267c28bd0bSEric Dumazet int idx = 0, s_idx; 10271da177e4SLinus Torvalds struct net_device *dev; 10287c28bd0bSEric Dumazet struct hlist_head *head; 10297c28bd0bSEric Dumazet struct hlist_node *node; 10301da177e4SLinus Torvalds 10317c28bd0bSEric Dumazet s_h = cb->args[0]; 10327c28bd0bSEric Dumazet s_idx = cb->args[1]; 10337c28bd0bSEric Dumazet 1034e67f88ddSEric Dumazet rcu_read_lock(); 10357c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 10367562f876SPavel Emelianov idx = 0; 10377c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 1038e67f88ddSEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 10391da177e4SLinus Torvalds if (idx < s_idx) 10407562f876SPavel Emelianov goto cont; 1041575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1042b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 10437c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 10447c28bd0bSEric Dumazet NLM_F_MULTI) <= 0) 10457c28bd0bSEric Dumazet goto out; 10467562f876SPavel Emelianov cont: 10477562f876SPavel Emelianov idx++; 10481da177e4SLinus Torvalds } 10497c28bd0bSEric Dumazet } 10507c28bd0bSEric Dumazet out: 1051e67f88ddSEric Dumazet rcu_read_unlock(); 10527c28bd0bSEric Dumazet cb->args[1] = idx; 10537c28bd0bSEric Dumazet cb->args[0] = h; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds return skb->len; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 1058e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 10595176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 106038f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 106138f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 10625176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1063da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 106476e87306SThomas Graf [IFLA_LINK] = { .type = NLA_U32 }, 1065fbaec0eaSJiri Pirko [IFLA_MASTER] = { .type = NLA_U32 }, 1066da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 1067da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 1068da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1069da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 107076e87306SThomas Graf [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1071d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1072f0630529SEric W. Biederman [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 10730b815a1aSStephen Hemminger [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1074c02db8c6SChris Wright [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 107557b61080SScott Feldman [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 107657b61080SScott Feldman [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1077f8ff182cSThomas Graf [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1078da5e0494SThomas Graf }; 1079e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy); 10801da177e4SLinus Torvalds 108138f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 108238f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 108338f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 108438f7b870SPatrick McHardy }; 108538f7b870SPatrick McHardy 1086c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 1087c02db8c6SChris Wright [IFLA_VF_INFO] = { .type = NLA_NESTED }, 1088c02db8c6SChris Wright }; 1089c02db8c6SChris Wright 1090c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1091c02db8c6SChris Wright [IFLA_VF_MAC] = { .type = NLA_BINARY, 1092c02db8c6SChris Wright .len = sizeof(struct ifla_vf_mac) }, 1093c02db8c6SChris Wright [IFLA_VF_VLAN] = { .type = NLA_BINARY, 1094c02db8c6SChris Wright .len = sizeof(struct ifla_vf_vlan) }, 1095c02db8c6SChris Wright [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 1096c02db8c6SChris Wright .len = sizeof(struct ifla_vf_tx_rate) }, 1097c02db8c6SChris Wright }; 1098c02db8c6SChris Wright 109957b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 110057b61080SScott Feldman [IFLA_PORT_VF] = { .type = NLA_U32 }, 110157b61080SScott Feldman [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 110257b61080SScott Feldman .len = PORT_PROFILE_MAX }, 110357b61080SScott Feldman [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 110457b61080SScott Feldman .len = sizeof(struct ifla_port_vsi)}, 110557b61080SScott Feldman [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 110657b61080SScott Feldman .len = PORT_UUID_MAX }, 110757b61080SScott Feldman [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 110857b61080SScott Feldman .len = PORT_UUID_MAX }, 110957b61080SScott Feldman [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 111057b61080SScott Feldman [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 111157b61080SScott Feldman }; 111257b61080SScott Feldman 111381adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 111481adee47SEric W. Biederman { 111581adee47SEric W. Biederman struct net *net; 111681adee47SEric W. Biederman /* Examine the link attributes and figure out which 111781adee47SEric W. Biederman * network namespace we are talking about. 111881adee47SEric W. Biederman */ 111981adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 112081adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1121f0630529SEric W. Biederman else if (tb[IFLA_NET_NS_FD]) 1122f0630529SEric W. Biederman net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 112381adee47SEric W. Biederman else 112481adee47SEric W. Biederman net = get_net(src_net); 112581adee47SEric W. Biederman return net; 112681adee47SEric W. Biederman } 112781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 112881adee47SEric W. Biederman 11291840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 11301840bb13SThomas Graf { 11311840bb13SThomas Graf if (dev) { 11321840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 11331840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 11341840bb13SThomas Graf return -EINVAL; 11351840bb13SThomas Graf 11361840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 11371840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 11381840bb13SThomas Graf return -EINVAL; 11391840bb13SThomas Graf } 11401840bb13SThomas Graf 1141cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 1142cf7afbfeSThomas Graf struct nlattr *af; 1143cf7afbfeSThomas Graf int rem, err; 1144cf7afbfeSThomas Graf 1145cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1146cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 1147cf7afbfeSThomas Graf 1148cf7afbfeSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1149cf7afbfeSThomas Graf return -EAFNOSUPPORT; 1150cf7afbfeSThomas Graf 1151cf7afbfeSThomas Graf if (!af_ops->set_link_af) 1152cf7afbfeSThomas Graf return -EOPNOTSUPP; 1153cf7afbfeSThomas Graf 1154cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 11556d3a9a68SKurt Van Dijck err = af_ops->validate_link_af(dev, af); 1156cf7afbfeSThomas Graf if (err < 0) 1157cf7afbfeSThomas Graf return err; 1158cf7afbfeSThomas Graf } 1159cf7afbfeSThomas Graf } 1160cf7afbfeSThomas Graf } 1161cf7afbfeSThomas Graf 11621840bb13SThomas Graf return 0; 11631840bb13SThomas Graf } 11641840bb13SThomas Graf 1165c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1166c02db8c6SChris Wright { 1167c02db8c6SChris Wright int rem, err = -EINVAL; 1168c02db8c6SChris Wright struct nlattr *vf; 1169c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 1170c02db8c6SChris Wright 1171c02db8c6SChris Wright nla_for_each_nested(vf, attr, rem) { 1172c02db8c6SChris Wright switch (nla_type(vf)) { 1173c02db8c6SChris Wright case IFLA_VF_MAC: { 1174c02db8c6SChris Wright struct ifla_vf_mac *ivm; 1175c02db8c6SChris Wright ivm = nla_data(vf); 1176c02db8c6SChris Wright err = -EOPNOTSUPP; 1177c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 1178c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 1179c02db8c6SChris Wright ivm->mac); 1180c02db8c6SChris Wright break; 1181c02db8c6SChris Wright } 1182c02db8c6SChris Wright case IFLA_VF_VLAN: { 1183c02db8c6SChris Wright struct ifla_vf_vlan *ivv; 1184c02db8c6SChris Wright ivv = nla_data(vf); 1185c02db8c6SChris Wright err = -EOPNOTSUPP; 1186c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 1187c02db8c6SChris Wright err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1188c02db8c6SChris Wright ivv->vlan, 1189c02db8c6SChris Wright ivv->qos); 1190c02db8c6SChris Wright break; 1191c02db8c6SChris Wright } 1192c02db8c6SChris Wright case IFLA_VF_TX_RATE: { 1193c02db8c6SChris Wright struct ifla_vf_tx_rate *ivt; 1194c02db8c6SChris Wright ivt = nla_data(vf); 1195c02db8c6SChris Wright err = -EOPNOTSUPP; 1196c02db8c6SChris Wright if (ops->ndo_set_vf_tx_rate) 1197c02db8c6SChris Wright err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, 1198c02db8c6SChris Wright ivt->rate); 1199c02db8c6SChris Wright break; 1200c02db8c6SChris Wright } 1201c02db8c6SChris Wright default: 1202c02db8c6SChris Wright err = -EINVAL; 1203c02db8c6SChris Wright break; 1204c02db8c6SChris Wright } 1205c02db8c6SChris Wright if (err) 1206c02db8c6SChris Wright break; 1207c02db8c6SChris Wright } 1208c02db8c6SChris Wright return err; 1209c02db8c6SChris Wright } 1210c02db8c6SChris Wright 1211fbaec0eaSJiri Pirko static int do_set_master(struct net_device *dev, int ifindex) 1212fbaec0eaSJiri Pirko { 1213fbaec0eaSJiri Pirko struct net_device *master_dev; 1214fbaec0eaSJiri Pirko const struct net_device_ops *ops; 1215fbaec0eaSJiri Pirko int err; 1216fbaec0eaSJiri Pirko 1217fbaec0eaSJiri Pirko if (dev->master) { 1218fbaec0eaSJiri Pirko if (dev->master->ifindex == ifindex) 1219fbaec0eaSJiri Pirko return 0; 1220fbaec0eaSJiri Pirko ops = dev->master->netdev_ops; 1221fbaec0eaSJiri Pirko if (ops->ndo_del_slave) { 1222fbaec0eaSJiri Pirko err = ops->ndo_del_slave(dev->master, dev); 1223fbaec0eaSJiri Pirko if (err) 1224fbaec0eaSJiri Pirko return err; 1225fbaec0eaSJiri Pirko } else { 1226fbaec0eaSJiri Pirko return -EOPNOTSUPP; 1227fbaec0eaSJiri Pirko } 1228fbaec0eaSJiri Pirko } 1229fbaec0eaSJiri Pirko 1230fbaec0eaSJiri Pirko if (ifindex) { 1231fbaec0eaSJiri Pirko master_dev = __dev_get_by_index(dev_net(dev), ifindex); 1232fbaec0eaSJiri Pirko if (!master_dev) 1233fbaec0eaSJiri Pirko return -EINVAL; 1234fbaec0eaSJiri Pirko ops = master_dev->netdev_ops; 1235fbaec0eaSJiri Pirko if (ops->ndo_add_slave) { 1236fbaec0eaSJiri Pirko err = ops->ndo_add_slave(master_dev, dev); 1237fbaec0eaSJiri Pirko if (err) 1238fbaec0eaSJiri Pirko return err; 1239fbaec0eaSJiri Pirko } else { 1240fbaec0eaSJiri Pirko return -EOPNOTSUPP; 1241fbaec0eaSJiri Pirko } 1242fbaec0eaSJiri Pirko } 1243fbaec0eaSJiri Pirko return 0; 1244fbaec0eaSJiri Pirko } 1245fbaec0eaSJiri Pirko 12460157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 124738f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 12480157f60cSPatrick McHardy { 1249d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 125038f7b870SPatrick McHardy int send_addr_notify = 0; 12510157f60cSPatrick McHardy int err; 12520157f60cSPatrick McHardy 1253f0630529SEric W. Biederman if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { 125481adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1255d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 1256d8a5ec67SEric W. Biederman err = PTR_ERR(net); 1257d8a5ec67SEric W. Biederman goto errout; 1258d8a5ec67SEric W. Biederman } 1259d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 1260d8a5ec67SEric W. Biederman put_net(net); 1261d8a5ec67SEric W. Biederman if (err) 1262d8a5ec67SEric W. Biederman goto errout; 1263d8a5ec67SEric W. Biederman modified = 1; 1264d8a5ec67SEric W. Biederman } 1265d8a5ec67SEric W. Biederman 12660157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 12670157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 12680157f60cSPatrick McHardy struct ifmap k_map; 12690157f60cSPatrick McHardy 1270d314774cSStephen Hemminger if (!ops->ndo_set_config) { 12710157f60cSPatrick McHardy err = -EOPNOTSUPP; 12720157f60cSPatrick McHardy goto errout; 12730157f60cSPatrick McHardy } 12740157f60cSPatrick McHardy 12750157f60cSPatrick McHardy if (!netif_device_present(dev)) { 12760157f60cSPatrick McHardy err = -ENODEV; 12770157f60cSPatrick McHardy goto errout; 12780157f60cSPatrick McHardy } 12790157f60cSPatrick McHardy 12800157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 12810157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 12820157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 12830157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 12840157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 12850157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 12860157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 12870157f60cSPatrick McHardy 1288d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 12890157f60cSPatrick McHardy if (err < 0) 12900157f60cSPatrick McHardy goto errout; 12910157f60cSPatrick McHardy 12920157f60cSPatrick McHardy modified = 1; 12930157f60cSPatrick McHardy } 12940157f60cSPatrick McHardy 12950157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 12960157f60cSPatrick McHardy struct sockaddr *sa; 12970157f60cSPatrick McHardy int len; 12980157f60cSPatrick McHardy 1299d314774cSStephen Hemminger if (!ops->ndo_set_mac_address) { 13000157f60cSPatrick McHardy err = -EOPNOTSUPP; 13010157f60cSPatrick McHardy goto errout; 13020157f60cSPatrick McHardy } 13030157f60cSPatrick McHardy 13040157f60cSPatrick McHardy if (!netif_device_present(dev)) { 13050157f60cSPatrick McHardy err = -ENODEV; 13060157f60cSPatrick McHardy goto errout; 13070157f60cSPatrick McHardy } 13080157f60cSPatrick McHardy 13090157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 13100157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 13110157f60cSPatrick McHardy if (!sa) { 13120157f60cSPatrick McHardy err = -ENOMEM; 13130157f60cSPatrick McHardy goto errout; 13140157f60cSPatrick McHardy } 13150157f60cSPatrick McHardy sa->sa_family = dev->type; 13160157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 13170157f60cSPatrick McHardy dev->addr_len); 1318d314774cSStephen Hemminger err = ops->ndo_set_mac_address(dev, sa); 13190157f60cSPatrick McHardy kfree(sa); 13200157f60cSPatrick McHardy if (err) 13210157f60cSPatrick McHardy goto errout; 13220157f60cSPatrick McHardy send_addr_notify = 1; 13230157f60cSPatrick McHardy modified = 1; 13240157f60cSPatrick McHardy } 13250157f60cSPatrick McHardy 13260157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 13270157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 13280157f60cSPatrick McHardy if (err < 0) 13290157f60cSPatrick McHardy goto errout; 13300157f60cSPatrick McHardy modified = 1; 13310157f60cSPatrick McHardy } 13320157f60cSPatrick McHardy 1333cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 1334cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1335cbda10faSVlad Dogaru modified = 1; 1336cbda10faSVlad Dogaru } 1337cbda10faSVlad Dogaru 13380157f60cSPatrick McHardy /* 13390157f60cSPatrick McHardy * Interface selected by interface index but interface 13400157f60cSPatrick McHardy * name provided implies that a name change has been 13410157f60cSPatrick McHardy * requested. 13420157f60cSPatrick McHardy */ 13430157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 13440157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 13450157f60cSPatrick McHardy if (err < 0) 13460157f60cSPatrick McHardy goto errout; 13470157f60cSPatrick McHardy modified = 1; 13480157f60cSPatrick McHardy } 13490157f60cSPatrick McHardy 13500b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 13510b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 13520b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 13530b815a1aSStephen Hemminger if (err < 0) 13540b815a1aSStephen Hemminger goto errout; 13550b815a1aSStephen Hemminger modified = 1; 13560b815a1aSStephen Hemminger } 13570b815a1aSStephen Hemminger 13580157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 13590157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 13600157f60cSPatrick McHardy send_addr_notify = 1; 13610157f60cSPatrick McHardy } 13620157f60cSPatrick McHardy 13630157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 13643729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 13655f9021cfSJohannes Berg if (err < 0) 13665f9021cfSJohannes Berg goto errout; 13670157f60cSPatrick McHardy } 13680157f60cSPatrick McHardy 1369fbaec0eaSJiri Pirko if (tb[IFLA_MASTER]) { 1370fbaec0eaSJiri Pirko err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 1371fbaec0eaSJiri Pirko if (err) 1372fbaec0eaSJiri Pirko goto errout; 1373fbaec0eaSJiri Pirko modified = 1; 1374fbaec0eaSJiri Pirko } 1375fbaec0eaSJiri Pirko 137693b2d4a2SDavid S. Miller if (tb[IFLA_TXQLEN]) 13770157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 13780157f60cSPatrick McHardy 13790157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 138093b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 13810157f60cSPatrick McHardy 13820157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 13830157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 13840157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 138593b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 13860157f60cSPatrick McHardy } 13870157f60cSPatrick McHardy 1388c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 1389c02db8c6SChris Wright struct nlattr *attr; 1390c02db8c6SChris Wright int rem; 1391c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1392253683bbSDavid Howells if (nla_type(attr) != IFLA_VF_INFO) { 1393253683bbSDavid Howells err = -EINVAL; 1394c02db8c6SChris Wright goto errout; 1395253683bbSDavid Howells } 1396c02db8c6SChris Wright err = do_setvfinfo(dev, attr); 1397ebc08a6fSWilliams, Mitch A if (err < 0) 1398ebc08a6fSWilliams, Mitch A goto errout; 1399ebc08a6fSWilliams, Mitch A modified = 1; 1400ebc08a6fSWilliams, Mitch A } 1401ebc08a6fSWilliams, Mitch A } 14020157f60cSPatrick McHardy err = 0; 14030157f60cSPatrick McHardy 140457b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 140557b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 140657b61080SScott Feldman struct nlattr *attr; 140757b61080SScott Feldman int vf; 140857b61080SScott Feldman int rem; 140957b61080SScott Feldman 141057b61080SScott Feldman err = -EOPNOTSUPP; 141157b61080SScott Feldman if (!ops->ndo_set_vf_port) 141257b61080SScott Feldman goto errout; 141357b61080SScott Feldman 141457b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 141557b61080SScott Feldman if (nla_type(attr) != IFLA_VF_PORT) 141657b61080SScott Feldman continue; 141757b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 141857b61080SScott Feldman attr, ifla_port_policy); 141957b61080SScott Feldman if (err < 0) 142057b61080SScott Feldman goto errout; 142157b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 142257b61080SScott Feldman err = -EOPNOTSUPP; 142357b61080SScott Feldman goto errout; 142457b61080SScott Feldman } 142557b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 142657b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 142757b61080SScott Feldman if (err < 0) 142857b61080SScott Feldman goto errout; 142957b61080SScott Feldman modified = 1; 143057b61080SScott Feldman } 143157b61080SScott Feldman } 143257b61080SScott Feldman err = 0; 143357b61080SScott Feldman 143457b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 143557b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 143657b61080SScott Feldman 143757b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 143857b61080SScott Feldman tb[IFLA_PORT_SELF], ifla_port_policy); 143957b61080SScott Feldman if (err < 0) 144057b61080SScott Feldman goto errout; 144157b61080SScott Feldman 144257b61080SScott Feldman err = -EOPNOTSUPP; 144357b61080SScott Feldman if (ops->ndo_set_vf_port) 144457b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 144557b61080SScott Feldman if (err < 0) 144657b61080SScott Feldman goto errout; 144757b61080SScott Feldman modified = 1; 144857b61080SScott Feldman } 1449f8ff182cSThomas Graf 1450f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 1451f8ff182cSThomas Graf struct nlattr *af; 1452f8ff182cSThomas Graf int rem; 1453f8ff182cSThomas Graf 1454f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1455f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 1456f8ff182cSThomas Graf 1457f8ff182cSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1458cf7afbfeSThomas Graf BUG(); 1459f8ff182cSThomas Graf 1460cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 1461f8ff182cSThomas Graf if (err < 0) 1462f8ff182cSThomas Graf goto errout; 1463f8ff182cSThomas Graf 1464f8ff182cSThomas Graf modified = 1; 1465f8ff182cSThomas Graf } 1466f8ff182cSThomas Graf } 146757b61080SScott Feldman err = 0; 146857b61080SScott Feldman 14690157f60cSPatrick McHardy errout: 14700157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 14710157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 147225985edcSLucas De Marchi "some changes committed already. Interface %s may " 14730157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 14740157f60cSPatrick McHardy "please check.\n", dev->name); 14750157f60cSPatrick McHardy 14760157f60cSPatrick McHardy if (send_addr_notify) 14770157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 14780157f60cSPatrick McHardy return err; 14790157f60cSPatrick McHardy } 14800157f60cSPatrick McHardy 1481da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1482da5e0494SThomas Graf { 14833b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1484da5e0494SThomas Graf struct ifinfomsg *ifm; 1485da5e0494SThomas Graf struct net_device *dev; 14860157f60cSPatrick McHardy int err; 1487da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 14881da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 14891da177e4SLinus Torvalds 1490da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1491da5e0494SThomas Graf if (err < 0) 1492da5e0494SThomas Graf goto errout; 14931da177e4SLinus Torvalds 14945176f91eSThomas Graf if (tb[IFLA_IFNAME]) 14955176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 149678e5b891SPatrick McHardy else 149778e5b891SPatrick McHardy ifname[0] = '\0'; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds err = -EINVAL; 1500da5e0494SThomas Graf ifm = nlmsg_data(nlh); 150151055be8SPatrick McHardy if (ifm->ifi_index > 0) 1502a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1503da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1504a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1505da5e0494SThomas Graf else 1506da5e0494SThomas Graf goto errout; 15071da177e4SLinus Torvalds 1508da5e0494SThomas Graf if (dev == NULL) { 1509da5e0494SThomas Graf err = -ENODEV; 1510da5e0494SThomas Graf goto errout; 1511da5e0494SThomas Graf } 15121da177e4SLinus Torvalds 1513e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1514e0d087afSEric Dumazet if (err < 0) 1515a3d12891SEric Dumazet goto errout; 1516da5e0494SThomas Graf 151738f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 1518da5e0494SThomas Graf errout: 15191da177e4SLinus Torvalds return err; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 152238f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 152338f7b870SPatrick McHardy { 15243b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 152538f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 152638f7b870SPatrick McHardy struct net_device *dev; 152738f7b870SPatrick McHardy struct ifinfomsg *ifm; 152838f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 152938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 153038f7b870SPatrick McHardy int err; 1531226bd341SEric Dumazet LIST_HEAD(list_kill); 153238f7b870SPatrick McHardy 153338f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 153438f7b870SPatrick McHardy if (err < 0) 153538f7b870SPatrick McHardy return err; 153638f7b870SPatrick McHardy 153738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 153838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 153938f7b870SPatrick McHardy 154038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 154138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1542881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 154338f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1544881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 154538f7b870SPatrick McHardy else 154638f7b870SPatrick McHardy return -EINVAL; 154738f7b870SPatrick McHardy 154838f7b870SPatrick McHardy if (!dev) 154938f7b870SPatrick McHardy return -ENODEV; 155038f7b870SPatrick McHardy 155138f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 155238f7b870SPatrick McHardy if (!ops) 155338f7b870SPatrick McHardy return -EOPNOTSUPP; 155438f7b870SPatrick McHardy 1555226bd341SEric Dumazet ops->dellink(dev, &list_kill); 1556226bd341SEric Dumazet unregister_netdevice_many(&list_kill); 1557226bd341SEric Dumazet list_del(&list_kill); 155838f7b870SPatrick McHardy return 0; 155938f7b870SPatrick McHardy } 156038f7b870SPatrick McHardy 15613729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 15623729d502SPatrick McHardy { 15633729d502SPatrick McHardy unsigned int old_flags; 15643729d502SPatrick McHardy int err; 15653729d502SPatrick McHardy 15663729d502SPatrick McHardy old_flags = dev->flags; 15673729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 15683729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 15693729d502SPatrick McHardy if (err < 0) 15703729d502SPatrick McHardy return err; 15713729d502SPatrick McHardy } 15723729d502SPatrick McHardy 15733729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 15743729d502SPatrick McHardy rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 15753729d502SPatrick McHardy 15763729d502SPatrick McHardy __dev_notify_flags(dev, old_flags); 15773729d502SPatrick McHardy return 0; 15783729d502SPatrick McHardy } 15793729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 15803729d502SPatrick McHardy 158181adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 158281adee47SEric W. Biederman char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1583e7199288SPavel Emelianov { 1584e7199288SPavel Emelianov int err; 1585e7199288SPavel Emelianov struct net_device *dev; 15862e59af3dSEric Dumazet unsigned int num_queues = 1; 15872e59af3dSEric Dumazet unsigned int real_num_queues = 1; 1588e7199288SPavel Emelianov 15892e59af3dSEric Dumazet if (ops->get_tx_queues) { 159081adee47SEric W. Biederman err = ops->get_tx_queues(src_net, tb, &num_queues, 1591e0d087afSEric Dumazet &real_num_queues); 15922e59af3dSEric Dumazet if (err) 15932e59af3dSEric Dumazet goto err; 15942e59af3dSEric Dumazet } 1595e7199288SPavel Emelianov err = -ENOMEM; 15962e59af3dSEric Dumazet dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1597e7199288SPavel Emelianov if (!dev) 1598e7199288SPavel Emelianov goto err; 1599e7199288SPavel Emelianov 160081adee47SEric W. Biederman dev_net_set(dev, net); 160181adee47SEric W. Biederman dev->rtnl_link_ops = ops; 16023729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 16032e59af3dSEric Dumazet dev->real_num_tx_queues = real_num_queues; 160481adee47SEric W. Biederman 1605e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1606e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1607e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1608e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1609e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1610e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1611e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1612e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1613e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1614e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1615e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 161693b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1617e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1618e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1619ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 1620ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1621e7199288SPavel Emelianov 1622e7199288SPavel Emelianov return dev; 1623e7199288SPavel Emelianov 1624e7199288SPavel Emelianov err: 1625e7199288SPavel Emelianov return ERR_PTR(err); 1626e7199288SPavel Emelianov } 1627e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1628e7199288SPavel Emelianov 1629e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group, 1630e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 1631e7ed828fSVlad Dogaru struct nlattr **tb) 1632e7ed828fSVlad Dogaru { 1633e7ed828fSVlad Dogaru struct net_device *dev; 1634e7ed828fSVlad Dogaru int err; 1635e7ed828fSVlad Dogaru 1636e7ed828fSVlad Dogaru for_each_netdev(net, dev) { 1637e7ed828fSVlad Dogaru if (dev->group == group) { 1638e7ed828fSVlad Dogaru err = do_setlink(dev, ifm, tb, NULL, 0); 1639e7ed828fSVlad Dogaru if (err < 0) 1640e7ed828fSVlad Dogaru return err; 1641e7ed828fSVlad Dogaru } 1642e7ed828fSVlad Dogaru } 1643e7ed828fSVlad Dogaru 1644e7ed828fSVlad Dogaru return 0; 1645e7ed828fSVlad Dogaru } 1646e7ed828fSVlad Dogaru 164738f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 164838f7b870SPatrick McHardy { 16493b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 165038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 165138f7b870SPatrick McHardy struct net_device *dev; 165238f7b870SPatrick McHardy struct ifinfomsg *ifm; 165338f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 165438f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 165538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 165638f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 165738f7b870SPatrick McHardy int err; 165838f7b870SPatrick McHardy 165995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 166038f7b870SPatrick McHardy replay: 16618072f085SThomas Graf #endif 166238f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 166338f7b870SPatrick McHardy if (err < 0) 166438f7b870SPatrick McHardy return err; 166538f7b870SPatrick McHardy 166638f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 166738f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 166838f7b870SPatrick McHardy else 166938f7b870SPatrick McHardy ifname[0] = '\0'; 167038f7b870SPatrick McHardy 167138f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 167238f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1673881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 1674e7ed828fSVlad Dogaru else { 1675e7ed828fSVlad Dogaru if (ifname[0]) 1676881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 167738f7b870SPatrick McHardy else 167838f7b870SPatrick McHardy dev = NULL; 1679e7ed828fSVlad Dogaru } 168038f7b870SPatrick McHardy 1681e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1682e0d087afSEric Dumazet if (err < 0) 16831840bb13SThomas Graf return err; 16841840bb13SThomas Graf 168538f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 168638f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 168738f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 168838f7b870SPatrick McHardy if (err < 0) 168938f7b870SPatrick McHardy return err; 169038f7b870SPatrick McHardy } else 169138f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 169238f7b870SPatrick McHardy 169338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 169438f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 169538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 169638f7b870SPatrick McHardy } else { 169738f7b870SPatrick McHardy kind[0] = '\0'; 169838f7b870SPatrick McHardy ops = NULL; 169938f7b870SPatrick McHardy } 170038f7b870SPatrick McHardy 170138f7b870SPatrick McHardy if (1) { 170238f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 170381adee47SEric W. Biederman struct net *dest_net; 170438f7b870SPatrick McHardy 170538f7b870SPatrick McHardy if (ops) { 170638f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 170738f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 170838f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 170938f7b870SPatrick McHardy ops->policy); 171038f7b870SPatrick McHardy if (err < 0) 171138f7b870SPatrick McHardy return err; 171238f7b870SPatrick McHardy data = attr; 171338f7b870SPatrick McHardy } 171438f7b870SPatrick McHardy if (ops->validate) { 171538f7b870SPatrick McHardy err = ops->validate(tb, data); 171638f7b870SPatrick McHardy if (err < 0) 171738f7b870SPatrick McHardy return err; 171838f7b870SPatrick McHardy } 171938f7b870SPatrick McHardy } 172038f7b870SPatrick McHardy 172138f7b870SPatrick McHardy if (dev) { 172238f7b870SPatrick McHardy int modified = 0; 172338f7b870SPatrick McHardy 172438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 172538f7b870SPatrick McHardy return -EEXIST; 172638f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 172738f7b870SPatrick McHardy return -EOPNOTSUPP; 172838f7b870SPatrick McHardy 172938f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 173038f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 173138f7b870SPatrick McHardy !ops->changelink) 173238f7b870SPatrick McHardy return -EOPNOTSUPP; 173338f7b870SPatrick McHardy 173438f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 173538f7b870SPatrick McHardy if (err < 0) 173638f7b870SPatrick McHardy return err; 173738f7b870SPatrick McHardy modified = 1; 173838f7b870SPatrick McHardy } 173938f7b870SPatrick McHardy 174038f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 174138f7b870SPatrick McHardy } 174238f7b870SPatrick McHardy 1743ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 1744ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 1745ffa934f1SPatrick McHardy return rtnl_group_changelink(net, 1746ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 1747ffa934f1SPatrick McHardy ifm, tb); 174838f7b870SPatrick McHardy return -ENODEV; 1749ffa934f1SPatrick McHardy } 175038f7b870SPatrick McHardy 17513729d502SPatrick McHardy if (ifm->ifi_index) 175238f7b870SPatrick McHardy return -EOPNOTSUPP; 17530e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 175438f7b870SPatrick McHardy return -EOPNOTSUPP; 175538f7b870SPatrick McHardy 175638f7b870SPatrick McHardy if (!ops) { 175795a5afcaSJohannes Berg #ifdef CONFIG_MODULES 175838f7b870SPatrick McHardy if (kind[0]) { 175938f7b870SPatrick McHardy __rtnl_unlock(); 176038f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 176138f7b870SPatrick McHardy rtnl_lock(); 176238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 176338f7b870SPatrick McHardy if (ops) 176438f7b870SPatrick McHardy goto replay; 176538f7b870SPatrick McHardy } 176638f7b870SPatrick McHardy #endif 176738f7b870SPatrick McHardy return -EOPNOTSUPP; 176838f7b870SPatrick McHardy } 176938f7b870SPatrick McHardy 177038f7b870SPatrick McHardy if (!ifname[0]) 177138f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 177238f7b870SPatrick McHardy 177381adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 177413ad1774SEric W. Biederman if (IS_ERR(dest_net)) 177513ad1774SEric W. Biederman return PTR_ERR(dest_net); 177613ad1774SEric W. Biederman 177781adee47SEric W. Biederman dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 177838f7b870SPatrick McHardy 1779e7199288SPavel Emelianov if (IS_ERR(dev)) 1780e7199288SPavel Emelianov err = PTR_ERR(dev); 1781e7199288SPavel Emelianov else if (ops->newlink) 178281adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 17832d85cba2SPatrick McHardy else 17842d85cba2SPatrick McHardy err = register_netdevice(dev); 178580032cffSDan Carpenter 178680032cffSDan Carpenter if (err < 0 && !IS_ERR(dev)) 178738f7b870SPatrick McHardy free_netdev(dev); 178880032cffSDan Carpenter if (err < 0) 17893729d502SPatrick McHardy goto out; 179081adee47SEric W. Biederman 17913729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 17923729d502SPatrick McHardy if (err < 0) 17933729d502SPatrick McHardy unregister_netdevice(dev); 17943729d502SPatrick McHardy out: 179581adee47SEric W. Biederman put_net(dest_net); 179638f7b870SPatrick McHardy return err; 179738f7b870SPatrick McHardy } 179838f7b870SPatrick McHardy } 179938f7b870SPatrick McHardy 1800b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1801711e2c33SJean Tourrilhes { 18023b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1803b60c5115SThomas Graf struct ifinfomsg *ifm; 1804a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 1805b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1806b60c5115SThomas Graf struct net_device *dev = NULL; 1807b60c5115SThomas Graf struct sk_buff *nskb; 1808339bf98fSThomas Graf int err; 1809711e2c33SJean Tourrilhes 1810b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1811b60c5115SThomas Graf if (err < 0) 18129918f230SEric Sesterhenn return err; 1813b60c5115SThomas Graf 1814a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 1815a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1816a3d12891SEric Dumazet 1817b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1818a3d12891SEric Dumazet if (ifm->ifi_index > 0) 1819a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1820a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 1821a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1822a3d12891SEric Dumazet else 1823b60c5115SThomas Graf return -EINVAL; 1824b60c5115SThomas Graf 1825a3d12891SEric Dumazet if (dev == NULL) 1826a3d12891SEric Dumazet return -ENODEV; 1827a3d12891SEric Dumazet 182838f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1829a3d12891SEric Dumazet if (nskb == NULL) 1830a3d12891SEric Dumazet return -ENOBUFS; 1831711e2c33SJean Tourrilhes 1832575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1833575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 183426932566SPatrick McHardy if (err < 0) { 183526932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 183626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 183726932566SPatrick McHardy kfree_skb(nskb); 1838a3d12891SEric Dumazet } else 183997c53cacSDenis V. Lunev err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1840b60c5115SThomas Graf 1841711e2c33SJean Tourrilhes return err; 1842711e2c33SJean Tourrilhes } 1843711e2c33SJean Tourrilhes 1844*c7ac8679SGreg Rose static u16 rtnl_calcit(struct sk_buff *skb) 1845*c7ac8679SGreg Rose { 1846*c7ac8679SGreg Rose return min_ifinfo_dump_size; 1847*c7ac8679SGreg Rose } 1848*c7ac8679SGreg Rose 184942bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds int idx; 18521da177e4SLinus Torvalds int s_idx = cb->family; 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds if (s_idx == 0) 18551da177e4SLinus Torvalds s_idx = 1; 185625239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 18571da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 18581da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 18591da177e4SLinus Torvalds continue; 1860e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1861e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 18621da177e4SLinus Torvalds continue; 18631da177e4SLinus Torvalds if (idx > s_idx) 18641da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1865e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 18661da177e4SLinus Torvalds break; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds cb->family = idx; 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds return skb->len; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 18741da177e4SLinus Torvalds { 1875c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 18761da177e4SLinus Torvalds struct sk_buff *skb; 18770ec6d3f4SThomas Graf int err = -ENOBUFS; 1878*c7ac8679SGreg Rose size_t if_info_size; 18791da177e4SLinus Torvalds 1880*c7ac8679SGreg Rose skb = nlmsg_new((if_info_size = if_nlmsg_size(dev)), GFP_KERNEL); 18810ec6d3f4SThomas Graf if (skb == NULL) 18820ec6d3f4SThomas Graf goto errout; 18831da177e4SLinus Torvalds 1884*c7ac8679SGreg Rose min_ifinfo_dump_size = max_t(u16, if_info_size, min_ifinfo_dump_size); 1885*c7ac8679SGreg Rose 1886575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 188726932566SPatrick McHardy if (err < 0) { 188826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 188926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 189026932566SPatrick McHardy kfree_skb(skb); 189126932566SPatrick McHardy goto errout; 189226932566SPatrick McHardy } 18931ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 18941ce85fe4SPablo Neira Ayuso return; 18950ec6d3f4SThomas Graf errout: 18960ec6d3f4SThomas Graf if (err < 0) 18974b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 19011da177e4SLinus Torvalds static struct rtattr **rta_buf; 19021da177e4SLinus Torvalds static int rtattr_max; 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds /* Process one rtnetlink message. */ 19051da177e4SLinus Torvalds 19061d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 19071da177e4SLinus Torvalds { 19083b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1909e2849863SThomas Graf rtnl_doit_func doit; 19101da177e4SLinus Torvalds int sz_idx, kind; 19111da177e4SLinus Torvalds int min_len; 19121da177e4SLinus Torvalds int family; 19131da177e4SLinus Torvalds int type; 19142907c35fSEric Dumazet int err; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds type = nlh->nlmsg_type; 19171da177e4SLinus Torvalds if (type > RTM_MAX) 1918038890feSThomas Graf return -EOPNOTSUPP; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds type -= RTM_BASE; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 19231da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 19241da177e4SLinus Torvalds return 0; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 19271da177e4SLinus Torvalds sz_idx = type>>2; 19281da177e4SLinus Torvalds kind = type&3; 19291da177e4SLinus Torvalds 19301d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 19311d00a4ebSThomas Graf return -EPERM; 19321da177e4SLinus Torvalds 1933b8f3ab42SDavid S. Miller if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 193497c53cacSDenis V. Lunev struct sock *rtnl; 1935e2849863SThomas Graf rtnl_dumpit_func dumpit; 1936*c7ac8679SGreg Rose rtnl_calcit_func calcit; 1937*c7ac8679SGreg Rose u16 min_dump_alloc = 0; 19381da177e4SLinus Torvalds 1939e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1940e2849863SThomas Graf if (dumpit == NULL) 1941038890feSThomas Graf return -EOPNOTSUPP; 1942*c7ac8679SGreg Rose calcit = rtnl_get_calcit(family, type); 1943*c7ac8679SGreg Rose if (calcit) 1944*c7ac8679SGreg Rose min_dump_alloc = calcit(skb); 19451da177e4SLinus Torvalds 19462907c35fSEric Dumazet __rtnl_unlock(); 194797c53cacSDenis V. Lunev rtnl = net->rtnl; 1948*c7ac8679SGreg Rose err = netlink_dump_start(rtnl, skb, nlh, dumpit, 1949*c7ac8679SGreg Rose NULL, min_dump_alloc); 19502907c35fSEric Dumazet rtnl_lock(); 19512907c35fSEric Dumazet return err; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 19571da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 19581d00a4ebSThomas Graf return -EINVAL; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 19611da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 19621da177e4SLinus Torvalds struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 19651da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 19661da177e4SLinus Torvalds if (flavor) { 19671da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 19681d00a4ebSThomas Graf return -EINVAL; 19691da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 1975e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1976e2849863SThomas Graf if (doit == NULL) 1977038890feSThomas Graf return -EOPNOTSUPP; 19781da177e4SLinus Torvalds 19791d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 1982cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 19831da177e4SLinus Torvalds { 19841536cc0dSDenis V. Lunev rtnl_lock(); 1985cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 19861536cc0dSDenis V. Lunev rtnl_unlock(); 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 19901da177e4SLinus Torvalds { 19911da177e4SLinus Torvalds struct net_device *dev = ptr; 1992e9dc8653SEric W. Biederman 19931da177e4SLinus Torvalds switch (event) { 19941da177e4SLinus Torvalds case NETDEV_UP: 19951da177e4SLinus Torvalds case NETDEV_DOWN: 199610de05afSPatrick McHardy case NETDEV_PRE_UP: 1997d90a909eSEric W. Biederman case NETDEV_POST_INIT: 1998d90a909eSEric W. Biederman case NETDEV_REGISTER: 19991da177e4SLinus Torvalds case NETDEV_CHANGE: 2000755d0e77SPatrick McHardy case NETDEV_PRE_TYPE_CHANGE: 20011da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 2002a2835763SPatrick McHardy case NETDEV_UNREGISTER: 2003d90a909eSEric W. Biederman case NETDEV_UNREGISTER_BATCH: 2004ac3d3f81SAmerigo Wang case NETDEV_RELEASE: 2005ac3d3f81SAmerigo Wang case NETDEV_JOIN: 20061da177e4SLinus Torvalds break; 20071da177e4SLinus Torvalds default: 20081da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 20091da177e4SLinus Torvalds break; 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds return NOTIFY_DONE; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 20151da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 20161da177e4SLinus Torvalds }; 20171da177e4SLinus Torvalds 201897c53cacSDenis V. Lunev 20192c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 202097c53cacSDenis V. Lunev { 202197c53cacSDenis V. Lunev struct sock *sk; 202297c53cacSDenis V. Lunev sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 20232907c35fSEric Dumazet rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 202497c53cacSDenis V. Lunev if (!sk) 202597c53cacSDenis V. Lunev return -ENOMEM; 202697c53cacSDenis V. Lunev net->rtnl = sk; 202797c53cacSDenis V. Lunev return 0; 202897c53cacSDenis V. Lunev } 202997c53cacSDenis V. Lunev 20302c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 203197c53cacSDenis V. Lunev { 2032b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 203397c53cacSDenis V. Lunev net->rtnl = NULL; 203497c53cacSDenis V. Lunev } 203597c53cacSDenis V. Lunev 203697c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 203797c53cacSDenis V. Lunev .init = rtnetlink_net_init, 203897c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 203997c53cacSDenis V. Lunev }; 204097c53cacSDenis V. Lunev 20411da177e4SLinus Torvalds void __init rtnetlink_init(void) 20421da177e4SLinus Torvalds { 20431da177e4SLinus Torvalds int i; 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds rtattr_max = 0; 20461da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 20471da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 20481da177e4SLinus Torvalds rtattr_max = rta_max[i]; 20491da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 20501da177e4SLinus Torvalds if (!rta_buf) 20511da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 20521da177e4SLinus Torvalds 205397c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 20541da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 205597c53cacSDenis V. Lunev 20561da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 20571da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 2058340d17fcSThomas Graf 2059*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 2060*c7ac8679SGreg Rose rtnl_dump_ifinfo, rtnl_calcit); 2061*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); 2062*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); 2063*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); 2064687ad8ccSThomas Graf 2065*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); 2066*c7ac8679SGreg Rose rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069