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 365*f8ff182cSThomas Graf static LIST_HEAD(rtnl_af_ops); 366*f8ff182cSThomas Graf 367*f8ff182cSThomas Graf static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 368*f8ff182cSThomas Graf { 369*f8ff182cSThomas Graf const struct rtnl_af_ops *ops; 370*f8ff182cSThomas Graf 371*f8ff182cSThomas Graf list_for_each_entry(ops, &rtnl_af_ops, list) { 372*f8ff182cSThomas Graf if (ops->family == family) 373*f8ff182cSThomas Graf return ops; 374*f8ff182cSThomas Graf } 375*f8ff182cSThomas Graf 376*f8ff182cSThomas Graf return NULL; 377*f8ff182cSThomas Graf } 378*f8ff182cSThomas Graf 379*f8ff182cSThomas Graf /** 380*f8ff182cSThomas Graf * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. 381*f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 382*f8ff182cSThomas Graf * 383*f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 384*f8ff182cSThomas Graf * 385*f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 386*f8ff182cSThomas Graf */ 387*f8ff182cSThomas Graf int __rtnl_af_register(struct rtnl_af_ops *ops) 388*f8ff182cSThomas Graf { 389*f8ff182cSThomas Graf list_add_tail(&ops->list, &rtnl_af_ops); 390*f8ff182cSThomas Graf return 0; 391*f8ff182cSThomas Graf } 392*f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_register); 393*f8ff182cSThomas Graf 394*f8ff182cSThomas Graf /** 395*f8ff182cSThomas Graf * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 396*f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to register 397*f8ff182cSThomas Graf * 398*f8ff182cSThomas Graf * Returns 0 on success or a negative error code. 399*f8ff182cSThomas Graf */ 400*f8ff182cSThomas Graf int rtnl_af_register(struct rtnl_af_ops *ops) 401*f8ff182cSThomas Graf { 402*f8ff182cSThomas Graf int err; 403*f8ff182cSThomas Graf 404*f8ff182cSThomas Graf rtnl_lock(); 405*f8ff182cSThomas Graf err = __rtnl_af_register(ops); 406*f8ff182cSThomas Graf rtnl_unlock(); 407*f8ff182cSThomas Graf return err; 408*f8ff182cSThomas Graf } 409*f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_register); 410*f8ff182cSThomas Graf 411*f8ff182cSThomas Graf /** 412*f8ff182cSThomas Graf * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 413*f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 414*f8ff182cSThomas Graf * 415*f8ff182cSThomas Graf * The caller must hold the rtnl_mutex. 416*f8ff182cSThomas Graf */ 417*f8ff182cSThomas Graf void __rtnl_af_unregister(struct rtnl_af_ops *ops) 418*f8ff182cSThomas Graf { 419*f8ff182cSThomas Graf list_del(&ops->list); 420*f8ff182cSThomas Graf } 421*f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 422*f8ff182cSThomas Graf 423*f8ff182cSThomas Graf /** 424*f8ff182cSThomas Graf * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 425*f8ff182cSThomas Graf * @ops: struct rtnl_af_ops * to unregister 426*f8ff182cSThomas Graf */ 427*f8ff182cSThomas Graf void rtnl_af_unregister(struct rtnl_af_ops *ops) 428*f8ff182cSThomas Graf { 429*f8ff182cSThomas Graf rtnl_lock(); 430*f8ff182cSThomas Graf __rtnl_af_unregister(ops); 431*f8ff182cSThomas Graf rtnl_unlock(); 432*f8ff182cSThomas Graf } 433*f8ff182cSThomas Graf EXPORT_SYMBOL_GPL(rtnl_af_unregister); 434*f8ff182cSThomas Graf 435*f8ff182cSThomas Graf static size_t rtnl_link_get_af_size(const struct net_device *dev) 436*f8ff182cSThomas Graf { 437*f8ff182cSThomas Graf struct rtnl_af_ops *af_ops; 438*f8ff182cSThomas Graf size_t size; 439*f8ff182cSThomas Graf 440*f8ff182cSThomas Graf /* IFLA_AF_SPEC */ 441*f8ff182cSThomas Graf size = nla_total_size(sizeof(struct nlattr)); 442*f8ff182cSThomas Graf 443*f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 444*f8ff182cSThomas Graf if (af_ops->get_link_af_size) { 445*f8ff182cSThomas Graf /* AF_* + nested data */ 446*f8ff182cSThomas Graf size += nla_total_size(sizeof(struct nlattr)) + 447*f8ff182cSThomas Graf af_ops->get_link_af_size(dev); 448*f8ff182cSThomas Graf } 449*f8ff182cSThomas Graf } 450*f8ff182cSThomas Graf 451*f8ff182cSThomas Graf return size; 452*f8ff182cSThomas Graf } 453*f8ff182cSThomas 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 */ 763*f8ff182cSThomas Graf + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 764*f8ff182cSThomas 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; 850*f8ff182cSThomas Graf struct nlattr *attr, *af_spec; 851*f8ff182cSThomas 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); 8711da177e4SLinus Torvalds 872b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 873b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 8741da177e4SLinus Torvalds 875b60c5115SThomas Graf if (dev->master) 876b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 877b60c5115SThomas Graf 878af356afaSPatrick McHardy if (dev->qdisc) 879af356afaSPatrick McHardy NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 880b00055aaSStefan Rompf 8810b815a1aSStephen Hemminger if (dev->ifalias) 8820b815a1aSStephen Hemminger NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 8830b815a1aSStephen Hemminger 884b00055aaSStefan Rompf if (1) { 8851da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 8861da177e4SLinus Torvalds .mem_start = dev->mem_start, 8871da177e4SLinus Torvalds .mem_end = dev->mem_end, 8881da177e4SLinus Torvalds .base_addr = dev->base_addr, 8891da177e4SLinus Torvalds .irq = dev->irq, 8901da177e4SLinus Torvalds .dma = dev->dma, 8911da177e4SLinus Torvalds .port = dev->if_port, 8921da177e4SLinus Torvalds }; 893b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds if (dev->addr_len) { 897b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 898b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 901b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 902b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 903b60c5115SThomas Graf if (attr == NULL) 904b60c5115SThomas Graf goto nla_put_failure; 905b60c5115SThomas Graf 90628172739SEric Dumazet stats = dev_get_stats(dev, &temp); 907b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 9081da177e4SLinus Torvalds 90910708f37SJan Engelhardt attr = nla_reserve(skb, IFLA_STATS64, 91010708f37SJan Engelhardt sizeof(struct rtnl_link_stats64)); 91110708f37SJan Engelhardt if (attr == NULL) 91210708f37SJan Engelhardt goto nla_put_failure; 91310708f37SJan Engelhardt copy_rtnl_link_stats64(nla_data(attr), stats); 91410708f37SJan Engelhardt 91557b61080SScott Feldman if (dev->dev.parent) 91657b61080SScott Feldman NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 91757b61080SScott Feldman 918ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { 919ebc08a6fSWilliams, Mitch A int i; 920ebc08a6fSWilliams, Mitch A 921c02db8c6SChris Wright struct nlattr *vfinfo, *vf; 922c02db8c6SChris Wright int num_vfs = dev_num_vf(dev->dev.parent); 923c02db8c6SChris Wright 924c02db8c6SChris Wright vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 925c02db8c6SChris Wright if (!vfinfo) 926c02db8c6SChris Wright goto nla_put_failure; 927c02db8c6SChris Wright for (i = 0; i < num_vfs; i++) { 928c02db8c6SChris Wright struct ifla_vf_info ivi; 929c02db8c6SChris Wright struct ifla_vf_mac vf_mac; 930c02db8c6SChris Wright struct ifla_vf_vlan vf_vlan; 931c02db8c6SChris Wright struct ifla_vf_tx_rate vf_tx_rate; 932ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 933ebc08a6fSWilliams, Mitch A break; 934c02db8c6SChris Wright vf_mac.vf = vf_vlan.vf = vf_tx_rate.vf = ivi.vf; 935c02db8c6SChris Wright memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 936c02db8c6SChris Wright vf_vlan.vlan = ivi.vlan; 937c02db8c6SChris Wright vf_vlan.qos = ivi.qos; 938c02db8c6SChris Wright vf_tx_rate.rate = ivi.tx_rate; 939c02db8c6SChris Wright vf = nla_nest_start(skb, IFLA_VF_INFO); 940c02db8c6SChris Wright if (!vf) { 941c02db8c6SChris Wright nla_nest_cancel(skb, vfinfo); 942c02db8c6SChris Wright goto nla_put_failure; 943ebc08a6fSWilliams, Mitch A } 944c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); 945c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); 946c02db8c6SChris Wright NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), &vf_tx_rate); 947c02db8c6SChris Wright nla_nest_end(skb, vf); 948c02db8c6SChris Wright } 949c02db8c6SChris Wright nla_nest_end(skb, vfinfo); 950ebc08a6fSWilliams, Mitch A } 95157b61080SScott Feldman 95257b61080SScott Feldman if (rtnl_port_fill(skb, dev)) 95357b61080SScott Feldman goto nla_put_failure; 95457b61080SScott Feldman 95538f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 95638f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 95738f7b870SPatrick McHardy goto nla_put_failure; 95838f7b870SPatrick McHardy } 95938f7b870SPatrick McHardy 960*f8ff182cSThomas Graf if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 961*f8ff182cSThomas Graf goto nla_put_failure; 962*f8ff182cSThomas Graf 963*f8ff182cSThomas Graf list_for_each_entry(af_ops, &rtnl_af_ops, list) { 964*f8ff182cSThomas Graf if (af_ops->fill_link_af) { 965*f8ff182cSThomas Graf struct nlattr *af; 966*f8ff182cSThomas Graf int err; 967*f8ff182cSThomas Graf 968*f8ff182cSThomas Graf if (!(af = nla_nest_start(skb, af_ops->family))) 969*f8ff182cSThomas Graf goto nla_put_failure; 970*f8ff182cSThomas Graf 971*f8ff182cSThomas Graf err = af_ops->fill_link_af(skb, dev); 972*f8ff182cSThomas Graf 973*f8ff182cSThomas Graf /* 974*f8ff182cSThomas Graf * Caller may return ENODATA to indicate that there 975*f8ff182cSThomas Graf * was no data to be dumped. This is not an error, it 976*f8ff182cSThomas Graf * means we should trim the attribute header and 977*f8ff182cSThomas Graf * continue. 978*f8ff182cSThomas Graf */ 979*f8ff182cSThomas Graf if (err == -ENODATA) 980*f8ff182cSThomas Graf nla_nest_cancel(skb, af); 981*f8ff182cSThomas Graf else if (err < 0) 982*f8ff182cSThomas Graf goto nla_put_failure; 983*f8ff182cSThomas Graf 984*f8ff182cSThomas Graf nla_nest_end(skb, af); 985*f8ff182cSThomas Graf } 986*f8ff182cSThomas Graf } 987*f8ff182cSThomas Graf 988*f8ff182cSThomas Graf nla_nest_end(skb, af_spec); 989*f8ff182cSThomas Graf 990b60c5115SThomas Graf return nlmsg_end(skb, nlh); 991b60c5115SThomas Graf 992b60c5115SThomas Graf nla_put_failure: 99326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 99426932566SPatrick McHardy return -EMSGSIZE; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 997b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 9981da177e4SLinus Torvalds { 9993b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 10007c28bd0bSEric Dumazet int h, s_h; 10017c28bd0bSEric Dumazet int idx = 0, s_idx; 10021da177e4SLinus Torvalds struct net_device *dev; 10037c28bd0bSEric Dumazet struct hlist_head *head; 10047c28bd0bSEric Dumazet struct hlist_node *node; 10051da177e4SLinus Torvalds 10067c28bd0bSEric Dumazet s_h = cb->args[0]; 10077c28bd0bSEric Dumazet s_idx = cb->args[1]; 10087c28bd0bSEric Dumazet 10097c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 10107562f876SPavel Emelianov idx = 0; 10117c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 10127c28bd0bSEric Dumazet hlist_for_each_entry(dev, node, head, index_hlist) { 10131da177e4SLinus Torvalds if (idx < s_idx) 10147562f876SPavel Emelianov goto cont; 1015575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1016b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 10177c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 10187c28bd0bSEric Dumazet NLM_F_MULTI) <= 0) 10197c28bd0bSEric Dumazet goto out; 10207562f876SPavel Emelianov cont: 10217562f876SPavel Emelianov idx++; 10221da177e4SLinus Torvalds } 10237c28bd0bSEric Dumazet } 10247c28bd0bSEric Dumazet out: 10257c28bd0bSEric Dumazet cb->args[1] = idx; 10267c28bd0bSEric Dumazet cb->args[0] = h; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds return skb->len; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 1031e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 10325176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 103338f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 103438f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 10355176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1036da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 103776e87306SThomas Graf [IFLA_LINK] = { .type = NLA_U32 }, 1038da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 1039da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 1040da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1041da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 104276e87306SThomas Graf [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1043d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 10440b815a1aSStephen Hemminger [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1045c02db8c6SChris Wright [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 104657b61080SScott Feldman [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 104757b61080SScott Feldman [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1048*f8ff182cSThomas Graf [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1049da5e0494SThomas Graf }; 1050e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy); 10511da177e4SLinus Torvalds 105238f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 105338f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 105438f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 105538f7b870SPatrick McHardy }; 105638f7b870SPatrick McHardy 1057c02db8c6SChris Wright static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 1058c02db8c6SChris Wright [IFLA_VF_INFO] = { .type = NLA_NESTED }, 1059c02db8c6SChris Wright }; 1060c02db8c6SChris Wright 1061c02db8c6SChris Wright static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1062c02db8c6SChris Wright [IFLA_VF_MAC] = { .type = NLA_BINARY, 1063c02db8c6SChris Wright .len = sizeof(struct ifla_vf_mac) }, 1064c02db8c6SChris Wright [IFLA_VF_VLAN] = { .type = NLA_BINARY, 1065c02db8c6SChris Wright .len = sizeof(struct ifla_vf_vlan) }, 1066c02db8c6SChris Wright [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 1067c02db8c6SChris Wright .len = sizeof(struct ifla_vf_tx_rate) }, 1068c02db8c6SChris Wright }; 1069c02db8c6SChris Wright 107057b61080SScott Feldman static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 107157b61080SScott Feldman [IFLA_PORT_VF] = { .type = NLA_U32 }, 107257b61080SScott Feldman [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 107357b61080SScott Feldman .len = PORT_PROFILE_MAX }, 107457b61080SScott Feldman [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 107557b61080SScott Feldman .len = sizeof(struct ifla_port_vsi)}, 107657b61080SScott Feldman [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 107757b61080SScott Feldman .len = PORT_UUID_MAX }, 107857b61080SScott Feldman [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 107957b61080SScott Feldman .len = PORT_UUID_MAX }, 108057b61080SScott Feldman [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 108157b61080SScott Feldman [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 108257b61080SScott Feldman }; 108357b61080SScott Feldman 108481adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 108581adee47SEric W. Biederman { 108681adee47SEric W. Biederman struct net *net; 108781adee47SEric W. Biederman /* Examine the link attributes and figure out which 108881adee47SEric W. Biederman * network namespace we are talking about. 108981adee47SEric W. Biederman */ 109081adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 109181adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 109281adee47SEric W. Biederman else 109381adee47SEric W. Biederman net = get_net(src_net); 109481adee47SEric W. Biederman return net; 109581adee47SEric W. Biederman } 109681adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 109781adee47SEric W. Biederman 10981840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 10991840bb13SThomas Graf { 11001840bb13SThomas Graf if (dev) { 11011840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 11021840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 11031840bb13SThomas Graf return -EINVAL; 11041840bb13SThomas Graf 11051840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 11061840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 11071840bb13SThomas Graf return -EINVAL; 11081840bb13SThomas Graf } 11091840bb13SThomas Graf 11101840bb13SThomas Graf return 0; 11111840bb13SThomas Graf } 11121840bb13SThomas Graf 1113c02db8c6SChris Wright static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1114c02db8c6SChris Wright { 1115c02db8c6SChris Wright int rem, err = -EINVAL; 1116c02db8c6SChris Wright struct nlattr *vf; 1117c02db8c6SChris Wright const struct net_device_ops *ops = dev->netdev_ops; 1118c02db8c6SChris Wright 1119c02db8c6SChris Wright nla_for_each_nested(vf, attr, rem) { 1120c02db8c6SChris Wright switch (nla_type(vf)) { 1121c02db8c6SChris Wright case IFLA_VF_MAC: { 1122c02db8c6SChris Wright struct ifla_vf_mac *ivm; 1123c02db8c6SChris Wright ivm = nla_data(vf); 1124c02db8c6SChris Wright err = -EOPNOTSUPP; 1125c02db8c6SChris Wright if (ops->ndo_set_vf_mac) 1126c02db8c6SChris Wright err = ops->ndo_set_vf_mac(dev, ivm->vf, 1127c02db8c6SChris Wright ivm->mac); 1128c02db8c6SChris Wright break; 1129c02db8c6SChris Wright } 1130c02db8c6SChris Wright case IFLA_VF_VLAN: { 1131c02db8c6SChris Wright struct ifla_vf_vlan *ivv; 1132c02db8c6SChris Wright ivv = nla_data(vf); 1133c02db8c6SChris Wright err = -EOPNOTSUPP; 1134c02db8c6SChris Wright if (ops->ndo_set_vf_vlan) 1135c02db8c6SChris Wright err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1136c02db8c6SChris Wright ivv->vlan, 1137c02db8c6SChris Wright ivv->qos); 1138c02db8c6SChris Wright break; 1139c02db8c6SChris Wright } 1140c02db8c6SChris Wright case IFLA_VF_TX_RATE: { 1141c02db8c6SChris Wright struct ifla_vf_tx_rate *ivt; 1142c02db8c6SChris Wright ivt = nla_data(vf); 1143c02db8c6SChris Wright err = -EOPNOTSUPP; 1144c02db8c6SChris Wright if (ops->ndo_set_vf_tx_rate) 1145c02db8c6SChris Wright err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, 1146c02db8c6SChris Wright ivt->rate); 1147c02db8c6SChris Wright break; 1148c02db8c6SChris Wright } 1149c02db8c6SChris Wright default: 1150c02db8c6SChris Wright err = -EINVAL; 1151c02db8c6SChris Wright break; 1152c02db8c6SChris Wright } 1153c02db8c6SChris Wright if (err) 1154c02db8c6SChris Wright break; 1155c02db8c6SChris Wright } 1156c02db8c6SChris Wright return err; 1157c02db8c6SChris Wright } 1158c02db8c6SChris Wright 11590157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 116038f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 11610157f60cSPatrick McHardy { 1162d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 116338f7b870SPatrick McHardy int send_addr_notify = 0; 11640157f60cSPatrick McHardy int err; 11650157f60cSPatrick McHardy 1166d8a5ec67SEric W. Biederman if (tb[IFLA_NET_NS_PID]) { 116781adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1168d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 1169d8a5ec67SEric W. Biederman err = PTR_ERR(net); 1170d8a5ec67SEric W. Biederman goto errout; 1171d8a5ec67SEric W. Biederman } 1172d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 1173d8a5ec67SEric W. Biederman put_net(net); 1174d8a5ec67SEric W. Biederman if (err) 1175d8a5ec67SEric W. Biederman goto errout; 1176d8a5ec67SEric W. Biederman modified = 1; 1177d8a5ec67SEric W. Biederman } 1178d8a5ec67SEric W. Biederman 11790157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 11800157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 11810157f60cSPatrick McHardy struct ifmap k_map; 11820157f60cSPatrick McHardy 1183d314774cSStephen Hemminger if (!ops->ndo_set_config) { 11840157f60cSPatrick McHardy err = -EOPNOTSUPP; 11850157f60cSPatrick McHardy goto errout; 11860157f60cSPatrick McHardy } 11870157f60cSPatrick McHardy 11880157f60cSPatrick McHardy if (!netif_device_present(dev)) { 11890157f60cSPatrick McHardy err = -ENODEV; 11900157f60cSPatrick McHardy goto errout; 11910157f60cSPatrick McHardy } 11920157f60cSPatrick McHardy 11930157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 11940157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 11950157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 11960157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 11970157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 11980157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 11990157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 12000157f60cSPatrick McHardy 1201d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 12020157f60cSPatrick McHardy if (err < 0) 12030157f60cSPatrick McHardy goto errout; 12040157f60cSPatrick McHardy 12050157f60cSPatrick McHardy modified = 1; 12060157f60cSPatrick McHardy } 12070157f60cSPatrick McHardy 12080157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 12090157f60cSPatrick McHardy struct sockaddr *sa; 12100157f60cSPatrick McHardy int len; 12110157f60cSPatrick McHardy 1212d314774cSStephen Hemminger if (!ops->ndo_set_mac_address) { 12130157f60cSPatrick McHardy err = -EOPNOTSUPP; 12140157f60cSPatrick McHardy goto errout; 12150157f60cSPatrick McHardy } 12160157f60cSPatrick McHardy 12170157f60cSPatrick McHardy if (!netif_device_present(dev)) { 12180157f60cSPatrick McHardy err = -ENODEV; 12190157f60cSPatrick McHardy goto errout; 12200157f60cSPatrick McHardy } 12210157f60cSPatrick McHardy 12220157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 12230157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 12240157f60cSPatrick McHardy if (!sa) { 12250157f60cSPatrick McHardy err = -ENOMEM; 12260157f60cSPatrick McHardy goto errout; 12270157f60cSPatrick McHardy } 12280157f60cSPatrick McHardy sa->sa_family = dev->type; 12290157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 12300157f60cSPatrick McHardy dev->addr_len); 1231d314774cSStephen Hemminger err = ops->ndo_set_mac_address(dev, sa); 12320157f60cSPatrick McHardy kfree(sa); 12330157f60cSPatrick McHardy if (err) 12340157f60cSPatrick McHardy goto errout; 12350157f60cSPatrick McHardy send_addr_notify = 1; 12360157f60cSPatrick McHardy modified = 1; 12370157f60cSPatrick McHardy } 12380157f60cSPatrick McHardy 12390157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 12400157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 12410157f60cSPatrick McHardy if (err < 0) 12420157f60cSPatrick McHardy goto errout; 12430157f60cSPatrick McHardy modified = 1; 12440157f60cSPatrick McHardy } 12450157f60cSPatrick McHardy 12460157f60cSPatrick McHardy /* 12470157f60cSPatrick McHardy * Interface selected by interface index but interface 12480157f60cSPatrick McHardy * name provided implies that a name change has been 12490157f60cSPatrick McHardy * requested. 12500157f60cSPatrick McHardy */ 12510157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 12520157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 12530157f60cSPatrick McHardy if (err < 0) 12540157f60cSPatrick McHardy goto errout; 12550157f60cSPatrick McHardy modified = 1; 12560157f60cSPatrick McHardy } 12570157f60cSPatrick McHardy 12580b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 12590b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 12600b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 12610b815a1aSStephen Hemminger if (err < 0) 12620b815a1aSStephen Hemminger goto errout; 12630b815a1aSStephen Hemminger modified = 1; 12640b815a1aSStephen Hemminger } 12650b815a1aSStephen Hemminger 12660157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 12670157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 12680157f60cSPatrick McHardy send_addr_notify = 1; 12690157f60cSPatrick McHardy } 12700157f60cSPatrick McHardy 12710157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 12723729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 12735f9021cfSJohannes Berg if (err < 0) 12745f9021cfSJohannes Berg goto errout; 12750157f60cSPatrick McHardy } 12760157f60cSPatrick McHardy 127793b2d4a2SDavid S. Miller if (tb[IFLA_TXQLEN]) 12780157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 12790157f60cSPatrick McHardy 12800157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 128193b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 12820157f60cSPatrick McHardy 12830157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 12840157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 12850157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 128693b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 12870157f60cSPatrick McHardy } 12880157f60cSPatrick McHardy 1289c02db8c6SChris Wright if (tb[IFLA_VFINFO_LIST]) { 1290c02db8c6SChris Wright struct nlattr *attr; 1291c02db8c6SChris Wright int rem; 1292c02db8c6SChris Wright nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1293253683bbSDavid Howells if (nla_type(attr) != IFLA_VF_INFO) { 1294253683bbSDavid Howells err = -EINVAL; 1295c02db8c6SChris Wright goto errout; 1296253683bbSDavid Howells } 1297c02db8c6SChris Wright err = do_setvfinfo(dev, attr); 1298ebc08a6fSWilliams, Mitch A if (err < 0) 1299ebc08a6fSWilliams, Mitch A goto errout; 1300ebc08a6fSWilliams, Mitch A modified = 1; 1301ebc08a6fSWilliams, Mitch A } 1302ebc08a6fSWilliams, Mitch A } 13030157f60cSPatrick McHardy err = 0; 13040157f60cSPatrick McHardy 130557b61080SScott Feldman if (tb[IFLA_VF_PORTS]) { 130657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 130757b61080SScott Feldman struct nlattr *attr; 130857b61080SScott Feldman int vf; 130957b61080SScott Feldman int rem; 131057b61080SScott Feldman 131157b61080SScott Feldman err = -EOPNOTSUPP; 131257b61080SScott Feldman if (!ops->ndo_set_vf_port) 131357b61080SScott Feldman goto errout; 131457b61080SScott Feldman 131557b61080SScott Feldman nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 131657b61080SScott Feldman if (nla_type(attr) != IFLA_VF_PORT) 131757b61080SScott Feldman continue; 131857b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 131957b61080SScott Feldman attr, ifla_port_policy); 132057b61080SScott Feldman if (err < 0) 132157b61080SScott Feldman goto errout; 132257b61080SScott Feldman if (!port[IFLA_PORT_VF]) { 132357b61080SScott Feldman err = -EOPNOTSUPP; 132457b61080SScott Feldman goto errout; 132557b61080SScott Feldman } 132657b61080SScott Feldman vf = nla_get_u32(port[IFLA_PORT_VF]); 132757b61080SScott Feldman err = ops->ndo_set_vf_port(dev, vf, port); 132857b61080SScott Feldman if (err < 0) 132957b61080SScott Feldman goto errout; 133057b61080SScott Feldman modified = 1; 133157b61080SScott Feldman } 133257b61080SScott Feldman } 133357b61080SScott Feldman err = 0; 133457b61080SScott Feldman 133557b61080SScott Feldman if (tb[IFLA_PORT_SELF]) { 133657b61080SScott Feldman struct nlattr *port[IFLA_PORT_MAX+1]; 133757b61080SScott Feldman 133857b61080SScott Feldman err = nla_parse_nested(port, IFLA_PORT_MAX, 133957b61080SScott Feldman tb[IFLA_PORT_SELF], ifla_port_policy); 134057b61080SScott Feldman if (err < 0) 134157b61080SScott Feldman goto errout; 134257b61080SScott Feldman 134357b61080SScott Feldman err = -EOPNOTSUPP; 134457b61080SScott Feldman if (ops->ndo_set_vf_port) 134557b61080SScott Feldman err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 134657b61080SScott Feldman if (err < 0) 134757b61080SScott Feldman goto errout; 134857b61080SScott Feldman modified = 1; 134957b61080SScott Feldman } 1350*f8ff182cSThomas Graf 1351*f8ff182cSThomas Graf if (tb[IFLA_AF_SPEC]) { 1352*f8ff182cSThomas Graf struct nlattr *af; 1353*f8ff182cSThomas Graf int rem; 1354*f8ff182cSThomas Graf 1355*f8ff182cSThomas Graf nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1356*f8ff182cSThomas Graf const struct rtnl_af_ops *af_ops; 1357*f8ff182cSThomas Graf 1358*f8ff182cSThomas Graf if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1359*f8ff182cSThomas Graf continue; 1360*f8ff182cSThomas Graf 1361*f8ff182cSThomas Graf if (!af_ops->parse_link_af) 1362*f8ff182cSThomas Graf continue; 1363*f8ff182cSThomas Graf 1364*f8ff182cSThomas Graf err = af_ops->parse_link_af(dev, af); 1365*f8ff182cSThomas Graf if (err < 0) 1366*f8ff182cSThomas Graf goto errout; 1367*f8ff182cSThomas Graf 1368*f8ff182cSThomas Graf modified = 1; 1369*f8ff182cSThomas Graf } 1370*f8ff182cSThomas Graf } 137157b61080SScott Feldman err = 0; 137257b61080SScott Feldman 13730157f60cSPatrick McHardy errout: 13740157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 13750157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 13760157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 13770157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 13780157f60cSPatrick McHardy "please check.\n", dev->name); 13790157f60cSPatrick McHardy 13800157f60cSPatrick McHardy if (send_addr_notify) 13810157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 13820157f60cSPatrick McHardy return err; 13830157f60cSPatrick McHardy } 13840157f60cSPatrick McHardy 1385da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1386da5e0494SThomas Graf { 13873b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1388da5e0494SThomas Graf struct ifinfomsg *ifm; 1389da5e0494SThomas Graf struct net_device *dev; 13900157f60cSPatrick McHardy int err; 1391da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 13921da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 13931da177e4SLinus Torvalds 1394da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1395da5e0494SThomas Graf if (err < 0) 1396da5e0494SThomas Graf goto errout; 13971da177e4SLinus Torvalds 13985176f91eSThomas Graf if (tb[IFLA_IFNAME]) 13995176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 140078e5b891SPatrick McHardy else 140178e5b891SPatrick McHardy ifname[0] = '\0'; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds err = -EINVAL; 1404da5e0494SThomas Graf ifm = nlmsg_data(nlh); 140551055be8SPatrick McHardy if (ifm->ifi_index > 0) 1406a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1407da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1408a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1409da5e0494SThomas Graf else 1410da5e0494SThomas Graf goto errout; 14111da177e4SLinus Torvalds 1412da5e0494SThomas Graf if (dev == NULL) { 1413da5e0494SThomas Graf err = -ENODEV; 1414da5e0494SThomas Graf goto errout; 1415da5e0494SThomas Graf } 14161da177e4SLinus Torvalds 1417e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1418e0d087afSEric Dumazet if (err < 0) 1419a3d12891SEric Dumazet goto errout; 1420da5e0494SThomas Graf 142138f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 1422da5e0494SThomas Graf errout: 14231da177e4SLinus Torvalds return err; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 142638f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 142738f7b870SPatrick McHardy { 14283b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 142938f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 143038f7b870SPatrick McHardy struct net_device *dev; 143138f7b870SPatrick McHardy struct ifinfomsg *ifm; 143238f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 143338f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 143438f7b870SPatrick McHardy int err; 143538f7b870SPatrick McHardy 143638f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 143738f7b870SPatrick McHardy if (err < 0) 143838f7b870SPatrick McHardy return err; 143938f7b870SPatrick McHardy 144038f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 144138f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 144238f7b870SPatrick McHardy 144338f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 144438f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1445881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 144638f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1447881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 144838f7b870SPatrick McHardy else 144938f7b870SPatrick McHardy return -EINVAL; 145038f7b870SPatrick McHardy 145138f7b870SPatrick McHardy if (!dev) 145238f7b870SPatrick McHardy return -ENODEV; 145338f7b870SPatrick McHardy 145438f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 145538f7b870SPatrick McHardy if (!ops) 145638f7b870SPatrick McHardy return -EOPNOTSUPP; 145738f7b870SPatrick McHardy 145823289a37SEric Dumazet ops->dellink(dev, NULL); 145938f7b870SPatrick McHardy return 0; 146038f7b870SPatrick McHardy } 146138f7b870SPatrick McHardy 14623729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 14633729d502SPatrick McHardy { 14643729d502SPatrick McHardy unsigned int old_flags; 14653729d502SPatrick McHardy int err; 14663729d502SPatrick McHardy 14673729d502SPatrick McHardy old_flags = dev->flags; 14683729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 14693729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 14703729d502SPatrick McHardy if (err < 0) 14713729d502SPatrick McHardy return err; 14723729d502SPatrick McHardy } 14733729d502SPatrick McHardy 14743729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 14753729d502SPatrick McHardy rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 14763729d502SPatrick McHardy 14773729d502SPatrick McHardy __dev_notify_flags(dev, old_flags); 14783729d502SPatrick McHardy return 0; 14793729d502SPatrick McHardy } 14803729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 14813729d502SPatrick McHardy 148281adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 148381adee47SEric W. Biederman char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1484e7199288SPavel Emelianov { 1485e7199288SPavel Emelianov int err; 1486e7199288SPavel Emelianov struct net_device *dev; 14872e59af3dSEric Dumazet unsigned int num_queues = 1; 14882e59af3dSEric Dumazet unsigned int real_num_queues = 1; 1489e7199288SPavel Emelianov 14902e59af3dSEric Dumazet if (ops->get_tx_queues) { 149181adee47SEric W. Biederman err = ops->get_tx_queues(src_net, tb, &num_queues, 1492e0d087afSEric Dumazet &real_num_queues); 14932e59af3dSEric Dumazet if (err) 14942e59af3dSEric Dumazet goto err; 14952e59af3dSEric Dumazet } 1496e7199288SPavel Emelianov err = -ENOMEM; 14972e59af3dSEric Dumazet dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1498e7199288SPavel Emelianov if (!dev) 1499e7199288SPavel Emelianov goto err; 1500e7199288SPavel Emelianov 150181adee47SEric W. Biederman dev_net_set(dev, net); 150281adee47SEric W. Biederman dev->rtnl_link_ops = ops; 15033729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 15042e59af3dSEric Dumazet dev->real_num_tx_queues = real_num_queues; 150581adee47SEric W. Biederman 1506e7199288SPavel Emelianov if (strchr(dev->name, '%')) { 1507e7199288SPavel Emelianov err = dev_alloc_name(dev, dev->name); 1508e7199288SPavel Emelianov if (err < 0) 1509e7199288SPavel Emelianov goto err_free; 1510e7199288SPavel Emelianov } 1511e7199288SPavel Emelianov 1512e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1513e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1514e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1515e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1516e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1517e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1518e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1519e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1520e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1521e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1522e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 152393b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1524e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1525e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1526e7199288SPavel Emelianov 1527e7199288SPavel Emelianov return dev; 1528e7199288SPavel Emelianov 1529e7199288SPavel Emelianov err_free: 1530e7199288SPavel Emelianov free_netdev(dev); 1531e7199288SPavel Emelianov err: 1532e7199288SPavel Emelianov return ERR_PTR(err); 1533e7199288SPavel Emelianov } 1534e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1535e7199288SPavel Emelianov 153638f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 153738f7b870SPatrick McHardy { 15383b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 153938f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 154038f7b870SPatrick McHardy struct net_device *dev; 154138f7b870SPatrick McHardy struct ifinfomsg *ifm; 154238f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 154338f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 154438f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 154538f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 154638f7b870SPatrick McHardy int err; 154738f7b870SPatrick McHardy 154895a5afcaSJohannes Berg #ifdef CONFIG_MODULES 154938f7b870SPatrick McHardy replay: 15508072f085SThomas Graf #endif 155138f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 155238f7b870SPatrick McHardy if (err < 0) 155338f7b870SPatrick McHardy return err; 155438f7b870SPatrick McHardy 155538f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 155638f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 155738f7b870SPatrick McHardy else 155838f7b870SPatrick McHardy ifname[0] = '\0'; 155938f7b870SPatrick McHardy 156038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 156138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1562881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 156338f7b870SPatrick McHardy else if (ifname[0]) 1564881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 156538f7b870SPatrick McHardy else 156638f7b870SPatrick McHardy dev = NULL; 156738f7b870SPatrick McHardy 1568e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1569e0d087afSEric Dumazet if (err < 0) 15701840bb13SThomas Graf return err; 15711840bb13SThomas Graf 157238f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 157338f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 157438f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 157538f7b870SPatrick McHardy if (err < 0) 157638f7b870SPatrick McHardy return err; 157738f7b870SPatrick McHardy } else 157838f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 157938f7b870SPatrick McHardy 158038f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 158138f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 158238f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 158338f7b870SPatrick McHardy } else { 158438f7b870SPatrick McHardy kind[0] = '\0'; 158538f7b870SPatrick McHardy ops = NULL; 158638f7b870SPatrick McHardy } 158738f7b870SPatrick McHardy 158838f7b870SPatrick McHardy if (1) { 158938f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 159081adee47SEric W. Biederman struct net *dest_net; 159138f7b870SPatrick McHardy 159238f7b870SPatrick McHardy if (ops) { 159338f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 159438f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 159538f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 159638f7b870SPatrick McHardy ops->policy); 159738f7b870SPatrick McHardy if (err < 0) 159838f7b870SPatrick McHardy return err; 159938f7b870SPatrick McHardy data = attr; 160038f7b870SPatrick McHardy } 160138f7b870SPatrick McHardy if (ops->validate) { 160238f7b870SPatrick McHardy err = ops->validate(tb, data); 160338f7b870SPatrick McHardy if (err < 0) 160438f7b870SPatrick McHardy return err; 160538f7b870SPatrick McHardy } 160638f7b870SPatrick McHardy } 160738f7b870SPatrick McHardy 160838f7b870SPatrick McHardy if (dev) { 160938f7b870SPatrick McHardy int modified = 0; 161038f7b870SPatrick McHardy 161138f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 161238f7b870SPatrick McHardy return -EEXIST; 161338f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 161438f7b870SPatrick McHardy return -EOPNOTSUPP; 161538f7b870SPatrick McHardy 161638f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 161738f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 161838f7b870SPatrick McHardy !ops->changelink) 161938f7b870SPatrick McHardy return -EOPNOTSUPP; 162038f7b870SPatrick McHardy 162138f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 162238f7b870SPatrick McHardy if (err < 0) 162338f7b870SPatrick McHardy return err; 162438f7b870SPatrick McHardy modified = 1; 162538f7b870SPatrick McHardy } 162638f7b870SPatrick McHardy 162738f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 162838f7b870SPatrick McHardy } 162938f7b870SPatrick McHardy 163038f7b870SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) 163138f7b870SPatrick McHardy return -ENODEV; 163238f7b870SPatrick McHardy 16333729d502SPatrick McHardy if (ifm->ifi_index) 163438f7b870SPatrick McHardy return -EOPNOTSUPP; 16350e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 163638f7b870SPatrick McHardy return -EOPNOTSUPP; 163738f7b870SPatrick McHardy 163838f7b870SPatrick McHardy if (!ops) { 163995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 164038f7b870SPatrick McHardy if (kind[0]) { 164138f7b870SPatrick McHardy __rtnl_unlock(); 164238f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 164338f7b870SPatrick McHardy rtnl_lock(); 164438f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 164538f7b870SPatrick McHardy if (ops) 164638f7b870SPatrick McHardy goto replay; 164738f7b870SPatrick McHardy } 164838f7b870SPatrick McHardy #endif 164938f7b870SPatrick McHardy return -EOPNOTSUPP; 165038f7b870SPatrick McHardy } 165138f7b870SPatrick McHardy 165238f7b870SPatrick McHardy if (!ifname[0]) 165338f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 165438f7b870SPatrick McHardy 165581adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 165681adee47SEric W. Biederman dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 165738f7b870SPatrick McHardy 1658e7199288SPavel Emelianov if (IS_ERR(dev)) 1659e7199288SPavel Emelianov err = PTR_ERR(dev); 1660e7199288SPavel Emelianov else if (ops->newlink) 166181adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 16622d85cba2SPatrick McHardy else 16632d85cba2SPatrick McHardy err = register_netdevice(dev); 166480032cffSDan Carpenter 166580032cffSDan Carpenter if (err < 0 && !IS_ERR(dev)) 166638f7b870SPatrick McHardy free_netdev(dev); 166780032cffSDan Carpenter if (err < 0) 16683729d502SPatrick McHardy goto out; 166981adee47SEric W. Biederman 16703729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 16713729d502SPatrick McHardy if (err < 0) 16723729d502SPatrick McHardy unregister_netdevice(dev); 16733729d502SPatrick McHardy out: 167481adee47SEric W. Biederman put_net(dest_net); 167538f7b870SPatrick McHardy return err; 167638f7b870SPatrick McHardy } 167738f7b870SPatrick McHardy } 167838f7b870SPatrick McHardy 1679b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1680711e2c33SJean Tourrilhes { 16813b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1682b60c5115SThomas Graf struct ifinfomsg *ifm; 1683a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 1684b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1685b60c5115SThomas Graf struct net_device *dev = NULL; 1686b60c5115SThomas Graf struct sk_buff *nskb; 1687339bf98fSThomas Graf int err; 1688711e2c33SJean Tourrilhes 1689b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1690b60c5115SThomas Graf if (err < 0) 16919918f230SEric Sesterhenn return err; 1692b60c5115SThomas Graf 1693a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 1694a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1695a3d12891SEric Dumazet 1696b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1697a3d12891SEric Dumazet if (ifm->ifi_index > 0) 1698a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1699a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 1700a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1701a3d12891SEric Dumazet else 1702b60c5115SThomas Graf return -EINVAL; 1703b60c5115SThomas Graf 1704a3d12891SEric Dumazet if (dev == NULL) 1705a3d12891SEric Dumazet return -ENODEV; 1706a3d12891SEric Dumazet 170738f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1708a3d12891SEric Dumazet if (nskb == NULL) 1709a3d12891SEric Dumazet return -ENOBUFS; 1710711e2c33SJean Tourrilhes 1711575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1712575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 171326932566SPatrick McHardy if (err < 0) { 171426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 171526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 171626932566SPatrick McHardy kfree_skb(nskb); 1717a3d12891SEric Dumazet } else 171897c53cacSDenis V. Lunev err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1719b60c5115SThomas Graf 1720711e2c33SJean Tourrilhes return err; 1721711e2c33SJean Tourrilhes } 1722711e2c33SJean Tourrilhes 172342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 17241da177e4SLinus Torvalds { 17251da177e4SLinus Torvalds int idx; 17261da177e4SLinus Torvalds int s_idx = cb->family; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds if (s_idx == 0) 17291da177e4SLinus Torvalds s_idx = 1; 173025239ceeSPatrick McHardy for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 17311da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 17321da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 17331da177e4SLinus Torvalds continue; 1734e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1735e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 17361da177e4SLinus Torvalds continue; 17371da177e4SLinus Torvalds if (idx > s_idx) 17381da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1739e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 17401da177e4SLinus Torvalds break; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds cb->family = idx; 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds return skb->len; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 17481da177e4SLinus Torvalds { 1749c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 17501da177e4SLinus Torvalds struct sk_buff *skb; 17510ec6d3f4SThomas Graf int err = -ENOBUFS; 17521da177e4SLinus Torvalds 175338f7b870SPatrick McHardy skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 17540ec6d3f4SThomas Graf if (skb == NULL) 17550ec6d3f4SThomas Graf goto errout; 17561da177e4SLinus Torvalds 1757575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 175826932566SPatrick McHardy if (err < 0) { 175926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 176026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 176126932566SPatrick McHardy kfree_skb(skb); 176226932566SPatrick McHardy goto errout; 176326932566SPatrick McHardy } 17641ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 17651ce85fe4SPablo Neira Ayuso return; 17660ec6d3f4SThomas Graf errout: 17670ec6d3f4SThomas Graf if (err < 0) 17684b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 17721da177e4SLinus Torvalds static struct rtattr **rta_buf; 17731da177e4SLinus Torvalds static int rtattr_max; 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds /* Process one rtnetlink message. */ 17761da177e4SLinus Torvalds 17771d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 17781da177e4SLinus Torvalds { 17793b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1780e2849863SThomas Graf rtnl_doit_func doit; 17811da177e4SLinus Torvalds int sz_idx, kind; 17821da177e4SLinus Torvalds int min_len; 17831da177e4SLinus Torvalds int family; 17841da177e4SLinus Torvalds int type; 17851c2d670fSPatrick McHardy int err; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds type = nlh->nlmsg_type; 17881da177e4SLinus Torvalds if (type > RTM_MAX) 1789038890feSThomas Graf return -EOPNOTSUPP; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds type -= RTM_BASE; 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 17941da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 17951da177e4SLinus Torvalds return 0; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 17981da177e4SLinus Torvalds sz_idx = type>>2; 17991da177e4SLinus Torvalds kind = type&3; 18001da177e4SLinus Torvalds 18011d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 18021d00a4ebSThomas Graf return -EPERM; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 180597c53cacSDenis V. Lunev struct sock *rtnl; 1806e2849863SThomas Graf rtnl_dumpit_func dumpit; 18071da177e4SLinus Torvalds 1808e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1809e2849863SThomas Graf if (dumpit == NULL) 1810038890feSThomas Graf return -EOPNOTSUPP; 18111da177e4SLinus Torvalds 18121c2d670fSPatrick McHardy __rtnl_unlock(); 181397c53cacSDenis V. Lunev rtnl = net->rtnl; 18141c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 18151c2d670fSPatrick McHardy rtnl_lock(); 18161c2d670fSPatrick McHardy return err; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 18221da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 18231d00a4ebSThomas Graf return -EINVAL; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 18261da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 18271da177e4SLinus Torvalds struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 18301da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 18311da177e4SLinus Torvalds if (flavor) { 18321da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 18331d00a4ebSThomas Graf return -EINVAL; 18341da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1841e2849863SThomas Graf if (doit == NULL) 1842038890feSThomas Graf return -EOPNOTSUPP; 18431da177e4SLinus Torvalds 18441d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 1847cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 18481da177e4SLinus Torvalds { 18491536cc0dSDenis V. Lunev rtnl_lock(); 1850cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 18511536cc0dSDenis V. Lunev rtnl_unlock(); 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 18551da177e4SLinus Torvalds { 18561da177e4SLinus Torvalds struct net_device *dev = ptr; 1857e9dc8653SEric W. Biederman 18581da177e4SLinus Torvalds switch (event) { 18591da177e4SLinus Torvalds case NETDEV_UP: 18601da177e4SLinus Torvalds case NETDEV_DOWN: 186110de05afSPatrick McHardy case NETDEV_PRE_UP: 1862d90a909eSEric W. Biederman case NETDEV_POST_INIT: 1863d90a909eSEric W. Biederman case NETDEV_REGISTER: 18641da177e4SLinus Torvalds case NETDEV_CHANGE: 1865755d0e77SPatrick McHardy case NETDEV_PRE_TYPE_CHANGE: 18661da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 1867a2835763SPatrick McHardy case NETDEV_UNREGISTER: 1868d90a909eSEric W. Biederman case NETDEV_UNREGISTER_BATCH: 18691da177e4SLinus Torvalds break; 18701da177e4SLinus Torvalds default: 18711da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 18721da177e4SLinus Torvalds break; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds return NOTIFY_DONE; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 18781da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 18791da177e4SLinus Torvalds }; 18801da177e4SLinus Torvalds 188197c53cacSDenis V. Lunev 18822c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 188397c53cacSDenis V. Lunev { 188497c53cacSDenis V. Lunev struct sock *sk; 188597c53cacSDenis V. Lunev sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 188697c53cacSDenis V. Lunev rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 188797c53cacSDenis V. Lunev if (!sk) 188897c53cacSDenis V. Lunev return -ENOMEM; 188997c53cacSDenis V. Lunev net->rtnl = sk; 189097c53cacSDenis V. Lunev return 0; 189197c53cacSDenis V. Lunev } 189297c53cacSDenis V. Lunev 18932c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 189497c53cacSDenis V. Lunev { 1895b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 189697c53cacSDenis V. Lunev net->rtnl = NULL; 189797c53cacSDenis V. Lunev } 189897c53cacSDenis V. Lunev 189997c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 190097c53cacSDenis V. Lunev .init = rtnetlink_net_init, 190197c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 190297c53cacSDenis V. Lunev }; 190397c53cacSDenis V. Lunev 19041da177e4SLinus Torvalds void __init rtnetlink_init(void) 19051da177e4SLinus Torvalds { 19061da177e4SLinus Torvalds int i; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds rtattr_max = 0; 19091da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 19101da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 19111da177e4SLinus Torvalds rtattr_max = rta_max[i]; 19121da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 19131da177e4SLinus Torvalds if (!rta_buf) 19141da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 19151da177e4SLinus Torvalds 191697c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 19171da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 191897c53cacSDenis V. Lunev 19191da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 19201da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 1921340d17fcSThomas Graf 1922340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 1923340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 192438f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); 192538f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); 1926687ad8ccSThomas Graf 1927687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 1928687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 1931