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; 59e2849863SThomas Graf }; 60e2849863SThomas Graf 616756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds void rtnl_lock(void) 641da177e4SLinus Torvalds { 656756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 661da177e4SLinus Torvalds } 67e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_lock); 681da177e4SLinus Torvalds 696756ae4bSStephen Hemminger void __rtnl_unlock(void) 701da177e4SLinus Torvalds { 716756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds void rtnl_unlock(void) 751da177e4SLinus Torvalds { 7658ec3b4dSHerbert Xu /* This fellow will unlock it for us. */ 771da177e4SLinus Torvalds netdev_run_todo(); 781da177e4SLinus Torvalds } 79e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unlock); 801da177e4SLinus Torvalds 816756ae4bSStephen Hemminger int rtnl_trylock(void) 826756ae4bSStephen Hemminger { 836756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 846756ae4bSStephen Hemminger } 85e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_trylock); 866756ae4bSStephen Hemminger 87c9c1014bSPatrick McHardy int rtnl_is_locked(void) 88c9c1014bSPatrick McHardy { 89c9c1014bSPatrick McHardy return mutex_is_locked(&rtnl_mutex); 90c9c1014bSPatrick McHardy } 91e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_is_locked); 92c9c1014bSPatrick McHardy 93a898def2SPaul E. McKenney #ifdef CONFIG_PROVE_LOCKING 94a898def2SPaul E. McKenney int lockdep_rtnl_is_held(void) 95a898def2SPaul E. McKenney { 96a898def2SPaul E. McKenney return lockdep_is_held(&rtnl_mutex); 97a898def2SPaul E. McKenney } 98a898def2SPaul E. McKenney EXPORT_SYMBOL(lockdep_rtnl_is_held); 99a898def2SPaul E. McKenney #endif /* #ifdef CONFIG_PROVE_LOCKING */ 100a898def2SPaul E. McKenney 10125239ceeSPatrick McHardy static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 102e2849863SThomas Graf 103e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 104e2849863SThomas Graf { 105e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 106e2849863SThomas Graf 107e2849863SThomas Graf /* 108e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 109e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 110e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 111e2849863SThomas Graf */ 112e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 113e2849863SThomas Graf 114e2849863SThomas Graf return msgindex; 115e2849863SThomas Graf } 116e2849863SThomas Graf 117e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 118e2849863SThomas Graf { 119e2849863SThomas Graf struct rtnl_link *tab; 120e2849863SThomas Graf 12125239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 122e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1230f87b1ddSPatrick McHardy else 1240f87b1ddSPatrick McHardy tab = NULL; 1250f87b1ddSPatrick McHardy 12651057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 127e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 128e2849863SThomas Graf 12951057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 130e2849863SThomas Graf } 131e2849863SThomas Graf 132e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 133e2849863SThomas Graf { 134e2849863SThomas Graf struct rtnl_link *tab; 135e2849863SThomas Graf 13625239ceeSPatrick McHardy if (protocol <= RTNL_FAMILY_MAX) 137e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 1380f87b1ddSPatrick McHardy else 1390f87b1ddSPatrick McHardy tab = NULL; 1400f87b1ddSPatrick McHardy 14151057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 142e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 143e2849863SThomas Graf 14451057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 145e2849863SThomas Graf } 146e2849863SThomas Graf 147e2849863SThomas Graf /** 148e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 149e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 150e2849863SThomas Graf * @msgtype: rtnetlink message type 151e2849863SThomas Graf * @doit: Function pointer called for each request message 152e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 153e2849863SThomas Graf * 154e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 155e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 156e2849863SThomas Graf * specified protocol family and message type is received. 157e2849863SThomas Graf * 158e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 159e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 160e2849863SThomas Graf * family exists. 161e2849863SThomas Graf * 162e2849863SThomas Graf * Returns 0 on success or a negative error code. 163e2849863SThomas Graf */ 164e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 165e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 166e2849863SThomas Graf { 167e2849863SThomas Graf struct rtnl_link *tab; 168e2849863SThomas Graf int msgindex; 169e2849863SThomas Graf 17025239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 171e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 172e2849863SThomas Graf 173e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 174e2849863SThomas Graf if (tab == NULL) { 175e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 176e2849863SThomas Graf if (tab == NULL) 177e2849863SThomas Graf return -ENOBUFS; 178e2849863SThomas Graf 179e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 180e2849863SThomas Graf } 181e2849863SThomas Graf 182e2849863SThomas Graf if (doit) 183e2849863SThomas Graf tab[msgindex].doit = doit; 184e2849863SThomas Graf 185e2849863SThomas Graf if (dumpit) 186e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 187e2849863SThomas Graf 188e2849863SThomas Graf return 0; 189e2849863SThomas Graf } 190e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 191e2849863SThomas Graf 192e2849863SThomas Graf /** 193e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 194e2849863SThomas Graf * 195e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 196e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 197e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 198e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 199e2849863SThomas Graf * of memory implies no sense in continueing. 200e2849863SThomas Graf */ 201e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 202e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 203e2849863SThomas Graf { 204e2849863SThomas Graf if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) 205e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 206e2849863SThomas Graf "protocol = %d, message type = %d\n", 207e2849863SThomas Graf protocol, msgtype); 208e2849863SThomas Graf } 209e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 210e2849863SThomas Graf 211e2849863SThomas Graf /** 212e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 213e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 214e2849863SThomas Graf * @msgtype: rtnetlink message type 215e2849863SThomas Graf * 216e2849863SThomas Graf * Returns 0 on success or a negative error code. 217e2849863SThomas Graf */ 218e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 219e2849863SThomas Graf { 220e2849863SThomas Graf int msgindex; 221e2849863SThomas Graf 22225239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 223e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 224e2849863SThomas Graf 225e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 226e2849863SThomas Graf return -ENOENT; 227e2849863SThomas Graf 228e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 229e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 230e2849863SThomas Graf 231e2849863SThomas Graf return 0; 232e2849863SThomas Graf } 233e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 234e2849863SThomas Graf 235e2849863SThomas Graf /** 236e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 237e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 238e2849863SThomas Graf * 239e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 240e2849863SThomas Graf * of a certain protocol family. 241e2849863SThomas Graf */ 242e2849863SThomas Graf void rtnl_unregister_all(int protocol) 243e2849863SThomas Graf { 24425239ceeSPatrick McHardy BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 245e2849863SThomas Graf 246e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 247e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 248e2849863SThomas Graf } 249e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2501da177e4SLinus Torvalds 25138f7b870SPatrick McHardy static LIST_HEAD(link_ops); 25238f7b870SPatrick McHardy 25338f7b870SPatrick McHardy /** 25438f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 25538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 25638f7b870SPatrick McHardy * 25738f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 25838f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 25938f7b870SPatrick McHardy * must be called before registering the devices. 26038f7b870SPatrick McHardy * 26138f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 26238f7b870SPatrick McHardy */ 26338f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 26438f7b870SPatrick McHardy { 2652d85cba2SPatrick McHardy if (!ops->dellink) 26623289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 2672d85cba2SPatrick McHardy 26838f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 26938f7b870SPatrick McHardy return 0; 27038f7b870SPatrick McHardy } 27138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 27238f7b870SPatrick McHardy 27338f7b870SPatrick McHardy /** 27438f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 27538f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 27638f7b870SPatrick McHardy * 27738f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 27838f7b870SPatrick McHardy */ 27938f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 28038f7b870SPatrick McHardy { 28138f7b870SPatrick McHardy int err; 28238f7b870SPatrick McHardy 28338f7b870SPatrick McHardy rtnl_lock(); 28438f7b870SPatrick McHardy err = __rtnl_link_register(ops); 28538f7b870SPatrick McHardy rtnl_unlock(); 28638f7b870SPatrick McHardy return err; 28738f7b870SPatrick McHardy } 28838f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 28938f7b870SPatrick McHardy 290669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 291669f87baSPavel Emelyanov { 292669f87baSPavel Emelyanov struct net_device *dev; 29323289a37SEric Dumazet LIST_HEAD(list_kill); 29423289a37SEric Dumazet 295669f87baSPavel Emelyanov for_each_netdev(net, dev) { 29623289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 29723289a37SEric Dumazet ops->dellink(dev, &list_kill); 298669f87baSPavel Emelyanov } 29923289a37SEric Dumazet unregister_netdevice_many(&list_kill); 300669f87baSPavel Emelyanov } 301669f87baSPavel Emelyanov 302669f87baSPavel Emelyanov void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 303669f87baSPavel Emelyanov { 304669f87baSPavel Emelyanov rtnl_lock(); 305669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 306669f87baSPavel Emelyanov rtnl_unlock(); 307669f87baSPavel Emelyanov } 308669f87baSPavel Emelyanov EXPORT_SYMBOL_GPL(rtnl_kill_links); 309669f87baSPavel Emelyanov 31038f7b870SPatrick McHardy /** 31138f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 31238f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 31338f7b870SPatrick McHardy * 3142d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 31538f7b870SPatrick McHardy */ 31638f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 31738f7b870SPatrick McHardy { 318881d966bSEric W. Biederman struct net *net; 3192d85cba2SPatrick McHardy 320881d966bSEric W. Biederman for_each_net(net) { 321669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 322881d966bSEric W. Biederman } 32338f7b870SPatrick McHardy list_del(&ops->list); 32438f7b870SPatrick McHardy } 32538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 32638f7b870SPatrick McHardy 32738f7b870SPatrick McHardy /** 32838f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 32938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 33038f7b870SPatrick McHardy */ 33138f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 33238f7b870SPatrick McHardy { 33338f7b870SPatrick McHardy rtnl_lock(); 33438f7b870SPatrick McHardy __rtnl_link_unregister(ops); 33538f7b870SPatrick McHardy rtnl_unlock(); 33638f7b870SPatrick McHardy } 33738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 33838f7b870SPatrick McHardy 33938f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 34038f7b870SPatrick McHardy { 34138f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 34238f7b870SPatrick McHardy 34338f7b870SPatrick McHardy list_for_each_entry(ops, &link_ops, list) { 34438f7b870SPatrick McHardy if (!strcmp(ops->kind, kind)) 34538f7b870SPatrick McHardy return ops; 34638f7b870SPatrick McHardy } 34738f7b870SPatrick McHardy return NULL; 34838f7b870SPatrick McHardy } 34938f7b870SPatrick McHardy 35038f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 35138f7b870SPatrick McHardy { 35238f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 35338f7b870SPatrick McHardy size_t size; 35438f7b870SPatrick McHardy 35538f7b870SPatrick McHardy if (!ops) 35638f7b870SPatrick McHardy return 0; 35738f7b870SPatrick McHardy 35838f7b870SPatrick McHardy size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 35938f7b870SPatrick McHardy nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 36038f7b870SPatrick McHardy 36138f7b870SPatrick McHardy if (ops->get_size) 36238f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 36338f7b870SPatrick McHardy size += nlmsg_total_size(sizeof(struct nlattr)) + 36438f7b870SPatrick McHardy ops->get_size(dev); 36538f7b870SPatrick McHardy 36638f7b870SPatrick McHardy if (ops->get_xstats_size) 36738f7b870SPatrick McHardy size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ 36838f7b870SPatrick McHardy 36938f7b870SPatrick McHardy return size; 37038f7b870SPatrick McHardy } 37138f7b870SPatrick McHardy 37238f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 37338f7b870SPatrick McHardy { 37438f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 37538f7b870SPatrick McHardy struct nlattr *linkinfo, *data; 37638f7b870SPatrick McHardy int err = -EMSGSIZE; 37738f7b870SPatrick McHardy 37838f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 37938f7b870SPatrick McHardy if (linkinfo == NULL) 38038f7b870SPatrick McHardy goto out; 38138f7b870SPatrick McHardy 38238f7b870SPatrick McHardy if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 38338f7b870SPatrick McHardy goto err_cancel_link; 38438f7b870SPatrick McHardy if (ops->fill_xstats) { 38538f7b870SPatrick McHardy err = ops->fill_xstats(skb, dev); 38638f7b870SPatrick McHardy if (err < 0) 38738f7b870SPatrick McHardy goto err_cancel_link; 38838f7b870SPatrick McHardy } 38938f7b870SPatrick McHardy if (ops->fill_info) { 39038f7b870SPatrick McHardy data = nla_nest_start(skb, IFLA_INFO_DATA); 39138f7b870SPatrick McHardy if (data == NULL) 39238f7b870SPatrick McHardy goto err_cancel_link; 39338f7b870SPatrick McHardy err = ops->fill_info(skb, dev); 39438f7b870SPatrick McHardy if (err < 0) 39538f7b870SPatrick McHardy goto err_cancel_data; 39638f7b870SPatrick McHardy nla_nest_end(skb, data); 39738f7b870SPatrick McHardy } 39838f7b870SPatrick McHardy 39938f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 40038f7b870SPatrick McHardy return 0; 40138f7b870SPatrick McHardy 40238f7b870SPatrick McHardy err_cancel_data: 40338f7b870SPatrick McHardy nla_nest_cancel(skb, data); 40438f7b870SPatrick McHardy err_cancel_link: 40538f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 40638f7b870SPatrick McHardy out: 40738f7b870SPatrick McHardy return err; 40838f7b870SPatrick McHardy } 40938f7b870SPatrick McHardy 410db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 4111da177e4SLinus Torvalds { 412f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 413f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 414f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 41514c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 416f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 417f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 418f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 419f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 420f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 421f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 4221da177e4SLinus Torvalds }; 4231da177e4SLinus Torvalds 424db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 4251da177e4SLinus Torvalds { 426f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 427f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 428f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 42914c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 430f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 431f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 432f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 433f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 4341da177e4SLinus Torvalds }; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 4371da177e4SLinus Torvalds { 4381da177e4SLinus Torvalds struct rtattr *rta; 4391da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); 4421da177e4SLinus Torvalds rta->rta_type = attrtype; 4431da177e4SLinus Torvalds rta->rta_len = size; 4441da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 445b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 4461da177e4SLinus Torvalds } 447e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill); 4481da177e4SLinus Torvalds 44997c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) 4501da177e4SLinus Torvalds { 45197c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 4521da177e4SLinus Torvalds int err = 0; 4531da177e4SLinus Torvalds 454ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 4551da177e4SLinus Torvalds if (echo) 4561da177e4SLinus Torvalds atomic_inc(&skb->users); 4571da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 4581da177e4SLinus Torvalds if (echo) 4591da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 4601da177e4SLinus Torvalds return err; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 46397c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 4642942e900SThomas Graf { 46597c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 46697c53cacSDenis V. Lunev 4672942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 4682942e900SThomas Graf } 469e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 4702942e900SThomas Graf 4711ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 47297676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 47397676b6bSThomas Graf { 47497c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 47597676b6bSThomas Graf int report = 0; 47697676b6bSThomas Graf 47797676b6bSThomas Graf if (nlh) 47897676b6bSThomas Graf report = nlmsg_report(nlh); 47997676b6bSThomas Graf 4801ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 48197676b6bSThomas Graf } 482e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 48397676b6bSThomas Graf 48497c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 48597676b6bSThomas Graf { 48697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 48797c53cacSDenis V. Lunev 48897676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 48997676b6bSThomas Graf } 490e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 49197676b6bSThomas Graf 4921da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 4931da177e4SLinus Torvalds { 4942d7202bfSThomas Graf struct nlattr *mx; 4952d7202bfSThomas Graf int i, valid = 0; 4961da177e4SLinus Torvalds 4972d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 4982d7202bfSThomas Graf if (mx == NULL) 4992d7202bfSThomas Graf return -ENOBUFS; 5002d7202bfSThomas Graf 5011da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 5022d7202bfSThomas Graf if (metrics[i]) { 5032d7202bfSThomas Graf valid++; 5042d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 5051da177e4SLinus Torvalds } 5062d7202bfSThomas Graf } 5071da177e4SLinus Torvalds 508a57d27fcSDavid S. Miller if (!valid) { 509a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 510a57d27fcSDavid S. Miller return 0; 511a57d27fcSDavid S. Miller } 5122d7202bfSThomas Graf 5132d7202bfSThomas Graf return nla_nest_end(skb, mx); 5142d7202bfSThomas Graf 5152d7202bfSThomas Graf nla_put_failure: 516bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 517bc3ed28cSThomas Graf return -EMSGSIZE; 5181da177e4SLinus Torvalds } 519e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 5201da177e4SLinus Torvalds 521e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 522e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 523e3703b3dSThomas Graf { 524e3703b3dSThomas Graf struct rta_cacheinfo ci = { 525e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 526e3703b3dSThomas Graf .rta_used = dst->__use, 527e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 528e3703b3dSThomas Graf .rta_error = error, 529e3703b3dSThomas Graf .rta_id = id, 530e3703b3dSThomas Graf .rta_ts = ts, 531e3703b3dSThomas Graf .rta_tsage = tsage, 532e3703b3dSThomas Graf }; 533e3703b3dSThomas Graf 534e3703b3dSThomas Graf if (expires) 535e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 536e3703b3dSThomas Graf 537e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 538e3703b3dSThomas Graf } 539e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 5401da177e4SLinus Torvalds 54193b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 542b00055aaSStefan Rompf { 543b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 544b00055aaSStefan Rompf 545b00055aaSStefan Rompf switch (transition) { 546b00055aaSStefan Rompf case IF_OPER_UP: 547b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 548b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 549b00055aaSStefan Rompf !netif_dormant(dev)) 550b00055aaSStefan Rompf operstate = IF_OPER_UP; 551b00055aaSStefan Rompf break; 552b00055aaSStefan Rompf 553b00055aaSStefan Rompf case IF_OPER_DORMANT: 554b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 555b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 556b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 557b00055aaSStefan Rompf break; 5583ff50b79SStephen Hemminger } 559b00055aaSStefan Rompf 560b00055aaSStefan Rompf if (dev->operstate != operstate) { 561b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 562b00055aaSStefan Rompf dev->operstate = operstate; 563b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 564b00055aaSStefan Rompf netdev_state_change(dev); 56593b2d4a2SDavid S. Miller } 566b00055aaSStefan Rompf } 567b00055aaSStefan Rompf 5683729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 5693729d502SPatrick McHardy const struct ifinfomsg *ifm) 5703729d502SPatrick McHardy { 5713729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 5723729d502SPatrick McHardy 5733729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 5743729d502SPatrick McHardy if (ifm->ifi_change) 5753729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 5763729d502SPatrick McHardy (dev->flags & ~ifm->ifi_change); 5773729d502SPatrick McHardy 5783729d502SPatrick McHardy return flags; 5793729d502SPatrick McHardy } 5803729d502SPatrick McHardy 581b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 582eeda3fd6SStephen Hemminger const struct net_device_stats *b) 5831da177e4SLinus Torvalds { 584b60c5115SThomas Graf a->rx_packets = b->rx_packets; 585b60c5115SThomas Graf a->tx_packets = b->tx_packets; 586b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 587b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 588b60c5115SThomas Graf a->rx_errors = b->rx_errors; 589b60c5115SThomas Graf a->tx_errors = b->tx_errors; 590b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 591b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 592b60c5115SThomas Graf 593b60c5115SThomas Graf a->multicast = b->multicast; 594b60c5115SThomas Graf a->collisions = b->collisions; 595b60c5115SThomas Graf 596b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 597b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 598b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 599b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 600b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 601b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 602b60c5115SThomas Graf 603b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 604b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 605b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 606b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 607b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 608b60c5115SThomas Graf 609b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 610b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 61110708f37SJan Engelhardt } 61210708f37SJan Engelhardt 61314a4b42bSJan Engelhardt static void copy_rtnl_link_stats64(void *v, const struct net_device_stats *b) 61410708f37SJan Engelhardt { 61514a4b42bSJan Engelhardt struct rtnl_link_stats64 a; 61610708f37SJan Engelhardt 61714a4b42bSJan Engelhardt a.rx_packets = b->rx_packets; 61814a4b42bSJan Engelhardt a.tx_packets = b->tx_packets; 61914a4b42bSJan Engelhardt a.rx_bytes = b->rx_bytes; 62014a4b42bSJan Engelhardt a.tx_bytes = b->tx_bytes; 62114a4b42bSJan Engelhardt a.rx_errors = b->rx_errors; 62214a4b42bSJan Engelhardt a.tx_errors = b->tx_errors; 62314a4b42bSJan Engelhardt a.rx_dropped = b->rx_dropped; 62414a4b42bSJan Engelhardt a.tx_dropped = b->tx_dropped; 62510708f37SJan Engelhardt 62614a4b42bSJan Engelhardt a.multicast = b->multicast; 62714a4b42bSJan Engelhardt a.collisions = b->collisions; 62810708f37SJan Engelhardt 62914a4b42bSJan Engelhardt a.rx_length_errors = b->rx_length_errors; 63014a4b42bSJan Engelhardt a.rx_over_errors = b->rx_over_errors; 63114a4b42bSJan Engelhardt a.rx_crc_errors = b->rx_crc_errors; 63214a4b42bSJan Engelhardt a.rx_frame_errors = b->rx_frame_errors; 63314a4b42bSJan Engelhardt a.rx_fifo_errors = b->rx_fifo_errors; 63414a4b42bSJan Engelhardt a.rx_missed_errors = b->rx_missed_errors; 63510708f37SJan Engelhardt 63614a4b42bSJan Engelhardt a.tx_aborted_errors = b->tx_aborted_errors; 63714a4b42bSJan Engelhardt a.tx_carrier_errors = b->tx_carrier_errors; 63814a4b42bSJan Engelhardt a.tx_fifo_errors = b->tx_fifo_errors; 63914a4b42bSJan Engelhardt a.tx_heartbeat_errors = b->tx_heartbeat_errors; 64014a4b42bSJan Engelhardt a.tx_window_errors = b->tx_window_errors; 64114a4b42bSJan Engelhardt 64214a4b42bSJan Engelhardt a.rx_compressed = b->rx_compressed; 64314a4b42bSJan Engelhardt a.tx_compressed = b->tx_compressed; 64414a4b42bSJan Engelhardt memcpy(v, &a, sizeof(a)); 64510708f37SJan Engelhardt } 646b60c5115SThomas Graf 647c02db8c6SChris Wright /* All VF info */ 648ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev) 649ebc08a6fSWilliams, Mitch A { 650c02db8c6SChris Wright if (dev->dev.parent && dev_is_pci(dev->dev.parent)) { 651c02db8c6SChris Wright 652c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 653c02db8c6SChris Wright size_t size = nlmsg_total_size(sizeof(struct nlattr)); 654c02db8c6SChris Wright size += nlmsg_total_size(num_vfs * sizeof(struct nlattr)); 655c02db8c6SChris Wright size += num_vfs * (sizeof(struct ifla_vf_mac) + 656c02db8c6SChris Wright sizeof(struct ifla_vf_vlan) + 657c02db8c6SChris Wright sizeof(struct ifla_vf_tx_rate)); 658c02db8c6SChris Wright return size; 659c02db8c6SChris Wright } else 660ebc08a6fSWilliams, Mitch A return 0; 661ebc08a6fSWilliams, Mitch A } 662ebc08a6fSWilliams, Mitch A 66357b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev) 66457b61080SScott Feldman { 66557b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 66657b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 66757b61080SScott Feldman + nla_total_size(sizeof(struct ifla_port_vsi)) 66857b61080SScott Feldman /* PORT_VSI_TYPE */ 66957b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 67057b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 67157b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 67257b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 67357b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 67457b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 67557b61080SScott Feldman + port_size; 67657b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 67757b61080SScott Feldman + port_size; 67857b61080SScott Feldman 67957b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 68057b61080SScott Feldman return 0; 68157b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 68257b61080SScott Feldman return port_self_size + vf_ports_size + 68357b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 68457b61080SScott Feldman else 68557b61080SScott Feldman return port_self_size; 68657b61080SScott Feldman } 68757b61080SScott Feldman 68838f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev) 689339bf98fSThomas Graf { 690339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 691339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 6920b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 693339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 694339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 695339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 696adcfe196SJan Engelhardt + nla_total_size(sizeof(struct rtnl_link_stats64)) 697339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 698339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 699339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 700339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 701339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 702339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 703339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 704339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 70538f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 706ebc08a6fSWilliams, Mitch A + nla_total_size(4) /* IFLA_NUM_VF */ 707c02db8c6SChris Wright + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */ 70857b61080SScott Feldman + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 70938f7b870SPatrick McHardy + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ 710339bf98fSThomas Graf } 711339bf98fSThomas Graf 71257b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 71357b61080SScott Feldman { 71457b61080SScott Feldman struct nlattr *vf_ports; 71557b61080SScott Feldman struct nlattr *vf_port; 71657b61080SScott Feldman int vf; 71757b61080SScott Feldman int err; 71857b61080SScott Feldman 71957b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 72057b61080SScott Feldman if (!vf_ports) 72157b61080SScott Feldman return -EMSGSIZE; 72257b61080SScott Feldman 72357b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 72457b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 725*8ca94183SScott Feldman if (!vf_port) 726*8ca94183SScott Feldman goto nla_put_failure; 72757b61080SScott Feldman NLA_PUT_U32(skb, IFLA_PORT_VF, vf); 72857b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 729*8ca94183SScott Feldman if (err == -EMSGSIZE) 730*8ca94183SScott Feldman goto nla_put_failure; 73157b61080SScott Feldman if (err) { 73257b61080SScott Feldman nla_nest_cancel(skb, vf_port); 73357b61080SScott Feldman continue; 73457b61080SScott Feldman } 73557b61080SScott Feldman nla_nest_end(skb, vf_port); 73657b61080SScott Feldman } 73757b61080SScott Feldman 73857b61080SScott Feldman nla_nest_end(skb, vf_ports); 73957b61080SScott Feldman 74057b61080SScott Feldman return 0; 741*8ca94183SScott Feldman 742*8ca94183SScott Feldman nla_put_failure: 743*8ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 744*8ca94183SScott Feldman return -EMSGSIZE; 74557b61080SScott Feldman } 74657b61080SScott Feldman 74757b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 74857b61080SScott Feldman { 74957b61080SScott Feldman struct nlattr *port_self; 75057b61080SScott Feldman int err; 75157b61080SScott Feldman 75257b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 75357b61080SScott Feldman if (!port_self) 75457b61080SScott Feldman return -EMSGSIZE; 75557b61080SScott Feldman 75657b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 75757b61080SScott Feldman if (err) { 75857b61080SScott Feldman nla_nest_cancel(skb, port_self); 759*8ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 76057b61080SScott Feldman } 76157b61080SScott Feldman 76257b61080SScott Feldman nla_nest_end(skb, port_self); 76357b61080SScott Feldman 76457b61080SScott Feldman return 0; 76557b61080SScott Feldman } 76657b61080SScott Feldman 76757b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev) 76857b61080SScott Feldman { 76957b61080SScott Feldman int err; 77057b61080SScott Feldman 77157b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 77257b61080SScott Feldman return 0; 77357b61080SScott Feldman 77457b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 77557b61080SScott Feldman if (err) 77657b61080SScott Feldman return err; 77757b61080SScott Feldman 77857b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 77957b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 78057b61080SScott Feldman if (err) 78157b61080SScott Feldman return err; 78257b61080SScott Feldman } 78357b61080SScott Feldman 78457b61080SScott Feldman return 0; 78557b61080SScott Feldman } 78657b61080SScott Feldman 787b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 788575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 789575c3e2aSPatrick McHardy unsigned int flags) 790b60c5115SThomas Graf { 791b60c5115SThomas Graf struct ifinfomsg *ifm; 7921da177e4SLinus Torvalds struct nlmsghdr *nlh; 793eeda3fd6SStephen Hemminger const struct net_device_stats *stats; 79496e74088SPavel Emelyanov struct nlattr *attr; 7951da177e4SLinus Torvalds 796b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 797b60c5115SThomas Graf if (nlh == NULL) 79826932566SPatrick McHardy return -EMSGSIZE; 7991da177e4SLinus Torvalds 800b60c5115SThomas Graf ifm = nlmsg_data(nlh); 801b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 802b60c5115SThomas Graf ifm->__ifi_pad = 0; 803b60c5115SThomas Graf ifm->ifi_type = dev->type; 804b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 805b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 806b60c5115SThomas Graf ifm->ifi_change = change; 8071da177e4SLinus Torvalds 808b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 809b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 810b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 811b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 812b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 813b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 8141da177e4SLinus Torvalds 815b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 816b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 8171da177e4SLinus Torvalds 818b60c5115SThomas Graf if (dev->master) 819b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 820b60c5115SThomas Graf 821af356afaSPatrick McHardy if (dev->qdisc) 822af356afaSPatrick McHardy NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 823b00055aaSStefan Rompf 8240b815a1aSStephen Hemminger if (dev->ifalias) 8250b815a1aSStephen Hemminger NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 8260b815a1aSStephen Hemminger 827b00055aaSStefan Rompf if (1) { 8281da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 8291da177e4SLinus Torvalds .mem_start = dev->mem_start, 8301da177e4SLinus Torvalds .mem_end = dev->mem_end, 8311da177e4SLinus Torvalds .base_addr = dev->base_addr, 8321da177e4SLinus Torvalds .irq = dev->irq, 8331da177e4SLinus Torvalds .dma = dev->dma, 8341da177e4SLinus Torvalds .port = dev->if_port, 8351da177e4SLinus Torvalds }; 836b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds if (dev->addr_len) { 840b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 841b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 844b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 845b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 846b60c5115SThomas Graf if (attr == NULL) 847b60c5115SThomas Graf goto nla_put_failure; 848b60c5115SThomas Graf 849eeda3fd6SStephen Hemminger stats = dev_get_stats(dev); 850b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 8511da177e4SLinus Torvalds 85210708f37SJan Engelhardt attr = nla_reserve(skb, IFLA_STATS64, 85310708f37SJan Engelhardt sizeof(struct rtnl_link_stats64)); 85410708f37SJan Engelhardt if (attr == NULL) 85510708f37SJan Engelhardt goto nla_put_failure; 85610708f37SJan Engelhardt copy_rtnl_link_stats64(nla_data(attr), stats); 85710708f37SJan Engelhardt 85857b61080SScott Feldman if (dev->dev.parent) 85957b61080SScott Feldman NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 86057b61080SScott Feldman 861ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { 862ebc08a6fSWilliams, Mitch A int i; 863ebc08a6fSWilliams, Mitch A 864c02db8c6SChris Wright struct nlattr *vfinfo, *vf; 865c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 866c02db8c6SChris Wright 867c02db8c6SChris Wright vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 868c02db8c6SChris Wright if (!vfinfo) 869c02db8c6SChris Wright goto nla_put_failure; 870c02db8c6SChris Wright for (i = 0; i < num_vfs; i++) { 871c02db8c6SChris Wright struct ifla_vf_info ivi; 872c02db8c6SChris Wright struct ifla_vf_mac vf_mac; 873c02db8c6SChris Wright struct ifla_vf_vlan vf_vlan; 874c02db8c6SChris Wright struct ifla_vf_tx_rate vf_tx_rate; 875ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 876ebc08a6fSWilliams, Mitch A break; 877c02db8c6SChris Wright vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf; 878c02db8c6SChris Wright memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 879c02db8c6SChris Wright vf_vlan.vlan = ivi.vlan; 880c02db8c6SChris Wright vf_vlan.qos = ivi.qos; 881c02db8c6SChris Wright vf_tx_rate.rate = ivi.tx_rate; 882c02db8c6SChris Wright vf = nla_nest_start(skb, IFLA_VF_INFO); 883c02db8c6SChris Wright if (!vf) { 884c02db8c6SChris Wright nla_nest_cancel(skb, vfinfo); 885c02db8c6SChris Wright goto nla_put_failure; 886ebc08a6fSWilliams, Mitch A } 887c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); 888c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); 889c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate); 890c02db8c6SChris Wright nla_nest_end(skb, vf); 891c02db8c6SChris Wright } 892c02db8c6SChris Wright nla_nest_end(skb, vfinfo); 893ebc08a6fSWilliams, Mitch A } 89457b61080SScott Feldman 89557b61080SScott Feldman if (rtnl_port_fill(skb, dev)) 89657b61080SScott Feldman goto nla_put_failure; 89757b61080SScott Feldman 89838f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 89938f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 90038f7b870SPatrick McHardy goto nla_put_failure; 90138f7b870SPatrick McHardy } 90238f7b870SPatrick McHardy 903b60c5115SThomas Graf return nlmsg_end(skb, nlh); 904b60c5115SThomas Graf 905b60c5115SThomas Graf nla_put_failure: 90626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 90726932566SPatrick McHardy return -EMSGSIZE; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 910b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 9111da177e4SLinus Torvalds { 9123b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 9137c28bd0bSEric Dumazet int h, s_h; 9147c28bd0bSEric Dumazet int idx = 0, s_idx; 9151da177e4SLinus Torvalds struct net_device *dev; 9167c28bd0bSEric Dumazet struct hlist_head *head; 9177c28bd0bSEric Dumazet struct hlist_node *node; 9181da177e4SLinus Torvalds 9197c28bd0bSEric Dumazet s_h = cb->args[0]; 9207c28bd0bSEric Dumazet s_idx = cb->args[1]; 9217c28bd0bSEric Dumazet 9227c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 9237562f876SPavel Emelianov idx = 0; 9247c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 9257c28bd0bSEric Dumazet hlist_for_each_entry(dev, node, head, index_hlist) { 9261da177e4SLinus Torvalds if (idx < s_idx) 9277562f876SPavel Emelianov goto cont; 928575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 929b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 9307c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 9317c28bd0bSEric Dumazet NLM_F_MULTI) <= 0) 9327c28bd0bSEric Dumazet goto out; 9337562f876SPavel Emelianov cont: 9347562f876SPavel Emelianov idx++; 9351da177e4SLinus Torvalds } 9367c28bd0bSEric Dumazet } 9377c28bd0bSEric Dumazet out: 9387c28bd0bSEric Dumazet cb->args[1] = idx; 9397c28bd0bSEric Dumazet cb->args[0] = h; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds return skb->len; 9421da177e4SLinus Torvalds } 9431da177e4SLinus Torvalds 944e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 9455176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 94638f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 94738f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 9485176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 949da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 95076e87306SThomas Graf [IFLA_LINK] = { .type = NLA_U32 }, 951da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 952da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 953da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 954da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 95576e87306SThomas Graf [IFLA_LINKINFO] = { .type = NLA_NESTED }, 956d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 9570b815a1aSStephen Hemminger [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 958c02db8c6SChris Wright [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 95957b61080SScott Feldman [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 96057b61080SScott Feldman [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 961da5e0494SThomas Graf }; 962e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy); 9631da177e4SLinus Torvalds 96438f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 96538f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 96638f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 96738f7b870SPatrick McHardy }; 96838f7b870SPatrick McHardy 969c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 970c02db8c6SChris Wright [IFLA_VF_INFO] = { .type = NLA_NESTED }, 971c02db8c6SChris Wright }; 972c02db8c6SChris Wright 973c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 974c02db8c6SChris Wright [IFLA_VF_MAC] = { .type = NLA_BINARY, 975c02db8c6SChris Wright .len = sizeof(struct ifla_vf_mac) }, 976c02db8c6SChris Wright [IFLA_VF_VLAN] = { .type = NLA_BINARY, 977c02db8c6SChris Wright .len = sizeof(struct ifla_vf_vlan) }, 978c02db8c6SChris Wright [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 979c02db8c6SChris Wright .len = sizeof(struct ifla_vf_tx_rate) }, 980c02db8c6SChris Wright }; 981c02db8c6SChris Wright 98257b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 98357b61080SScott Feldman [IFLA_PORT_VF] = { .type = NLA_U32 }, 98457b61080SScott Feldman [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 98557b61080SScott Feldman .len = PORT_PROFILE_MAX }, 98657b61080SScott Feldman [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 98757b61080SScott Feldman .len = sizeof(struct ifla_port_vsi)}, 98857b61080SScott Feldman [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 98957b61080SScott Feldman .len = PORT_UUID_MAX }, 99057b61080SScott Feldman [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 99157b61080SScott Feldman .len = PORT_UUID_MAX }, 99257b61080SScott Feldman [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 99357b61080SScott Feldman [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 99457b61080SScott Feldman }; 99557b61080SScott Feldman 99681adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 99781adee47SEric W. Biederman { 99881adee47SEric W. Biederman struct net *net; 99981adee47SEric W. Biederman /* Examine the link attributes and figure out which 100081adee47SEric W. Biederman * network namespace we are talking about. 100181adee47SEric W. Biederman */ 100281adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 100381adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 100481adee47SEric W. Biederman else 100581adee47SEric W. Biederman net = get_net(src_net); 100681adee47SEric W. Biederman return net; 100781adee47SEric W. Biederman } 100881adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 100981adee47SEric W. Biederman 10101840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 10111840bb13SThomas Graf { 10121840bb13SThomas Graf if (dev) { 10131840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 10141840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 10151840bb13SThomas Graf return -EINVAL; 10161840bb13SThomas Graf 10171840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 10181840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 10191840bb13SThomas Graf return -EINVAL; 10201840bb13SThomas Graf } 10211840bb13SThomas Graf 10221840bb13SThomas Graf return 0; 10231840bb13SThomas Graf } 10241840bb13SThomas Graf 1025c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1026c02db8c6SChris Wright { 1027c02db8c6SChris Wright int rem, err = -EINVAL; 1028c02db8c6SChris Wright struct nlattr *vf; 1029c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 1030c02db8c6SChris Wright 1031c02db8c6SChris Wright nla_for_each_nested(vf, attr, rem) { 1032c02db8c6SChris Wright switch (nla_type(vf)) { 1033c02db8c6SChris Wright case IFLA_VF_MAC: { 1034c02db8c6SChris Wright struct ifla_vf_mac *ivm; 1035c02db8c6SChris Wright ivm = nla_data(vf); 1036c02db8c6SChris Wright err = -EOPNOTSUPP; 1037c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 1038c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 1039c02db8c6SChris Wright ivm->mac); 1040c02db8c6SChris Wright break; 1041c02db8c6SChris Wright } 1042c02db8c6SChris Wright case IFLA_VF_VLAN: { 1043c02db8c6SChris Wright struct ifla_vf_vlan *ivv; 1044c02db8c6SChris Wright ivv = nla_data(vf); 1045c02db8c6SChris Wright err = -EOPNOTSUPP; 1046c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 1047c02db8c6SChris Wright err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1048c02db8c6SChris Wright ivv->vlan, 1049c02db8c6SChris Wright ivv->qos); 1050c02db8c6SChris Wright break; 1051c02db8c6SChris Wright } 1052c02db8c6SChris Wright case IFLA_VF_TX_RATE: { 1053c02db8c6SChris Wright struct ifla_vf_tx_rate *ivt; 1054c02db8c6SChris Wright ivt = nla_data(vf); 1055c02db8c6SChris Wright err = -EOPNOTSUPP; 1056c02db8c6SChris Wright if (ops->ndo_set_vf_tx_rate) 1057c02db8c6SChris Wright err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, 1058c02db8c6SChris Wright ivt->rate); 1059c02db8c6SChris Wright break; 1060c02db8c6SChris Wright } 1061c02db8c6SChris Wright default: 1062c02db8c6SChris Wright err = -EINVAL; 1063c02db8c6SChris Wright break; 1064c02db8c6SChris Wright } 1065c02db8c6SChris Wright if (err) 1066c02db8c6SChris Wright break; 1067c02db8c6SChris Wright } 1068c02db8c6SChris Wright return err; 1069c02db8c6SChris Wright } 1070c02db8c6SChris Wright 10710157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 107238f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 10730157f60cSPatrick McHardy { 1074d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 107538f7b870SPatrick McHardy int send_addr_notify = 0; 10760157f60cSPatrick McHardy int err; 10770157f60cSPatrick McHardy 1078d8a5ec67SEric W. Biederman if (tb[IFLA_NET_NS_PID]) { 107981adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1080d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 1081d8a5ec67SEric W. Biederman err = PTR_ERR(net); 1082d8a5ec67SEric W. Biederman goto errout; 1083d8a5ec67SEric W. Biederman } 1084d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 1085d8a5ec67SEric W. Biederman put_net(net); 1086d8a5ec67SEric W. Biederman if (err) 1087d8a5ec67SEric W. Biederman goto errout; 1088d8a5ec67SEric W. Biederman modified = 1; 1089d8a5ec67SEric W. Biederman } 1090d8a5ec67SEric W. Biederman 10910157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 10920157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 10930157f60cSPatrick McHardy struct ifmap k_map; 10940157f60cSPatrick McHardy 1095d314774cSStephen Hemminger if (!ops->ndo_set_config) { 10960157f60cSPatrick McHardy err = -EOPNOTSUPP; 10970157f60cSPatrick McHardy goto errout; 10980157f60cSPatrick McHardy } 10990157f60cSPatrick McHardy 11000157f60cSPatrick McHardy if (!netif_device_present(dev)) { 11010157f60cSPatrick McHardy err = -ENODEV; 11020157f60cSPatrick McHardy goto errout; 11030157f60cSPatrick McHardy } 11040157f60cSPatrick McHardy 11050157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 11060157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 11070157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 11080157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 11090157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 11100157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 11110157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 11120157f60cSPatrick McHardy 1113d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 11140157f60cSPatrick McHardy if (err < 0) 11150157f60cSPatrick McHardy goto errout; 11160157f60cSPatrick McHardy 11170157f60cSPatrick McHardy modified = 1; 11180157f60cSPatrick McHardy } 11190157f60cSPatrick McHardy 11200157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 11210157f60cSPatrick McHardy struct sockaddr *sa; 11220157f60cSPatrick McHardy int len; 11230157f60cSPatrick McHardy 1124d314774cSStephen Hemminger if (!ops->ndo_set_mac_address) { 11250157f60cSPatrick McHardy err = -EOPNOTSUPP; 11260157f60cSPatrick McHardy goto errout; 11270157f60cSPatrick McHardy } 11280157f60cSPatrick McHardy 11290157f60cSPatrick McHardy if (!netif_device_present(dev)) { 11300157f60cSPatrick McHardy err = -ENODEV; 11310157f60cSPatrick McHardy goto errout; 11320157f60cSPatrick McHardy } 11330157f60cSPatrick McHardy 11340157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 11350157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 11360157f60cSPatrick McHardy if (!sa) { 11370157f60cSPatrick McHardy err = -ENOMEM; 11380157f60cSPatrick McHardy goto errout; 11390157f60cSPatrick McHardy } 11400157f60cSPatrick McHardy sa->sa_family = dev->type; 11410157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 11420157f60cSPatrick McHardy dev->addr_len); 1143d314774cSStephen Hemminger err = ops->ndo_set_mac_address(dev, sa); 11440157f60cSPatrick McHardy kfree(sa); 11450157f60cSPatrick McHardy if (err) 11460157f60cSPatrick McHardy goto errout; 11470157f60cSPatrick McHardy send_addr_notify = 1; 11480157f60cSPatrick McHardy modified = 1; 11490157f60cSPatrick McHardy } 11500157f60cSPatrick McHardy 11510157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 11520157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 11530157f60cSPatrick McHardy if (err < 0) 11540157f60cSPatrick McHardy goto errout; 11550157f60cSPatrick McHardy modified = 1; 11560157f60cSPatrick McHardy } 11570157f60cSPatrick McHardy 11580157f60cSPatrick McHardy /* 11590157f60cSPatrick McHardy * Interface selected by interface index but interface 11600157f60cSPatrick McHardy * name provided implies that a name change has been 11610157f60cSPatrick McHardy * requested. 11620157f60cSPatrick McHardy */ 11630157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 11640157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 11650157f60cSPatrick McHardy if (err < 0) 11660157f60cSPatrick McHardy goto errout; 11670157f60cSPatrick McHardy modified = 1; 11680157f60cSPatrick McHardy } 11690157f60cSPatrick McHardy 11700b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 11710b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 11720b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 11730b815a1aSStephen Hemminger if (err < 0) 11740b815a1aSStephen Hemminger goto errout; 11750b815a1aSStephen Hemminger modified = 1; 11760b815a1aSStephen Hemminger } 11770b815a1aSStephen Hemminger 11780157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 11790157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 11800157f60cSPatrick McHardy send_addr_notify = 1; 11810157f60cSPatrick McHardy } 11820157f60cSPatrick McHardy 11830157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 11843729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 11855f9021cfSJohannes Berg if (err < 0) 11865f9021cfSJohannes Berg goto errout; 11870157f60cSPatrick McHardy } 11880157f60cSPatrick McHardy 118993b2d4a2SDavid S. Miller if (tb[IFLA_TXQLEN]) 11900157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 11910157f60cSPatrick McHardy 11920157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 119393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 11940157f60cSPatrick McHardy 11950157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 11960157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 11970157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 119893b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 11990157f60cSPatrick McHardy } 12000157f60cSPatrick McHardy 1201c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 1202c02db8c6SChris Wright struct nlattr *attr; 1203c02db8c6SChris Wright int rem; 1204c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1205253683bbSDavid Howells if (nla_type(attr) != IFLA_VF_INFO) { 1206253683bbSDavid Howells err = -EINVAL; 1207c02db8c6SChris Wright goto errout; 1208253683bbSDavid Howells } 1209c02db8c6SChris Wright err = do_setvfinfo(dev, attr); 1210ebc08a6fSWilliams, Mitch A if (err < 0) 1211ebc08a6fSWilliams, Mitch A goto errout; 1212ebc08a6fSWilliams, Mitch A modified = 1; 1213ebc08a6fSWilliams, Mitch A } 1214ebc08a6fSWilliams, Mitch A } 12150157f60cSPatrick McHardy err = 0; 12160157f60cSPatrick McHardy 121757b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 121857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 121957b61080SScott Feldman struct nlattr *attr; 122057b61080SScott Feldman int vf; 122157b61080SScott Feldman int rem; 122257b61080SScott Feldman 122357b61080SScott Feldman err = -EOPNOTSUPP; 122457b61080SScott Feldman if (!ops->ndo_set_vf_port) 122557b61080SScott Feldman goto errout; 122657b61080SScott Feldman 122757b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 122857b61080SScott Feldman if (nla_type(attr) != IFLA_VF_PORT) 122957b61080SScott Feldman continue; 123057b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 123157b61080SScott Feldman attr, ifla_port_policy); 123257b61080SScott Feldman if (err < 0) 123357b61080SScott Feldman goto errout; 123457b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 123557b61080SScott Feldman err = -EOPNOTSUPP; 123657b61080SScott Feldman goto errout; 123757b61080SScott Feldman } 123857b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 123957b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 124057b61080SScott Feldman if (err < 0) 124157b61080SScott Feldman goto errout; 124257b61080SScott Feldman modified = 1; 124357b61080SScott Feldman } 124457b61080SScott Feldman } 124557b61080SScott Feldman err = 0; 124657b61080SScott Feldman 124757b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 124857b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 124957b61080SScott Feldman 125057b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 125157b61080SScott Feldman tb[IFLA_PORT_SELF], ifla_port_policy); 125257b61080SScott Feldman if (err < 0) 125357b61080SScott Feldman goto errout; 125457b61080SScott Feldman 125557b61080SScott Feldman err = -EOPNOTSUPP; 125657b61080SScott Feldman if (ops->ndo_set_vf_port) 125757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 125857b61080SScott Feldman if (err < 0) 125957b61080SScott Feldman goto errout; 126057b61080SScott Feldman modified = 1; 126157b61080SScott Feldman } 126257b61080SScott Feldman err = 0; 126357b61080SScott Feldman 12640157f60cSPatrick McHardy errout: 12650157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 12660157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 12670157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 12680157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 12690157f60cSPatrick McHardy "please check.\n", dev->name); 12700157f60cSPatrick McHardy 12710157f60cSPatrick McHardy if (send_addr_notify) 12720157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 12730157f60cSPatrick McHardy return err; 12740157f60cSPatrick McHardy } 12750157f60cSPatrick McHardy 1276da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1277da5e0494SThomas Graf { 12783b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1279da5e0494SThomas Graf struct ifinfomsg *ifm; 1280da5e0494SThomas Graf struct net_device *dev; 12810157f60cSPatrick McHardy int err; 1282da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 12831da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 12841da177e4SLinus Torvalds 1285da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1286da5e0494SThomas Graf if (err < 0) 1287da5e0494SThomas Graf goto errout; 12881da177e4SLinus Torvalds 12895176f91eSThomas Graf if (tb[IFLA_IFNAME]) 12905176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 129178e5b891SPatrick McHardy else 129278e5b891SPatrick McHardy ifname[0] = '\0'; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds err = -EINVAL; 1295da5e0494SThomas Graf ifm = nlmsg_data(nlh); 129651055be8SPatrick McHardy if (ifm->ifi_index > 0) 1297a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1298da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1299a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1300da5e0494SThomas Graf else 1301da5e0494SThomas Graf goto errout; 13021da177e4SLinus Torvalds 1303da5e0494SThomas Graf if (dev == NULL) { 1304da5e0494SThomas Graf err = -ENODEV; 1305da5e0494SThomas Graf goto errout; 1306da5e0494SThomas Graf } 13071da177e4SLinus Torvalds 1308e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1309e0d087afSEric Dumazet if (err < 0) 1310a3d12891SEric Dumazet goto errout; 1311da5e0494SThomas Graf 131238f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 1313da5e0494SThomas Graf errout: 13141da177e4SLinus Torvalds return err; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 131738f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 131838f7b870SPatrick McHardy { 13193b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 132038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 132138f7b870SPatrick McHardy struct net_device *dev; 132238f7b870SPatrick McHardy struct ifinfomsg *ifm; 132338f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 132438f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 132538f7b870SPatrick McHardy int err; 132638f7b870SPatrick McHardy 132738f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 132838f7b870SPatrick McHardy if (err < 0) 132938f7b870SPatrick McHardy return err; 133038f7b870SPatrick McHardy 133138f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 133238f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 133338f7b870SPatrick McHardy 133438f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 133538f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1336881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 133738f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1338881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 133938f7b870SPatrick McHardy else 134038f7b870SPatrick McHardy return -EINVAL; 134138f7b870SPatrick McHardy 134238f7b870SPatrick McHardy if (!dev) 134338f7b870SPatrick McHardy return -ENODEV; 134438f7b870SPatrick McHardy 134538f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 134638f7b870SPatrick McHardy if (!ops) 134738f7b870SPatrick McHardy return -EOPNOTSUPP; 134838f7b870SPatrick McHardy 134923289a37SEric Dumazet ops->dellink(dev, NULL); 135038f7b870SPatrick McHardy return 0; 135138f7b870SPatrick McHardy } 135238f7b870SPatrick McHardy 13533729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 13543729d502SPatrick McHardy { 13553729d502SPatrick McHardy unsigned int old_flags; 13563729d502SPatrick McHardy int err; 13573729d502SPatrick McHardy 13583729d502SPatrick McHardy old_flags = dev->flags; 13593729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 13603729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 13613729d502SPatrick McHardy if (err < 0) 13623729d502SPatrick McHardy return err; 13633729d502SPatrick McHardy } 13643729d502SPatrick McHardy 13653729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 13663729d502SPatrick McHardy rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 13673729d502SPatrick McHardy 13683729d502SPatrick McHardy __dev_notify_flags(dev, old_flags); 13693729d502SPatrick McHardy return 0; 13703729d502SPatrick McHardy } 13713729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 13723729d502SPatrick McHardy 137381adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 137481adee47SEric W. Biederman char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1375e7199288SPavel Emelianov { 1376e7199288SPavel Emelianov int err; 1377e7199288SPavel Emelianov struct net_device *dev; 13782e59af3dSEric Dumazet unsigned int num_queues = 1; 13792e59af3dSEric Dumazet unsigned int real_num_queues = 1; 1380e7199288SPavel Emelianov 13812e59af3dSEric Dumazet if (ops->get_tx_queues) { 138281adee47SEric W. Biederman err = ops->get_tx_queues(src_net, tb, &num_queues, 1383e0d087afSEric Dumazet &real_num_queues); 13842e59af3dSEric Dumazet if (err) 13852e59af3dSEric Dumazet goto err; 13862e59af3dSEric Dumazet } 1387e7199288SPavel Emelianov err = -ENOMEM; 13882e59af3dSEric Dumazet dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1389e7199288SPavel Emelianov if (!dev) 1390e7199288SPavel Emelianov goto err; 1391e7199288SPavel Emelianov 139281adee47SEric W. Biederman dev_net_set(dev, net); 139381adee47SEric W. Biederman dev->rtnl_link_ops = ops; 13943729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 13952e59af3dSEric Dumazet dev->real_num_tx_queues = real_num_queues; 139681adee47SEric W. Biederman 1397e7199288SPavel Emelianov if (strchr(dev->name, '%')) { 1398e7199288SPavel Emelianov err = dev_alloc_name(dev, dev->name); 1399e7199288SPavel Emelianov if (err < 0) 1400e7199288SPavel Emelianov goto err_free; 1401e7199288SPavel Emelianov } 1402e7199288SPavel Emelianov 1403e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1404e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1405e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1406e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1407e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1408e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1409e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1410e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1411e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1412e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1413e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 141493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1415e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1416e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1417e7199288SPavel Emelianov 1418e7199288SPavel Emelianov return dev; 1419e7199288SPavel Emelianov 1420e7199288SPavel Emelianov err_free: 1421e7199288SPavel Emelianov free_netdev(dev); 1422e7199288SPavel Emelianov err: 1423e7199288SPavel Emelianov return ERR_PTR(err); 1424e7199288SPavel Emelianov } 1425e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1426e7199288SPavel Emelianov 142738f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 142838f7b870SPatrick McHardy { 14293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 143038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 143138f7b870SPatrick McHardy struct net_device *dev; 143238f7b870SPatrick McHardy struct ifinfomsg *ifm; 143338f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 143438f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 143538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 143638f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 143738f7b870SPatrick McHardy int err; 143838f7b870SPatrick McHardy 143995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 144038f7b870SPatrick McHardy replay: 14418072f085SThomas Graf #endif 144238f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 144338f7b870SPatrick McHardy if (err < 0) 144438f7b870SPatrick McHardy return err; 144538f7b870SPatrick McHardy 144638f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 144738f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 144838f7b870SPatrick McHardy else 144938f7b870SPatrick McHardy ifname[0] = '\0'; 145038f7b870SPatrick McHardy 145138f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 145238f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1453881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 145438f7b870SPatrick McHardy else if (ifname[0]) 1455881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 145638f7b870SPatrick McHardy else 145738f7b870SPatrick McHardy dev = NULL; 145838f7b870SPatrick McHardy 1459e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1460e0d087afSEric Dumazet if (err < 0) 14611840bb13SThomas Graf return err; 14621840bb13SThomas Graf 146338f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 146438f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 146538f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 146638f7b870SPatrick McHardy if (err < 0) 146738f7b870SPatrick McHardy return err; 146838f7b870SPatrick McHardy } else 146938f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 147038f7b870SPatrick McHardy 147138f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 147238f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 147338f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 147438f7b870SPatrick McHardy } else { 147538f7b870SPatrick McHardy kind[0] = '\0'; 147638f7b870SPatrick McHardy ops = NULL; 147738f7b870SPatrick McHardy } 147838f7b870SPatrick McHardy 147938f7b870SPatrick McHardy if (1) { 148038f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 148181adee47SEric W. Biederman struct net *dest_net; 148238f7b870SPatrick McHardy 148338f7b870SPatrick McHardy if (ops) { 148438f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 148538f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 148638f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 148738f7b870SPatrick McHardy ops->policy); 148838f7b870SPatrick McHardy if (err < 0) 148938f7b870SPatrick McHardy return err; 149038f7b870SPatrick McHardy data = attr; 149138f7b870SPatrick McHardy } 149238f7b870SPatrick McHardy if (ops->validate) { 149338f7b870SPatrick McHardy err = ops->validate(tb, data); 149438f7b870SPatrick McHardy if (err < 0) 149538f7b870SPatrick McHardy return err; 149638f7b870SPatrick McHardy } 149738f7b870SPatrick McHardy } 149838f7b870SPatrick McHardy 149938f7b870SPatrick McHardy if (dev) { 150038f7b870SPatrick McHardy int modified = 0; 150138f7b870SPatrick McHardy 150238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 150338f7b870SPatrick McHardy return -EEXIST; 150438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 150538f7b870SPatrick McHardy return -EOPNOTSUPP; 150638f7b870SPatrick McHardy 150738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 150838f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 150938f7b870SPatrick McHardy !ops->changelink) 151038f7b870SPatrick McHardy return -EOPNOTSUPP; 151138f7b870SPatrick McHardy 151238f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 151338f7b870SPatrick McHardy if (err < 0) 151438f7b870SPatrick McHardy return err; 151538f7b870SPatrick McHardy modified = 1; 151638f7b870SPatrick McHardy } 151738f7b870SPatrick McHardy 151838f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 151938f7b870SPatrick McHardy } 152038f7b870SPatrick McHardy 152138f7b870SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) 152238f7b870SPatrick McHardy return -ENODEV; 152338f7b870SPatrick McHardy 15243729d502SPatrick McHardy if (ifm->ifi_index) 152538f7b870SPatrick McHardy return -EOPNOTSUPP; 15260e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 152738f7b870SPatrick McHardy return -EOPNOTSUPP; 152838f7b870SPatrick McHardy 152938f7b870SPatrick McHardy if (!ops) { 153095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 153138f7b870SPatrick McHardy if (kind[0]) { 153238f7b870SPatrick McHardy __rtnl_unlock(); 153338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 153438f7b870SPatrick McHardy rtnl_lock(); 153538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 153638f7b870SPatrick McHardy if (ops) 153738f7b870SPatrick McHardy goto replay; 153838f7b870SPatrick McHardy } 153938f7b870SPatrick McHardy #endif 154038f7b870SPatrick McHardy return -EOPNOTSUPP; 154138f7b870SPatrick McHardy } 154238f7b870SPatrick McHardy 154338f7b870SPatrick McHardy if (!ifname[0]) 154438f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 154538f7b870SPatrick McHardy 154681adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 154781adee47SEric W. Biederman dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 154838f7b870SPatrick McHardy 1549e7199288SPavel Emelianov if (IS_ERR(dev)) 1550e7199288SPavel Emelianov err = PTR_ERR(dev); 1551e7199288SPavel Emelianov else if (ops->newlink) 155281adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 15532d85cba2SPatrick McHardy else 15542d85cba2SPatrick McHardy err = register_netdevice(dev); 155580032cffSDan Carpenter 155680032cffSDan Carpenter if (err < 0 && !IS_ERR(dev)) 155738f7b870SPatrick McHardy free_netdev(dev); 155880032cffSDan Carpenter if (err < 0) 15593729d502SPatrick McHardy goto out; 156081adee47SEric W. Biederman 15613729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 15623729d502SPatrick McHardy if (err < 0) 15633729d502SPatrick McHardy unregister_netdevice(dev); 15643729d502SPatrick McHardy out: 156581adee47SEric W. Biederman put_net(dest_net); 156638f7b870SPatrick McHardy return err; 156738f7b870SPatrick McHardy } 156838f7b870SPatrick McHardy } 156938f7b870SPatrick McHardy 1570b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1571711e2c33SJean Tourrilhes { 15723b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1573b60c5115SThomas Graf struct ifinfomsg *ifm; 1574a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 1575b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1576b60c5115SThomas Graf struct net_device *dev = NULL; 1577b60c5115SThomas Graf struct sk_buff *nskb; 1578339bf98fSThomas Graf int err; 1579711e2c33SJean Tourrilhes 1580b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1581b60c5115SThomas Graf if (err < 0) 15829918f230SEric Sesterhenn return err; 1583b60c5115SThomas Graf 1584a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 1585a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1586a3d12891SEric Dumazet 1587b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1588a3d12891SEric Dumazet if (ifm->ifi_index > 0) 1589a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1590a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 1591a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1592a3d12891SEric Dumazet else 1593b60c5115SThomas Graf return -EINVAL; 1594b60c5115SThomas Graf 1595a3d12891SEric Dumazet if (dev == NULL) 1596a3d12891SEric Dumazet return -ENODEV; 1597a3d12891SEric Dumazet 159838f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1599a3d12891SEric Dumazet if (nskb == NULL) 1600a3d12891SEric Dumazet return -ENOBUFS; 1601711e2c33SJean Tourrilhes 1602575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1603575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 160426932566SPatrick McHardy if (err < 0) { 160526932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 160626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 160726932566SPatrick McHardy kfree_skb(nskb); 1608a3d12891SEric Dumazet } else 160997c53cacSDenis V. Lunev err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1610b60c5115SThomas Graf 1611711e2c33SJean Tourrilhes return err; 1612711e2c33SJean Tourrilhes } 1613711e2c33SJean Tourrilhes 161442bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds int idx; 16171da177e4SLinus Torvalds int s_idx = cb->family; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds if (s_idx == 0) 16201da177e4SLinus Torvalds s_idx = 1; 162125239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 16221da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 16231da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 16241da177e4SLinus Torvalds continue; 1625e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1626e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 16271da177e4SLinus Torvalds continue; 16281da177e4SLinus Torvalds if (idx > s_idx) 16291da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1630e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 16311da177e4SLinus Torvalds break; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds cb->family = idx; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds return skb->len; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 16391da177e4SLinus Torvalds { 1640c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 16411da177e4SLinus Torvalds struct sk_buff *skb; 16420ec6d3f4SThomas Graf int err = -ENOBUFS; 16431da177e4SLinus Torvalds 164438f7b870SPatrick McHardy skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 16450ec6d3f4SThomas Graf if (skb == NULL) 16460ec6d3f4SThomas Graf goto errout; 16471da177e4SLinus Torvalds 1648575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 164926932566SPatrick McHardy if (err < 0) { 165026932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 165126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 165226932566SPatrick McHardy kfree_skb(skb); 165326932566SPatrick McHardy goto errout; 165426932566SPatrick McHardy } 16551ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 16561ce85fe4SPablo Neira Ayuso return; 16570ec6d3f4SThomas Graf errout: 16580ec6d3f4SThomas Graf if (err < 0) 16594b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 16631da177e4SLinus Torvalds static struct rtattr **rta_buf; 16641da177e4SLinus Torvalds static int rtattr_max; 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* Process one rtnetlink message. */ 16671da177e4SLinus Torvalds 16681d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 16691da177e4SLinus Torvalds { 16703b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1671e2849863SThomas Graf rtnl_doit_func doit; 16721da177e4SLinus Torvalds int sz_idx, kind; 16731da177e4SLinus Torvalds int min_len; 16741da177e4SLinus Torvalds int family; 16751da177e4SLinus Torvalds int type; 16761c2d670fSPatrick McHardy int err; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds type = nlh->nlmsg_type; 16791da177e4SLinus Torvalds if (type > RTM_MAX) 1680038890feSThomas Graf return -EOPNOTSUPP; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds type -= RTM_BASE; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 16851da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 16861da177e4SLinus Torvalds return 0; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 16891da177e4SLinus Torvalds sz_idx = type>>2; 16901da177e4SLinus Torvalds kind = type&3; 16911da177e4SLinus Torvalds 16921d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 16931d00a4ebSThomas Graf return -EPERM; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 169697c53cacSDenis V. Lunev struct sock *rtnl; 1697e2849863SThomas Graf rtnl_dumpit_func dumpit; 16981da177e4SLinus Torvalds 1699e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1700e2849863SThomas Graf if (dumpit == NULL) 1701038890feSThomas Graf return -EOPNOTSUPP; 17021da177e4SLinus Torvalds 17031c2d670fSPatrick McHardy __rtnl_unlock(); 170497c53cacSDenis V. Lunev rtnl = net->rtnl; 17051c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 17061c2d670fSPatrick McHardy rtnl_lock(); 17071c2d670fSPatrick McHardy return err; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 17131da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 17141d00a4ebSThomas Graf return -EINVAL; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 17171da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 17181da177e4SLinus Torvalds struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 17211da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 17221da177e4SLinus Torvalds if (flavor) { 17231da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 17241d00a4ebSThomas Graf return -EINVAL; 17251da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 1731e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1732e2849863SThomas Graf if (doit == NULL) 1733038890feSThomas Graf return -EOPNOTSUPP; 17341da177e4SLinus Torvalds 17351d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 1738cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 17391da177e4SLinus Torvalds { 17401536cc0dSDenis V. Lunev rtnl_lock(); 1741cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 17421536cc0dSDenis V. Lunev rtnl_unlock(); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds struct net_device *dev = ptr; 1748e9dc8653SEric W. Biederman 17491da177e4SLinus Torvalds switch (event) { 17501da177e4SLinus Torvalds case NETDEV_UP: 17511da177e4SLinus Torvalds case NETDEV_DOWN: 175210de05afSPatrick McHardy case NETDEV_PRE_UP: 1753d90a909eSEric W. Biederman case NETDEV_POST_INIT: 1754d90a909eSEric W. Biederman case NETDEV_REGISTER: 17551da177e4SLinus Torvalds case NETDEV_CHANGE: 1756755d0e77SPatrick McHardy case NETDEV_PRE_TYPE_CHANGE: 17571da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 1758a2835763SPatrick McHardy case NETDEV_UNREGISTER: 1759d90a909eSEric W. Biederman case NETDEV_UNREGISTER_BATCH: 17601da177e4SLinus Torvalds break; 17611da177e4SLinus Torvalds default: 17621da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 17631da177e4SLinus Torvalds break; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds return NOTIFY_DONE; 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 17691da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 17701da177e4SLinus Torvalds }; 17711da177e4SLinus Torvalds 177297c53cacSDenis V. Lunev 17732c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 177497c53cacSDenis V. Lunev { 177597c53cacSDenis V. Lunev struct sock *sk; 177697c53cacSDenis V. Lunev sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 177797c53cacSDenis V. Lunev rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 177897c53cacSDenis V. Lunev if (!sk) 177997c53cacSDenis V. Lunev return -ENOMEM; 178097c53cacSDenis V. Lunev net->rtnl = sk; 178197c53cacSDenis V. Lunev return 0; 178297c53cacSDenis V. Lunev } 178397c53cacSDenis V. Lunev 17842c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 178597c53cacSDenis V. Lunev { 1786b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 178797c53cacSDenis V. Lunev net->rtnl = NULL; 178897c53cacSDenis V. Lunev } 178997c53cacSDenis V. Lunev 179097c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 179197c53cacSDenis V. Lunev .init = rtnetlink_net_init, 179297c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 179397c53cacSDenis V. Lunev }; 179497c53cacSDenis V. Lunev 17951da177e4SLinus Torvalds void __init rtnetlink_init(void) 17961da177e4SLinus Torvalds { 17971da177e4SLinus Torvalds int i; 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds rtattr_max = 0; 18001da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 18011da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 18021da177e4SLinus Torvalds rtattr_max = rta_max[i]; 18031da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 18041da177e4SLinus Torvalds if (!rta_buf) 18051da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 18061da177e4SLinus Torvalds 180797c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 18081da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 180997c53cacSDenis V. Lunev 18101da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 18111da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 1812340d17fcSThomas Graf 1813340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 1814340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 181538f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); 181638f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); 1817687ad8ccSThomas Graf 1818687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 1819687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 1822