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> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <asm/uaccess.h> 401da177e4SLinus Torvalds #include <asm/system.h> 411da177e4SLinus Torvalds #include <asm/string.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> 541da177e4SLinus Torvalds 55e2849863SThomas Graf struct rtnl_link 56e2849863SThomas Graf { 57e2849863SThomas Graf rtnl_doit_func doit; 58e2849863SThomas Graf rtnl_dumpit_func dumpit; 59e2849863SThomas Graf }; 60e2849863SThomas Graf 616756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 6256fc85acSThomas Graf static struct sock *rtnl; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds void rtnl_lock(void) 651da177e4SLinus Torvalds { 666756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 671da177e4SLinus Torvalds } 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 { 766756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 776756ae4bSStephen Hemminger if (rtnl && rtnl->sk_receive_queue.qlen) 786756ae4bSStephen Hemminger rtnl->sk_data_ready(rtnl, 0); 791da177e4SLinus Torvalds netdev_run_todo(); 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 826756ae4bSStephen Hemminger int rtnl_trylock(void) 836756ae4bSStephen Hemminger { 846756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 856756ae4bSStephen Hemminger } 866756ae4bSStephen Hemminger 871da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds memset(tb, 0, sizeof(struct rtattr*)*maxattr); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds while (RTA_OK(rta, len)) { 921da177e4SLinus Torvalds unsigned flavor = rta->rta_type; 931da177e4SLinus Torvalds if (flavor && flavor <= maxattr) 941da177e4SLinus Torvalds tb[flavor-1] = rta; 951da177e4SLinus Torvalds rta = RTA_NEXT(rta, len); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds return 0; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 10042bad1daSAdrian Bunk static struct rtnl_link *rtnl_msg_handlers[NPROTO]; 101e2849863SThomas Graf 102e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 103e2849863SThomas Graf { 104e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 105e2849863SThomas Graf 106e2849863SThomas Graf /* 107e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 108e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 109e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 110e2849863SThomas Graf */ 111e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 112e2849863SThomas Graf 113e2849863SThomas Graf return msgindex; 114e2849863SThomas Graf } 115e2849863SThomas Graf 116e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 117e2849863SThomas Graf { 118e2849863SThomas Graf struct rtnl_link *tab; 119e2849863SThomas Graf 120e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 12151057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 122e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 123e2849863SThomas Graf 12451057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 125e2849863SThomas Graf } 126e2849863SThomas Graf 127e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 128e2849863SThomas Graf { 129e2849863SThomas Graf struct rtnl_link *tab; 130e2849863SThomas Graf 131e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 13251057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 133e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 134e2849863SThomas Graf 13551057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 136e2849863SThomas Graf } 137e2849863SThomas Graf 138e2849863SThomas Graf /** 139e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 140e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 141e2849863SThomas Graf * @msgtype: rtnetlink message type 142e2849863SThomas Graf * @doit: Function pointer called for each request message 143e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 144e2849863SThomas Graf * 145e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 146e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 147e2849863SThomas Graf * specified protocol family and message type is received. 148e2849863SThomas Graf * 149e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 150e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 151e2849863SThomas Graf * family exists. 152e2849863SThomas Graf * 153e2849863SThomas Graf * Returns 0 on success or a negative error code. 154e2849863SThomas Graf */ 155e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 156e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 157e2849863SThomas Graf { 158e2849863SThomas Graf struct rtnl_link *tab; 159e2849863SThomas Graf int msgindex; 160e2849863SThomas Graf 161e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 162e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 163e2849863SThomas Graf 164e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 165e2849863SThomas Graf if (tab == NULL) { 166e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 167e2849863SThomas Graf if (tab == NULL) 168e2849863SThomas Graf return -ENOBUFS; 169e2849863SThomas Graf 170e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 171e2849863SThomas Graf } 172e2849863SThomas Graf 173e2849863SThomas Graf if (doit) 174e2849863SThomas Graf tab[msgindex].doit = doit; 175e2849863SThomas Graf 176e2849863SThomas Graf if (dumpit) 177e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 178e2849863SThomas Graf 179e2849863SThomas Graf return 0; 180e2849863SThomas Graf } 181e2849863SThomas Graf 182e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 183e2849863SThomas Graf 184e2849863SThomas Graf /** 185e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 186e2849863SThomas Graf * 187e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 188e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 189e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 190e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 191e2849863SThomas Graf * of memory implies no sense in continueing. 192e2849863SThomas Graf */ 193e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 194e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 195e2849863SThomas Graf { 196e2849863SThomas Graf if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) 197e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 198e2849863SThomas Graf "protocol = %d, message type = %d\n", 199e2849863SThomas Graf protocol, msgtype); 200e2849863SThomas Graf } 201e2849863SThomas Graf 202e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 203e2849863SThomas Graf 204e2849863SThomas Graf /** 205e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 206e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 207e2849863SThomas Graf * @msgtype: rtnetlink message type 208e2849863SThomas Graf * 209e2849863SThomas Graf * Returns 0 on success or a negative error code. 210e2849863SThomas Graf */ 211e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 212e2849863SThomas Graf { 213e2849863SThomas Graf int msgindex; 214e2849863SThomas Graf 215e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 216e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 217e2849863SThomas Graf 218e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 219e2849863SThomas Graf return -ENOENT; 220e2849863SThomas Graf 221e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 222e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 223e2849863SThomas Graf 224e2849863SThomas Graf return 0; 225e2849863SThomas Graf } 226e2849863SThomas Graf 227e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 228e2849863SThomas Graf 229e2849863SThomas Graf /** 230e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 231e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 232e2849863SThomas Graf * 233e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 234e2849863SThomas Graf * of a certain protocol family. 235e2849863SThomas Graf */ 236e2849863SThomas Graf void rtnl_unregister_all(int protocol) 237e2849863SThomas Graf { 238e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 239e2849863SThomas Graf 240e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 241e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 242e2849863SThomas Graf } 243e2849863SThomas Graf 244e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2451da177e4SLinus Torvalds 246db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 2471da177e4SLinus Torvalds { 248f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 249f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 250f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 25114c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 252f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 253f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 254f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 255f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 256f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 257f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 2581da177e4SLinus Torvalds }; 2591da177e4SLinus Torvalds 260db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 2611da177e4SLinus Torvalds { 262f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 263f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 264f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 26514c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 266f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 267f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 268f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 269f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 2701da177e4SLinus Torvalds }; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds struct rtattr *rta; 2751da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); 2781da177e4SLinus Torvalds rta->rta_type = attrtype; 2791da177e4SLinus Torvalds rta->rta_len = size; 2801da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 281b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds size_t ret = RTA_PAYLOAD(rta); 2871da177e4SLinus Torvalds char *src = RTA_DATA(rta); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds if (ret > 0 && src[ret - 1] == '\0') 2901da177e4SLinus Torvalds ret--; 2911da177e4SLinus Torvalds if (size > 0) { 2921da177e4SLinus Torvalds size_t len = (ret >= size) ? size - 1 : ret; 2931da177e4SLinus Torvalds memset(dest, 0, size); 2941da177e4SLinus Torvalds memcpy(dest, src, len); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds return ret; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds int err = 0; 3021da177e4SLinus Torvalds 303ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 3041da177e4SLinus Torvalds if (echo) 3051da177e4SLinus Torvalds atomic_inc(&skb->users); 3061da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 3071da177e4SLinus Torvalds if (echo) 3081da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 3091da177e4SLinus Torvalds return err; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3122942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid) 3132942e900SThomas Graf { 3142942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 3152942e900SThomas Graf } 3162942e900SThomas Graf 31797676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, 31897676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 31997676b6bSThomas Graf { 32097676b6bSThomas Graf int report = 0; 32197676b6bSThomas Graf 32297676b6bSThomas Graf if (nlh) 32397676b6bSThomas Graf report = nlmsg_report(nlh); 32497676b6bSThomas Graf 32597676b6bSThomas Graf return nlmsg_notify(rtnl, skb, pid, group, report, flags); 32697676b6bSThomas Graf } 32797676b6bSThomas Graf 32897676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error) 32997676b6bSThomas Graf { 33097676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 33197676b6bSThomas Graf } 33297676b6bSThomas Graf 3331da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 3341da177e4SLinus Torvalds { 3352d7202bfSThomas Graf struct nlattr *mx; 3362d7202bfSThomas Graf int i, valid = 0; 3371da177e4SLinus Torvalds 3382d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 3392d7202bfSThomas Graf if (mx == NULL) 3402d7202bfSThomas Graf return -ENOBUFS; 3412d7202bfSThomas Graf 3421da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 3432d7202bfSThomas Graf if (metrics[i]) { 3442d7202bfSThomas Graf valid++; 3452d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 3461da177e4SLinus Torvalds } 3472d7202bfSThomas Graf } 3481da177e4SLinus Torvalds 349a57d27fcSDavid S. Miller if (!valid) { 350a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 351a57d27fcSDavid S. Miller return 0; 352a57d27fcSDavid S. Miller } 3532d7202bfSThomas Graf 3542d7202bfSThomas Graf return nla_nest_end(skb, mx); 3552d7202bfSThomas Graf 3562d7202bfSThomas Graf nla_put_failure: 3572d7202bfSThomas Graf return nla_nest_cancel(skb, mx); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 361e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 362e3703b3dSThomas Graf { 363e3703b3dSThomas Graf struct rta_cacheinfo ci = { 364e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 365e3703b3dSThomas Graf .rta_used = dst->__use, 366e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 367e3703b3dSThomas Graf .rta_error = error, 368e3703b3dSThomas Graf .rta_id = id, 369e3703b3dSThomas Graf .rta_ts = ts, 370e3703b3dSThomas Graf .rta_tsage = tsage, 371e3703b3dSThomas Graf }; 372e3703b3dSThomas Graf 373e3703b3dSThomas Graf if (expires) 374e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 375e3703b3dSThomas Graf 376e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 377e3703b3dSThomas Graf } 378e3703b3dSThomas Graf 379e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 3801da177e4SLinus Torvalds 381b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition) 382b00055aaSStefan Rompf { 383b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 384b00055aaSStefan Rompf 385b00055aaSStefan Rompf switch(transition) { 386b00055aaSStefan Rompf case IF_OPER_UP: 387b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 388b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 389b00055aaSStefan Rompf !netif_dormant(dev)) 390b00055aaSStefan Rompf operstate = IF_OPER_UP; 391b00055aaSStefan Rompf break; 392b00055aaSStefan Rompf 393b00055aaSStefan Rompf case IF_OPER_DORMANT: 394b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 395b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 396b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 397b00055aaSStefan Rompf break; 3983ff50b79SStephen Hemminger } 399b00055aaSStefan Rompf 400b00055aaSStefan Rompf if (dev->operstate != operstate) { 401b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 402b00055aaSStefan Rompf dev->operstate = operstate; 403b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 404b00055aaSStefan Rompf netdev_state_change(dev); 405b00055aaSStefan Rompf } 406b00055aaSStefan Rompf } 407b00055aaSStefan Rompf 408b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 409b60c5115SThomas Graf struct net_device_stats *b) 4101da177e4SLinus Torvalds { 411b60c5115SThomas Graf a->rx_packets = b->rx_packets; 412b60c5115SThomas Graf a->tx_packets = b->tx_packets; 413b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 414b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 415b60c5115SThomas Graf a->rx_errors = b->rx_errors; 416b60c5115SThomas Graf a->tx_errors = b->tx_errors; 417b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 418b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 419b60c5115SThomas Graf 420b60c5115SThomas Graf a->multicast = b->multicast; 421b60c5115SThomas Graf a->collisions = b->collisions; 422b60c5115SThomas Graf 423b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 424b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 425b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 426b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 427b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 428b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 429b60c5115SThomas Graf 430b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 431b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 432b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 433b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 434b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 435b60c5115SThomas Graf 436b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 437b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 438b60c5115SThomas Graf }; 439b60c5115SThomas Graf 440575c3e2aSPatrick McHardy static inline size_t if_nlmsg_size(void) 441339bf98fSThomas Graf { 442339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 443339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 444339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 445339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 446339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 447339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 448339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 449339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 450339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 451339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 452339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 453339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 454339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 455575c3e2aSPatrick McHardy + nla_total_size(1); /* IFLA_LINKMODE */ 456339bf98fSThomas Graf } 457339bf98fSThomas Graf 458b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 459575c3e2aSPatrick McHardy int type, u32 pid, u32 seq, u32 change, 460575c3e2aSPatrick McHardy unsigned int flags) 461b60c5115SThomas Graf { 462b60c5115SThomas Graf struct ifinfomsg *ifm; 4631da177e4SLinus Torvalds struct nlmsghdr *nlh; 4641da177e4SLinus Torvalds 465b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 466b60c5115SThomas Graf if (nlh == NULL) 46726932566SPatrick McHardy return -EMSGSIZE; 4681da177e4SLinus Torvalds 469b60c5115SThomas Graf ifm = nlmsg_data(nlh); 470b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 471b60c5115SThomas Graf ifm->__ifi_pad = 0; 472b60c5115SThomas Graf ifm->ifi_type = dev->type; 473b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 474b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 475b60c5115SThomas Graf ifm->ifi_change = change; 4761da177e4SLinus Torvalds 477b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 478b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 479b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight); 480b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 481b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 482b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 483b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 4841da177e4SLinus Torvalds 485b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 486b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 4871da177e4SLinus Torvalds 488b60c5115SThomas Graf if (dev->master) 489b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 490b60c5115SThomas Graf 491b60c5115SThomas Graf if (dev->qdisc_sleeping) 492b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); 493b00055aaSStefan Rompf 494b00055aaSStefan Rompf if (1) { 4951da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 4961da177e4SLinus Torvalds .mem_start = dev->mem_start, 4971da177e4SLinus Torvalds .mem_end = dev->mem_end, 4981da177e4SLinus Torvalds .base_addr = dev->base_addr, 4991da177e4SLinus Torvalds .irq = dev->irq, 5001da177e4SLinus Torvalds .dma = dev->dma, 5011da177e4SLinus Torvalds .port = dev->if_port, 5021da177e4SLinus Torvalds }; 503b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (dev->addr_len) { 507b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 508b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (dev->get_stats) { 512b60c5115SThomas Graf struct net_device_stats *stats = dev->get_stats(dev); 5131da177e4SLinus Torvalds if (stats) { 514b60c5115SThomas Graf struct nlattr *attr; 5151da177e4SLinus Torvalds 516b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 517b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 518b60c5115SThomas Graf if (attr == NULL) 519b60c5115SThomas Graf goto nla_put_failure; 520b60c5115SThomas Graf 521b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 525b60c5115SThomas Graf return nlmsg_end(skb, nlh); 526b60c5115SThomas Graf 527b60c5115SThomas Graf nla_put_failure: 52826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 52926932566SPatrick McHardy return -EMSGSIZE; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 532b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds int idx; 5351da177e4SLinus Torvalds int s_idx = cb->args[0]; 5361da177e4SLinus Torvalds struct net_device *dev; 5371da177e4SLinus Torvalds 5387562f876SPavel Emelianov idx = 0; 5397562f876SPavel Emelianov for_each_netdev(dev) { 5401da177e4SLinus Torvalds if (idx < s_idx) 5417562f876SPavel Emelianov goto cont; 542575c3e2aSPatrick McHardy if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 543b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 544b60c5115SThomas Graf cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) 5451da177e4SLinus Torvalds break; 5467562f876SPavel Emelianov cont: 5477562f876SPavel Emelianov idx++; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds cb->args[0] = idx; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds return skb->len; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 554ef7c79edSPatrick McHardy static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 5555176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 5565176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 557da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 558da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 559da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 560da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 561da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 562da5e0494SThomas Graf }; 5631da177e4SLinus Torvalds 564*0157f60cSPatrick McHardy static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 565*0157f60cSPatrick McHardy struct nlattr **tb, char *ifname) 566*0157f60cSPatrick McHardy { 567*0157f60cSPatrick McHardy int modified = 0, send_addr_notify = 0; 568*0157f60cSPatrick McHardy int err; 569*0157f60cSPatrick McHardy 570*0157f60cSPatrick McHardy if (tb[IFLA_MAP]) { 571*0157f60cSPatrick McHardy struct rtnl_link_ifmap *u_map; 572*0157f60cSPatrick McHardy struct ifmap k_map; 573*0157f60cSPatrick McHardy 574*0157f60cSPatrick McHardy if (!dev->set_config) { 575*0157f60cSPatrick McHardy err = -EOPNOTSUPP; 576*0157f60cSPatrick McHardy goto errout; 577*0157f60cSPatrick McHardy } 578*0157f60cSPatrick McHardy 579*0157f60cSPatrick McHardy if (!netif_device_present(dev)) { 580*0157f60cSPatrick McHardy err = -ENODEV; 581*0157f60cSPatrick McHardy goto errout; 582*0157f60cSPatrick McHardy } 583*0157f60cSPatrick McHardy 584*0157f60cSPatrick McHardy u_map = nla_data(tb[IFLA_MAP]); 585*0157f60cSPatrick McHardy k_map.mem_start = (unsigned long) u_map->mem_start; 586*0157f60cSPatrick McHardy k_map.mem_end = (unsigned long) u_map->mem_end; 587*0157f60cSPatrick McHardy k_map.base_addr = (unsigned short) u_map->base_addr; 588*0157f60cSPatrick McHardy k_map.irq = (unsigned char) u_map->irq; 589*0157f60cSPatrick McHardy k_map.dma = (unsigned char) u_map->dma; 590*0157f60cSPatrick McHardy k_map.port = (unsigned char) u_map->port; 591*0157f60cSPatrick McHardy 592*0157f60cSPatrick McHardy err = dev->set_config(dev, &k_map); 593*0157f60cSPatrick McHardy if (err < 0) 594*0157f60cSPatrick McHardy goto errout; 595*0157f60cSPatrick McHardy 596*0157f60cSPatrick McHardy modified = 1; 597*0157f60cSPatrick McHardy } 598*0157f60cSPatrick McHardy 599*0157f60cSPatrick McHardy if (tb[IFLA_ADDRESS]) { 600*0157f60cSPatrick McHardy struct sockaddr *sa; 601*0157f60cSPatrick McHardy int len; 602*0157f60cSPatrick McHardy 603*0157f60cSPatrick McHardy if (!dev->set_mac_address) { 604*0157f60cSPatrick McHardy err = -EOPNOTSUPP; 605*0157f60cSPatrick McHardy goto errout; 606*0157f60cSPatrick McHardy } 607*0157f60cSPatrick McHardy 608*0157f60cSPatrick McHardy if (!netif_device_present(dev)) { 609*0157f60cSPatrick McHardy err = -ENODEV; 610*0157f60cSPatrick McHardy goto errout; 611*0157f60cSPatrick McHardy } 612*0157f60cSPatrick McHardy 613*0157f60cSPatrick McHardy len = sizeof(sa_family_t) + dev->addr_len; 614*0157f60cSPatrick McHardy sa = kmalloc(len, GFP_KERNEL); 615*0157f60cSPatrick McHardy if (!sa) { 616*0157f60cSPatrick McHardy err = -ENOMEM; 617*0157f60cSPatrick McHardy goto errout; 618*0157f60cSPatrick McHardy } 619*0157f60cSPatrick McHardy sa->sa_family = dev->type; 620*0157f60cSPatrick McHardy memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 621*0157f60cSPatrick McHardy dev->addr_len); 622*0157f60cSPatrick McHardy err = dev->set_mac_address(dev, sa); 623*0157f60cSPatrick McHardy kfree(sa); 624*0157f60cSPatrick McHardy if (err) 625*0157f60cSPatrick McHardy goto errout; 626*0157f60cSPatrick McHardy send_addr_notify = 1; 627*0157f60cSPatrick McHardy modified = 1; 628*0157f60cSPatrick McHardy } 629*0157f60cSPatrick McHardy 630*0157f60cSPatrick McHardy if (tb[IFLA_MTU]) { 631*0157f60cSPatrick McHardy err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 632*0157f60cSPatrick McHardy if (err < 0) 633*0157f60cSPatrick McHardy goto errout; 634*0157f60cSPatrick McHardy modified = 1; 635*0157f60cSPatrick McHardy } 636*0157f60cSPatrick McHardy 637*0157f60cSPatrick McHardy /* 638*0157f60cSPatrick McHardy * Interface selected by interface index but interface 639*0157f60cSPatrick McHardy * name provided implies that a name change has been 640*0157f60cSPatrick McHardy * requested. 641*0157f60cSPatrick McHardy */ 642*0157f60cSPatrick McHardy if (ifm->ifi_index > 0 && ifname[0]) { 643*0157f60cSPatrick McHardy err = dev_change_name(dev, ifname); 644*0157f60cSPatrick McHardy if (err < 0) 645*0157f60cSPatrick McHardy goto errout; 646*0157f60cSPatrick McHardy modified = 1; 647*0157f60cSPatrick McHardy } 648*0157f60cSPatrick McHardy 649*0157f60cSPatrick McHardy if (tb[IFLA_BROADCAST]) { 650*0157f60cSPatrick McHardy nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 651*0157f60cSPatrick McHardy send_addr_notify = 1; 652*0157f60cSPatrick McHardy } 653*0157f60cSPatrick McHardy 654*0157f60cSPatrick McHardy if (ifm->ifi_flags || ifm->ifi_change) { 655*0157f60cSPatrick McHardy unsigned int flags = ifm->ifi_flags; 656*0157f60cSPatrick McHardy 657*0157f60cSPatrick McHardy /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 658*0157f60cSPatrick McHardy if (ifm->ifi_change) 659*0157f60cSPatrick McHardy flags = (flags & ifm->ifi_change) | 660*0157f60cSPatrick McHardy (dev->flags & ~ifm->ifi_change); 661*0157f60cSPatrick McHardy dev_change_flags(dev, flags); 662*0157f60cSPatrick McHardy } 663*0157f60cSPatrick McHardy 664*0157f60cSPatrick McHardy if (tb[IFLA_TXQLEN]) 665*0157f60cSPatrick McHardy dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 666*0157f60cSPatrick McHardy 667*0157f60cSPatrick McHardy if (tb[IFLA_WEIGHT]) 668*0157f60cSPatrick McHardy dev->weight = nla_get_u32(tb[IFLA_WEIGHT]); 669*0157f60cSPatrick McHardy 670*0157f60cSPatrick McHardy if (tb[IFLA_OPERSTATE]) 671*0157f60cSPatrick McHardy set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 672*0157f60cSPatrick McHardy 673*0157f60cSPatrick McHardy if (tb[IFLA_LINKMODE]) { 674*0157f60cSPatrick McHardy write_lock_bh(&dev_base_lock); 675*0157f60cSPatrick McHardy dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 676*0157f60cSPatrick McHardy write_unlock_bh(&dev_base_lock); 677*0157f60cSPatrick McHardy } 678*0157f60cSPatrick McHardy 679*0157f60cSPatrick McHardy err = 0; 680*0157f60cSPatrick McHardy 681*0157f60cSPatrick McHardy errout: 682*0157f60cSPatrick McHardy if (err < 0 && modified && net_ratelimit()) 683*0157f60cSPatrick McHardy printk(KERN_WARNING "A link change request failed with " 684*0157f60cSPatrick McHardy "some changes comitted already. Interface %s may " 685*0157f60cSPatrick McHardy "have been left with an inconsistent configuration, " 686*0157f60cSPatrick McHardy "please check.\n", dev->name); 687*0157f60cSPatrick McHardy 688*0157f60cSPatrick McHardy if (send_addr_notify) 689*0157f60cSPatrick McHardy call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 690*0157f60cSPatrick McHardy return err; 691*0157f60cSPatrick McHardy } 692*0157f60cSPatrick McHardy 693da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 694da5e0494SThomas Graf { 695da5e0494SThomas Graf struct ifinfomsg *ifm; 696da5e0494SThomas Graf struct net_device *dev; 697*0157f60cSPatrick McHardy int err; 698da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 6991da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 7001da177e4SLinus Torvalds 701da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 702da5e0494SThomas Graf if (err < 0) 703da5e0494SThomas Graf goto errout; 7041da177e4SLinus Torvalds 7055176f91eSThomas Graf if (tb[IFLA_IFNAME]) 7065176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 70778e5b891SPatrick McHardy else 70878e5b891SPatrick McHardy ifname[0] = '\0'; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds err = -EINVAL; 711da5e0494SThomas Graf ifm = nlmsg_data(nlh); 71251055be8SPatrick McHardy if (ifm->ifi_index > 0) 713da5e0494SThomas Graf dev = dev_get_by_index(ifm->ifi_index); 714da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 715da5e0494SThomas Graf dev = dev_get_by_name(ifname); 716da5e0494SThomas Graf else 717da5e0494SThomas Graf goto errout; 7181da177e4SLinus Torvalds 719da5e0494SThomas Graf if (dev == NULL) { 720da5e0494SThomas Graf err = -ENODEV; 721da5e0494SThomas Graf goto errout; 722da5e0494SThomas Graf } 7231da177e4SLinus Torvalds 724da5e0494SThomas Graf if (tb[IFLA_ADDRESS] && 725da5e0494SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 726da5e0494SThomas Graf goto errout_dev; 727da5e0494SThomas Graf 728da5e0494SThomas Graf if (tb[IFLA_BROADCAST] && 729da5e0494SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 730da5e0494SThomas Graf goto errout_dev; 731da5e0494SThomas Graf 732*0157f60cSPatrick McHardy err = do_setlink(dev, ifm, tb, ifname); 733da5e0494SThomas Graf errout_dev: 7341da177e4SLinus Torvalds dev_put(dev); 735da5e0494SThomas Graf errout: 7361da177e4SLinus Torvalds return err; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 739b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 740711e2c33SJean Tourrilhes { 741b60c5115SThomas Graf struct ifinfomsg *ifm; 742b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 743b60c5115SThomas Graf struct net_device *dev = NULL; 744b60c5115SThomas Graf struct sk_buff *nskb; 745339bf98fSThomas Graf int err; 746711e2c33SJean Tourrilhes 747b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 748b60c5115SThomas Graf if (err < 0) 7499918f230SEric Sesterhenn return err; 750b60c5115SThomas Graf 751b60c5115SThomas Graf ifm = nlmsg_data(nlh); 75251055be8SPatrick McHardy if (ifm->ifi_index > 0) { 753711e2c33SJean Tourrilhes dev = dev_get_by_index(ifm->ifi_index); 754b60c5115SThomas Graf if (dev == NULL) 755711e2c33SJean Tourrilhes return -ENODEV; 756b60c5115SThomas Graf } else 757b60c5115SThomas Graf return -EINVAL; 758b60c5115SThomas Graf 759575c3e2aSPatrick McHardy nskb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL); 760b60c5115SThomas Graf if (nskb == NULL) { 761b60c5115SThomas Graf err = -ENOBUFS; 762b60c5115SThomas Graf goto errout; 763b60c5115SThomas Graf } 764711e2c33SJean Tourrilhes 765575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 766575c3e2aSPatrick McHardy nlh->nlmsg_seq, 0, 0); 76726932566SPatrick McHardy if (err < 0) { 76826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 76926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 77026932566SPatrick McHardy kfree_skb(nskb); 77126932566SPatrick McHardy goto errout; 77226932566SPatrick McHardy } 773b974179aSPatrick McHardy err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); 774b60c5115SThomas Graf errout: 775711e2c33SJean Tourrilhes dev_put(dev); 776b60c5115SThomas Graf 777711e2c33SJean Tourrilhes return err; 778711e2c33SJean Tourrilhes } 779711e2c33SJean Tourrilhes 78042bad1daSAdrian Bunk static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 7811da177e4SLinus Torvalds { 7821da177e4SLinus Torvalds int idx; 7831da177e4SLinus Torvalds int s_idx = cb->family; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds if (s_idx == 0) 7861da177e4SLinus Torvalds s_idx = 1; 7871da177e4SLinus Torvalds for (idx=1; idx<NPROTO; idx++) { 7881da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 7891da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 7901da177e4SLinus Torvalds continue; 791e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 792e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 7931da177e4SLinus Torvalds continue; 7941da177e4SLinus Torvalds if (idx > s_idx) 7951da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 796e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 7971da177e4SLinus Torvalds break; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds cb->family = idx; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds return skb->len; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds struct sk_buff *skb; 8070ec6d3f4SThomas Graf int err = -ENOBUFS; 8081da177e4SLinus Torvalds 809575c3e2aSPatrick McHardy skb = nlmsg_new(if_nlmsg_size(), GFP_KERNEL); 8100ec6d3f4SThomas Graf if (skb == NULL) 8110ec6d3f4SThomas Graf goto errout; 8121da177e4SLinus Torvalds 813575c3e2aSPatrick McHardy err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); 81426932566SPatrick McHardy if (err < 0) { 81526932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 81626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 81726932566SPatrick McHardy kfree_skb(skb); 81826932566SPatrick McHardy goto errout; 81926932566SPatrick McHardy } 8200ec6d3f4SThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 8210ec6d3f4SThomas Graf errout: 8220ec6d3f4SThomas Graf if (err < 0) 8230ec6d3f4SThomas Graf rtnl_set_sk_err(RTNLGRP_LINK, err); 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 8271da177e4SLinus Torvalds static struct rtattr **rta_buf; 8281da177e4SLinus Torvalds static int rtattr_max; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* Process one rtnetlink message. */ 8311da177e4SLinus Torvalds 8321d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 8331da177e4SLinus Torvalds { 834e2849863SThomas Graf rtnl_doit_func doit; 8351da177e4SLinus Torvalds int sz_idx, kind; 8361da177e4SLinus Torvalds int min_len; 8371da177e4SLinus Torvalds int family; 8381da177e4SLinus Torvalds int type; 8391c2d670fSPatrick McHardy int err; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds type = nlh->nlmsg_type; 8421da177e4SLinus Torvalds if (type > RTM_MAX) 843038890feSThomas Graf return -EOPNOTSUPP; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds type -= RTM_BASE; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 8481da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 8491da177e4SLinus Torvalds return 0; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; 8521d00a4ebSThomas Graf if (family >= NPROTO) 8531d00a4ebSThomas Graf return -EAFNOSUPPORT; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds sz_idx = type>>2; 8561da177e4SLinus Torvalds kind = type&3; 8571da177e4SLinus Torvalds 8581d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 8591d00a4ebSThomas Graf return -EPERM; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 862e2849863SThomas Graf rtnl_dumpit_func dumpit; 8631da177e4SLinus Torvalds 864e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 865e2849863SThomas Graf if (dumpit == NULL) 866038890feSThomas Graf return -EOPNOTSUPP; 8671da177e4SLinus Torvalds 8681c2d670fSPatrick McHardy __rtnl_unlock(); 8691c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 8701c2d670fSPatrick McHardy rtnl_lock(); 8711c2d670fSPatrick McHardy return err; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 8771da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 8781d00a4ebSThomas Graf return -EINVAL; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 8811da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 8821da177e4SLinus Torvalds struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 8851da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 8861da177e4SLinus Torvalds if (flavor) { 8871da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 8881d00a4ebSThomas Graf return -EINVAL; 8891da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 895e2849863SThomas Graf doit = rtnl_get_doit(family, type); 896e2849863SThomas Graf if (doit == NULL) 897038890feSThomas Graf return -EOPNOTSUPP; 8981da177e4SLinus Torvalds 8991d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len) 9031da177e4SLinus Torvalds { 9049ac4a169SThomas Graf unsigned int qlen = 0; 9052a0a6ebeSHerbert Xu 9061da177e4SLinus Torvalds do { 9076756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 9089ac4a169SThomas Graf netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); 9096756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds netdev_run_todo(); 9122a0a6ebeSHerbert Xu } while (qlen); 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 9161da177e4SLinus Torvalds { 9171da177e4SLinus Torvalds struct net_device *dev = ptr; 9181da177e4SLinus Torvalds switch (event) { 9191da177e4SLinus Torvalds case NETDEV_UNREGISTER: 9201da177e4SLinus Torvalds rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 9211da177e4SLinus Torvalds break; 9221da177e4SLinus Torvalds case NETDEV_REGISTER: 9231da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 9241da177e4SLinus Torvalds break; 9251da177e4SLinus Torvalds case NETDEV_UP: 9261da177e4SLinus Torvalds case NETDEV_DOWN: 9271da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 9281da177e4SLinus Torvalds break; 9291da177e4SLinus Torvalds case NETDEV_CHANGE: 9301da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 9311da177e4SLinus Torvalds break; 9321da177e4SLinus Torvalds default: 9331da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 9341da177e4SLinus Torvalds break; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds return NOTIFY_DONE; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 9401da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 9411da177e4SLinus Torvalds }; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds void __init rtnetlink_init(void) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds int i; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds rtattr_max = 0; 9481da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 9491da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 9501da177e4SLinus Torvalds rtattr_max = rta_max[i]; 9511da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 9521da177e4SLinus Torvalds if (!rta_buf) 9531da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 9541da177e4SLinus Torvalds 95506628607SPatrick McHardy rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, 9561c2d670fSPatrick McHardy &rtnl_mutex, THIS_MODULE); 9571da177e4SLinus Torvalds if (rtnl == NULL) 9581da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 9591da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 9601da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 961340d17fcSThomas Graf 962340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 963340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 964687ad8ccSThomas Graf 965687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 966687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill); 9701da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy); 9711da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse); 9721da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics); 9731da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock); 9746756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock); 9751da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock); 9762942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast); 97797676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify); 97897676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err); 979