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 9342bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO]; 94e2849863SThomas Graf 95e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 96e2849863SThomas Graf { 97e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 98e2849863SThomas Graf 99e2849863SThomas Graf /* 100e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 101e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 102e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 103e2849863SThomas Graf */ 104e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 105e2849863SThomas Graf 106e2849863SThomas Graf return msgindex; 107e2849863SThomas Graf } 108e2849863SThomas Graf 109e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 110e2849863SThomas Graf { 111e2849863SThomas Graf struct rtnl_link *tab; 112e2849863SThomas Graf 113e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 11451057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 115e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 116e2849863SThomas Graf 11751057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 118e2849863SThomas Graf } 119e2849863SThomas Graf 120e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 121e2849863SThomas Graf { 122e2849863SThomas Graf struct rtnl_link *tab; 123e2849863SThomas Graf 124e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 12551057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 126e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 127e2849863SThomas Graf 12851057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 129e2849863SThomas Graf } 130e2849863SThomas Graf 131e2849863SThomas Graf /** 132e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 133e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 134e2849863SThomas Graf * @msgtype: rtnetlink message type 135e2849863SThomas Graf * @doit: Function pointer called for each request message 136e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 137e2849863SThomas Graf * 138e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 139e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 140e2849863SThomas Graf * specified protocol family and message type is received. 141e2849863SThomas Graf * 142e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 143e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 144e2849863SThomas Graf * family exists. 145e2849863SThomas Graf * 146e2849863SThomas Graf * Returns 0 on success or a negative error code. 147e2849863SThomas Graf */ 148e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 149e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 150e2849863SThomas Graf { 151e2849863SThomas Graf struct rtnl_link *tab; 152e2849863SThomas Graf int msgindex; 153e2849863SThomas Graf 154e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 155e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 156e2849863SThomas Graf 157e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 158e2849863SThomas Graf if (tab == NULL) { 159e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 160e2849863SThomas Graf if (tab == NULL) 161e2849863SThomas Graf return -ENOBUFS; 162e2849863SThomas Graf 163e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 164e2849863SThomas Graf } 165e2849863SThomas Graf 166e2849863SThomas Graf if (doit) 167e2849863SThomas Graf tab[msgindex].doit = doit; 168e2849863SThomas Graf 169e2849863SThomas Graf if (dumpit) 170e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 171e2849863SThomas Graf 172e2849863SThomas Graf return 0; 173e2849863SThomas Graf } 174e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 175e2849863SThomas Graf 176e2849863SThomas Graf /** 177e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 178e2849863SThomas Graf * 179e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 180e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 181e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 182e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 183e2849863SThomas Graf * of memory implies no sense in continueing. 184e2849863SThomas Graf */ 185e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 186e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 187e2849863SThomas Graf { 188e2849863SThomas Graf if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) 189e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 190e2849863SThomas Graf "protocol = %d, message type = %d\n", 191e2849863SThomas Graf protocol, msgtype); 192e2849863SThomas Graf } 193e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 194e2849863SThomas Graf 195e2849863SThomas Graf /** 196e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 197e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 198e2849863SThomas Graf * @msgtype: rtnetlink message type 199e2849863SThomas Graf * 200e2849863SThomas Graf * Returns 0 on success or a negative error code. 201e2849863SThomas Graf */ 202e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 203e2849863SThomas Graf { 204e2849863SThomas Graf int msgindex; 205e2849863SThomas Graf 206e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 207e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 208e2849863SThomas Graf 209e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 210e2849863SThomas Graf return -ENOENT; 211e2849863SThomas Graf 212e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 213e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 214e2849863SThomas Graf 215e2849863SThomas Graf return 0; 216e2849863SThomas Graf } 217e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 218e2849863SThomas Graf 219e2849863SThomas Graf /** 220e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 221e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 222e2849863SThomas Graf * 223e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 224e2849863SThomas Graf * of a certain protocol family. 225e2849863SThomas Graf */ 226e2849863SThomas Graf void rtnl_unregister_all(int protocol) 227e2849863SThomas Graf { 228e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 229e2849863SThomas Graf 230e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 231e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 232e2849863SThomas Graf } 233e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2341da177e4SLinus Torvalds 23538f7b870SPatrick McHardy static LIST_HEAD(link_ops); 23638f7b870SPatrick McHardy 23738f7b870SPatrick McHardy /** 23838f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 23938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 24038f7b870SPatrick McHardy * 24138f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 24238f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 24338f7b870SPatrick McHardy * must be called before registering the devices. 24438f7b870SPatrick McHardy * 24538f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 24638f7b870SPatrick McHardy */ 24738f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 24838f7b870SPatrick McHardy { 2492d85cba2SPatrick McHardy if (!ops->dellink) 25023289a37SEric Dumazet ops->dellink = unregister_netdevice_queue; 2512d85cba2SPatrick McHardy 25238f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 25338f7b870SPatrick McHardy return 0; 25438f7b870SPatrick McHardy } 25538f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 25638f7b870SPatrick McHardy 25738f7b870SPatrick McHardy /** 25838f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 25938f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 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 { 26538f7b870SPatrick McHardy int err; 26638f7b870SPatrick McHardy 26738f7b870SPatrick McHardy rtnl_lock(); 26838f7b870SPatrick McHardy err = __rtnl_link_register(ops); 26938f7b870SPatrick McHardy rtnl_unlock(); 27038f7b870SPatrick McHardy return err; 27138f7b870SPatrick McHardy } 27238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 27338f7b870SPatrick McHardy 274669f87baSPavel Emelyanov static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 275669f87baSPavel Emelyanov { 276669f87baSPavel Emelyanov struct net_device *dev; 27723289a37SEric Dumazet LIST_HEAD(list_kill); 27823289a37SEric Dumazet 279669f87baSPavel Emelyanov for_each_netdev(net, dev) { 28023289a37SEric Dumazet if (dev->rtnl_link_ops == ops) 28123289a37SEric Dumazet ops->dellink(dev, &list_kill); 282669f87baSPavel Emelyanov } 28323289a37SEric Dumazet unregister_netdevice_many(&list_kill); 284669f87baSPavel Emelyanov } 285669f87baSPavel Emelyanov 286669f87baSPavel Emelyanov void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 287669f87baSPavel Emelyanov { 288669f87baSPavel Emelyanov rtnl_lock(); 289669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 290669f87baSPavel Emelyanov rtnl_unlock(); 291669f87baSPavel Emelyanov } 292669f87baSPavel Emelyanov EXPORT_SYMBOL_GPL(rtnl_kill_links); 293669f87baSPavel Emelyanov 29438f7b870SPatrick McHardy /** 29538f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 29638f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 29738f7b870SPatrick McHardy * 2982d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 29938f7b870SPatrick McHardy */ 30038f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 30138f7b870SPatrick McHardy { 302881d966bSEric W. Biederman struct net *net; 3032d85cba2SPatrick McHardy 304881d966bSEric W. Biederman for_each_net(net) { 305669f87baSPavel Emelyanov __rtnl_kill_links(net, ops); 306881d966bSEric W. Biederman } 30738f7b870SPatrick McHardy list_del(&ops->list); 30838f7b870SPatrick McHardy } 30938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 31038f7b870SPatrick McHardy 31138f7b870SPatrick McHardy /** 31238f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 31338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 31438f7b870SPatrick McHardy */ 31538f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 31638f7b870SPatrick McHardy { 31738f7b870SPatrick McHardy rtnl_lock(); 31838f7b870SPatrick McHardy __rtnl_link_unregister(ops); 31938f7b870SPatrick McHardy rtnl_unlock(); 32038f7b870SPatrick McHardy } 32138f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 32238f7b870SPatrick McHardy 32338f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 32438f7b870SPatrick McHardy { 32538f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 32638f7b870SPatrick McHardy 32738f7b870SPatrick McHardy list_for_each_entry(ops, &link_ops, list) { 32838f7b870SPatrick McHardy if (!strcmp(ops->kind, kind)) 32938f7b870SPatrick McHardy return ops; 33038f7b870SPatrick McHardy } 33138f7b870SPatrick McHardy return NULL; 33238f7b870SPatrick McHardy } 33338f7b870SPatrick McHardy 33438f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 33538f7b870SPatrick McHardy { 33638f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 33738f7b870SPatrick McHardy size_t size; 33838f7b870SPatrick McHardy 33938f7b870SPatrick McHardy if (!ops) 34038f7b870SPatrick McHardy return 0; 34138f7b870SPatrick McHardy 34238f7b870SPatrick McHardy size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 34338f7b870SPatrick McHardy nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 34438f7b870SPatrick McHardy 34538f7b870SPatrick McHardy if (ops->get_size) 34638f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 34738f7b870SPatrick McHardy size += nlmsg_total_size(sizeof(struct nlattr)) + 34838f7b870SPatrick McHardy ops->get_size(dev); 34938f7b870SPatrick McHardy 35038f7b870SPatrick McHardy if (ops->get_xstats_size) 35138f7b870SPatrick McHardy size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ 35238f7b870SPatrick McHardy 35338f7b870SPatrick McHardy return size; 35438f7b870SPatrick McHardy } 35538f7b870SPatrick McHardy 35638f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 35738f7b870SPatrick McHardy { 35838f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 35938f7b870SPatrick McHardy struct nlattr *linkinfo, *data; 36038f7b870SPatrick McHardy int err = -EMSGSIZE; 36138f7b870SPatrick McHardy 36238f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 36338f7b870SPatrick McHardy if (linkinfo == NULL) 36438f7b870SPatrick McHardy goto out; 36538f7b870SPatrick McHardy 36638f7b870SPatrick McHardy if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 36738f7b870SPatrick McHardy goto err_cancel_link; 36838f7b870SPatrick McHardy if (ops->fill_xstats) { 36938f7b870SPatrick McHardy err = ops->fill_xstats(skb, dev); 37038f7b870SPatrick McHardy if (err < 0) 37138f7b870SPatrick McHardy goto err_cancel_link; 37238f7b870SPatrick McHardy } 37338f7b870SPatrick McHardy if (ops->fill_info) { 37438f7b870SPatrick McHardy data = nla_nest_start(skb, IFLA_INFO_DATA); 37538f7b870SPatrick McHardy if (data == NULL) 37638f7b870SPatrick McHardy goto err_cancel_link; 37738f7b870SPatrick McHardy err = ops->fill_info(skb, dev); 37838f7b870SPatrick McHardy if (err < 0) 37938f7b870SPatrick McHardy goto err_cancel_data; 38038f7b870SPatrick McHardy nla_nest_end(skb, data); 38138f7b870SPatrick McHardy } 38238f7b870SPatrick McHardy 38338f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 38438f7b870SPatrick McHardy return 0; 38538f7b870SPatrick McHardy 38638f7b870SPatrick McHardy err_cancel_data: 38738f7b870SPatrick McHardy nla_nest_cancel(skb, data); 38838f7b870SPatrick McHardy err_cancel_link: 38938f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 39038f7b870SPatrick McHardy out: 39138f7b870SPatrick McHardy return err; 39238f7b870SPatrick McHardy } 39338f7b870SPatrick McHardy 394db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 3951da177e4SLinus Torvalds { 396f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 397f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 398f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 39914c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 400f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 401f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 402f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 403f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 404f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 405f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 4061da177e4SLinus Torvalds }; 4071da177e4SLinus Torvalds 408db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 4091da177e4SLinus Torvalds { 410f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 411f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 412f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 41314c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 414f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 415f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 416f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 417f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 4181da177e4SLinus Torvalds }; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 4211da177e4SLinus Torvalds { 4221da177e4SLinus Torvalds struct rtattr *rta; 4231da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); 4261da177e4SLinus Torvalds rta->rta_type = attrtype; 4271da177e4SLinus Torvalds rta->rta_len = size; 4281da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 429b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 4301da177e4SLinus Torvalds } 431e0d087afSEric Dumazet EXPORT_SYMBOL(__rta_fill); 4321da177e4SLinus Torvalds 43397c53cacSDenis V. Lunev int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) 4341da177e4SLinus Torvalds { 43597c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 4361da177e4SLinus Torvalds int err = 0; 4371da177e4SLinus Torvalds 438ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 4391da177e4SLinus Torvalds if (echo) 4401da177e4SLinus Torvalds atomic_inc(&skb->users); 4411da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 4421da177e4SLinus Torvalds if (echo) 4431da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 4441da177e4SLinus Torvalds return err; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 44797c53cacSDenis V. Lunev int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 4482942e900SThomas Graf { 44997c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 45097c53cacSDenis V. Lunev 4512942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 4522942e900SThomas Graf } 453e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_unicast); 4542942e900SThomas Graf 4551ce85fe4SPablo Neira Ayuso void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 45697676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 45797676b6bSThomas Graf { 45897c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 45997676b6bSThomas Graf int report = 0; 46097676b6bSThomas Graf 46197676b6bSThomas Graf if (nlh) 46297676b6bSThomas Graf report = nlmsg_report(nlh); 46397676b6bSThomas Graf 4641ce85fe4SPablo Neira Ayuso nlmsg_notify(rtnl, skb, pid, group, report, flags); 46597676b6bSThomas Graf } 466e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_notify); 46797676b6bSThomas Graf 46897c53cacSDenis V. Lunev void rtnl_set_sk_err(struct net *net, u32 group, int error) 46997676b6bSThomas Graf { 47097c53cacSDenis V. Lunev struct sock *rtnl = net->rtnl; 47197c53cacSDenis V. Lunev 47297676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 47397676b6bSThomas Graf } 474e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_set_sk_err); 47597676b6bSThomas Graf 4761da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 4771da177e4SLinus Torvalds { 4782d7202bfSThomas Graf struct nlattr *mx; 4792d7202bfSThomas Graf int i, valid = 0; 4801da177e4SLinus Torvalds 4812d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 4822d7202bfSThomas Graf if (mx == NULL) 4832d7202bfSThomas Graf return -ENOBUFS; 4842d7202bfSThomas Graf 4851da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 4862d7202bfSThomas Graf if (metrics[i]) { 4872d7202bfSThomas Graf valid++; 4882d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 4891da177e4SLinus Torvalds } 4902d7202bfSThomas Graf } 4911da177e4SLinus Torvalds 492a57d27fcSDavid S. Miller if (!valid) { 493a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 494a57d27fcSDavid S. Miller return 0; 495a57d27fcSDavid S. Miller } 4962d7202bfSThomas Graf 4972d7202bfSThomas Graf return nla_nest_end(skb, mx); 4982d7202bfSThomas Graf 4992d7202bfSThomas Graf nla_put_failure: 500bc3ed28cSThomas Graf nla_nest_cancel(skb, mx); 501bc3ed28cSThomas Graf return -EMSGSIZE; 5021da177e4SLinus Torvalds } 503e0d087afSEric Dumazet EXPORT_SYMBOL(rtnetlink_put_metrics); 5041da177e4SLinus Torvalds 505e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 506e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 507e3703b3dSThomas Graf { 508e3703b3dSThomas Graf struct rta_cacheinfo ci = { 509e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 510e3703b3dSThomas Graf .rta_used = dst->__use, 511e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 512e3703b3dSThomas Graf .rta_error = error, 513e3703b3dSThomas Graf .rta_id = id, 514e3703b3dSThomas Graf .rta_ts = ts, 515e3703b3dSThomas Graf .rta_tsage = tsage, 516e3703b3dSThomas Graf }; 517e3703b3dSThomas Graf 518e3703b3dSThomas Graf if (expires) 519e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 520e3703b3dSThomas Graf 521e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 522e3703b3dSThomas Graf } 523e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 5241da177e4SLinus Torvalds 52593b2d4a2SDavid S. Miller static void set_operstate(struct net_device *dev, unsigned char transition) 526b00055aaSStefan Rompf { 527b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 528b00055aaSStefan Rompf 529b00055aaSStefan Rompf switch (transition) { 530b00055aaSStefan Rompf case IF_OPER_UP: 531b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 532b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 533b00055aaSStefan Rompf !netif_dormant(dev)) 534b00055aaSStefan Rompf operstate = IF_OPER_UP; 535b00055aaSStefan Rompf break; 536b00055aaSStefan Rompf 537b00055aaSStefan Rompf case IF_OPER_DORMANT: 538b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 539b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 540b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 541b00055aaSStefan Rompf break; 5423ff50b79SStephen Hemminger } 543b00055aaSStefan Rompf 544b00055aaSStefan Rompf if (dev->operstate != operstate) { 545b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 546b00055aaSStefan Rompf dev->operstate = operstate; 547b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 548b00055aaSStefan Rompf netdev_state_change(dev); 54993b2d4a2SDavid S. Miller } 550b00055aaSStefan Rompf } 551b00055aaSStefan Rompf 552*3729d502SPatrick McHardy static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 553*3729d502SPatrick McHardy const struct ifinfomsg *ifm) 554*3729d502SPatrick McHardy { 555*3729d502SPatrick McHardy unsigned int flags = ifm->ifi_flags; 556*3729d502SPatrick McHardy 557*3729d502SPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 558*3729d502SPatrick McHardy if (ifm->ifi_change) 559*3729d502SPatrick McHardy flags = (flags & ifm->ifi_change) | 560*3729d502SPatrick McHardy (dev->flags & ~ifm->ifi_change); 561*3729d502SPatrick McHardy 562*3729d502SPatrick McHardy return flags; 563*3729d502SPatrick McHardy } 564*3729d502SPatrick McHardy 565b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 566eeda3fd6SStephen Hemminger const struct net_device_stats *b) 5671da177e4SLinus Torvalds { 568b60c5115SThomas Graf a->rx_packets = b->rx_packets; 569b60c5115SThomas Graf a->tx_packets = b->tx_packets; 570b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 571b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 572b60c5115SThomas Graf a->rx_errors = b->rx_errors; 573b60c5115SThomas Graf a->tx_errors = b->tx_errors; 574b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 575b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 576b60c5115SThomas Graf 577b60c5115SThomas Graf a->multicast = b->multicast; 578b60c5115SThomas Graf a->collisions = b->collisions; 579b60c5115SThomas Graf 580b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 581b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 582b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 583b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 584b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 585b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 586b60c5115SThomas Graf 587b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 588b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 589b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 590b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 591b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 592b60c5115SThomas Graf 593b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 594b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 595b60c5115SThomas Graf }; 596b60c5115SThomas Graf 597ebc08a6fSWilliams, Mitch A static inline int rtnl_vfinfo_size(const struct net_device *dev) 598ebc08a6fSWilliams, Mitch A { 599ebc08a6fSWilliams, Mitch A if (dev->dev.parent && dev_is_pci(dev->dev.parent)) 600ebc08a6fSWilliams, Mitch A return dev_num_vf(dev->dev.parent) * 601ebc08a6fSWilliams, Mitch A sizeof(struct ifla_vf_info); 602ebc08a6fSWilliams, Mitch A else 603ebc08a6fSWilliams, Mitch A return 0; 604ebc08a6fSWilliams, Mitch A } 605ebc08a6fSWilliams, Mitch A 60638f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev) 607339bf98fSThomas Graf { 608339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 609339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 6100b815a1aSStephen Hemminger + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 611339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 612339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 613339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 614339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 615339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 616339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 617339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 618339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 619339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 620339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 621339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 62238f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 623ebc08a6fSWilliams, Mitch A + nla_total_size(4) /* IFLA_NUM_VF */ 624ebc08a6fSWilliams, Mitch A + nla_total_size(rtnl_vfinfo_size(dev)) /* IFLA_VFINFO */ 62538f7b870SPatrick McHardy + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ 626339bf98fSThomas Graf } 627339bf98fSThomas Graf 628b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 629575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 630575c3e2aSPatrick McHardy unsigned int flags) 631b60c5115SThomas Graf { 632b60c5115SThomas Graf struct ifinfomsg *ifm; 6331da177e4SLinus Torvalds struct nlmsghdr *nlh; 634eeda3fd6SStephen Hemminger const struct net_device_stats *stats; 63596e74088SPavel Emelyanov struct nlattr *attr; 6361da177e4SLinus Torvalds 637b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 638b60c5115SThomas Graf if (nlh == NULL) 63926932566SPatrick McHardy return -EMSGSIZE; 6401da177e4SLinus Torvalds 641b60c5115SThomas Graf ifm = nlmsg_data(nlh); 642b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 643b60c5115SThomas Graf ifm->__ifi_pad = 0; 644b60c5115SThomas Graf ifm->ifi_type = dev->type; 645b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 646b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 647b60c5115SThomas Graf ifm->ifi_change = change; 6481da177e4SLinus Torvalds 649b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 650b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 651b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 652b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 653b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 654b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 6551da177e4SLinus Torvalds 656b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 657b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 6581da177e4SLinus Torvalds 659b60c5115SThomas Graf if (dev->master) 660b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 661b60c5115SThomas Graf 662af356afaSPatrick McHardy if (dev->qdisc) 663af356afaSPatrick McHardy NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 664b00055aaSStefan Rompf 6650b815a1aSStephen Hemminger if (dev->ifalias) 6660b815a1aSStephen Hemminger NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 6670b815a1aSStephen Hemminger 668b00055aaSStefan Rompf if (1) { 6691da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 6701da177e4SLinus Torvalds .mem_start = dev->mem_start, 6711da177e4SLinus Torvalds .mem_end = dev->mem_end, 6721da177e4SLinus Torvalds .base_addr = dev->base_addr, 6731da177e4SLinus Torvalds .irq = dev->irq, 6741da177e4SLinus Torvalds .dma = dev->dma, 6751da177e4SLinus Torvalds .port = dev->if_port, 6761da177e4SLinus Torvalds }; 677b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds if (dev->addr_len) { 681b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 682b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 685b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 686b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 687b60c5115SThomas Graf if (attr == NULL) 688b60c5115SThomas Graf goto nla_put_failure; 689b60c5115SThomas Graf 690eeda3fd6SStephen Hemminger stats = dev_get_stats(dev); 691b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 6921da177e4SLinus Torvalds 693ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) { 694ebc08a6fSWilliams, Mitch A int i; 695ebc08a6fSWilliams, Mitch A struct ifla_vf_info ivi; 696ebc08a6fSWilliams, Mitch A 697ebc08a6fSWilliams, Mitch A NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 698ebc08a6fSWilliams, Mitch A for (i = 0; i < dev_num_vf(dev->dev.parent); i++) { 699ebc08a6fSWilliams, Mitch A if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 700ebc08a6fSWilliams, Mitch A break; 701ebc08a6fSWilliams, Mitch A NLA_PUT(skb, IFLA_VFINFO, sizeof(ivi), &ivi); 702ebc08a6fSWilliams, Mitch A } 703ebc08a6fSWilliams, Mitch A } 70438f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 70538f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 70638f7b870SPatrick McHardy goto nla_put_failure; 70738f7b870SPatrick McHardy } 70838f7b870SPatrick McHardy 709b60c5115SThomas Graf return nlmsg_end(skb, nlh); 710b60c5115SThomas Graf 711b60c5115SThomas Graf nla_put_failure: 71226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 71326932566SPatrick McHardy return -EMSGSIZE; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 7171da177e4SLinus Torvalds { 7183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 7197c28bd0bSEric Dumazet int h, s_h; 7207c28bd0bSEric Dumazet int idx = 0, s_idx; 7211da177e4SLinus Torvalds struct net_device *dev; 7227c28bd0bSEric Dumazet struct hlist_head *head; 7237c28bd0bSEric Dumazet struct hlist_node *node; 7241da177e4SLinus Torvalds 7257c28bd0bSEric Dumazet s_h = cb->args[0]; 7267c28bd0bSEric Dumazet s_idx = cb->args[1]; 7277c28bd0bSEric Dumazet 7287c28bd0bSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 7297562f876SPavel Emelianov idx = 0; 7307c28bd0bSEric Dumazet head = &net->dev_index_head[h]; 7317c28bd0bSEric Dumazet hlist_for_each_entry(dev, node, head, index_hlist) { 7321da177e4SLinus Torvalds if (idx < s_idx) 7337562f876SPavel Emelianov goto cont; 734575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 735b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 7367c28bd0bSEric Dumazet cb->nlh->nlmsg_seq, 0, 7377c28bd0bSEric Dumazet NLM_F_MULTI) <= 0) 7387c28bd0bSEric Dumazet goto out; 7397562f876SPavel Emelianov cont: 7407562f876SPavel Emelianov idx++; 7411da177e4SLinus Torvalds } 7427c28bd0bSEric Dumazet } 7437c28bd0bSEric Dumazet out: 7447c28bd0bSEric Dumazet cb->args[1] = idx; 7457c28bd0bSEric Dumazet cb->args[0] = h; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds return skb->len; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 750e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 7515176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 75238f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 75338f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 7545176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 755da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 75676e87306SThomas Graf [IFLA_LINK] = { .type = NLA_U32 }, 757da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 758da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 759da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 760da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 76176e87306SThomas Graf [IFLA_LINKINFO] = { .type = NLA_NESTED }, 762d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 7630b815a1aSStephen Hemminger [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 764ebc08a6fSWilliams, Mitch A [IFLA_VF_MAC] = { .type = NLA_BINARY, 765ebc08a6fSWilliams, Mitch A .len = sizeof(struct ifla_vf_mac) }, 766ebc08a6fSWilliams, Mitch A [IFLA_VF_VLAN] = { .type = NLA_BINARY, 767ebc08a6fSWilliams, Mitch A .len = sizeof(struct ifla_vf_vlan) }, 768ebc08a6fSWilliams, Mitch A [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 769ebc08a6fSWilliams, Mitch A .len = sizeof(struct ifla_vf_tx_rate) }, 770da5e0494SThomas Graf }; 771e0d087afSEric Dumazet EXPORT_SYMBOL(ifla_policy); 7721da177e4SLinus Torvalds 77338f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 77438f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 77538f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 77638f7b870SPatrick McHardy }; 77738f7b870SPatrick McHardy 77881adee47SEric W. Biederman struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 77981adee47SEric W. Biederman { 78081adee47SEric W. Biederman struct net *net; 78181adee47SEric W. Biederman /* Examine the link attributes and figure out which 78281adee47SEric W. Biederman * network namespace we are talking about. 78381adee47SEric W. Biederman */ 78481adee47SEric W. Biederman if (tb[IFLA_NET_NS_PID]) 78581adee47SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 78681adee47SEric W. Biederman else 78781adee47SEric W. Biederman net = get_net(src_net); 78881adee47SEric W. Biederman return net; 78981adee47SEric W. Biederman } 79081adee47SEric W. Biederman EXPORT_SYMBOL(rtnl_link_get_net); 79181adee47SEric W. Biederman 7921840bb13SThomas Graf static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 7931840bb13SThomas Graf { 7941840bb13SThomas Graf if (dev) { 7951840bb13SThomas Graf if (tb[IFLA_ADDRESS] && 7961840bb13SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 7971840bb13SThomas Graf return -EINVAL; 7981840bb13SThomas Graf 7991840bb13SThomas Graf if (tb[IFLA_BROADCAST] && 8001840bb13SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 8011840bb13SThomas Graf return -EINVAL; 8021840bb13SThomas Graf } 8031840bb13SThomas Graf 8041840bb13SThomas Graf return 0; 8051840bb13SThomas Graf } 8061840bb13SThomas Graf 8070157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 80838f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 8090157f60cSPatrick McHardy { 810d314774cSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 81138f7b870SPatrick McHardy int send_addr_notify = 0; 8120157f60cSPatrick McHardy int err; 8130157f60cSPatrick McHardy 814d8a5ec67SEric W. Biederman if (tb[IFLA_NET_NS_PID]) { 81581adee47SEric W. Biederman struct net *net = rtnl_link_get_net(dev_net(dev), tb); 816d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 817d8a5ec67SEric W. Biederman err = PTR_ERR(net); 818d8a5ec67SEric W. Biederman goto errout; 819d8a5ec67SEric W. Biederman } 820d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 821d8a5ec67SEric W. Biederman put_net(net); 822d8a5ec67SEric W. Biederman if (err) 823d8a5ec67SEric W. Biederman goto errout; 824d8a5ec67SEric W. Biederman modified = 1; 825d8a5ec67SEric W. Biederman } 826d8a5ec67SEric W. Biederman 8270157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 8280157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 8290157f60cSPatrick McHardy struct ifmap k_map; 8300157f60cSPatrick McHardy 831d314774cSStephen Hemminger if (!ops->ndo_set_config) { 8320157f60cSPatrick McHardy err = -EOPNOTSUPP; 8330157f60cSPatrick McHardy goto errout; 8340157f60cSPatrick McHardy } 8350157f60cSPatrick McHardy 8360157f60cSPatrick McHardy if (!netif_device_present(dev)) { 8370157f60cSPatrick McHardy err = -ENODEV; 8380157f60cSPatrick McHardy goto errout; 8390157f60cSPatrick McHardy } 8400157f60cSPatrick McHardy 8410157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 8420157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 8430157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 8440157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 8450157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 8460157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 8470157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 8480157f60cSPatrick McHardy 849d314774cSStephen Hemminger err = ops->ndo_set_config(dev, &k_map); 8500157f60cSPatrick McHardy if (err < 0) 8510157f60cSPatrick McHardy goto errout; 8520157f60cSPatrick McHardy 8530157f60cSPatrick McHardy modified = 1; 8540157f60cSPatrick McHardy } 8550157f60cSPatrick McHardy 8560157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 8570157f60cSPatrick McHardy struct sockaddr *sa; 8580157f60cSPatrick McHardy int len; 8590157f60cSPatrick McHardy 860d314774cSStephen Hemminger if (!ops->ndo_set_mac_address) { 8610157f60cSPatrick McHardy err = -EOPNOTSUPP; 8620157f60cSPatrick McHardy goto errout; 8630157f60cSPatrick McHardy } 8640157f60cSPatrick McHardy 8650157f60cSPatrick McHardy if (!netif_device_present(dev)) { 8660157f60cSPatrick McHardy err = -ENODEV; 8670157f60cSPatrick McHardy goto errout; 8680157f60cSPatrick McHardy } 8690157f60cSPatrick McHardy 8700157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 8710157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 8720157f60cSPatrick McHardy if (!sa) { 8730157f60cSPatrick McHardy err = -ENOMEM; 8740157f60cSPatrick McHardy goto errout; 8750157f60cSPatrick McHardy } 8760157f60cSPatrick McHardy sa->sa_family = dev->type; 8770157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 8780157f60cSPatrick McHardy dev->addr_len); 879d314774cSStephen Hemminger err = ops->ndo_set_mac_address(dev, sa); 8800157f60cSPatrick McHardy kfree(sa); 8810157f60cSPatrick McHardy if (err) 8820157f60cSPatrick McHardy goto errout; 8830157f60cSPatrick McHardy send_addr_notify = 1; 8840157f60cSPatrick McHardy modified = 1; 8850157f60cSPatrick McHardy } 8860157f60cSPatrick McHardy 8870157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 8880157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 8890157f60cSPatrick McHardy if (err < 0) 8900157f60cSPatrick McHardy goto errout; 8910157f60cSPatrick McHardy modified = 1; 8920157f60cSPatrick McHardy } 8930157f60cSPatrick McHardy 8940157f60cSPatrick McHardy /* 8950157f60cSPatrick McHardy * Interface selected by interface index but interface 8960157f60cSPatrick McHardy * name provided implies that a name change has been 8970157f60cSPatrick McHardy * requested. 8980157f60cSPatrick McHardy */ 8990157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 9000157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 9010157f60cSPatrick McHardy if (err < 0) 9020157f60cSPatrick McHardy goto errout; 9030157f60cSPatrick McHardy modified = 1; 9040157f60cSPatrick McHardy } 9050157f60cSPatrick McHardy 9060b815a1aSStephen Hemminger if (tb[IFLA_IFALIAS]) { 9070b815a1aSStephen Hemminger err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 9080b815a1aSStephen Hemminger nla_len(tb[IFLA_IFALIAS])); 9090b815a1aSStephen Hemminger if (err < 0) 9100b815a1aSStephen Hemminger goto errout; 9110b815a1aSStephen Hemminger modified = 1; 9120b815a1aSStephen Hemminger } 9130b815a1aSStephen Hemminger 9140157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 9150157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 9160157f60cSPatrick McHardy send_addr_notify = 1; 9170157f60cSPatrick McHardy } 9180157f60cSPatrick McHardy 9190157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 920*3729d502SPatrick McHardy err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 9215f9021cfSJohannes Berg if (err < 0) 9225f9021cfSJohannes Berg goto errout; 9230157f60cSPatrick McHardy } 9240157f60cSPatrick McHardy 92593b2d4a2SDavid S. Miller if (tb[IFLA_TXQLEN]) 9260157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 9270157f60cSPatrick McHardy 9280157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 92993b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 9300157f60cSPatrick McHardy 9310157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 9320157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 9330157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 93493b2d4a2SDavid S. Miller write_unlock_bh(&dev_base_lock); 9350157f60cSPatrick McHardy } 9360157f60cSPatrick McHardy 937ebc08a6fSWilliams, Mitch A if (tb[IFLA_VF_MAC]) { 938ebc08a6fSWilliams, Mitch A struct ifla_vf_mac *ivm; 939ebc08a6fSWilliams, Mitch A ivm = nla_data(tb[IFLA_VF_MAC]); 9404edb2466SWilliams, Mitch A err = -EOPNOTSUPP; 941ebc08a6fSWilliams, Mitch A if (ops->ndo_set_vf_mac) 942ebc08a6fSWilliams, Mitch A err = ops->ndo_set_vf_mac(dev, ivm->vf, ivm->mac); 943ebc08a6fSWilliams, Mitch A if (err < 0) 944ebc08a6fSWilliams, Mitch A goto errout; 945ebc08a6fSWilliams, Mitch A modified = 1; 946ebc08a6fSWilliams, Mitch A } 947ebc08a6fSWilliams, Mitch A 948ebc08a6fSWilliams, Mitch A if (tb[IFLA_VF_VLAN]) { 949ebc08a6fSWilliams, Mitch A struct ifla_vf_vlan *ivv; 950ebc08a6fSWilliams, Mitch A ivv = nla_data(tb[IFLA_VF_VLAN]); 9514edb2466SWilliams, Mitch A err = -EOPNOTSUPP; 952ebc08a6fSWilliams, Mitch A if (ops->ndo_set_vf_vlan) 953ebc08a6fSWilliams, Mitch A err = ops->ndo_set_vf_vlan(dev, ivv->vf, 9544edb2466SWilliams, Mitch A ivv->vlan, 9554edb2466SWilliams, Mitch A ivv->qos); 956ebc08a6fSWilliams, Mitch A if (err < 0) 957ebc08a6fSWilliams, Mitch A goto errout; 958ebc08a6fSWilliams, Mitch A modified = 1; 959ebc08a6fSWilliams, Mitch A } 960ebc08a6fSWilliams, Mitch A err = 0; 961ebc08a6fSWilliams, Mitch A 962ebc08a6fSWilliams, Mitch A if (tb[IFLA_VF_TX_RATE]) { 963ebc08a6fSWilliams, Mitch A struct ifla_vf_tx_rate *ivt; 964ebc08a6fSWilliams, Mitch A ivt = nla_data(tb[IFLA_VF_TX_RATE]); 9654edb2466SWilliams, Mitch A err = -EOPNOTSUPP; 966ebc08a6fSWilliams, Mitch A if (ops->ndo_set_vf_tx_rate) 967ebc08a6fSWilliams, Mitch A err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, ivt->rate); 968ebc08a6fSWilliams, Mitch A if (err < 0) 969ebc08a6fSWilliams, Mitch A goto errout; 970ebc08a6fSWilliams, Mitch A modified = 1; 971ebc08a6fSWilliams, Mitch A } 9720157f60cSPatrick McHardy err = 0; 9730157f60cSPatrick McHardy 9740157f60cSPatrick McHardy errout: 9750157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 9760157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 9770157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 9780157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 9790157f60cSPatrick McHardy "please check.\n", dev->name); 9800157f60cSPatrick McHardy 9810157f60cSPatrick McHardy if (send_addr_notify) 9820157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 9830157f60cSPatrick McHardy return err; 9840157f60cSPatrick McHardy } 9850157f60cSPatrick McHardy 986da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 987da5e0494SThomas Graf { 9883b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 989da5e0494SThomas Graf struct ifinfomsg *ifm; 990da5e0494SThomas Graf struct net_device *dev; 9910157f60cSPatrick McHardy int err; 992da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 9931da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 9941da177e4SLinus Torvalds 995da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 996da5e0494SThomas Graf if (err < 0) 997da5e0494SThomas Graf goto errout; 9981da177e4SLinus Torvalds 9995176f91eSThomas Graf if (tb[IFLA_IFNAME]) 10005176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 100178e5b891SPatrick McHardy else 100278e5b891SPatrick McHardy ifname[0] = '\0'; 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds err = -EINVAL; 1005da5e0494SThomas Graf ifm = nlmsg_data(nlh); 100651055be8SPatrick McHardy if (ifm->ifi_index > 0) 1007a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1008da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 1009a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1010da5e0494SThomas Graf else 1011da5e0494SThomas Graf goto errout; 10121da177e4SLinus Torvalds 1013da5e0494SThomas Graf if (dev == NULL) { 1014da5e0494SThomas Graf err = -ENODEV; 1015da5e0494SThomas Graf goto errout; 1016da5e0494SThomas Graf } 10171da177e4SLinus Torvalds 1018e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1019e0d087afSEric Dumazet if (err < 0) 1020a3d12891SEric Dumazet goto errout; 1021da5e0494SThomas Graf 102238f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 1023da5e0494SThomas Graf errout: 10241da177e4SLinus Torvalds return err; 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 102738f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 102838f7b870SPatrick McHardy { 10293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 103038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 103138f7b870SPatrick McHardy struct net_device *dev; 103238f7b870SPatrick McHardy struct ifinfomsg *ifm; 103338f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 103438f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 103538f7b870SPatrick McHardy int err; 103638f7b870SPatrick McHardy 103738f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 103838f7b870SPatrick McHardy if (err < 0) 103938f7b870SPatrick McHardy return err; 104038f7b870SPatrick McHardy 104138f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 104238f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 104338f7b870SPatrick McHardy 104438f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 104538f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1046881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 104738f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 1048881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 104938f7b870SPatrick McHardy else 105038f7b870SPatrick McHardy return -EINVAL; 105138f7b870SPatrick McHardy 105238f7b870SPatrick McHardy if (!dev) 105338f7b870SPatrick McHardy return -ENODEV; 105438f7b870SPatrick McHardy 105538f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 105638f7b870SPatrick McHardy if (!ops) 105738f7b870SPatrick McHardy return -EOPNOTSUPP; 105838f7b870SPatrick McHardy 105923289a37SEric Dumazet ops->dellink(dev, NULL); 106038f7b870SPatrick McHardy return 0; 106138f7b870SPatrick McHardy } 106238f7b870SPatrick McHardy 1063*3729d502SPatrick McHardy int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 1064*3729d502SPatrick McHardy { 1065*3729d502SPatrick McHardy unsigned int old_flags; 1066*3729d502SPatrick McHardy int err; 1067*3729d502SPatrick McHardy 1068*3729d502SPatrick McHardy old_flags = dev->flags; 1069*3729d502SPatrick McHardy if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 1070*3729d502SPatrick McHardy err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 1071*3729d502SPatrick McHardy if (err < 0) 1072*3729d502SPatrick McHardy return err; 1073*3729d502SPatrick McHardy } 1074*3729d502SPatrick McHardy 1075*3729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 1076*3729d502SPatrick McHardy rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 1077*3729d502SPatrick McHardy 1078*3729d502SPatrick McHardy __dev_notify_flags(dev, old_flags); 1079*3729d502SPatrick McHardy return 0; 1080*3729d502SPatrick McHardy } 1081*3729d502SPatrick McHardy EXPORT_SYMBOL(rtnl_configure_link); 1082*3729d502SPatrick McHardy 108381adee47SEric W. Biederman struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 108481adee47SEric W. Biederman char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1085e7199288SPavel Emelianov { 1086e7199288SPavel Emelianov int err; 1087e7199288SPavel Emelianov struct net_device *dev; 10882e59af3dSEric Dumazet unsigned int num_queues = 1; 10892e59af3dSEric Dumazet unsigned int real_num_queues = 1; 1090e7199288SPavel Emelianov 10912e59af3dSEric Dumazet if (ops->get_tx_queues) { 109281adee47SEric W. Biederman err = ops->get_tx_queues(src_net, tb, &num_queues, 1093e0d087afSEric Dumazet &real_num_queues); 10942e59af3dSEric Dumazet if (err) 10952e59af3dSEric Dumazet goto err; 10962e59af3dSEric Dumazet } 1097e7199288SPavel Emelianov err = -ENOMEM; 10982e59af3dSEric Dumazet dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1099e7199288SPavel Emelianov if (!dev) 1100e7199288SPavel Emelianov goto err; 1101e7199288SPavel Emelianov 110281adee47SEric W. Biederman dev_net_set(dev, net); 110381adee47SEric W. Biederman dev->rtnl_link_ops = ops; 1104*3729d502SPatrick McHardy dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 11052e59af3dSEric Dumazet dev->real_num_tx_queues = real_num_queues; 110681adee47SEric W. Biederman 1107e7199288SPavel Emelianov if (strchr(dev->name, '%')) { 1108e7199288SPavel Emelianov err = dev_alloc_name(dev, dev->name); 1109e7199288SPavel Emelianov if (err < 0) 1110e7199288SPavel Emelianov goto err_free; 1111e7199288SPavel Emelianov } 1112e7199288SPavel Emelianov 1113e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1114e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1115e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1116e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1117e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1118e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1119e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1120e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1121e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1122e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1123e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 112493b2d4a2SDavid S. Miller set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1125e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1126e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1127e7199288SPavel Emelianov 1128e7199288SPavel Emelianov return dev; 1129e7199288SPavel Emelianov 1130e7199288SPavel Emelianov err_free: 1131e7199288SPavel Emelianov free_netdev(dev); 1132e7199288SPavel Emelianov err: 1133e7199288SPavel Emelianov return ERR_PTR(err); 1134e7199288SPavel Emelianov } 1135e0d087afSEric Dumazet EXPORT_SYMBOL(rtnl_create_link); 1136e7199288SPavel Emelianov 113738f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 113838f7b870SPatrick McHardy { 11393b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 114038f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 114138f7b870SPatrick McHardy struct net_device *dev; 114238f7b870SPatrick McHardy struct ifinfomsg *ifm; 114338f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 114438f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 114538f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 114638f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 114738f7b870SPatrick McHardy int err; 114838f7b870SPatrick McHardy 114995a5afcaSJohannes Berg #ifdef CONFIG_MODULES 115038f7b870SPatrick McHardy replay: 11518072f085SThomas Graf #endif 115238f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 115338f7b870SPatrick McHardy if (err < 0) 115438f7b870SPatrick McHardy return err; 115538f7b870SPatrick McHardy 115638f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 115738f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 115838f7b870SPatrick McHardy else 115938f7b870SPatrick McHardy ifname[0] = '\0'; 116038f7b870SPatrick McHardy 116138f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 116238f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1163881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 116438f7b870SPatrick McHardy else if (ifname[0]) 1165881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 116638f7b870SPatrick McHardy else 116738f7b870SPatrick McHardy dev = NULL; 116838f7b870SPatrick McHardy 1169e0d087afSEric Dumazet err = validate_linkmsg(dev, tb); 1170e0d087afSEric Dumazet if (err < 0) 11711840bb13SThomas Graf return err; 11721840bb13SThomas Graf 117338f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 117438f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 117538f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 117638f7b870SPatrick McHardy if (err < 0) 117738f7b870SPatrick McHardy return err; 117838f7b870SPatrick McHardy } else 117938f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 118038f7b870SPatrick McHardy 118138f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 118238f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 118338f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 118438f7b870SPatrick McHardy } else { 118538f7b870SPatrick McHardy kind[0] = '\0'; 118638f7b870SPatrick McHardy ops = NULL; 118738f7b870SPatrick McHardy } 118838f7b870SPatrick McHardy 118938f7b870SPatrick McHardy if (1) { 119038f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 119181adee47SEric W. Biederman struct net *dest_net; 119238f7b870SPatrick McHardy 119338f7b870SPatrick McHardy if (ops) { 119438f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 119538f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 119638f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 119738f7b870SPatrick McHardy ops->policy); 119838f7b870SPatrick McHardy if (err < 0) 119938f7b870SPatrick McHardy return err; 120038f7b870SPatrick McHardy data = attr; 120138f7b870SPatrick McHardy } 120238f7b870SPatrick McHardy if (ops->validate) { 120338f7b870SPatrick McHardy err = ops->validate(tb, data); 120438f7b870SPatrick McHardy if (err < 0) 120538f7b870SPatrick McHardy return err; 120638f7b870SPatrick McHardy } 120738f7b870SPatrick McHardy } 120838f7b870SPatrick McHardy 120938f7b870SPatrick McHardy if (dev) { 121038f7b870SPatrick McHardy int modified = 0; 121138f7b870SPatrick McHardy 121238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 121338f7b870SPatrick McHardy return -EEXIST; 121438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 121538f7b870SPatrick McHardy return -EOPNOTSUPP; 121638f7b870SPatrick McHardy 121738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 121838f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 121938f7b870SPatrick McHardy !ops->changelink) 122038f7b870SPatrick McHardy return -EOPNOTSUPP; 122138f7b870SPatrick McHardy 122238f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 122338f7b870SPatrick McHardy if (err < 0) 122438f7b870SPatrick McHardy return err; 122538f7b870SPatrick McHardy modified = 1; 122638f7b870SPatrick McHardy } 122738f7b870SPatrick McHardy 122838f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 122938f7b870SPatrick McHardy } 123038f7b870SPatrick McHardy 123138f7b870SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) 123238f7b870SPatrick McHardy return -ENODEV; 123338f7b870SPatrick McHardy 1234*3729d502SPatrick McHardy if (ifm->ifi_index) 123538f7b870SPatrick McHardy return -EOPNOTSUPP; 12360e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 123738f7b870SPatrick McHardy return -EOPNOTSUPP; 123838f7b870SPatrick McHardy 123938f7b870SPatrick McHardy if (!ops) { 124095a5afcaSJohannes Berg #ifdef CONFIG_MODULES 124138f7b870SPatrick McHardy if (kind[0]) { 124238f7b870SPatrick McHardy __rtnl_unlock(); 124338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 124438f7b870SPatrick McHardy rtnl_lock(); 124538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 124638f7b870SPatrick McHardy if (ops) 124738f7b870SPatrick McHardy goto replay; 124838f7b870SPatrick McHardy } 124938f7b870SPatrick McHardy #endif 125038f7b870SPatrick McHardy return -EOPNOTSUPP; 125138f7b870SPatrick McHardy } 125238f7b870SPatrick McHardy 125338f7b870SPatrick McHardy if (!ifname[0]) 125438f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 125538f7b870SPatrick McHardy 125681adee47SEric W. Biederman dest_net = rtnl_link_get_net(net, tb); 125781adee47SEric W. Biederman dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 125838f7b870SPatrick McHardy 1259e7199288SPavel Emelianov if (IS_ERR(dev)) 1260e7199288SPavel Emelianov err = PTR_ERR(dev); 1261e7199288SPavel Emelianov else if (ops->newlink) 126281adee47SEric W. Biederman err = ops->newlink(net, dev, tb, data); 12632d85cba2SPatrick McHardy else 12642d85cba2SPatrick McHardy err = register_netdevice(dev); 1265*3729d502SPatrick McHardy if (err < 0 && !IS_ERR(dev)) { 126638f7b870SPatrick McHardy free_netdev(dev); 1267*3729d502SPatrick McHardy goto out; 1268*3729d502SPatrick McHardy } 126981adee47SEric W. Biederman 1270*3729d502SPatrick McHardy err = rtnl_configure_link(dev, ifm); 1271*3729d502SPatrick McHardy if (err < 0) 1272*3729d502SPatrick McHardy unregister_netdevice(dev); 1273*3729d502SPatrick McHardy out: 127481adee47SEric W. Biederman put_net(dest_net); 127538f7b870SPatrick McHardy return err; 127638f7b870SPatrick McHardy } 127738f7b870SPatrick McHardy } 127838f7b870SPatrick McHardy 1279b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1280711e2c33SJean Tourrilhes { 12813b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1282b60c5115SThomas Graf struct ifinfomsg *ifm; 1283a3d12891SEric Dumazet char ifname[IFNAMSIZ]; 1284b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1285b60c5115SThomas Graf struct net_device *dev = NULL; 1286b60c5115SThomas Graf struct sk_buff *nskb; 1287339bf98fSThomas Graf int err; 1288711e2c33SJean Tourrilhes 1289b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1290b60c5115SThomas Graf if (err < 0) 12919918f230SEric Sesterhenn return err; 1292b60c5115SThomas Graf 1293a3d12891SEric Dumazet if (tb[IFLA_IFNAME]) 1294a3d12891SEric Dumazet nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1295a3d12891SEric Dumazet 1296b60c5115SThomas Graf ifm = nlmsg_data(nlh); 1297a3d12891SEric Dumazet if (ifm->ifi_index > 0) 1298a3d12891SEric Dumazet dev = __dev_get_by_index(net, ifm->ifi_index); 1299a3d12891SEric Dumazet else if (tb[IFLA_IFNAME]) 1300a3d12891SEric Dumazet dev = __dev_get_by_name(net, ifname); 1301a3d12891SEric Dumazet else 1302b60c5115SThomas Graf return -EINVAL; 1303b60c5115SThomas Graf 1304a3d12891SEric Dumazet if (dev == NULL) 1305a3d12891SEric Dumazet return -ENODEV; 1306a3d12891SEric Dumazet 130738f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1308a3d12891SEric Dumazet if (nskb == NULL) 1309a3d12891SEric Dumazet return -ENOBUFS; 1310711e2c33SJean Tourrilhes 1311575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1312575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 131326932566SPatrick McHardy if (err < 0) { 131426932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 131526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 131626932566SPatrick McHardy kfree_skb(nskb); 1317a3d12891SEric Dumazet } else 131897c53cacSDenis V. Lunev err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1319b60c5115SThomas Graf 1320711e2c33SJean Tourrilhes return err; 1321711e2c33SJean Tourrilhes } 1322711e2c33SJean Tourrilhes 132342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 13241da177e4SLinus Torvalds { 13251da177e4SLinus Torvalds int idx; 13261da177e4SLinus Torvalds int s_idx = cb->family; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds if (s_idx == 0) 13291da177e4SLinus Torvalds s_idx = 1; 13301da177e4SLinus Torvalds for (idx = 1; idx < NPROTO; idx++) { 13311da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 13321da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 13331da177e4SLinus Torvalds continue; 1334e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1335e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 13361da177e4SLinus Torvalds continue; 13371da177e4SLinus Torvalds if (idx > s_idx) 13381da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1339e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 13401da177e4SLinus Torvalds break; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds cb->family = idx; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds return skb->len; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 13481da177e4SLinus Torvalds { 1349c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 13501da177e4SLinus Torvalds struct sk_buff *skb; 13510ec6d3f4SThomas Graf int err = -ENOBUFS; 13521da177e4SLinus Torvalds 135338f7b870SPatrick McHardy skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 13540ec6d3f4SThomas Graf if (skb == NULL) 13550ec6d3f4SThomas Graf goto errout; 13561da177e4SLinus Torvalds 1357575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 135826932566SPatrick McHardy if (err < 0) { 135926932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 136026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 136126932566SPatrick McHardy kfree_skb(skb); 136226932566SPatrick McHardy goto errout; 136326932566SPatrick McHardy } 13641ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 13651ce85fe4SPablo Neira Ayuso return; 13660ec6d3f4SThomas Graf errout: 13670ec6d3f4SThomas Graf if (err < 0) 13684b3da706SEric W. Biederman rtnl_set_sk_err(net, RTNLGRP_LINK, err); 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 13721da177e4SLinus Torvalds static struct rtattr **rta_buf; 13731da177e4SLinus Torvalds static int rtattr_max; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds /* Process one rtnetlink message. */ 13761da177e4SLinus Torvalds 13771d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 13781da177e4SLinus Torvalds { 13793b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 1380e2849863SThomas Graf rtnl_doit_func doit; 13811da177e4SLinus Torvalds int sz_idx, kind; 13821da177e4SLinus Torvalds int min_len; 13831da177e4SLinus Torvalds int family; 13841da177e4SLinus Torvalds int type; 13851c2d670fSPatrick McHardy int err; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds type = nlh->nlmsg_type; 13881da177e4SLinus Torvalds if (type > RTM_MAX) 1389038890feSThomas Graf return -EOPNOTSUPP; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds type -= RTM_BASE; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 13941da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 13951da177e4SLinus Torvalds return 0; 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 13981d00a4ebSThomas Graf if (family >= NPROTO) 13991d00a4ebSThomas Graf return -EAFNOSUPPORT; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds sz_idx = type>>2; 14021da177e4SLinus Torvalds kind = type&3; 14031da177e4SLinus Torvalds 14041d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 14051d00a4ebSThomas Graf return -EPERM; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 140897c53cacSDenis V. Lunev struct sock *rtnl; 1409e2849863SThomas Graf rtnl_dumpit_func dumpit; 14101da177e4SLinus Torvalds 1411e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1412e2849863SThomas Graf if (dumpit == NULL) 1413038890feSThomas Graf return -EOPNOTSUPP; 14141da177e4SLinus Torvalds 14151c2d670fSPatrick McHardy __rtnl_unlock(); 141697c53cacSDenis V. Lunev rtnl = net->rtnl; 14171c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 14181c2d670fSPatrick McHardy rtnl_lock(); 14191c2d670fSPatrick McHardy return err; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 14251da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 14261d00a4ebSThomas Graf return -EINVAL; 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 14291da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 14301da177e4SLinus Torvalds struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 14331da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 14341da177e4SLinus Torvalds if (flavor) { 14351da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 14361d00a4ebSThomas Graf return -EINVAL; 14371da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 1443e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1444e2849863SThomas Graf if (doit == NULL) 1445038890feSThomas Graf return -EOPNOTSUPP; 14461da177e4SLinus Torvalds 14471d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 1450cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 14511da177e4SLinus Torvalds { 14521536cc0dSDenis V. Lunev rtnl_lock(); 1453cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 14541536cc0dSDenis V. Lunev rtnl_unlock(); 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 14581da177e4SLinus Torvalds { 14591da177e4SLinus Torvalds struct net_device *dev = ptr; 1460e9dc8653SEric W. Biederman 14611da177e4SLinus Torvalds switch (event) { 14621da177e4SLinus Torvalds case NETDEV_UP: 14631da177e4SLinus Torvalds case NETDEV_DOWN: 146410de05afSPatrick McHardy case NETDEV_PRE_UP: 1465d90a909eSEric W. Biederman case NETDEV_POST_INIT: 1466d90a909eSEric W. Biederman case NETDEV_REGISTER: 14671da177e4SLinus Torvalds case NETDEV_CHANGE: 14681da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 1469a2835763SPatrick McHardy case NETDEV_UNREGISTER: 1470d90a909eSEric W. Biederman case NETDEV_UNREGISTER_BATCH: 14711da177e4SLinus Torvalds break; 14721da177e4SLinus Torvalds default: 14731da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 14741da177e4SLinus Torvalds break; 14751da177e4SLinus Torvalds } 14761da177e4SLinus Torvalds return NOTIFY_DONE; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 14801da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 14811da177e4SLinus Torvalds }; 14821da177e4SLinus Torvalds 148397c53cacSDenis V. Lunev 14842c8c1e72SAlexey Dobriyan static int __net_init rtnetlink_net_init(struct net *net) 148597c53cacSDenis V. Lunev { 148697c53cacSDenis V. Lunev struct sock *sk; 148797c53cacSDenis V. Lunev sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 148897c53cacSDenis V. Lunev rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 148997c53cacSDenis V. Lunev if (!sk) 149097c53cacSDenis V. Lunev return -ENOMEM; 149197c53cacSDenis V. Lunev net->rtnl = sk; 149297c53cacSDenis V. Lunev return 0; 149397c53cacSDenis V. Lunev } 149497c53cacSDenis V. Lunev 14952c8c1e72SAlexey Dobriyan static void __net_exit rtnetlink_net_exit(struct net *net) 149697c53cacSDenis V. Lunev { 1497b7c6ba6eSDenis V. Lunev netlink_kernel_release(net->rtnl); 149897c53cacSDenis V. Lunev net->rtnl = NULL; 149997c53cacSDenis V. Lunev } 150097c53cacSDenis V. Lunev 150197c53cacSDenis V. Lunev static struct pernet_operations rtnetlink_net_ops = { 150297c53cacSDenis V. Lunev .init = rtnetlink_net_init, 150397c53cacSDenis V. Lunev .exit = rtnetlink_net_exit, 150497c53cacSDenis V. Lunev }; 150597c53cacSDenis V. Lunev 15061da177e4SLinus Torvalds void __init rtnetlink_init(void) 15071da177e4SLinus Torvalds { 15081da177e4SLinus Torvalds int i; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds rtattr_max = 0; 15111da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 15121da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 15131da177e4SLinus Torvalds rtattr_max = rta_max[i]; 15141da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 15151da177e4SLinus Torvalds if (!rta_buf) 15161da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 15171da177e4SLinus Torvalds 151897c53cacSDenis V. Lunev if (register_pernet_subsys(&rtnetlink_net_ops)) 15191da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 152097c53cacSDenis V. Lunev 15211da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 15221da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 1523340d17fcSThomas Graf 1524340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 1525340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 152638f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); 152738f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); 1528687ad8ccSThomas Graf 1529687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 1530687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 1533