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> 38d8a5ec67SEric W. Biederman #include <linux/nsproxy.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <asm/uaccess.h> 411da177e4SLinus Torvalds #include <asm/system.h> 421da177e4SLinus Torvalds #include <asm/string.h> 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #include <linux/inet.h> 451da177e4SLinus Torvalds #include <linux/netdevice.h> 461da177e4SLinus Torvalds #include <net/ip.h> 471da177e4SLinus Torvalds #include <net/protocol.h> 481da177e4SLinus Torvalds #include <net/arp.h> 491da177e4SLinus Torvalds #include <net/route.h> 501da177e4SLinus Torvalds #include <net/udp.h> 511da177e4SLinus Torvalds #include <net/sock.h> 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 5314c0b97dSThomas Graf #include <net/fib_rules.h> 54e2849863SThomas Graf #include <net/rtnetlink.h> 551da177e4SLinus Torvalds 56e2849863SThomas Graf struct rtnl_link 57e2849863SThomas Graf { 58e2849863SThomas Graf rtnl_doit_func doit; 59e2849863SThomas Graf rtnl_dumpit_func dumpit; 60e2849863SThomas Graf }; 61e2849863SThomas Graf 626756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 6356fc85acSThomas Graf static struct sock *rtnl; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds void rtnl_lock(void) 661da177e4SLinus Torvalds { 676756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 706756ae4bSStephen Hemminger void __rtnl_unlock(void) 711da177e4SLinus Torvalds { 726756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds void rtnl_unlock(void) 761da177e4SLinus Torvalds { 776756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 781da177e4SLinus Torvalds netdev_run_todo(); 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 816756ae4bSStephen Hemminger int rtnl_trylock(void) 826756ae4bSStephen Hemminger { 836756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 846756ae4bSStephen Hemminger } 856756ae4bSStephen Hemminger 861da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds memset(tb, 0, sizeof(struct rtattr*)*maxattr); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds while (RTA_OK(rta, len)) { 911da177e4SLinus Torvalds unsigned flavor = rta->rta_type; 921da177e4SLinus Torvalds if (flavor && flavor <= maxattr) 931da177e4SLinus Torvalds tb[flavor-1] = rta; 941da177e4SLinus Torvalds rta = RTA_NEXT(rta, len); 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds return 0; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 992371baa4SPatrick McHardy int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr, 1002371baa4SPatrick McHardy struct rtattr *rta, int len) 101afdc3238SPatrick McHardy { 102afdc3238SPatrick McHardy if (RTA_PAYLOAD(rta) < len) 103afdc3238SPatrick McHardy return -1; 104afdc3238SPatrick McHardy if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { 105afdc3238SPatrick McHardy rta = RTA_DATA(rta) + RTA_ALIGN(len); 106afdc3238SPatrick McHardy return rtattr_parse_nested(tb, maxattr, rta); 107afdc3238SPatrick McHardy } 108afdc3238SPatrick McHardy memset(tb, 0, sizeof(struct rtattr *) * maxattr); 109afdc3238SPatrick McHardy return 0; 110afdc3238SPatrick McHardy } 111afdc3238SPatrick McHardy 11242bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO]; 113e2849863SThomas Graf 114e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 115e2849863SThomas Graf { 116e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 117e2849863SThomas Graf 118e2849863SThomas Graf /* 119e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 120e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 121e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 122e2849863SThomas Graf */ 123e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 124e2849863SThomas Graf 125e2849863SThomas Graf return msgindex; 126e2849863SThomas Graf } 127e2849863SThomas Graf 128e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 129e2849863SThomas Graf { 130e2849863SThomas Graf struct rtnl_link *tab; 131e2849863SThomas Graf 132e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 13351057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 134e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 135e2849863SThomas Graf 13651057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 137e2849863SThomas Graf } 138e2849863SThomas Graf 139e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 140e2849863SThomas Graf { 141e2849863SThomas Graf struct rtnl_link *tab; 142e2849863SThomas Graf 143e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 14451057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 145e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 146e2849863SThomas Graf 14751057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 148e2849863SThomas Graf } 149e2849863SThomas Graf 150e2849863SThomas Graf /** 151e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 152e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 153e2849863SThomas Graf * @msgtype: rtnetlink message type 154e2849863SThomas Graf * @doit: Function pointer called for each request message 155e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 156e2849863SThomas Graf * 157e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 158e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 159e2849863SThomas Graf * specified protocol family and message type is received. 160e2849863SThomas Graf * 161e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 162e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 163e2849863SThomas Graf * family exists. 164e2849863SThomas Graf * 165e2849863SThomas Graf * Returns 0 on success or a negative error code. 166e2849863SThomas Graf */ 167e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 168e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 169e2849863SThomas Graf { 170e2849863SThomas Graf struct rtnl_link *tab; 171e2849863SThomas Graf int msgindex; 172e2849863SThomas Graf 173e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 174e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 175e2849863SThomas Graf 176e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 177e2849863SThomas Graf if (tab == NULL) { 178e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 179e2849863SThomas Graf if (tab == NULL) 180e2849863SThomas Graf return -ENOBUFS; 181e2849863SThomas Graf 182e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 183e2849863SThomas Graf } 184e2849863SThomas Graf 185e2849863SThomas Graf if (doit) 186e2849863SThomas Graf tab[msgindex].doit = doit; 187e2849863SThomas Graf 188e2849863SThomas Graf if (dumpit) 189e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 190e2849863SThomas Graf 191e2849863SThomas Graf return 0; 192e2849863SThomas Graf } 193e2849863SThomas Graf 194e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 195e2849863SThomas Graf 196e2849863SThomas Graf /** 197e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 198e2849863SThomas Graf * 199e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 200e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 201e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 202e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 203e2849863SThomas Graf * of memory implies no sense in continueing. 204e2849863SThomas Graf */ 205e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 206e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 207e2849863SThomas Graf { 208e2849863SThomas Graf if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) 209e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 210e2849863SThomas Graf "protocol = %d, message type = %d\n", 211e2849863SThomas Graf protocol, msgtype); 212e2849863SThomas Graf } 213e2849863SThomas Graf 214e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 215e2849863SThomas Graf 216e2849863SThomas Graf /** 217e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 218e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 219e2849863SThomas Graf * @msgtype: rtnetlink message type 220e2849863SThomas Graf * 221e2849863SThomas Graf * Returns 0 on success or a negative error code. 222e2849863SThomas Graf */ 223e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 224e2849863SThomas Graf { 225e2849863SThomas Graf int msgindex; 226e2849863SThomas Graf 227e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 228e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 229e2849863SThomas Graf 230e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 231e2849863SThomas Graf return -ENOENT; 232e2849863SThomas Graf 233e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 234e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 235e2849863SThomas Graf 236e2849863SThomas Graf return 0; 237e2849863SThomas Graf } 238e2849863SThomas Graf 239e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 240e2849863SThomas Graf 241e2849863SThomas Graf /** 242e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 243e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 244e2849863SThomas Graf * 245e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 246e2849863SThomas Graf * of a certain protocol family. 247e2849863SThomas Graf */ 248e2849863SThomas Graf void rtnl_unregister_all(int protocol) 249e2849863SThomas Graf { 250e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 251e2849863SThomas Graf 252e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 253e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 254e2849863SThomas Graf } 255e2849863SThomas Graf 256e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2571da177e4SLinus Torvalds 25838f7b870SPatrick McHardy static LIST_HEAD(link_ops); 25938f7b870SPatrick McHardy 26038f7b870SPatrick McHardy /** 26138f7b870SPatrick McHardy * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 26238f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 26338f7b870SPatrick McHardy * 26438f7b870SPatrick McHardy * The caller must hold the rtnl_mutex. This function should be used 26538f7b870SPatrick McHardy * by drivers that create devices during module initialization. It 26638f7b870SPatrick McHardy * must be called before registering the devices. 26738f7b870SPatrick McHardy * 26838f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 26938f7b870SPatrick McHardy */ 27038f7b870SPatrick McHardy int __rtnl_link_register(struct rtnl_link_ops *ops) 27138f7b870SPatrick McHardy { 2722d85cba2SPatrick McHardy if (!ops->dellink) 2732d85cba2SPatrick McHardy ops->dellink = unregister_netdevice; 2742d85cba2SPatrick McHardy 27538f7b870SPatrick McHardy list_add_tail(&ops->list, &link_ops); 27638f7b870SPatrick McHardy return 0; 27738f7b870SPatrick McHardy } 27838f7b870SPatrick McHardy 27938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_register); 28038f7b870SPatrick McHardy 28138f7b870SPatrick McHardy /** 28238f7b870SPatrick McHardy * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 28338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to register 28438f7b870SPatrick McHardy * 28538f7b870SPatrick McHardy * Returns 0 on success or a negative error code. 28638f7b870SPatrick McHardy */ 28738f7b870SPatrick McHardy int rtnl_link_register(struct rtnl_link_ops *ops) 28838f7b870SPatrick McHardy { 28938f7b870SPatrick McHardy int err; 29038f7b870SPatrick McHardy 29138f7b870SPatrick McHardy rtnl_lock(); 29238f7b870SPatrick McHardy err = __rtnl_link_register(ops); 29338f7b870SPatrick McHardy rtnl_unlock(); 29438f7b870SPatrick McHardy return err; 29538f7b870SPatrick McHardy } 29638f7b870SPatrick McHardy 29738f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_register); 29838f7b870SPatrick McHardy 29938f7b870SPatrick McHardy /** 30038f7b870SPatrick McHardy * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 30138f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 30238f7b870SPatrick McHardy * 3032d85cba2SPatrick McHardy * The caller must hold the rtnl_mutex. 30438f7b870SPatrick McHardy */ 30538f7b870SPatrick McHardy void __rtnl_link_unregister(struct rtnl_link_ops *ops) 30638f7b870SPatrick McHardy { 3072d85cba2SPatrick McHardy struct net_device *dev, *n; 308881d966bSEric W. Biederman struct net *net; 3092d85cba2SPatrick McHardy 310881d966bSEric W. Biederman for_each_net(net) { 311881d966bSEric W. Biederman for_each_netdev_safe(net, dev, n) { 3122d85cba2SPatrick McHardy if (dev->rtnl_link_ops == ops) 3132d85cba2SPatrick McHardy ops->dellink(dev); 3142d85cba2SPatrick McHardy } 315881d966bSEric W. Biederman } 31638f7b870SPatrick McHardy list_del(&ops->list); 31738f7b870SPatrick McHardy } 31838f7b870SPatrick McHardy 31938f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 32038f7b870SPatrick McHardy 32138f7b870SPatrick McHardy /** 32238f7b870SPatrick McHardy * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 32338f7b870SPatrick McHardy * @ops: struct rtnl_link_ops * to unregister 32438f7b870SPatrick McHardy */ 32538f7b870SPatrick McHardy void rtnl_link_unregister(struct rtnl_link_ops *ops) 32638f7b870SPatrick McHardy { 32738f7b870SPatrick McHardy rtnl_lock(); 32838f7b870SPatrick McHardy __rtnl_link_unregister(ops); 32938f7b870SPatrick McHardy rtnl_unlock(); 33038f7b870SPatrick McHardy } 33138f7b870SPatrick McHardy 33238f7b870SPatrick McHardy EXPORT_SYMBOL_GPL(rtnl_link_unregister); 33338f7b870SPatrick McHardy 33438f7b870SPatrick McHardy static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 33538f7b870SPatrick McHardy { 33638f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 33738f7b870SPatrick McHardy 33838f7b870SPatrick McHardy list_for_each_entry(ops, &link_ops, list) { 33938f7b870SPatrick McHardy if (!strcmp(ops->kind, kind)) 34038f7b870SPatrick McHardy return ops; 34138f7b870SPatrick McHardy } 34238f7b870SPatrick McHardy return NULL; 34338f7b870SPatrick McHardy } 34438f7b870SPatrick McHardy 34538f7b870SPatrick McHardy static size_t rtnl_link_get_size(const struct net_device *dev) 34638f7b870SPatrick McHardy { 34738f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 34838f7b870SPatrick McHardy size_t size; 34938f7b870SPatrick McHardy 35038f7b870SPatrick McHardy if (!ops) 35138f7b870SPatrick McHardy return 0; 35238f7b870SPatrick McHardy 35338f7b870SPatrick McHardy size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 35438f7b870SPatrick McHardy nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 35538f7b870SPatrick McHardy 35638f7b870SPatrick McHardy if (ops->get_size) 35738f7b870SPatrick McHardy /* IFLA_INFO_DATA + nested data */ 35838f7b870SPatrick McHardy size += nlmsg_total_size(sizeof(struct nlattr)) + 35938f7b870SPatrick McHardy ops->get_size(dev); 36038f7b870SPatrick McHardy 36138f7b870SPatrick McHardy if (ops->get_xstats_size) 36238f7b870SPatrick McHardy size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ 36338f7b870SPatrick McHardy 36438f7b870SPatrick McHardy return size; 36538f7b870SPatrick McHardy } 36638f7b870SPatrick McHardy 36738f7b870SPatrick McHardy static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 36838f7b870SPatrick McHardy { 36938f7b870SPatrick McHardy const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 37038f7b870SPatrick McHardy struct nlattr *linkinfo, *data; 37138f7b870SPatrick McHardy int err = -EMSGSIZE; 37238f7b870SPatrick McHardy 37338f7b870SPatrick McHardy linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 37438f7b870SPatrick McHardy if (linkinfo == NULL) 37538f7b870SPatrick McHardy goto out; 37638f7b870SPatrick McHardy 37738f7b870SPatrick McHardy if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 37838f7b870SPatrick McHardy goto err_cancel_link; 37938f7b870SPatrick McHardy if (ops->fill_xstats) { 38038f7b870SPatrick McHardy err = ops->fill_xstats(skb, dev); 38138f7b870SPatrick McHardy if (err < 0) 38238f7b870SPatrick McHardy goto err_cancel_link; 38338f7b870SPatrick McHardy } 38438f7b870SPatrick McHardy if (ops->fill_info) { 38538f7b870SPatrick McHardy data = nla_nest_start(skb, IFLA_INFO_DATA); 38638f7b870SPatrick McHardy if (data == NULL) 38738f7b870SPatrick McHardy goto err_cancel_link; 38838f7b870SPatrick McHardy err = ops->fill_info(skb, dev); 38938f7b870SPatrick McHardy if (err < 0) 39038f7b870SPatrick McHardy goto err_cancel_data; 39138f7b870SPatrick McHardy nla_nest_end(skb, data); 39238f7b870SPatrick McHardy } 39338f7b870SPatrick McHardy 39438f7b870SPatrick McHardy nla_nest_end(skb, linkinfo); 39538f7b870SPatrick McHardy return 0; 39638f7b870SPatrick McHardy 39738f7b870SPatrick McHardy err_cancel_data: 39838f7b870SPatrick McHardy nla_nest_cancel(skb, data); 39938f7b870SPatrick McHardy err_cancel_link: 40038f7b870SPatrick McHardy nla_nest_cancel(skb, linkinfo); 40138f7b870SPatrick McHardy out: 40238f7b870SPatrick McHardy return err; 40338f7b870SPatrick McHardy } 40438f7b870SPatrick McHardy 405db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 4061da177e4SLinus Torvalds { 407f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 408f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 409f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 41014c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 411f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 412f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 413f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 414f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 415f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 416f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 4171da177e4SLinus Torvalds }; 4181da177e4SLinus Torvalds 419db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 4201da177e4SLinus Torvalds { 421f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 422f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 423f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 42414c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 425f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 426f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 427f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 428f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 4291da177e4SLinus Torvalds }; 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds struct rtattr *rta; 4341da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); 4371da177e4SLinus Torvalds rta->rta_type = attrtype; 4381da177e4SLinus Torvalds rta->rta_len = size; 4391da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 440b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds size_t ret = RTA_PAYLOAD(rta); 4461da177e4SLinus Torvalds char *src = RTA_DATA(rta); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (ret > 0 && src[ret - 1] == '\0') 4491da177e4SLinus Torvalds ret--; 4501da177e4SLinus Torvalds if (size > 0) { 4511da177e4SLinus Torvalds size_t len = (ret >= size) ? size - 1 : ret; 4521da177e4SLinus Torvalds memset(dest, 0, size); 4531da177e4SLinus Torvalds memcpy(dest, src, len); 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds return ret; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) 4591da177e4SLinus Torvalds { 4601da177e4SLinus Torvalds int err = 0; 4611da177e4SLinus Torvalds 462ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 4631da177e4SLinus Torvalds if (echo) 4641da177e4SLinus Torvalds atomic_inc(&skb->users); 4651da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 4661da177e4SLinus Torvalds if (echo) 4671da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 4681da177e4SLinus Torvalds return err; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4712942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid) 4722942e900SThomas Graf { 4732942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 4742942e900SThomas Graf } 4752942e900SThomas Graf 47697676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, 47797676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 47897676b6bSThomas Graf { 47997676b6bSThomas Graf int report = 0; 48097676b6bSThomas Graf 48197676b6bSThomas Graf if (nlh) 48297676b6bSThomas Graf report = nlmsg_report(nlh); 48397676b6bSThomas Graf 48497676b6bSThomas Graf return nlmsg_notify(rtnl, skb, pid, group, report, flags); 48597676b6bSThomas Graf } 48697676b6bSThomas Graf 48797676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error) 48897676b6bSThomas Graf { 48997676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 49097676b6bSThomas Graf } 49197676b6bSThomas Graf 4921da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 4931da177e4SLinus Torvalds { 4942d7202bfSThomas Graf struct nlattr *mx; 4952d7202bfSThomas Graf int i, valid = 0; 4961da177e4SLinus Torvalds 4972d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 4982d7202bfSThomas Graf if (mx == NULL) 4992d7202bfSThomas Graf return -ENOBUFS; 5002d7202bfSThomas Graf 5011da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 5022d7202bfSThomas Graf if (metrics[i]) { 5032d7202bfSThomas Graf valid++; 5042d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 5051da177e4SLinus Torvalds } 5062d7202bfSThomas Graf } 5071da177e4SLinus Torvalds 508a57d27fcSDavid S. Miller if (!valid) { 509a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 510a57d27fcSDavid S. Miller return 0; 511a57d27fcSDavid S. Miller } 5122d7202bfSThomas Graf 5132d7202bfSThomas Graf return nla_nest_end(skb, mx); 5142d7202bfSThomas Graf 5152d7202bfSThomas Graf nla_put_failure: 5162d7202bfSThomas Graf return nla_nest_cancel(skb, mx); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 519e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 520e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 521e3703b3dSThomas Graf { 522e3703b3dSThomas Graf struct rta_cacheinfo ci = { 523e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 524e3703b3dSThomas Graf .rta_used = dst->__use, 525e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 526e3703b3dSThomas Graf .rta_error = error, 527e3703b3dSThomas Graf .rta_id = id, 528e3703b3dSThomas Graf .rta_ts = ts, 529e3703b3dSThomas Graf .rta_tsage = tsage, 530e3703b3dSThomas Graf }; 531e3703b3dSThomas Graf 532e3703b3dSThomas Graf if (expires) 533e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 534e3703b3dSThomas Graf 535e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 536e3703b3dSThomas Graf } 537e3703b3dSThomas Graf 538e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 5391da177e4SLinus Torvalds 540b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition) 541b00055aaSStefan Rompf { 542b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 543b00055aaSStefan Rompf 544b00055aaSStefan Rompf switch(transition) { 545b00055aaSStefan Rompf case IF_OPER_UP: 546b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 547b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 548b00055aaSStefan Rompf !netif_dormant(dev)) 549b00055aaSStefan Rompf operstate = IF_OPER_UP; 550b00055aaSStefan Rompf break; 551b00055aaSStefan Rompf 552b00055aaSStefan Rompf case IF_OPER_DORMANT: 553b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 554b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 555b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 556b00055aaSStefan Rompf break; 5573ff50b79SStephen Hemminger } 558b00055aaSStefan Rompf 559b00055aaSStefan Rompf if (dev->operstate != operstate) { 560b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 561b00055aaSStefan Rompf dev->operstate = operstate; 562b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 563b00055aaSStefan Rompf netdev_state_change(dev); 564b00055aaSStefan Rompf } 565b00055aaSStefan Rompf } 566b00055aaSStefan Rompf 567b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 568b60c5115SThomas Graf struct net_device_stats *b) 5691da177e4SLinus Torvalds { 570b60c5115SThomas Graf a->rx_packets = b->rx_packets; 571b60c5115SThomas Graf a->tx_packets = b->tx_packets; 572b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 573b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 574b60c5115SThomas Graf a->rx_errors = b->rx_errors; 575b60c5115SThomas Graf a->tx_errors = b->tx_errors; 576b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 577b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 578b60c5115SThomas Graf 579b60c5115SThomas Graf a->multicast = b->multicast; 580b60c5115SThomas Graf a->collisions = b->collisions; 581b60c5115SThomas Graf 582b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 583b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 584b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 585b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 586b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 587b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 588b60c5115SThomas Graf 589b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 590b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 591b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 592b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 593b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 594b60c5115SThomas Graf 595b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 596b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 597b60c5115SThomas Graf }; 598b60c5115SThomas Graf 59938f7b870SPatrick McHardy static inline size_t if_nlmsg_size(const struct net_device *dev) 600339bf98fSThomas Graf { 601339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 602339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 603339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 604339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 605339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 606339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 607339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 608339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 609339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 610339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 611339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 612339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 613339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 61438f7b870SPatrick McHardy + nla_total_size(1) /* IFLA_LINKMODE */ 61538f7b870SPatrick McHardy + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ 616339bf98fSThomas Graf } 617339bf98fSThomas Graf 618b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 619575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 620575c3e2aSPatrick McHardy unsigned int flags) 621b60c5115SThomas Graf { 622b60c5115SThomas Graf struct ifinfomsg *ifm; 6231da177e4SLinus Torvalds struct nlmsghdr *nlh; 6241da177e4SLinus Torvalds 625b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 626b60c5115SThomas Graf if (nlh == NULL) 62726932566SPatrick McHardy return -EMSGSIZE; 6281da177e4SLinus Torvalds 629b60c5115SThomas Graf ifm = nlmsg_data(nlh); 630b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 631b60c5115SThomas Graf ifm->__ifi_pad = 0; 632b60c5115SThomas Graf ifm->ifi_type = dev->type; 633b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 634b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 635b60c5115SThomas Graf ifm->ifi_change = change; 6361da177e4SLinus Torvalds 637b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 638b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 639b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 640b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 641b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 642b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 6431da177e4SLinus Torvalds 644b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 645b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 6461da177e4SLinus Torvalds 647b60c5115SThomas Graf if (dev->master) 648b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 649b60c5115SThomas Graf 650b60c5115SThomas Graf if (dev->qdisc_sleeping) 651b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); 652b00055aaSStefan Rompf 653b00055aaSStefan Rompf if (1) { 6541da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 6551da177e4SLinus Torvalds .mem_start = dev->mem_start, 6561da177e4SLinus Torvalds .mem_end = dev->mem_end, 6571da177e4SLinus Torvalds .base_addr = dev->base_addr, 6581da177e4SLinus Torvalds .irq = dev->irq, 6591da177e4SLinus Torvalds .dma = dev->dma, 6601da177e4SLinus Torvalds .port = dev->if_port, 6611da177e4SLinus Torvalds }; 662b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds if (dev->addr_len) { 666b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 667b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds if (dev->get_stats) { 671b60c5115SThomas Graf struct net_device_stats *stats = dev->get_stats(dev); 6721da177e4SLinus Torvalds if (stats) { 673b60c5115SThomas Graf struct nlattr *attr; 6741da177e4SLinus Torvalds 675b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 676b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 677b60c5115SThomas Graf if (attr == NULL) 678b60c5115SThomas Graf goto nla_put_failure; 679b60c5115SThomas Graf 680b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 68438f7b870SPatrick McHardy if (dev->rtnl_link_ops) { 68538f7b870SPatrick McHardy if (rtnl_link_fill(skb, dev) < 0) 68638f7b870SPatrick McHardy goto nla_put_failure; 68738f7b870SPatrick McHardy } 68838f7b870SPatrick McHardy 689b60c5115SThomas Graf return nlmsg_end(skb, nlh); 690b60c5115SThomas Graf 691b60c5115SThomas Graf nla_put_failure: 69226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 69326932566SPatrick McHardy return -EMSGSIZE; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 696b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 6971da177e4SLinus Torvalds { 698881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 6991da177e4SLinus Torvalds int idx; 7001da177e4SLinus Torvalds int s_idx = cb->args[0]; 7011da177e4SLinus Torvalds struct net_device *dev; 7021da177e4SLinus Torvalds 7037562f876SPavel Emelianov idx = 0; 704881d966bSEric W. Biederman for_each_netdev(net, dev) { 7051da177e4SLinus Torvalds if (idx < s_idx) 7067562f876SPavel Emelianov goto cont; 707575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 708b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 709b60c5115SThomas Graf cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) 7101da177e4SLinus Torvalds break; 7117562f876SPavel Emelianov cont: 7127562f876SPavel Emelianov idx++; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds cb->args[0] = idx; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds return skb->len; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 719e7199288SPavel Emelianov const struct nla_policy ifla_policy[IFLA_MAX+1] = { 7205176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 72138f7b870SPatrick McHardy [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 72238f7b870SPatrick McHardy [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 7235176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 724da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 725da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 726da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 727da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 728da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 729d8a5ec67SEric W. Biederman [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 730da5e0494SThomas Graf }; 7311da177e4SLinus Torvalds 73238f7b870SPatrick McHardy static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 73338f7b870SPatrick McHardy [IFLA_INFO_KIND] = { .type = NLA_STRING }, 73438f7b870SPatrick McHardy [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 73538f7b870SPatrick McHardy }; 73638f7b870SPatrick McHardy 737d8a5ec67SEric W. Biederman static struct net *get_net_ns_by_pid(pid_t pid) 738d8a5ec67SEric W. Biederman { 739d8a5ec67SEric W. Biederman struct task_struct *tsk; 740d8a5ec67SEric W. Biederman struct net *net; 741d8a5ec67SEric W. Biederman 742d8a5ec67SEric W. Biederman /* Lookup the network namespace */ 743d8a5ec67SEric W. Biederman net = ERR_PTR(-ESRCH); 744d8a5ec67SEric W. Biederman rcu_read_lock(); 745d8a5ec67SEric W. Biederman tsk = find_task_by_pid(pid); 746d8a5ec67SEric W. Biederman if (tsk) { 747*cf7b708cSPavel Emelyanov struct nsproxy *nsproxy; 748*cf7b708cSPavel Emelyanov nsproxy = task_nsproxy(tsk); 749*cf7b708cSPavel Emelyanov if (nsproxy) 750*cf7b708cSPavel Emelyanov net = get_net(nsproxy->net_ns); 751d8a5ec67SEric W. Biederman } 752d8a5ec67SEric W. Biederman rcu_read_unlock(); 753d8a5ec67SEric W. Biederman return net; 754d8a5ec67SEric W. Biederman } 755d8a5ec67SEric W. Biederman 7560157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 75738f7b870SPatrick McHardy struct nlattr **tb, char *ifname, int modified) 7580157f60cSPatrick McHardy { 75938f7b870SPatrick McHardy int send_addr_notify = 0; 7600157f60cSPatrick McHardy int err; 7610157f60cSPatrick McHardy 762d8a5ec67SEric W. Biederman if (tb[IFLA_NET_NS_PID]) { 763d8a5ec67SEric W. Biederman struct net *net; 764d8a5ec67SEric W. Biederman net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 765d8a5ec67SEric W. Biederman if (IS_ERR(net)) { 766d8a5ec67SEric W. Biederman err = PTR_ERR(net); 767d8a5ec67SEric W. Biederman goto errout; 768d8a5ec67SEric W. Biederman } 769d8a5ec67SEric W. Biederman err = dev_change_net_namespace(dev, net, ifname); 770d8a5ec67SEric W. Biederman put_net(net); 771d8a5ec67SEric W. Biederman if (err) 772d8a5ec67SEric W. Biederman goto errout; 773d8a5ec67SEric W. Biederman modified = 1; 774d8a5ec67SEric W. Biederman } 775d8a5ec67SEric W. Biederman 7760157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 7770157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 7780157f60cSPatrick McHardy struct ifmap k_map; 7790157f60cSPatrick McHardy 7800157f60cSPatrick McHardy if (!dev->set_config) { 7810157f60cSPatrick McHardy err = -EOPNOTSUPP; 7820157f60cSPatrick McHardy goto errout; 7830157f60cSPatrick McHardy } 7840157f60cSPatrick McHardy 7850157f60cSPatrick McHardy if (!netif_device_present(dev)) { 7860157f60cSPatrick McHardy err = -ENODEV; 7870157f60cSPatrick McHardy goto errout; 7880157f60cSPatrick McHardy } 7890157f60cSPatrick McHardy 7900157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 7910157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 7920157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 7930157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 7940157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 7950157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 7960157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 7970157f60cSPatrick McHardy 7980157f60cSPatrick McHardy err = dev->set_config(dev, &k_map); 7990157f60cSPatrick McHardy if (err < 0) 8000157f60cSPatrick McHardy goto errout; 8010157f60cSPatrick McHardy 8020157f60cSPatrick McHardy modified = 1; 8030157f60cSPatrick McHardy } 8040157f60cSPatrick McHardy 8050157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 8060157f60cSPatrick McHardy struct sockaddr *sa; 8070157f60cSPatrick McHardy int len; 8080157f60cSPatrick McHardy 8090157f60cSPatrick McHardy if (!dev->set_mac_address) { 8100157f60cSPatrick McHardy err = -EOPNOTSUPP; 8110157f60cSPatrick McHardy goto errout; 8120157f60cSPatrick McHardy } 8130157f60cSPatrick McHardy 8140157f60cSPatrick McHardy if (!netif_device_present(dev)) { 8150157f60cSPatrick McHardy err = -ENODEV; 8160157f60cSPatrick McHardy goto errout; 8170157f60cSPatrick McHardy } 8180157f60cSPatrick McHardy 8190157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 8200157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 8210157f60cSPatrick McHardy if (!sa) { 8220157f60cSPatrick McHardy err = -ENOMEM; 8230157f60cSPatrick McHardy goto errout; 8240157f60cSPatrick McHardy } 8250157f60cSPatrick McHardy sa->sa_family = dev->type; 8260157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 8270157f60cSPatrick McHardy dev->addr_len); 8280157f60cSPatrick McHardy err = dev->set_mac_address(dev, sa); 8290157f60cSPatrick McHardy kfree(sa); 8300157f60cSPatrick McHardy if (err) 8310157f60cSPatrick McHardy goto errout; 8320157f60cSPatrick McHardy send_addr_notify = 1; 8330157f60cSPatrick McHardy modified = 1; 8340157f60cSPatrick McHardy } 8350157f60cSPatrick McHardy 8360157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 8370157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 8380157f60cSPatrick McHardy if (err < 0) 8390157f60cSPatrick McHardy goto errout; 8400157f60cSPatrick McHardy modified = 1; 8410157f60cSPatrick McHardy } 8420157f60cSPatrick McHardy 8430157f60cSPatrick McHardy /* 8440157f60cSPatrick McHardy * Interface selected by interface index but interface 8450157f60cSPatrick McHardy * name provided implies that a name change has been 8460157f60cSPatrick McHardy * requested. 8470157f60cSPatrick McHardy */ 8480157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 8490157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 8500157f60cSPatrick McHardy if (err < 0) 8510157f60cSPatrick McHardy goto errout; 8520157f60cSPatrick McHardy modified = 1; 8530157f60cSPatrick McHardy } 8540157f60cSPatrick McHardy 8550157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 8560157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 8570157f60cSPatrick McHardy send_addr_notify = 1; 8580157f60cSPatrick McHardy } 8590157f60cSPatrick McHardy 8600157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 8610157f60cSPatrick McHardy unsigned int flags = ifm->ifi_flags; 8620157f60cSPatrick McHardy 8630157f60cSPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 8640157f60cSPatrick McHardy if (ifm->ifi_change) 8650157f60cSPatrick McHardy flags = (flags & ifm->ifi_change) | 8660157f60cSPatrick McHardy (dev->flags & ~ifm->ifi_change); 8670157f60cSPatrick McHardy dev_change_flags(dev, flags); 8680157f60cSPatrick McHardy } 8690157f60cSPatrick McHardy 8700157f60cSPatrick McHardy if (tb[IFLA_TXQLEN]) 8710157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 8720157f60cSPatrick McHardy 8730157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 8740157f60cSPatrick McHardy set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 8750157f60cSPatrick McHardy 8760157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 8770157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 8780157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 8790157f60cSPatrick McHardy write_unlock_bh(&dev_base_lock); 8800157f60cSPatrick McHardy } 8810157f60cSPatrick McHardy 8820157f60cSPatrick McHardy err = 0; 8830157f60cSPatrick McHardy 8840157f60cSPatrick McHardy errout: 8850157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 8860157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 8870157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 8880157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 8890157f60cSPatrick McHardy "please check.\n", dev->name); 8900157f60cSPatrick McHardy 8910157f60cSPatrick McHardy if (send_addr_notify) 8920157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 8930157f60cSPatrick McHardy return err; 8940157f60cSPatrick McHardy } 8950157f60cSPatrick McHardy 896da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 897da5e0494SThomas Graf { 898881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 899da5e0494SThomas Graf struct ifinfomsg *ifm; 900da5e0494SThomas Graf struct net_device *dev; 9010157f60cSPatrick McHardy int err; 902da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 9031da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 9041da177e4SLinus Torvalds 905da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 906da5e0494SThomas Graf if (err < 0) 907da5e0494SThomas Graf goto errout; 9081da177e4SLinus Torvalds 9095176f91eSThomas Graf if (tb[IFLA_IFNAME]) 9105176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 91178e5b891SPatrick McHardy else 91278e5b891SPatrick McHardy ifname[0] = '\0'; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds err = -EINVAL; 915da5e0494SThomas Graf ifm = nlmsg_data(nlh); 91651055be8SPatrick McHardy if (ifm->ifi_index > 0) 917881d966bSEric W. Biederman dev = dev_get_by_index(net, ifm->ifi_index); 918da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 919881d966bSEric W. Biederman dev = dev_get_by_name(net, ifname); 920da5e0494SThomas Graf else 921da5e0494SThomas Graf goto errout; 9221da177e4SLinus Torvalds 923da5e0494SThomas Graf if (dev == NULL) { 924da5e0494SThomas Graf err = -ENODEV; 925da5e0494SThomas Graf goto errout; 926da5e0494SThomas Graf } 9271da177e4SLinus Torvalds 928da5e0494SThomas Graf if (tb[IFLA_ADDRESS] && 929da5e0494SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 930da5e0494SThomas Graf goto errout_dev; 931da5e0494SThomas Graf 932da5e0494SThomas Graf if (tb[IFLA_BROADCAST] && 933da5e0494SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 934da5e0494SThomas Graf goto errout_dev; 935da5e0494SThomas Graf 93638f7b870SPatrick McHardy err = do_setlink(dev, ifm, tb, ifname, 0); 937da5e0494SThomas Graf errout_dev: 9381da177e4SLinus Torvalds dev_put(dev); 939da5e0494SThomas Graf errout: 9401da177e4SLinus Torvalds return err; 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 94338f7b870SPatrick McHardy static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 94438f7b870SPatrick McHardy { 945881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 94638f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 94738f7b870SPatrick McHardy struct net_device *dev; 94838f7b870SPatrick McHardy struct ifinfomsg *ifm; 94938f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 95038f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 95138f7b870SPatrick McHardy int err; 95238f7b870SPatrick McHardy 95338f7b870SPatrick McHardy err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 95438f7b870SPatrick McHardy if (err < 0) 95538f7b870SPatrick McHardy return err; 95638f7b870SPatrick McHardy 95738f7b870SPatrick McHardy if (tb[IFLA_IFNAME]) 95838f7b870SPatrick McHardy nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 95938f7b870SPatrick McHardy 96038f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 96138f7b870SPatrick McHardy if (ifm->ifi_index > 0) 962881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 96338f7b870SPatrick McHardy else if (tb[IFLA_IFNAME]) 964881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 96538f7b870SPatrick McHardy else 96638f7b870SPatrick McHardy return -EINVAL; 96738f7b870SPatrick McHardy 96838f7b870SPatrick McHardy if (!dev) 96938f7b870SPatrick McHardy return -ENODEV; 97038f7b870SPatrick McHardy 97138f7b870SPatrick McHardy ops = dev->rtnl_link_ops; 97238f7b870SPatrick McHardy if (!ops) 97338f7b870SPatrick McHardy return -EOPNOTSUPP; 97438f7b870SPatrick McHardy 97538f7b870SPatrick McHardy ops->dellink(dev); 97638f7b870SPatrick McHardy return 0; 97738f7b870SPatrick McHardy } 97838f7b870SPatrick McHardy 979881d966bSEric W. Biederman struct net_device *rtnl_create_link(struct net *net, char *ifname, 980e7199288SPavel Emelianov const struct rtnl_link_ops *ops, struct nlattr *tb[]) 981e7199288SPavel Emelianov { 982e7199288SPavel Emelianov int err; 983e7199288SPavel Emelianov struct net_device *dev; 984e7199288SPavel Emelianov 985e7199288SPavel Emelianov err = -ENOMEM; 986e7199288SPavel Emelianov dev = alloc_netdev(ops->priv_size, ifname, ops->setup); 987e7199288SPavel Emelianov if (!dev) 988e7199288SPavel Emelianov goto err; 989e7199288SPavel Emelianov 990e7199288SPavel Emelianov if (strchr(dev->name, '%')) { 991e7199288SPavel Emelianov err = dev_alloc_name(dev, dev->name); 992e7199288SPavel Emelianov if (err < 0) 993e7199288SPavel Emelianov goto err_free; 994e7199288SPavel Emelianov } 995e7199288SPavel Emelianov 996881d966bSEric W. Biederman dev->nd_net = net; 997e7199288SPavel Emelianov dev->rtnl_link_ops = ops; 998e7199288SPavel Emelianov 999e7199288SPavel Emelianov if (tb[IFLA_MTU]) 1000e7199288SPavel Emelianov dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1001e7199288SPavel Emelianov if (tb[IFLA_ADDRESS]) 1002e7199288SPavel Emelianov memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1003e7199288SPavel Emelianov nla_len(tb[IFLA_ADDRESS])); 1004e7199288SPavel Emelianov if (tb[IFLA_BROADCAST]) 1005e7199288SPavel Emelianov memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1006e7199288SPavel Emelianov nla_len(tb[IFLA_BROADCAST])); 1007e7199288SPavel Emelianov if (tb[IFLA_TXQLEN]) 1008e7199288SPavel Emelianov dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1009e7199288SPavel Emelianov if (tb[IFLA_OPERSTATE]) 1010e7199288SPavel Emelianov set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1011e7199288SPavel Emelianov if (tb[IFLA_LINKMODE]) 1012e7199288SPavel Emelianov dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1013e7199288SPavel Emelianov 1014e7199288SPavel Emelianov return dev; 1015e7199288SPavel Emelianov 1016e7199288SPavel Emelianov err_free: 1017e7199288SPavel Emelianov free_netdev(dev); 1018e7199288SPavel Emelianov err: 1019e7199288SPavel Emelianov return ERR_PTR(err); 1020e7199288SPavel Emelianov } 1021e7199288SPavel Emelianov 102238f7b870SPatrick McHardy static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 102338f7b870SPatrick McHardy { 1024881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 102538f7b870SPatrick McHardy const struct rtnl_link_ops *ops; 102638f7b870SPatrick McHardy struct net_device *dev; 102738f7b870SPatrick McHardy struct ifinfomsg *ifm; 102838f7b870SPatrick McHardy char kind[MODULE_NAME_LEN]; 102938f7b870SPatrick McHardy char ifname[IFNAMSIZ]; 103038f7b870SPatrick McHardy struct nlattr *tb[IFLA_MAX+1]; 103138f7b870SPatrick McHardy struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 103238f7b870SPatrick McHardy int err; 103338f7b870SPatrick McHardy 10348072f085SThomas Graf #ifdef CONFIG_KMOD 103538f7b870SPatrick McHardy replay: 10368072f085SThomas Graf #endif 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 else 104438f7b870SPatrick McHardy ifname[0] = '\0'; 104538f7b870SPatrick McHardy 104638f7b870SPatrick McHardy ifm = nlmsg_data(nlh); 104738f7b870SPatrick McHardy if (ifm->ifi_index > 0) 1048881d966bSEric W. Biederman dev = __dev_get_by_index(net, ifm->ifi_index); 104938f7b870SPatrick McHardy else if (ifname[0]) 1050881d966bSEric W. Biederman dev = __dev_get_by_name(net, ifname); 105138f7b870SPatrick McHardy else 105238f7b870SPatrick McHardy dev = NULL; 105338f7b870SPatrick McHardy 105438f7b870SPatrick McHardy if (tb[IFLA_LINKINFO]) { 105538f7b870SPatrick McHardy err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 105638f7b870SPatrick McHardy tb[IFLA_LINKINFO], ifla_info_policy); 105738f7b870SPatrick McHardy if (err < 0) 105838f7b870SPatrick McHardy return err; 105938f7b870SPatrick McHardy } else 106038f7b870SPatrick McHardy memset(linkinfo, 0, sizeof(linkinfo)); 106138f7b870SPatrick McHardy 106238f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_KIND]) { 106338f7b870SPatrick McHardy nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 106438f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 106538f7b870SPatrick McHardy } else { 106638f7b870SPatrick McHardy kind[0] = '\0'; 106738f7b870SPatrick McHardy ops = NULL; 106838f7b870SPatrick McHardy } 106938f7b870SPatrick McHardy 107038f7b870SPatrick McHardy if (1) { 107138f7b870SPatrick McHardy struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 107238f7b870SPatrick McHardy 107338f7b870SPatrick McHardy if (ops) { 107438f7b870SPatrick McHardy if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 107538f7b870SPatrick McHardy err = nla_parse_nested(attr, ops->maxtype, 107638f7b870SPatrick McHardy linkinfo[IFLA_INFO_DATA], 107738f7b870SPatrick McHardy ops->policy); 107838f7b870SPatrick McHardy if (err < 0) 107938f7b870SPatrick McHardy return err; 108038f7b870SPatrick McHardy data = attr; 108138f7b870SPatrick McHardy } 108238f7b870SPatrick McHardy if (ops->validate) { 108338f7b870SPatrick McHardy err = ops->validate(tb, data); 108438f7b870SPatrick McHardy if (err < 0) 108538f7b870SPatrick McHardy return err; 108638f7b870SPatrick McHardy } 108738f7b870SPatrick McHardy } 108838f7b870SPatrick McHardy 108938f7b870SPatrick McHardy if (dev) { 109038f7b870SPatrick McHardy int modified = 0; 109138f7b870SPatrick McHardy 109238f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_EXCL) 109338f7b870SPatrick McHardy return -EEXIST; 109438f7b870SPatrick McHardy if (nlh->nlmsg_flags & NLM_F_REPLACE) 109538f7b870SPatrick McHardy return -EOPNOTSUPP; 109638f7b870SPatrick McHardy 109738f7b870SPatrick McHardy if (linkinfo[IFLA_INFO_DATA]) { 109838f7b870SPatrick McHardy if (!ops || ops != dev->rtnl_link_ops || 109938f7b870SPatrick McHardy !ops->changelink) 110038f7b870SPatrick McHardy return -EOPNOTSUPP; 110138f7b870SPatrick McHardy 110238f7b870SPatrick McHardy err = ops->changelink(dev, tb, data); 110338f7b870SPatrick McHardy if (err < 0) 110438f7b870SPatrick McHardy return err; 110538f7b870SPatrick McHardy modified = 1; 110638f7b870SPatrick McHardy } 110738f7b870SPatrick McHardy 110838f7b870SPatrick McHardy return do_setlink(dev, ifm, tb, ifname, modified); 110938f7b870SPatrick McHardy } 111038f7b870SPatrick McHardy 111138f7b870SPatrick McHardy if (!(nlh->nlmsg_flags & NLM_F_CREATE)) 111238f7b870SPatrick McHardy return -ENODEV; 111338f7b870SPatrick McHardy 111438f7b870SPatrick McHardy if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change) 111538f7b870SPatrick McHardy return -EOPNOTSUPP; 11160e06877cSPatrick McHardy if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 111738f7b870SPatrick McHardy return -EOPNOTSUPP; 111838f7b870SPatrick McHardy 111938f7b870SPatrick McHardy if (!ops) { 112038f7b870SPatrick McHardy #ifdef CONFIG_KMOD 112138f7b870SPatrick McHardy if (kind[0]) { 112238f7b870SPatrick McHardy __rtnl_unlock(); 112338f7b870SPatrick McHardy request_module("rtnl-link-%s", kind); 112438f7b870SPatrick McHardy rtnl_lock(); 112538f7b870SPatrick McHardy ops = rtnl_link_ops_get(kind); 112638f7b870SPatrick McHardy if (ops) 112738f7b870SPatrick McHardy goto replay; 112838f7b870SPatrick McHardy } 112938f7b870SPatrick McHardy #endif 113038f7b870SPatrick McHardy return -EOPNOTSUPP; 113138f7b870SPatrick McHardy } 113238f7b870SPatrick McHardy 113338f7b870SPatrick McHardy if (!ifname[0]) 113438f7b870SPatrick McHardy snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 113538f7b870SPatrick McHardy 1136881d966bSEric W. Biederman dev = rtnl_create_link(net, ifname, ops, tb); 113738f7b870SPatrick McHardy 1138e7199288SPavel Emelianov if (IS_ERR(dev)) 1139e7199288SPavel Emelianov err = PTR_ERR(dev); 1140e7199288SPavel Emelianov else if (ops->newlink) 114138f7b870SPatrick McHardy err = ops->newlink(dev, tb, data); 11422d85cba2SPatrick McHardy else 11432d85cba2SPatrick McHardy err = register_netdevice(dev); 1144e7199288SPavel Emelianov 1145e7199288SPavel Emelianov if (err < 0 && !IS_ERR(dev)) 114638f7b870SPatrick McHardy free_netdev(dev); 114738f7b870SPatrick McHardy return err; 114838f7b870SPatrick McHardy } 114938f7b870SPatrick McHardy } 115038f7b870SPatrick McHardy 1151b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1152711e2c33SJean Tourrilhes { 1153881d966bSEric W. Biederman struct net *net = skb->sk->sk_net; 1154b60c5115SThomas Graf struct ifinfomsg *ifm; 1155b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 1156b60c5115SThomas Graf struct net_device *dev = NULL; 1157b60c5115SThomas Graf struct sk_buff *nskb; 1158339bf98fSThomas Graf int err; 1159711e2c33SJean Tourrilhes 1160b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1161b60c5115SThomas Graf if (err < 0) 11629918f230SEric Sesterhenn return err; 1163b60c5115SThomas Graf 1164b60c5115SThomas Graf ifm = nlmsg_data(nlh); 116551055be8SPatrick McHardy if (ifm->ifi_index > 0) { 1166881d966bSEric W. Biederman dev = dev_get_by_index(net, ifm->ifi_index); 1167b60c5115SThomas Graf if (dev == NULL) 1168711e2c33SJean Tourrilhes return -ENODEV; 1169b60c5115SThomas Graf } else 1170b60c5115SThomas Graf return -EINVAL; 1171b60c5115SThomas Graf 117238f7b870SPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 1173b60c5115SThomas Graf if (nskb == NULL) { 1174b60c5115SThomas Graf err = -ENOBUFS; 1175b60c5115SThomas Graf goto errout; 1176b60c5115SThomas Graf } 1177711e2c33SJean Tourrilhes 1178575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1179575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 118026932566SPatrick McHardy if (err < 0) { 118126932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 118226932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 118326932566SPatrick McHardy kfree_skb(nskb); 118426932566SPatrick McHardy goto errout; 118526932566SPatrick McHardy } 1186b974179aSPatrick McHardy err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); 1187b60c5115SThomas Graf errout: 1188711e2c33SJean Tourrilhes dev_put(dev); 1189b60c5115SThomas Graf 1190711e2c33SJean Tourrilhes return err; 1191711e2c33SJean Tourrilhes } 1192711e2c33SJean Tourrilhes 119342bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 11941da177e4SLinus Torvalds { 11951da177e4SLinus Torvalds int idx; 11961da177e4SLinus Torvalds int s_idx = cb->family; 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds if (s_idx == 0) 11991da177e4SLinus Torvalds s_idx = 1; 12001da177e4SLinus Torvalds for (idx=1; idx<NPROTO; idx++) { 12011da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 12021da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 12031da177e4SLinus Torvalds continue; 1204e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 1205e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 12061da177e4SLinus Torvalds continue; 12071da177e4SLinus Torvalds if (idx > s_idx) 12081da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 1209e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 12101da177e4SLinus Torvalds break; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds cb->family = idx; 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds return skb->len; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 12181da177e4SLinus Torvalds { 12191da177e4SLinus Torvalds struct sk_buff *skb; 12200ec6d3f4SThomas Graf int err = -ENOBUFS; 12211da177e4SLinus Torvalds 122238f7b870SPatrick McHardy skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); 12230ec6d3f4SThomas Graf if (skb == NULL) 12240ec6d3f4SThomas Graf goto errout; 12251da177e4SLinus Torvalds 1226575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 122726932566SPatrick McHardy if (err < 0) { 122826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 122926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 123026932566SPatrick McHardy kfree_skb(skb); 123126932566SPatrick McHardy goto errout; 123226932566SPatrick McHardy } 12330ec6d3f4SThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 12340ec6d3f4SThomas Graf errout: 12350ec6d3f4SThomas Graf if (err < 0) 12360ec6d3f4SThomas Graf rtnl_set_sk_err(RTNLGRP_LINK, err); 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 12401da177e4SLinus Torvalds static struct rtattr **rta_buf; 12411da177e4SLinus Torvalds static int rtattr_max; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds /* Process one rtnetlink message. */ 12441da177e4SLinus Torvalds 12451d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 12461da177e4SLinus Torvalds { 1247e2849863SThomas Graf rtnl_doit_func doit; 12481da177e4SLinus Torvalds int sz_idx, kind; 12491da177e4SLinus Torvalds int min_len; 12501da177e4SLinus Torvalds int family; 12511da177e4SLinus Torvalds int type; 12521c2d670fSPatrick McHardy int err; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds type = nlh->nlmsg_type; 12551da177e4SLinus Torvalds if (type > RTM_MAX) 1256038890feSThomas Graf return -EOPNOTSUPP; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds type -= RTM_BASE; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 12611da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 12621da177e4SLinus Torvalds return 0; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; 12651d00a4ebSThomas Graf if (family >= NPROTO) 12661d00a4ebSThomas Graf return -EAFNOSUPPORT; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds sz_idx = type>>2; 12691da177e4SLinus Torvalds kind = type&3; 12701da177e4SLinus Torvalds 12711d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 12721d00a4ebSThomas Graf return -EPERM; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 1275e2849863SThomas Graf rtnl_dumpit_func dumpit; 12761da177e4SLinus Torvalds 1277e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 1278e2849863SThomas Graf if (dumpit == NULL) 1279038890feSThomas Graf return -EOPNOTSUPP; 12801da177e4SLinus Torvalds 12811c2d670fSPatrick McHardy __rtnl_unlock(); 12821c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 12831c2d670fSPatrick McHardy rtnl_lock(); 12841c2d670fSPatrick McHardy return err; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 12901da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 12911d00a4ebSThomas Graf return -EINVAL; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 12941da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 12951da177e4SLinus Torvalds struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 12981da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 12991da177e4SLinus Torvalds if (flavor) { 13001da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 13011d00a4ebSThomas Graf return -EINVAL; 13021da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 1308e2849863SThomas Graf doit = rtnl_get_doit(family, type); 1309e2849863SThomas Graf if (doit == NULL) 1310038890feSThomas Graf return -EOPNOTSUPP; 13111da177e4SLinus Torvalds 13121d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 1315cd40b7d3SDenis V. Lunev static void rtnetlink_rcv(struct sk_buff *skb) 13161da177e4SLinus Torvalds { 13171536cc0dSDenis V. Lunev rtnl_lock(); 1318cd40b7d3SDenis V. Lunev netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 13191536cc0dSDenis V. Lunev rtnl_unlock(); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds struct net_device *dev = ptr; 1325e9dc8653SEric W. Biederman 1326e9dc8653SEric W. Biederman if (dev->nd_net != &init_net) 1327e9dc8653SEric W. Biederman return NOTIFY_DONE; 1328e9dc8653SEric W. Biederman 13291da177e4SLinus Torvalds switch (event) { 13301da177e4SLinus Torvalds case NETDEV_UNREGISTER: 13311da177e4SLinus Torvalds rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 13321da177e4SLinus Torvalds break; 13331da177e4SLinus Torvalds case NETDEV_REGISTER: 13341da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 13351da177e4SLinus Torvalds break; 13361da177e4SLinus Torvalds case NETDEV_UP: 13371da177e4SLinus Torvalds case NETDEV_DOWN: 13381da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 13391da177e4SLinus Torvalds break; 13401da177e4SLinus Torvalds case NETDEV_CHANGE: 13411da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 13421da177e4SLinus Torvalds break; 13431da177e4SLinus Torvalds default: 13441da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 13451da177e4SLinus Torvalds break; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds return NOTIFY_DONE; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 13511da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 13521da177e4SLinus Torvalds }; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds void __init rtnetlink_init(void) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds int i; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds rtattr_max = 0; 13591da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 13601da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 13611da177e4SLinus Torvalds rtattr_max = rta_max[i]; 13621da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 13631da177e4SLinus Torvalds if (!rta_buf) 13641da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 13651da177e4SLinus Torvalds 1366b4b51029SEric W. Biederman rtnl = netlink_kernel_create(&init_net, NETLINK_ROUTE, RTNLGRP_MAX, 1367b4b51029SEric W. Biederman rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 13681da177e4SLinus Torvalds if (rtnl == NULL) 13691da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 13701da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 13711da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 1372340d17fcSThomas Graf 1373340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 1374340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 137538f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); 137638f7b870SPatrick McHardy rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); 1377687ad8ccSThomas Graf 1378687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 1379687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill); 13831da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy); 13841da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse); 13852371baa4SPatrick McHardy EXPORT_SYMBOL(__rtattr_parse_nested_compat); 13861da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics); 13871da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock); 13886756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock); 13891da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock); 13902942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast); 139197676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify); 139297676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err); 1393e7199288SPavel Emelianov EXPORT_SYMBOL(rtnl_create_link); 1394e7199288SPavel Emelianov EXPORT_SYMBOL(ifla_policy); 1395