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 30238f7b870SPatrick McHardy /** 30338f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 30438f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 30538f7b870SPatrick McHardy * 3062d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 30738f7b870SPatrick McHardy */ 30838f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 30938f7b870SPatrick McHardy { 310881d966bSEric W. Biederman struct net *net; 3112d85cba2SPatrick McHardy 312881d966bSEric W. Biederman for_each_net(net) { 313669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 314881d966bSEric W. Biederman } 31538f7b870SPatrick McHardy list_del(&ops->list); 31638f7b870SPatrick McHardy } 31738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 31838f7b870SPatrick McHardy 31938f7b870SPatrick McHardy /** 32038f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 32138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 32238f7b870SPatrick McHardy */ 32338f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 32438f7b870SPatrick McHardy { 32538f7b870SPatrick McHardy rtnl_lock(); 32638f7b870SPatrick McHardy __rtnl_link_unregister(ops); 32738f7b870SPatrick McHardy rtnl_unlock(); 32838f7b870SPatrick McHardy } 32938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 33038f7b870SPatrick McHardy 33138f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 33238f7b870SPatrick McHardy { 33338f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 33438f7b870SPatrick McHardy 33538f7b870SPatrick McHardy list_for_each_entry(ops, &link_ops, list) { 33638f7b870SPatrick McHardy if (!strcmp(ops->kind, kind)) 33738f7b870SPatrick McHardy return ops; 33838f7b870SPatrick McHardy } 33938f7b870SPatrick McHardy return NULL; 34038f7b870SPatrick McHardy } 34138f7b870SPatrick McHardy 34238f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 34338f7b870SPatrick McHardy { 34438f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 34538f7b870SPatrick McHardy size_t size; 34638f7b870SPatrick McHardy 34738f7b870SPatrick McHardy if (!ops) 34838f7b870SPatrick McHardy return 0; 34938f7b870SPatrick McHardy 350369cf77aSThomas Graf size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 351369cf77aSThomas Graf nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 35238f7b870SPatrick McHardy 35338f7b870SPatrick McHardy if (ops->get_size) 35438f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 355369cf77aSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 35638f7b870SPatrick McHardy ops->get_size(dev); 35738f7b870SPatrick McHardy 35838f7b870SPatrick McHardy if (ops->get_xstats_size) 359369cf77aSThomas Graf /* IFLA_INFO_XSTATS */ 360369cf77aSThomas Graf size += nla_total_size(ops->get_xstats_size(dev)); 36138f7b870SPatrick McHardy 36238f7b870SPatrick McHardy return size; 36338f7b870SPatrick McHardy } 36438f7b870SPatrick McHardy 365f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 366f8ff182cSThomas Graf 367f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 368f8ff182cSThomas Graf { 369f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 370f8ff182cSThomas Graf 371f8ff182cSThomas Graf list_for_each_entry(ops, &rtnl_af_ops, list) { 372f8ff182cSThomas Graf if (ops->family == family) 373f8ff182cSThomas Graf return ops; 374f8ff182cSThomas Graf } 375f8ff182cSThomas Graf 376f8ff182cSThomas Graf return NULL; 377f8ff182cSThomas Graf } 378f8ff182cSThomas Graf 379f8ff182cSThomas Graf /** 380f8ff182cSThomas Graf * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. 381f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 382f8ff182cSThomas Graf * 383f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 384f8ff182cSThomas Graf * 385f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 386f8ff182cSThomas Graf */ 387f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops) 388f8ff182cSThomas Graf { 389f8ff182cSThomas Graf list_add_tail(&ops->list, &rtnl_af_ops); 390f8ff182cSThomas Graf return 0; 391f8ff182cSThomas Graf } 392f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register); 393f8ff182cSThomas Graf 394f8ff182cSThomas Graf /** 395f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 396f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 397f8ff182cSThomas Graf * 398f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 399f8ff182cSThomas Graf */ 400f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops) 401f8ff182cSThomas Graf { 402f8ff182cSThomas Graf int err; 403f8ff182cSThomas Graf 404f8ff182cSThomas Graf rtnl_lock(); 405f8ff182cSThomas Graf err = __rtnl_af_register(ops); 406f8ff182cSThomas Graf rtnl_unlock(); 407f8ff182cSThomas Graf return err; 408f8ff182cSThomas Graf } 409f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 410f8ff182cSThomas Graf 411f8ff182cSThomas Graf /** 412f8ff182cSThomas Graf * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 413f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 414f8ff182cSThomas Graf * 415f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 416f8ff182cSThomas Graf */ 417f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops) 418f8ff182cSThomas Graf { 419f8ff182cSThomas Graf list_del(&ops->list); 420f8ff182cSThomas Graf } 421f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 422f8ff182cSThomas Graf 423f8ff182cSThomas Graf /** 424f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 425f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 426f8ff182cSThomas Graf */ 427f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 428f8ff182cSThomas Graf { 429f8ff182cSThomas Graf rtnl_lock(); 430f8ff182cSThomas Graf __rtnl_af_unregister(ops); 431f8ff182cSThomas Graf rtnl_unlock(); 432f8ff182cSThomas Graf } 433f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 434f8ff182cSThomas Graf 435f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev) 436f8ff182cSThomas Graf { 437f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 438f8ff182cSThomas Graf size_t size; 439f8ff182cSThomas Graf 440f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 441f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 442f8ff182cSThomas Graf 443f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 444f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 445f8ff182cSThomas Graf /* AF_* + nested data */ 446f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 447f8ff182cSThomas Graf af_ops->get_link_af_size(dev); 448f8ff182cSThomas Graf } 449f8ff182cSThomas Graf } 450f8ff182cSThomas Graf 451f8ff182cSThomas Graf return size; 452f8ff182cSThomas Graf } 453f8ff182cSThomas Graf 45438f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 45538f7b870SPatrick McHardy { 45638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 45738f7b870SPatrick McHardy struct nlattr *linkinfo, *data; 45838f7b870SPatrick McHardy int err = -EMSGSIZE; 45938f7b870SPatrick McHardy 46038f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 46138f7b870SPatrick McHardy if (linkinfo == NULL) 46238f7b870SPatrick McHardy goto out; 46338f7b870SPatrick McHardy 46438f7b870SPatrick McHardy if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 46538f7b870SPatrick McHardy goto err_cancel_link; 46638f7b870SPatrick McHardy if (ops->fill_xstats) { 46738f7b870SPatrick McHardy err = ops->fill_xstats(skb, dev); 46838f7b870SPatrick McHardy if (err < 0) 46938f7b870SPatrick McHardy goto err_cancel_link; 47038f7b870SPatrick McHardy } 47138f7b870SPatrick McHardy if (ops->fill_info) { 47238f7b870SPatrick McHardy data = nla_nest_start(skb, IFLA_INFO_DATA); 47338f7b870SPatrick McHardy if (data == NULL) 47438f7b870SPatrick McHardy goto err_cancel_link; 47538f7b870SPatrick McHardy err = ops->fill_info(skb, dev); 47638f7b870SPatrick McHardy if (err < 0) 47738f7b870SPatrick McHardy goto err_cancel_data; 47838f7b870SPatrick McHardy nla_nest_end(skb, data); 47938f7b870SPatrick McHardy } 48038f7b870SPatrick McHardy 48138f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 48238f7b870SPatrick McHardy return 0; 48338f7b870SPatrick McHardy 48438f7b870SPatrick McHardy err_cancel_data: 48538f7b870SPatrick McHardy nla_nest_cancel(skb, data); 48638f7b870SPatrick McHardy err_cancel_link: 48738f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 48838f7b870SPatrick McHardy out: 48938f7b870SPatrick McHardy return err; 49038f7b870SPatrick McHardy } 49138f7b870SPatrick McHardy 492db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 4931da177e4SLinus Torvalds { 494f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 495f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 496f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 49714c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 498f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 499f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 500f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 501f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 502f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 503f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 5041da177e4SLinus Torvalds }; 5051da177e4SLinus Torvalds 506db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 5071da177e4SLinus Torvalds { 508f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 509f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 510f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 51114c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 512f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 513f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 514f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 515f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 5161da177e4SLinus Torvalds }; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 5191da177e4SLinus Torvalds { 5201da177e4SLinus Torvalds struct rtattr *rta; 5211da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); 5241da177e4SLinus Torvalds rta->rta_type = attrtype; 5251da177e4SLinus Torvalds rta->rta_len = size; 5261da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 527b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 5281da177e4SLinus Torvalds } 529e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill); 5301da177e4SLinus Torvalds 53197c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) 5321da177e4SLinus Torvalds { 53397c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 5341da177e4SLinus Torvalds int err = 0; 5351da177e4SLinus Torvalds 536ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 5371da177e4SLinus Torvalds if (echo) 5381da177e4SLinus Torvalds atomic_inc(&skb->users); 5391da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 5401da177e4SLinus Torvalds if (echo) 5411da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 5421da177e4SLinus Torvalds return err; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 54597c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 5462942e900SThomas Graf { 54797c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 54897c53cacSDenis V. Lunev 5492942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 5502942e900SThomas Graf } 551e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 5522942e900SThomas Graf 5531ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 55497676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 55597676b6bSThomas Graf { 55697c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 55797676b6bSThomas Graf int report = 0; 55897676b6bSThomas Graf 55997676b6bSThomas Graf if (nlh) 56097676b6bSThomas Graf report = nlmsg_report(nlh); 56197676b6bSThomas Graf 5621ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 56397676b6bSThomas Graf } 564e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 56597676b6bSThomas Graf 56697c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 56797676b6bSThomas Graf { 56897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 56997c53cacSDenis V. Lunev 57097676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 57197676b6bSThomas Graf } 572e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 57397676b6bSThomas Graf 5741da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 5751da177e4SLinus Torvalds { 5762d7202bfSThomas Graf struct nlattr *mx; 5772d7202bfSThomas Graf int i, valid = 0; 5781da177e4SLinus Torvalds 5792d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 5802d7202bfSThomas Graf if (mx == NULL) 5812d7202bfSThomas Graf return -ENOBUFS; 5822d7202bfSThomas Graf 5831da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 5842d7202bfSThomas Graf if (metrics[i]) { 5852d7202bfSThomas Graf valid++; 5862d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 5871da177e4SLinus Torvalds } 5882d7202bfSThomas Graf } 5891da177e4SLinus Torvalds 590a57d27fcSDavid S. Miller if (!valid) { 591a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 592a57d27fcSDavid S. Miller return 0; 593a57d27fcSDavid S. Miller } 5942d7202bfSThomas Graf 5952d7202bfSThomas Graf return nla_nest_end(skb, mx); 5962d7202bfSThomas Graf 5972d7202bfSThomas Graf nla_put_failure: 598bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 599bc3ed28cSThomas Graf return -EMSGSIZE; 6001da177e4SLinus Torvalds } 601e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 6021da177e4SLinus Torvalds 603e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 604e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 605e3703b3dSThomas Graf { 606e3703b3dSThomas Graf struct rta_cacheinfo ci = { 607e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 608e3703b3dSThomas Graf .rta_used = dst->__use, 609e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 610e3703b3dSThomas Graf .rta_error = error, 611e3703b3dSThomas Graf .rta_id = id, 612e3703b3dSThomas Graf .rta_ts = ts, 613e3703b3dSThomas Graf .rta_tsage = tsage, 614e3703b3dSThomas Graf }; 615e3703b3dSThomas Graf 616e3703b3dSThomas Graf if (expires) 617e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 618e3703b3dSThomas Graf 619e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 620e3703b3dSThomas Graf } 621e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 6221da177e4SLinus Torvalds 62393b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 624b00055aaSStefan Rompf { 625b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 626b00055aaSStefan Rompf 627b00055aaSStefan Rompf switch (transition) { 628b00055aaSStefan Rompf case IF_OPER_UP: 629b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 630b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 631b00055aaSStefan Rompf !netif_dormant(dev)) 632b00055aaSStefan Rompf operstate = IF_OPER_UP; 633b00055aaSStefan Rompf break; 634b00055aaSStefan Rompf 635b00055aaSStefan Rompf case IF_OPER_DORMANT: 636b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 637b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 638b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 639b00055aaSStefan Rompf break; 6403ff50b79SStephen Hemminger } 641b00055aaSStefan Rompf 642b00055aaSStefan Rompf if (dev->operstate != operstate) { 643b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 644b00055aaSStefan Rompf dev->operstate = operstate; 645b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 646b00055aaSStefan Rompf netdev_state_change(dev); 64793b2d4a2SDavid S. Miller } 648b00055aaSStefan Rompf } 649b00055aaSStefan Rompf 6503729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 6513729d502SPatrick McHardy const struct ifinfomsg *ifm) 6523729d502SPatrick McHardy { 6533729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 6543729d502SPatrick McHardy 6553729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 6563729d502SPatrick McHardy if (ifm->ifi_change) 6573729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 6583729d502SPatrick McHardy (dev->flags & ~ifm->ifi_change); 6593729d502SPatrick McHardy 6603729d502SPatrick McHardy return flags; 6613729d502SPatrick McHardy } 6623729d502SPatrick McHardy 663b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 664be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *b) 6651da177e4SLinus Torvalds { 666b60c5115SThomas Graf a->rx_packets = b->rx_packets; 667b60c5115SThomas Graf a->tx_packets = b->tx_packets; 668b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 669b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 670b60c5115SThomas Graf a->rx_errors = b->rx_errors; 671b60c5115SThomas Graf a->tx_errors = b->tx_errors; 672b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 673b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 674b60c5115SThomas Graf 675b60c5115SThomas Graf a->multicast = b->multicast; 676b60c5115SThomas Graf a->collisions = b->collisions; 677b60c5115SThomas Graf 678b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 679b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 680b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 681b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 682b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 683b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 684b60c5115SThomas Graf 685b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 686b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 687b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 688b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 689b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 690b60c5115SThomas Graf 691b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 692b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 69310708f37SJan Engelhardt } 69410708f37SJan Engelhardt 695be1f3c2cSBen Hutchings static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) 69610708f37SJan Engelhardt { 697afdcba37SEric Dumazet memcpy(v, b, sizeof(*b)); 69810708f37SJan Engelhardt } 699b60c5115SThomas Graf 700c02db8c6SChris Wright /* All VF info */ 701ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev) 702ebc08a6fSWilliams, Mitch A { 703c02db8c6SChris Wright if (dev->dev.parent && dev_is_pci(dev->dev.parent)) { 704c02db8c6SChris Wright 705c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 706045de01aSScott Feldman size_t size = nla_total_size(sizeof(struct nlattr)); 707045de01aSScott Feldman size += nla_total_size(num_vfs * sizeof(struct nlattr)); 708045de01aSScott Feldman size += num_vfs * 709045de01aSScott Feldman (nla_total_size(sizeof(struct ifla_vf_mac)) + 710045de01aSScott Feldman nla_total_size(sizeof(struct ifla_vf_vlan)) + 711045de01aSScott Feldman nla_total_size(sizeof(struct ifla_vf_tx_rate))); 712c02db8c6SChris Wright return size; 713c02db8c6SChris Wright } else 714ebc08a6fSWilliams, Mitch A return 0; 715ebc08a6fSWilliams, Mitch A } 716ebc08a6fSWilliams, Mitch A 71757b61080SScott Feldman static size_t rtnl_port_size(const struct net_device *dev) 71857b61080SScott Feldman { 71957b61080SScott Feldman size_t port_size = nla_total_size(4) /* PORT_VF */ 72057b61080SScott Feldman + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 72157b61080SScott Feldman + nla_total_size(sizeof(struct ifla_port_vsi)) 72257b61080SScott Feldman /* PORT_VSI_TYPE */ 72357b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 72457b61080SScott Feldman + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 72557b61080SScott Feldman + nla_total_size(1) /* PROT_VDP_REQUEST */ 72657b61080SScott Feldman + nla_total_size(2); /* PORT_VDP_RESPONSE */ 72757b61080SScott Feldman size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 72857b61080SScott Feldman size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 72957b61080SScott Feldman + port_size; 73057b61080SScott Feldman size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 73157b61080SScott Feldman + port_size; 73257b61080SScott Feldman 73357b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 73457b61080SScott Feldman return 0; 73557b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) 73657b61080SScott Feldman return port_self_size + vf_ports_size + 73757b61080SScott Feldman vf_port_size * dev_num_vf(dev->dev.parent); 73857b61080SScott Feldman else 73957b61080SScott Feldman return port_self_size; 74057b61080SScott Feldman } 74157b61080SScott Feldman 7429e34a5b5SEric Dumazet static noinline size_t if_nlmsg_size(const struct net_device *dev) 743339bf98fSThomas Graf { 744339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 745339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 7460b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 747339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 748339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 749339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 750adcfe196SJan Engelhardt + nla_total_size(sizeof(struct rtnl_link_stats64)) 751339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 752339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 753339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 754339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 755339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 756339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 757339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 758339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 75938f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 760ebc08a6fSWilliams, Mitch A + nla_total_size(4) /* IFLA_NUM_VF */ 761c02db8c6SChris Wright + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */ 76257b61080SScott Feldman + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 763f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 764f8ff182cSThomas Graf + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */ 765339bf98fSThomas Graf } 766339bf98fSThomas Graf 76757b61080SScott Feldman static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 76857b61080SScott Feldman { 76957b61080SScott Feldman struct nlattr *vf_ports; 77057b61080SScott Feldman struct nlattr *vf_port; 77157b61080SScott Feldman int vf; 77257b61080SScott Feldman int err; 77357b61080SScott Feldman 77457b61080SScott Feldman vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 77557b61080SScott Feldman if (!vf_ports) 77657b61080SScott Feldman return -EMSGSIZE; 77757b61080SScott Feldman 77857b61080SScott Feldman for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 77957b61080SScott Feldman vf_port = nla_nest_start(skb, IFLA_VF_PORT); 7808ca94183SScott Feldman if (!vf_port) 7818ca94183SScott Feldman goto nla_put_failure; 78257b61080SScott Feldman NLA_PUT_U32(skb, IFLA_PORT_VF, vf); 78357b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 7848ca94183SScott Feldman if (err == -EMSGSIZE) 7858ca94183SScott Feldman goto nla_put_failure; 78657b61080SScott Feldman if (err) { 78757b61080SScott Feldman nla_nest_cancel(skb, vf_port); 78857b61080SScott Feldman continue; 78957b61080SScott Feldman } 79057b61080SScott Feldman nla_nest_end(skb, vf_port); 79157b61080SScott Feldman } 79257b61080SScott Feldman 79357b61080SScott Feldman nla_nest_end(skb, vf_ports); 79457b61080SScott Feldman 79557b61080SScott Feldman return 0; 7968ca94183SScott Feldman 7978ca94183SScott Feldman nla_put_failure: 7988ca94183SScott Feldman nla_nest_cancel(skb, vf_ports); 7998ca94183SScott Feldman return -EMSGSIZE; 80057b61080SScott Feldman } 80157b61080SScott Feldman 80257b61080SScott Feldman static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 80357b61080SScott Feldman { 80457b61080SScott Feldman struct nlattr *port_self; 80557b61080SScott Feldman int err; 80657b61080SScott Feldman 80757b61080SScott Feldman port_self = nla_nest_start(skb, IFLA_PORT_SELF); 80857b61080SScott Feldman if (!port_self) 80957b61080SScott Feldman return -EMSGSIZE; 81057b61080SScott Feldman 81157b61080SScott Feldman err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 81257b61080SScott Feldman if (err) { 81357b61080SScott Feldman nla_nest_cancel(skb, port_self); 8148ca94183SScott Feldman return (err == -EMSGSIZE) ? err : 0; 81557b61080SScott Feldman } 81657b61080SScott Feldman 81757b61080SScott Feldman nla_nest_end(skb, port_self); 81857b61080SScott Feldman 81957b61080SScott Feldman return 0; 82057b61080SScott Feldman } 82157b61080SScott Feldman 82257b61080SScott Feldman static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev) 82357b61080SScott Feldman { 82457b61080SScott Feldman int err; 82557b61080SScott Feldman 82657b61080SScott Feldman if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 82757b61080SScott Feldman return 0; 82857b61080SScott Feldman 82957b61080SScott Feldman err = rtnl_port_self_fill(skb, dev); 83057b61080SScott Feldman if (err) 83157b61080SScott Feldman return err; 83257b61080SScott Feldman 83357b61080SScott Feldman if (dev_num_vf(dev->dev.parent)) { 83457b61080SScott Feldman err = rtnl_vf_ports_fill(skb, dev); 83557b61080SScott Feldman if (err) 83657b61080SScott Feldman return err; 83757b61080SScott Feldman } 83857b61080SScott Feldman 83957b61080SScott Feldman return 0; 84057b61080SScott Feldman } 84157b61080SScott Feldman 842b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 843575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 844575c3e2aSPatrick McHardy unsigned int flags) 845b60c5115SThomas Graf { 846b60c5115SThomas Graf struct ifinfomsg *ifm; 8471da177e4SLinus Torvalds struct nlmsghdr *nlh; 84828172739SEric Dumazet struct rtnl_link_stats64 temp; 849be1f3c2cSBen Hutchings const struct rtnl_link_stats64 *stats; 850f8ff182cSThomas Graf struct nlattr *attr, *af_spec; 851f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 8521da177e4SLinus Torvalds 853b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 854b60c5115SThomas Graf if (nlh == NULL) 85526932566SPatrick McHardy return -EMSGSIZE; 8561da177e4SLinus Torvalds 857b60c5115SThomas Graf ifm = nlmsg_data(nlh); 858b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 859b60c5115SThomas Graf ifm->__ifi_pad = 0; 860b60c5115SThomas Graf ifm->ifi_type = dev->type; 861b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 862b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 863b60c5115SThomas Graf ifm->ifi_change = change; 8641da177e4SLinus Torvalds 865b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 866b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 867b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 868b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 869b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 870b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 871cbda10faSVlad Dogaru NLA_PUT_U32(skb, IFLA_GROUP, dev->group); 8721da177e4SLinus Torvalds 873b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 874b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 8751da177e4SLinus Torvalds 876b60c5115SThomas Graf if (dev->master) 877b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 878b60c5115SThomas Graf 879af356afaSPatrick McHardy if (dev->qdisc) 880af356afaSPatrick McHardy NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 881b00055aaSStefan Rompf 8820b815a1aSStephen Hemminger if (dev->ifalias) 8830b815a1aSStephen Hemminger NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 8840b815a1aSStephen Hemminger 885b00055aaSStefan Rompf if (1) { 8861da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 8871da177e4SLinus Torvalds .mem_start = dev->mem_start, 8881da177e4SLinus Torvalds .mem_end = dev->mem_end, 8891da177e4SLinus Torvalds .base_addr = dev->base_addr, 8901da177e4SLinus Torvalds .irq = dev->irq, 8911da177e4SLinus Torvalds .dma = dev->dma, 8921da177e4SLinus Torvalds .port = dev->if_port, 8931da177e4SLinus Torvalds }; 894b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds if (dev->addr_len) { 898b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 899b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 902b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 903b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 904b60c5115SThomas Graf if (attr == NULL) 905b60c5115SThomas Graf goto nla_put_failure; 906b60c5115SThomas Graf 90728172739SEric Dumazet stats = dev_get_stats(dev, &temp); 908b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 9091da177e4SLinus Torvalds 91010708f37SJan Engelhardt attr = nla_reserve(skb, IFLA_STATS64, 91110708f37SJan Engelhardt sizeof(struct rtnl_link_stats64)); 91210708f37SJan Engelhardt if (attr == NULL) 91310708f37SJan Engelhardt goto nla_put_failure; 91410708f37SJan Engelhardt copy_rtnl_link_stats64(nla_data(attr), stats); 91510708f37SJan Engelhardt 91657b61080SScott Feldman if (dev->dev.parent) 91757b61080SScott Feldman NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 91857b61080SScott Feldman 919ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { 920ebc08a6fSWilliams, Mitch A int i; 921ebc08a6fSWilliams, Mitch A 922c02db8c6SChris Wright struct nlattr *vfinfo, *vf; 923c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 924c02db8c6SChris Wright 925c02db8c6SChris Wright vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 926c02db8c6SChris Wright if (!vfinfo) 927c02db8c6SChris Wright goto nla_put_failure; 928c02db8c6SChris Wright for (i = 0; i < num_vfs; i++) { 929c02db8c6SChris Wright struct ifla_vf_info ivi; 930c02db8c6SChris Wright struct ifla_vf_mac vf_mac; 931c02db8c6SChris Wright struct ifla_vf_vlan vf_vlan; 932c02db8c6SChris Wright struct ifla_vf_tx_rate vf_tx_rate; 933ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 934ebc08a6fSWilliams, Mitch A break; 935c02db8c6SChris Wright vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf; 936c02db8c6SChris Wright memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 937c02db8c6SChris Wright vf_vlan.vlan = ivi.vlan; 938c02db8c6SChris Wright vf_vlan.qos = ivi.qos; 939c02db8c6SChris Wright vf_tx_rate.rate = ivi.tx_rate; 940c02db8c6SChris Wright vf = nla_nest_start(skb, IFLA_VF_INFO); 941c02db8c6SChris Wright if (!vf) { 942c02db8c6SChris Wright nla_nest_cancel(skb, vfinfo); 943c02db8c6SChris Wright goto nla_put_failure; 944ebc08a6fSWilliams, Mitch A } 945c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); 946c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); 947c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate); 948c02db8c6SChris Wright nla_nest_end(skb, vf); 949c02db8c6SChris Wright } 950c02db8c6SChris Wright nla_nest_end(skb, vfinfo); 951ebc08a6fSWilliams, Mitch A } 95257b61080SScott Feldman 95357b61080SScott Feldman if (rtnl_port_fill(skb, dev)) 95457b61080SScott Feldman goto nla_put_failure; 95557b61080SScott Feldman 95638f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 95738f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 95838f7b870SPatrick McHardy goto nla_put_failure; 95938f7b870SPatrick McHardy } 96038f7b870SPatrick McHardy 961f8ff182cSThomas Graf if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 962f8ff182cSThomas Graf goto nla_put_failure; 963f8ff182cSThomas Graf 964f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 965f8ff182cSThomas Graf if (af_ops->fill_link_af) { 966f8ff182cSThomas Graf struct nlattr *af; 967f8ff182cSThomas Graf int err; 968f8ff182cSThomas Graf 969f8ff182cSThomas Graf if (!(af = nla_nest_start(skb, af_ops->family))) 970f8ff182cSThomas Graf goto nla_put_failure; 971f8ff182cSThomas Graf 972f8ff182cSThomas Graf err = af_ops->fill_link_af(skb, dev); 973f8ff182cSThomas Graf 974f8ff182cSThomas Graf /* 975f8ff182cSThomas Graf * Caller may return ENODATA to indicate that there 976f8ff182cSThomas Graf * was no data to be dumped. This is not an error, it 977f8ff182cSThomas Graf * means we should trim the attribute header and 978f8ff182cSThomas Graf * continue. 979f8ff182cSThomas Graf */ 980f8ff182cSThomas Graf if (err == -ENODATA) 981f8ff182cSThomas Graf nla_nest_cancel(skb, af); 982f8ff182cSThomas Graf else if (err < 0) 983f8ff182cSThomas Graf goto nla_put_failure; 984f8ff182cSThomas Graf 985f8ff182cSThomas Graf nla_nest_end(skb, af); 986f8ff182cSThomas Graf } 987f8ff182cSThomas Graf } 988f8ff182cSThomas Graf 989f8ff182cSThomas Graf nla_nest_end(skb, af_spec); 990f8ff182cSThomas Graf 991b60c5115SThomas Graf return nlmsg_end(skb, nlh); 992b60c5115SThomas Graf 993b60c5115SThomas Graf nla_put_failure: 99426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 99526932566SPatrick McHardy return -EMSGSIZE; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 998b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 9991da177e4SLinus Torvalds { 10003b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 10017c28bd0bSEric Dumazet int h, s_h; 10027c28bd0bSEric Dumazet int idx = 0, s_idx; 10031da177e4SLinus Torvalds struct net_device *dev; 10047c28bd0bSEric Dumazet struct hlist_head *head; 10057c28bd0bSEric Dumazet struct hlist_node *node; 10061da177e4SLinus Torvalds 10077c28bd0bSEric Dumazet s_h = cb->args[0]; 10087c28bd0bSEric Dumazet s_idx = cb->args[1]; 10097c28bd0bSEric Dumazet 10107c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 10117562f876SPavel Emelianov idx = 0; 10127c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 10137c28bd0bSEric Dumazet hlist_for_each_entry(dev, node, head, index_hlist) { 10141da177e4SLinus Torvalds if (idx < s_idx) 10157562f876SPavel Emelianov goto cont; 1016575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1017b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 10187c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 10197c28bd0bSEric Dumazet NLM_F_MULTI) <= 0) 10207c28bd0bSEric Dumazet goto out; 10217562f876SPavel Emelianov cont: 10227562f876SPavel Emelianov idx++; 10231da177e4SLinus Torvalds } 10247c28bd0bSEric Dumazet } 10257c28bd0bSEric Dumazet out: 10267c28bd0bSEric Dumazet cb->args[1] = idx; 10277c28bd0bSEric Dumazet cb->args[0] = h; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds return skb->len; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 1032e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 10335176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 103438f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 103538f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 10365176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1037da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 103876e87306SThomas Graf [IFLA_LINK] = { .type = NLA_U32 }, 1039da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 1040da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 1041da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1042da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 104376e87306SThomas Graf [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1044d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 10450b815a1aSStephen Hemminger [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1046c02db8c6SChris Wright [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 104757b61080SScott Feldman [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 104857b61080SScott Feldman [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1049f8ff182cSThomas Graf [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1050da5e0494SThomas Graf }; 1051e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy); 10521da177e4SLinus Torvalds 105338f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 105438f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 105538f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 105638f7b870SPatrick McHardy }; 105738f7b870SPatrick McHardy 1058c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 1059c02db8c6SChris Wright [IFLA_VF_INFO] = { .type = NLA_NESTED }, 1060c02db8c6SChris Wright }; 1061c02db8c6SChris Wright 1062c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1063c02db8c6SChris Wright [IFLA_VF_MAC] = { .type = NLA_BINARY, 1064c02db8c6SChris Wright .len = sizeof(struct ifla_vf_mac) }, 1065c02db8c6SChris Wright [IFLA_VF_VLAN] = { .type = NLA_BINARY, 1066c02db8c6SChris Wright .len = sizeof(struct ifla_vf_vlan) }, 1067c02db8c6SChris Wright [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 1068c02db8c6SChris Wright .len = sizeof(struct ifla_vf_tx_rate) }, 1069c02db8c6SChris Wright }; 1070c02db8c6SChris Wright 107157b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 107257b61080SScott Feldman [IFLA_PORT_VF] = { .type = NLA_U32 }, 107357b61080SScott Feldman [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 107457b61080SScott Feldman .len = PORT_PROFILE_MAX }, 107557b61080SScott Feldman [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 107657b61080SScott Feldman .len = sizeof(struct ifla_port_vsi)}, 107757b61080SScott Feldman [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 107857b61080SScott Feldman .len = PORT_UUID_MAX }, 107957b61080SScott Feldman [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 108057b61080SScott Feldman .len = PORT_UUID_MAX }, 108157b61080SScott Feldman [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 108257b61080SScott Feldman [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 108357b61080SScott Feldman }; 108457b61080SScott Feldman 108581adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 108681adee47SEric W. Biederman { 108781adee47SEric W. Biederman struct net *net; 108881adee47SEric W. Biederman /* Examine the link attributes and figure out which 108981adee47SEric W. Biederman * network namespace we are talking about. 109081adee47SEric W. Biederman */ 109181adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 109281adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 109381adee47SEric W. Biederman else 109481adee47SEric W. Biederman net = get_net(src_net); 109581adee47SEric W. Biederman return net; 109681adee47SEric W. Biederman } 109781adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 109881adee47SEric W. Biederman 10991840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 11001840bb13SThomas Graf { 11011840bb13SThomas Graf if (dev) { 11021840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 11031840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 11041840bb13SThomas Graf return -EINVAL; 11051840bb13SThomas Graf 11061840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 11071840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 11081840bb13SThomas Graf return -EINVAL; 11091840bb13SThomas Graf } 11101840bb13SThomas Graf 1111cf7afbfeSThomas Graf if (tb[IFLA_AF_SPEC]) { 1112cf7afbfeSThomas Graf struct nlattr *af; 1113cf7afbfeSThomas Graf int rem, err; 1114cf7afbfeSThomas Graf 1115cf7afbfeSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1116cf7afbfeSThomas Graf const struct rtnl_af_ops *af_ops; 1117cf7afbfeSThomas Graf 1118cf7afbfeSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1119cf7afbfeSThomas Graf return -EAFNOSUPPORT; 1120cf7afbfeSThomas Graf 1121cf7afbfeSThomas Graf if (!af_ops->set_link_af) 1122cf7afbfeSThomas Graf return -EOPNOTSUPP; 1123cf7afbfeSThomas Graf 1124cf7afbfeSThomas Graf if (af_ops->validate_link_af) { 1125cf7afbfeSThomas Graf err = af_ops->validate_link_af(dev, 1126cf7afbfeSThomas Graf tb[IFLA_AF_SPEC]); 1127cf7afbfeSThomas Graf if (err < 0) 1128cf7afbfeSThomas Graf return err; 1129cf7afbfeSThomas Graf } 1130cf7afbfeSThomas Graf } 1131cf7afbfeSThomas Graf } 1132cf7afbfeSThomas Graf 11331840bb13SThomas Graf return 0; 11341840bb13SThomas Graf } 11351840bb13SThomas Graf 1136c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1137c02db8c6SChris Wright { 1138c02db8c6SChris Wright int rem, err = -EINVAL; 1139c02db8c6SChris Wright struct nlattr *vf; 1140c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 1141c02db8c6SChris Wright 1142c02db8c6SChris Wright nla_for_each_nested(vf, attr, rem) { 1143c02db8c6SChris Wright switch (nla_type(vf)) { 1144c02db8c6SChris Wright case IFLA_VF_MAC: { 1145c02db8c6SChris Wright struct ifla_vf_mac *ivm; 1146c02db8c6SChris Wright ivm = nla_data(vf); 1147c02db8c6SChris Wright err = -EOPNOTSUPP; 1148c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 1149c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 1150c02db8c6SChris Wright ivm->mac); 1151c02db8c6SChris Wright break; 1152c02db8c6SChris Wright } 1153c02db8c6SChris Wright case IFLA_VF_VLAN: { 1154c02db8c6SChris Wright struct ifla_vf_vlan *ivv; 1155c02db8c6SChris Wright ivv = nla_data(vf); 1156c02db8c6SChris Wright err = -EOPNOTSUPP; 1157c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 1158c02db8c6SChris Wright err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1159c02db8c6SChris Wright ivv->vlan, 1160c02db8c6SChris Wright ivv->qos); 1161c02db8c6SChris Wright break; 1162c02db8c6SChris Wright } 1163c02db8c6SChris Wright case IFLA_VF_TX_RATE: { 1164c02db8c6SChris Wright struct ifla_vf_tx_rate *ivt; 1165c02db8c6SChris Wright ivt = nla_data(vf); 1166c02db8c6SChris Wright err = -EOPNOTSUPP; 1167c02db8c6SChris Wright if (ops->ndo_set_vf_tx_rate) 1168c02db8c6SChris Wright err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, 1169c02db8c6SChris Wright ivt->rate); 1170c02db8c6SChris Wright break; 1171c02db8c6SChris Wright } 1172c02db8c6SChris Wright default: 1173c02db8c6SChris Wright err = -EINVAL; 1174c02db8c6SChris Wright break; 1175c02db8c6SChris Wright } 1176c02db8c6SChris Wright if (err) 1177c02db8c6SChris Wright break; 1178c02db8c6SChris Wright } 1179c02db8c6SChris Wright return err; 1180c02db8c6SChris Wright } 1181c02db8c6SChris Wright 11820157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 118338f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 11840157f60cSPatrick McHardy { 1185d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 118638f7b870SPatrick McHardy int send_addr_notify = 0; 11870157f60cSPatrick McHardy int err; 11880157f60cSPatrick McHardy 1189d8a5ec67SEric W. Biederman if (tb[IFLA_NET_NS_PID]) { 119081adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1191d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 1192d8a5ec67SEric W. Biederman err = PTR_ERR(net); 1193d8a5ec67SEric W. Biederman goto errout; 1194d8a5ec67SEric W. Biederman } 1195d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 1196d8a5ec67SEric W. Biederman put_net(net); 1197d8a5ec67SEric W. Biederman if (err) 1198d8a5ec67SEric W. Biederman goto errout; 1199d8a5ec67SEric W. Biederman modified = 1; 1200d8a5ec67SEric W. Biederman } 1201d8a5ec67SEric W. Biederman 12020157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 12030157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 12040157f60cSPatrick McHardy struct ifmap k_map; 12050157f60cSPatrick McHardy 1206d314774cSStephen Hemminger if (!ops->ndo_set_config) { 12070157f60cSPatrick McHardy err = -EOPNOTSUPP; 12080157f60cSPatrick McHardy goto errout; 12090157f60cSPatrick McHardy } 12100157f60cSPatrick McHardy 12110157f60cSPatrick McHardy if (!netif_device_present(dev)) { 12120157f60cSPatrick McHardy err = -ENODEV; 12130157f60cSPatrick McHardy goto errout; 12140157f60cSPatrick McHardy } 12150157f60cSPatrick McHardy 12160157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 12170157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 12180157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 12190157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 12200157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 12210157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 12220157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 12230157f60cSPatrick McHardy 1224d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 12250157f60cSPatrick McHardy if (err < 0) 12260157f60cSPatrick McHardy goto errout; 12270157f60cSPatrick McHardy 12280157f60cSPatrick McHardy modified = 1; 12290157f60cSPatrick McHardy } 12300157f60cSPatrick McHardy 12310157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 12320157f60cSPatrick McHardy struct sockaddr *sa; 12330157f60cSPatrick McHardy int len; 12340157f60cSPatrick McHardy 1235d314774cSStephen Hemminger if (!ops->ndo_set_mac_address) { 12360157f60cSPatrick McHardy err = -EOPNOTSUPP; 12370157f60cSPatrick McHardy goto errout; 12380157f60cSPatrick McHardy } 12390157f60cSPatrick McHardy 12400157f60cSPatrick McHardy if (!netif_device_present(dev)) { 12410157f60cSPatrick McHardy err = -ENODEV; 12420157f60cSPatrick McHardy goto errout; 12430157f60cSPatrick McHardy } 12440157f60cSPatrick McHardy 12450157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 12460157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 12470157f60cSPatrick McHardy if (!sa) { 12480157f60cSPatrick McHardy err = -ENOMEM; 12490157f60cSPatrick McHardy goto errout; 12500157f60cSPatrick McHardy } 12510157f60cSPatrick McHardy sa->sa_family = dev->type; 12520157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 12530157f60cSPatrick McHardy dev->addr_len); 1254d314774cSStephen Hemminger err = ops->ndo_set_mac_address(dev, sa); 12550157f60cSPatrick McHardy kfree(sa); 12560157f60cSPatrick McHardy if (err) 12570157f60cSPatrick McHardy goto errout; 12580157f60cSPatrick McHardy send_addr_notify = 1; 12590157f60cSPatrick McHardy modified = 1; 12600157f60cSPatrick McHardy } 12610157f60cSPatrick McHardy 12620157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 12630157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 12640157f60cSPatrick McHardy if (err < 0) 12650157f60cSPatrick McHardy goto errout; 12660157f60cSPatrick McHardy modified = 1; 12670157f60cSPatrick McHardy } 12680157f60cSPatrick McHardy 1269cbda10faSVlad Dogaru if (tb[IFLA_GROUP]) { 1270cbda10faSVlad Dogaru dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1271cbda10faSVlad Dogaru modified = 1; 1272cbda10faSVlad Dogaru } 1273cbda10faSVlad Dogaru 12740157f60cSPatrick McHardy /* 12750157f60cSPatrick McHardy * Interface selected by interface index but interface 12760157f60cSPatrick McHardy * name provided implies that a name change has been 12770157f60cSPatrick McHardy * requested. 12780157f60cSPatrick McHardy */ 12790157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 12800157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 12810157f60cSPatrick McHardy if (err < 0) 12820157f60cSPatrick McHardy goto errout; 12830157f60cSPatrick McHardy modified = 1; 12840157f60cSPatrick McHardy } 12850157f60cSPatrick McHardy 12860b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 12870b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 12880b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 12890b815a1aSStephen Hemminger if (err < 0) 12900b815a1aSStephen Hemminger goto errout; 12910b815a1aSStephen Hemminger modified = 1; 12920b815a1aSStephen Hemminger } 12930b815a1aSStephen Hemminger 12940157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 12950157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 12960157f60cSPatrick McHardy send_addr_notify = 1; 12970157f60cSPatrick McHardy } 12980157f60cSPatrick McHardy 12990157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 13003729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 13015f9021cfSJohannes Berg if (err < 0) 13025f9021cfSJohannes Berg goto errout; 13030157f60cSPatrick McHardy } 13040157f60cSPatrick McHardy 130593b2d4a2SDavid S. Miller if (tb[IFLA_TXQLEN]) 13060157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 13070157f60cSPatrick McHardy 13080157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 130993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 13100157f60cSPatrick McHardy 13110157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 13120157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 13130157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 131493b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 13150157f60cSPatrick McHardy } 13160157f60cSPatrick McHardy 1317c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 1318c02db8c6SChris Wright struct nlattr *attr; 1319c02db8c6SChris Wright int rem; 1320c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1321253683bbSDavid Howells if (nla_type(attr) != IFLA_VF_INFO) { 1322253683bbSDavid Howells err = -EINVAL; 1323c02db8c6SChris Wright goto errout; 1324253683bbSDavid Howells } 1325c02db8c6SChris Wright err = do_setvfinfo(dev, attr); 1326ebc08a6fSWilliams, Mitch A if (err < 0) 1327ebc08a6fSWilliams, Mitch A goto errout; 1328ebc08a6fSWilliams, Mitch A modified = 1; 1329ebc08a6fSWilliams, Mitch A } 1330ebc08a6fSWilliams, Mitch A } 13310157f60cSPatrick McHardy err = 0; 13320157f60cSPatrick McHardy 133357b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 133457b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 133557b61080SScott Feldman struct nlattr *attr; 133657b61080SScott Feldman int vf; 133757b61080SScott Feldman int rem; 133857b61080SScott Feldman 133957b61080SScott Feldman err = -EOPNOTSUPP; 134057b61080SScott Feldman if (!ops->ndo_set_vf_port) 134157b61080SScott Feldman goto errout; 134257b61080SScott Feldman 134357b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 134457b61080SScott Feldman if (nla_type(attr) != IFLA_VF_PORT) 134557b61080SScott Feldman continue; 134657b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 134757b61080SScott Feldman attr, ifla_port_policy); 134857b61080SScott Feldman if (err < 0) 134957b61080SScott Feldman goto errout; 135057b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 135157b61080SScott Feldman err = -EOPNOTSUPP; 135257b61080SScott Feldman goto errout; 135357b61080SScott Feldman } 135457b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 135557b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 135657b61080SScott Feldman if (err < 0) 135757b61080SScott Feldman goto errout; 135857b61080SScott Feldman modified = 1; 135957b61080SScott Feldman } 136057b61080SScott Feldman } 136157b61080SScott Feldman err = 0; 136257b61080SScott Feldman 136357b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 136457b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 136557b61080SScott Feldman 136657b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 136757b61080SScott Feldman tb[IFLA_PORT_SELF], ifla_port_policy); 136857b61080SScott Feldman if (err < 0) 136957b61080SScott Feldman goto errout; 137057b61080SScott Feldman 137157b61080SScott Feldman err = -EOPNOTSUPP; 137257b61080SScott Feldman if (ops->ndo_set_vf_port) 137357b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 137457b61080SScott Feldman if (err < 0) 137557b61080SScott Feldman goto errout; 137657b61080SScott Feldman modified = 1; 137757b61080SScott Feldman } 1378f8ff182cSThomas Graf 1379f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 1380f8ff182cSThomas Graf struct nlattr *af; 1381f8ff182cSThomas Graf int rem; 1382f8ff182cSThomas Graf 1383f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1384f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 1385f8ff182cSThomas Graf 1386f8ff182cSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1387cf7afbfeSThomas Graf BUG(); 1388f8ff182cSThomas Graf 1389cf7afbfeSThomas Graf err = af_ops->set_link_af(dev, af); 1390f8ff182cSThomas Graf if (err < 0) 1391f8ff182cSThomas Graf goto errout; 1392f8ff182cSThomas Graf 1393f8ff182cSThomas Graf modified = 1; 1394f8ff182cSThomas Graf } 1395f8ff182cSThomas Graf } 139657b61080SScott Feldman err = 0; 139757b61080SScott Feldman 13980157f60cSPatrick McHardy errout: 13990157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 14000157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 14010157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 14020157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 14030157f60cSPatrick McHardy "please check.\n", dev->name); 14040157f60cSPatrick McHardy 14050157f60cSPatrick McHardy if (send_addr_notify) 14060157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 14070157f60cSPatrick McHardy return err; 14080157f60cSPatrick McHardy } 14090157f60cSPatrick McHardy 1410da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1411da5e0494SThomas Graf { 14123b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1413da5e0494SThomas Graf struct ifinfomsg *ifm; 1414da5e0494SThomas Graf struct net_device *dev; 14150157f60cSPatrick McHardy int err; 1416da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 14171da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 14181da177e4SLinus Torvalds 1419da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1420da5e0494SThomas Graf if (err < 0) 1421da5e0494SThomas Graf goto errout; 14221da177e4SLinus Torvalds 14235176f91eSThomas Graf if (tb[IFLA_IFNAME]) 14245176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 142578e5b891SPatrick McHardy else 142678e5b891SPatrick McHardy ifname[0] = '\0'; 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds err = -EINVAL; 1429da5e0494SThomas Graf ifm = nlmsg_data(nlh); 143051055be8SPatrick McHardy if (ifm->ifi_index > 0) 1431a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1432da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1433a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1434da5e0494SThomas Graf else 1435da5e0494SThomas Graf goto errout; 14361da177e4SLinus Torvalds 1437da5e0494SThomas Graf if (dev == NULL) { 1438da5e0494SThomas Graf err = -ENODEV; 1439da5e0494SThomas Graf goto errout; 1440da5e0494SThomas Graf } 14411da177e4SLinus Torvalds 1442e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1443e0d087afSEric Dumazet if (err < 0) 1444a3d12891SEric Dumazet goto errout; 1445da5e0494SThomas Graf 144638f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 1447da5e0494SThomas Graf errout: 14481da177e4SLinus Torvalds return err; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 145138f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 145238f7b870SPatrick McHardy { 14533b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 145438f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 145538f7b870SPatrick McHardy struct net_device *dev; 145638f7b870SPatrick McHardy struct ifinfomsg *ifm; 145738f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 145838f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 145938f7b870SPatrick McHardy int err; 146038f7b870SPatrick McHardy 146138f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 146238f7b870SPatrick McHardy if (err < 0) 146338f7b870SPatrick McHardy return err; 146438f7b870SPatrick McHardy 146538f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 146638f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 146738f7b870SPatrick McHardy 146838f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 146938f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1470881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 147138f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1472881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 147338f7b870SPatrick McHardy else 147438f7b870SPatrick McHardy return -EINVAL; 147538f7b870SPatrick McHardy 147638f7b870SPatrick McHardy if (!dev) 147738f7b870SPatrick McHardy return -ENODEV; 147838f7b870SPatrick McHardy 147938f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 148038f7b870SPatrick McHardy if (!ops) 148138f7b870SPatrick McHardy return -EOPNOTSUPP; 148238f7b870SPatrick McHardy 148323289a37SEric Dumazet ops->dellink(dev, NULL); 148438f7b870SPatrick McHardy return 0; 148538f7b870SPatrick McHardy } 148638f7b870SPatrick McHardy 14873729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 14883729d502SPatrick McHardy { 14893729d502SPatrick McHardy unsigned int old_flags; 14903729d502SPatrick McHardy int err; 14913729d502SPatrick McHardy 14923729d502SPatrick McHardy old_flags = dev->flags; 14933729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 14943729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 14953729d502SPatrick McHardy if (err < 0) 14963729d502SPatrick McHardy return err; 14973729d502SPatrick McHardy } 14983729d502SPatrick McHardy 14993729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 15003729d502SPatrick McHardy rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 15013729d502SPatrick McHardy 15023729d502SPatrick McHardy __dev_notify_flags(dev, old_flags); 15033729d502SPatrick McHardy return 0; 15043729d502SPatrick McHardy } 15053729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 15063729d502SPatrick McHardy 150781adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 150881adee47SEric W. Biederman char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1509e7199288SPavel Emelianov { 1510e7199288SPavel Emelianov int err; 1511e7199288SPavel Emelianov struct net_device *dev; 15122e59af3dSEric Dumazet unsigned int num_queues = 1; 15132e59af3dSEric Dumazet unsigned int real_num_queues = 1; 1514e7199288SPavel Emelianov 15152e59af3dSEric Dumazet if (ops->get_tx_queues) { 151681adee47SEric W. Biederman err = ops->get_tx_queues(src_net, tb, &num_queues, 1517e0d087afSEric Dumazet &real_num_queues); 15182e59af3dSEric Dumazet if (err) 15192e59af3dSEric Dumazet goto err; 15202e59af3dSEric Dumazet } 1521e7199288SPavel Emelianov err = -ENOMEM; 15222e59af3dSEric Dumazet dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1523e7199288SPavel Emelianov if (!dev) 1524e7199288SPavel Emelianov goto err; 1525e7199288SPavel Emelianov 152681adee47SEric W. Biederman dev_net_set(dev, net); 152781adee47SEric W. Biederman dev->rtnl_link_ops = ops; 15283729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 15292e59af3dSEric Dumazet dev->real_num_tx_queues = real_num_queues; 153081adee47SEric W. Biederman 1531e7199288SPavel Emelianov if (strchr(dev->name, '%')) { 1532e7199288SPavel Emelianov err = dev_alloc_name(dev, dev->name); 1533e7199288SPavel Emelianov if (err < 0) 1534e7199288SPavel Emelianov goto err_free; 1535e7199288SPavel Emelianov } 1536e7199288SPavel Emelianov 1537e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1538e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1539e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1540e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1541e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1542e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1543e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1544e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1545e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1546e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1547e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 154893b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1549e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1550e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1551*ffa934f1SPatrick McHardy if (tb[IFLA_GROUP]) 1552*ffa934f1SPatrick McHardy dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1553e7199288SPavel Emelianov 1554e7199288SPavel Emelianov return dev; 1555e7199288SPavel Emelianov 1556e7199288SPavel Emelianov err_free: 1557e7199288SPavel Emelianov free_netdev(dev); 1558e7199288SPavel Emelianov err: 1559e7199288SPavel Emelianov return ERR_PTR(err); 1560e7199288SPavel Emelianov } 1561e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1562e7199288SPavel Emelianov 1563e7ed828fSVlad Dogaru static int rtnl_group_changelink(struct net *net, int group, 1564e7ed828fSVlad Dogaru struct ifinfomsg *ifm, 1565e7ed828fSVlad Dogaru struct nlattr **tb) 1566e7ed828fSVlad Dogaru { 1567e7ed828fSVlad Dogaru struct net_device *dev; 1568e7ed828fSVlad Dogaru int err; 1569e7ed828fSVlad Dogaru 1570e7ed828fSVlad Dogaru for_each_netdev(net, dev) { 1571e7ed828fSVlad Dogaru if (dev->group == group) { 1572e7ed828fSVlad Dogaru err = do_setlink(dev, ifm, tb, NULL, 0); 1573e7ed828fSVlad Dogaru if (err < 0) 1574e7ed828fSVlad Dogaru return err; 1575e7ed828fSVlad Dogaru } 1576e7ed828fSVlad Dogaru } 1577e7ed828fSVlad Dogaru 1578e7ed828fSVlad Dogaru return 0; 1579e7ed828fSVlad Dogaru } 1580e7ed828fSVlad Dogaru 158138f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 158238f7b870SPatrick McHardy { 15833b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 158438f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 158538f7b870SPatrick McHardy struct net_device *dev; 158638f7b870SPatrick McHardy struct ifinfomsg *ifm; 158738f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 158838f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 158938f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 159038f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 159138f7b870SPatrick McHardy int err; 159238f7b870SPatrick McHardy 159395a5afcaSJohannes Berg #ifdef CONFIG_MODULES 159438f7b870SPatrick McHardy replay: 15958072f085SThomas Graf #endif 159638f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 159738f7b870SPatrick McHardy if (err < 0) 159838f7b870SPatrick McHardy return err; 159938f7b870SPatrick McHardy 160038f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 160138f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 160238f7b870SPatrick McHardy else 160338f7b870SPatrick McHardy ifname[0] = '\0'; 160438f7b870SPatrick McHardy 160538f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 160638f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1607881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 1608e7ed828fSVlad Dogaru else { 1609e7ed828fSVlad Dogaru if (ifname[0]) 1610881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 161138f7b870SPatrick McHardy else 161238f7b870SPatrick McHardy dev = NULL; 1613e7ed828fSVlad Dogaru } 161438f7b870SPatrick McHardy 1615e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1616e0d087afSEric Dumazet if (err < 0) 16171840bb13SThomas Graf return err; 16181840bb13SThomas Graf 161938f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 162038f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 162138f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 162238f7b870SPatrick McHardy if (err < 0) 162338f7b870SPatrick McHardy return err; 162438f7b870SPatrick McHardy } else 162538f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 162638f7b870SPatrick McHardy 162738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 162838f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 162938f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 163038f7b870SPatrick McHardy } else { 163138f7b870SPatrick McHardy kind[0] = '\0'; 163238f7b870SPatrick McHardy ops = NULL; 163338f7b870SPatrick McHardy } 163438f7b870SPatrick McHardy 163538f7b870SPatrick McHardy if (1) { 163638f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 163781adee47SEric W. Biederman struct net *dest_net; 163838f7b870SPatrick McHardy 163938f7b870SPatrick McHardy if (ops) { 164038f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 164138f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 164238f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 164338f7b870SPatrick McHardy ops->policy); 164438f7b870SPatrick McHardy if (err < 0) 164538f7b870SPatrick McHardy return err; 164638f7b870SPatrick McHardy data = attr; 164738f7b870SPatrick McHardy } 164838f7b870SPatrick McHardy if (ops->validate) { 164938f7b870SPatrick McHardy err = ops->validate(tb, data); 165038f7b870SPatrick McHardy if (err < 0) 165138f7b870SPatrick McHardy return err; 165238f7b870SPatrick McHardy } 165338f7b870SPatrick McHardy } 165438f7b870SPatrick McHardy 165538f7b870SPatrick McHardy if (dev) { 165638f7b870SPatrick McHardy int modified = 0; 165738f7b870SPatrick McHardy 165838f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 165938f7b870SPatrick McHardy return -EEXIST; 166038f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 166138f7b870SPatrick McHardy return -EOPNOTSUPP; 166238f7b870SPatrick McHardy 166338f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 166438f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 166538f7b870SPatrick McHardy !ops->changelink) 166638f7b870SPatrick McHardy return -EOPNOTSUPP; 166738f7b870SPatrick McHardy 166838f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 166938f7b870SPatrick McHardy if (err < 0) 167038f7b870SPatrick McHardy return err; 167138f7b870SPatrick McHardy modified = 1; 167238f7b870SPatrick McHardy } 167338f7b870SPatrick McHardy 167438f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 167538f7b870SPatrick McHardy } 167638f7b870SPatrick McHardy 1677*ffa934f1SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 1678*ffa934f1SPatrick McHardy if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 1679*ffa934f1SPatrick McHardy return rtnl_group_changelink(net, 1680*ffa934f1SPatrick McHardy nla_get_u32(tb[IFLA_GROUP]), 1681*ffa934f1SPatrick McHardy ifm, tb); 168238f7b870SPatrick McHardy return -ENODEV; 1683*ffa934f1SPatrick McHardy } 168438f7b870SPatrick McHardy 16853729d502SPatrick McHardy if (ifm->ifi_index) 168638f7b870SPatrick McHardy return -EOPNOTSUPP; 16870e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 168838f7b870SPatrick McHardy return -EOPNOTSUPP; 168938f7b870SPatrick McHardy 169038f7b870SPatrick McHardy if (!ops) { 169195a5afcaSJohannes Berg #ifdef CONFIG_MODULES 169238f7b870SPatrick McHardy if (kind[0]) { 169338f7b870SPatrick McHardy __rtnl_unlock(); 169438f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 169538f7b870SPatrick McHardy rtnl_lock(); 169638f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 169738f7b870SPatrick McHardy if (ops) 169838f7b870SPatrick McHardy goto replay; 169938f7b870SPatrick McHardy } 170038f7b870SPatrick McHardy #endif 170138f7b870SPatrick McHardy return -EOPNOTSUPP; 170238f7b870SPatrick McHardy } 170338f7b870SPatrick McHardy 170438f7b870SPatrick McHardy if (!ifname[0]) 170538f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 170638f7b870SPatrick McHardy 170781adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 170881adee47SEric W. Biederman dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 170938f7b870SPatrick McHardy 1710e7199288SPavel Emelianov if (IS_ERR(dev)) 1711e7199288SPavel Emelianov err = PTR_ERR(dev); 1712e7199288SPavel Emelianov else if (ops->newlink) 171381adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 17142d85cba2SPatrick McHardy else 17152d85cba2SPatrick McHardy err = register_netdevice(dev); 171680032cffSDan Carpenter 171780032cffSDan Carpenter if (err < 0 && !IS_ERR(dev)) 171838f7b870SPatrick McHardy free_netdev(dev); 171980032cffSDan Carpenter if (err < 0) 17203729d502SPatrick McHardy goto out; 172181adee47SEric W. Biederman 17223729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 17233729d502SPatrick McHardy if (err < 0) 17243729d502SPatrick McHardy unregister_netdevice(dev); 17253729d502SPatrick McHardy out: 172681adee47SEric W. Biederman put_net(dest_net); 172738f7b870SPatrick McHardy return err; 172838f7b870SPatrick McHardy } 172938f7b870SPatrick McHardy } 173038f7b870SPatrick McHardy 1731b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1732711e2c33SJean Tourrilhes { 17333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1734b60c5115SThomas Graf struct ifinfomsg *ifm; 1735a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 1736b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1737b60c5115SThomas Graf struct net_device *dev = NULL; 1738b60c5115SThomas Graf struct sk_buff *nskb; 1739339bf98fSThomas Graf int err; 1740711e2c33SJean Tourrilhes 1741b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1742b60c5115SThomas Graf if (err < 0) 17439918f230SEric Sesterhenn return err; 1744b60c5115SThomas Graf 1745a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 1746a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1747a3d12891SEric Dumazet 1748b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1749a3d12891SEric Dumazet if (ifm->ifi_index > 0) 1750a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1751a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 1752a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1753a3d12891SEric Dumazet else 1754b60c5115SThomas Graf return -EINVAL; 1755b60c5115SThomas Graf 1756a3d12891SEric Dumazet if (dev == NULL) 1757a3d12891SEric Dumazet return -ENODEV; 1758a3d12891SEric Dumazet 175938f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1760a3d12891SEric Dumazet if (nskb == NULL) 1761a3d12891SEric Dumazet return -ENOBUFS; 1762711e2c33SJean Tourrilhes 1763575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1764575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 176526932566SPatrick McHardy if (err < 0) { 176626932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 176726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 176826932566SPatrick McHardy kfree_skb(nskb); 1769a3d12891SEric Dumazet } else 177097c53cacSDenis V. Lunev err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1771b60c5115SThomas Graf 1772711e2c33SJean Tourrilhes return err; 1773711e2c33SJean Tourrilhes } 1774711e2c33SJean Tourrilhes 177542bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds int idx; 17781da177e4SLinus Torvalds int s_idx = cb->family; 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds if (s_idx == 0) 17811da177e4SLinus Torvalds s_idx = 1; 178225239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 17831da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 17841da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 17851da177e4SLinus Torvalds continue; 1786e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1787e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 17881da177e4SLinus Torvalds continue; 17891da177e4SLinus Torvalds if (idx > s_idx) 17901da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1791e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 17921da177e4SLinus Torvalds break; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds cb->family = idx; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds return skb->len; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 18001da177e4SLinus Torvalds { 1801c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 18021da177e4SLinus Torvalds struct sk_buff *skb; 18030ec6d3f4SThomas Graf int err = -ENOBUFS; 18041da177e4SLinus Torvalds 180538f7b870SPatrick McHardy skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 18060ec6d3f4SThomas Graf if (skb == NULL) 18070ec6d3f4SThomas Graf goto errout; 18081da177e4SLinus Torvalds 1809575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 181026932566SPatrick McHardy if (err < 0) { 181126932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 181226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 181326932566SPatrick McHardy kfree_skb(skb); 181426932566SPatrick McHardy goto errout; 181526932566SPatrick McHardy } 18161ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 18171ce85fe4SPablo Neira Ayuso return; 18180ec6d3f4SThomas Graf errout: 18190ec6d3f4SThomas Graf if (err < 0) 18204b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 18241da177e4SLinus Torvalds static struct rtattr **rta_buf; 18251da177e4SLinus Torvalds static int rtattr_max; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds /* Process one rtnetlink message. */ 18281da177e4SLinus Torvalds 18291d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 18301da177e4SLinus Torvalds { 18313b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1832e2849863SThomas Graf rtnl_doit_func doit; 18331da177e4SLinus Torvalds int sz_idx, kind; 18341da177e4SLinus Torvalds int min_len; 18351da177e4SLinus Torvalds int family; 18361da177e4SLinus Torvalds int type; 18371c2d670fSPatrick McHardy int err; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds type = nlh->nlmsg_type; 18401da177e4SLinus Torvalds if (type > RTM_MAX) 1841038890feSThomas Graf return -EOPNOTSUPP; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds type -= RTM_BASE; 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 18461da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 18471da177e4SLinus Torvalds return 0; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 18501da177e4SLinus Torvalds sz_idx = type>>2; 18511da177e4SLinus Torvalds kind = type&3; 18521da177e4SLinus Torvalds 18531d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 18541d00a4ebSThomas Graf return -EPERM; 18551da177e4SLinus Torvalds 18560ab03c2bSJan Engelhardt if (kind == 2 && (nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) { 185797c53cacSDenis V. Lunev struct sock *rtnl; 1858e2849863SThomas Graf rtnl_dumpit_func dumpit; 18591da177e4SLinus Torvalds 1860e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1861e2849863SThomas Graf if (dumpit == NULL) 1862038890feSThomas Graf return -EOPNOTSUPP; 18631da177e4SLinus Torvalds 18641c2d670fSPatrick McHardy __rtnl_unlock(); 186597c53cacSDenis V. Lunev rtnl = net->rtnl; 18661c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 18671c2d670fSPatrick McHardy rtnl_lock(); 18681c2d670fSPatrick McHardy return err; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 18741da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 18751d00a4ebSThomas Graf return -EINVAL; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 18781da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 18791da177e4SLinus Torvalds struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 18821da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 18831da177e4SLinus Torvalds if (flavor) { 18841da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 18851d00a4ebSThomas Graf return -EINVAL; 18861da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 1892e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1893e2849863SThomas Graf if (doit == NULL) 1894038890feSThomas Graf return -EOPNOTSUPP; 18951da177e4SLinus Torvalds 18961d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 1899cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 19001da177e4SLinus Torvalds { 19011536cc0dSDenis V. Lunev rtnl_lock(); 1902cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 19031536cc0dSDenis V. Lunev rtnl_unlock(); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds struct net_device *dev = ptr; 1909e9dc8653SEric W. Biederman 19101da177e4SLinus Torvalds switch (event) { 19111da177e4SLinus Torvalds case NETDEV_UP: 19121da177e4SLinus Torvalds case NETDEV_DOWN: 191310de05afSPatrick McHardy case NETDEV_PRE_UP: 1914d90a909eSEric W. Biederman case NETDEV_POST_INIT: 1915d90a909eSEric W. Biederman case NETDEV_REGISTER: 19161da177e4SLinus Torvalds case NETDEV_CHANGE: 1917755d0e77SPatrick McHardy case NETDEV_PRE_TYPE_CHANGE: 19181da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 1919a2835763SPatrick McHardy case NETDEV_UNREGISTER: 1920d90a909eSEric W. Biederman case NETDEV_UNREGISTER_BATCH: 19211da177e4SLinus Torvalds break; 19221da177e4SLinus Torvalds default: 19231da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 19241da177e4SLinus Torvalds break; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds return NOTIFY_DONE; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 19301da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 19311da177e4SLinus Torvalds }; 19321da177e4SLinus Torvalds 193397c53cacSDenis V. Lunev 19342c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 193597c53cacSDenis V. Lunev { 193697c53cacSDenis V. Lunev struct sock *sk; 193797c53cacSDenis V. Lunev sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 193897c53cacSDenis V. Lunev rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 193997c53cacSDenis V. Lunev if (!sk) 194097c53cacSDenis V. Lunev return -ENOMEM; 194197c53cacSDenis V. Lunev net->rtnl = sk; 194297c53cacSDenis V. Lunev return 0; 194397c53cacSDenis V. Lunev } 194497c53cacSDenis V. Lunev 19452c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 194697c53cacSDenis V. Lunev { 1947b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 194897c53cacSDenis V. Lunev net->rtnl = NULL; 194997c53cacSDenis V. Lunev } 195097c53cacSDenis V. Lunev 195197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 195297c53cacSDenis V. Lunev .init = rtnetlink_net_init, 195397c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 195497c53cacSDenis V. Lunev }; 195597c53cacSDenis V. Lunev 19561da177e4SLinus Torvalds void __init rtnetlink_init(void) 19571da177e4SLinus Torvalds { 19581da177e4SLinus Torvalds int i; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds rtattr_max = 0; 19611da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 19621da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 19631da177e4SLinus Torvalds rtattr_max = rta_max[i]; 19641da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 19651da177e4SLinus Torvalds if (!rta_buf) 19661da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 19671da177e4SLinus Torvalds 196897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 19691da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 197097c53cacSDenis V. Lunev 19711da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 19721da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 1973340d17fcSThomas Graf 1974340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 1975340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 197638f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); 197738f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); 1978687ad8ccSThomas Graf 1979687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 1980687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 1983