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> 54711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK 55711e2c33SJean Tourrilhes #include <linux/wireless.h> 56711e2c33SJean Tourrilhes #include <net/iw_handler.h> 57711e2c33SJean Tourrilhes #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ 581da177e4SLinus Torvalds 59e2849863SThomas Graf struct rtnl_link 60e2849863SThomas Graf { 61e2849863SThomas Graf rtnl_doit_func doit; 62e2849863SThomas Graf rtnl_dumpit_func dumpit; 63e2849863SThomas Graf }; 64e2849863SThomas Graf 656756ae4bSStephen Hemminger static DEFINE_MUTEX(rtnl_mutex); 6656fc85acSThomas Graf static struct sock *rtnl; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds void rtnl_lock(void) 691da177e4SLinus Torvalds { 706756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 736756ae4bSStephen Hemminger void __rtnl_unlock(void) 741da177e4SLinus Torvalds { 756756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds void rtnl_unlock(void) 791da177e4SLinus Torvalds { 806756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 816756ae4bSStephen Hemminger if (rtnl && rtnl->sk_receive_queue.qlen) 826756ae4bSStephen Hemminger rtnl->sk_data_ready(rtnl, 0); 831da177e4SLinus Torvalds netdev_run_todo(); 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds 866756ae4bSStephen Hemminger int rtnl_trylock(void) 876756ae4bSStephen Hemminger { 886756ae4bSStephen Hemminger return mutex_trylock(&rtnl_mutex); 896756ae4bSStephen Hemminger } 906756ae4bSStephen Hemminger 911da177e4SLinus Torvalds int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds memset(tb, 0, sizeof(struct rtattr*)*maxattr); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds while (RTA_OK(rta, len)) { 961da177e4SLinus Torvalds unsigned flavor = rta->rta_type; 971da177e4SLinus Torvalds if (flavor && flavor <= maxattr) 981da177e4SLinus Torvalds tb[flavor-1] = rta; 991da177e4SLinus Torvalds rta = RTA_NEXT(rta, len); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds return 0; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 104e2849863SThomas Graf struct rtnl_link *rtnl_msg_handlers[NPROTO]; 105e2849863SThomas Graf 106e2849863SThomas Graf static inline int rtm_msgindex(int msgtype) 107e2849863SThomas Graf { 108e2849863SThomas Graf int msgindex = msgtype - RTM_BASE; 109e2849863SThomas Graf 110e2849863SThomas Graf /* 111e2849863SThomas Graf * msgindex < 0 implies someone tried to register a netlink 112e2849863SThomas Graf * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 113e2849863SThomas Graf * the message type has not been added to linux/rtnetlink.h 114e2849863SThomas Graf */ 115e2849863SThomas Graf BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 116e2849863SThomas Graf 117e2849863SThomas Graf return msgindex; 118e2849863SThomas Graf } 119e2849863SThomas Graf 120e2849863SThomas Graf static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 121e2849863SThomas Graf { 122e2849863SThomas Graf struct rtnl_link *tab; 123e2849863SThomas Graf 124e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 12551057f2fSThomas Graf if (tab == NULL || tab[msgindex].doit == NULL) 126e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 127e2849863SThomas Graf 12851057f2fSThomas Graf return tab ? tab[msgindex].doit : NULL; 129e2849863SThomas Graf } 130e2849863SThomas Graf 131e2849863SThomas Graf static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 132e2849863SThomas Graf { 133e2849863SThomas Graf struct rtnl_link *tab; 134e2849863SThomas Graf 135e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 13651057f2fSThomas Graf if (tab == NULL || tab[msgindex].dumpit == NULL) 137e2849863SThomas Graf tab = rtnl_msg_handlers[PF_UNSPEC]; 138e2849863SThomas Graf 13951057f2fSThomas Graf return tab ? tab[msgindex].dumpit : NULL; 140e2849863SThomas Graf } 141e2849863SThomas Graf 142e2849863SThomas Graf /** 143e2849863SThomas Graf * __rtnl_register - Register a rtnetlink message type 144e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 145e2849863SThomas Graf * @msgtype: rtnetlink message type 146e2849863SThomas Graf * @doit: Function pointer called for each request message 147e2849863SThomas Graf * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 148e2849863SThomas Graf * 149e2849863SThomas Graf * Registers the specified function pointers (at least one of them has 150e2849863SThomas Graf * to be non-NULL) to be called whenever a request message for the 151e2849863SThomas Graf * specified protocol family and message type is received. 152e2849863SThomas Graf * 153e2849863SThomas Graf * The special protocol family PF_UNSPEC may be used to define fallback 154e2849863SThomas Graf * function pointers for the case when no entry for the specific protocol 155e2849863SThomas Graf * family exists. 156e2849863SThomas Graf * 157e2849863SThomas Graf * Returns 0 on success or a negative error code. 158e2849863SThomas Graf */ 159e2849863SThomas Graf int __rtnl_register(int protocol, int msgtype, 160e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 161e2849863SThomas Graf { 162e2849863SThomas Graf struct rtnl_link *tab; 163e2849863SThomas Graf int msgindex; 164e2849863SThomas Graf 165e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 166e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 167e2849863SThomas Graf 168e2849863SThomas Graf tab = rtnl_msg_handlers[protocol]; 169e2849863SThomas Graf if (tab == NULL) { 170e2849863SThomas Graf tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 171e2849863SThomas Graf if (tab == NULL) 172e2849863SThomas Graf return -ENOBUFS; 173e2849863SThomas Graf 174e2849863SThomas Graf rtnl_msg_handlers[protocol] = tab; 175e2849863SThomas Graf } 176e2849863SThomas Graf 177e2849863SThomas Graf if (doit) 178e2849863SThomas Graf tab[msgindex].doit = doit; 179e2849863SThomas Graf 180e2849863SThomas Graf if (dumpit) 181e2849863SThomas Graf tab[msgindex].dumpit = dumpit; 182e2849863SThomas Graf 183e2849863SThomas Graf return 0; 184e2849863SThomas Graf } 185e2849863SThomas Graf 186e2849863SThomas Graf EXPORT_SYMBOL_GPL(__rtnl_register); 187e2849863SThomas Graf 188e2849863SThomas Graf /** 189e2849863SThomas Graf * rtnl_register - Register a rtnetlink message type 190e2849863SThomas Graf * 191e2849863SThomas Graf * Identical to __rtnl_register() but panics on failure. This is useful 192e2849863SThomas Graf * as failure of this function is very unlikely, it can only happen due 193e2849863SThomas Graf * to lack of memory when allocating the chain to store all message 194e2849863SThomas Graf * handlers for a protocol. Meant for use in init functions where lack 195e2849863SThomas Graf * of memory implies no sense in continueing. 196e2849863SThomas Graf */ 197e2849863SThomas Graf void rtnl_register(int protocol, int msgtype, 198e2849863SThomas Graf rtnl_doit_func doit, rtnl_dumpit_func dumpit) 199e2849863SThomas Graf { 200e2849863SThomas Graf if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) 201e2849863SThomas Graf panic("Unable to register rtnetlink message handler, " 202e2849863SThomas Graf "protocol = %d, message type = %d\n", 203e2849863SThomas Graf protocol, msgtype); 204e2849863SThomas Graf } 205e2849863SThomas Graf 206e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_register); 207e2849863SThomas Graf 208e2849863SThomas Graf /** 209e2849863SThomas Graf * rtnl_unregister - Unregister a rtnetlink message type 210e2849863SThomas Graf * @protocol: Protocol family or PF_UNSPEC 211e2849863SThomas Graf * @msgtype: rtnetlink message type 212e2849863SThomas Graf * 213e2849863SThomas Graf * Returns 0 on success or a negative error code. 214e2849863SThomas Graf */ 215e2849863SThomas Graf int rtnl_unregister(int protocol, int msgtype) 216e2849863SThomas Graf { 217e2849863SThomas Graf int msgindex; 218e2849863SThomas Graf 219e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 220e2849863SThomas Graf msgindex = rtm_msgindex(msgtype); 221e2849863SThomas Graf 222e2849863SThomas Graf if (rtnl_msg_handlers[protocol] == NULL) 223e2849863SThomas Graf return -ENOENT; 224e2849863SThomas Graf 225e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].doit = NULL; 226e2849863SThomas Graf rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 227e2849863SThomas Graf 228e2849863SThomas Graf return 0; 229e2849863SThomas Graf } 230e2849863SThomas Graf 231e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister); 232e2849863SThomas Graf 233e2849863SThomas Graf /** 234e2849863SThomas Graf * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 235e2849863SThomas Graf * @protocol : Protocol family or PF_UNSPEC 236e2849863SThomas Graf * 237e2849863SThomas Graf * Identical to calling rtnl_unregster() for all registered message types 238e2849863SThomas Graf * of a certain protocol family. 239e2849863SThomas Graf */ 240e2849863SThomas Graf void rtnl_unregister_all(int protocol) 241e2849863SThomas Graf { 242e2849863SThomas Graf BUG_ON(protocol < 0 || protocol >= NPROTO); 243e2849863SThomas Graf 244e2849863SThomas Graf kfree(rtnl_msg_handlers[protocol]); 245e2849863SThomas Graf rtnl_msg_handlers[protocol] = NULL; 246e2849863SThomas Graf } 247e2849863SThomas Graf 248e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_unregister_all); 2491da177e4SLinus Torvalds 250db46edc6SThomas Graf static const int rtm_min[RTM_NR_FAMILIES] = 2511da177e4SLinus Torvalds { 252f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 253f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 254f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 25514c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 256f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 257f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 258f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 259f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 260f90a0a74SThomas Graf [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 261f90a0a74SThomas Graf [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 2621da177e4SLinus Torvalds }; 2631da177e4SLinus Torvalds 264db46edc6SThomas Graf static const int rta_max[RTM_NR_FAMILIES] = 2651da177e4SLinus Torvalds { 266f90a0a74SThomas Graf [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 267f90a0a74SThomas Graf [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 268f90a0a74SThomas Graf [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 26914c0b97dSThomas Graf [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 270f90a0a74SThomas Graf [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 271f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 272f90a0a74SThomas Graf [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 273f90a0a74SThomas Graf [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 2741da177e4SLinus Torvalds }; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds struct rtattr *rta; 2791da177e4SLinus Torvalds int size = RTA_LENGTH(attrlen); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); 2821da177e4SLinus Torvalds rta->rta_type = attrtype; 2831da177e4SLinus Torvalds rta->rta_len = size; 2841da177e4SLinus Torvalds memcpy(RTA_DATA(rta), data, attrlen); 285b3563c4fSPatrick McHardy memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds size_t ret = RTA_PAYLOAD(rta); 2911da177e4SLinus Torvalds char *src = RTA_DATA(rta); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds if (ret > 0 && src[ret - 1] == '\0') 2941da177e4SLinus Torvalds ret--; 2951da177e4SLinus Torvalds if (size > 0) { 2961da177e4SLinus Torvalds size_t len = (ret >= size) ? size - 1 : ret; 2971da177e4SLinus Torvalds memset(dest, 0, size); 2981da177e4SLinus Torvalds memcpy(dest, src, len); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds return ret; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds int err = 0; 3061da177e4SLinus Torvalds 307ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = group; 3081da177e4SLinus Torvalds if (echo) 3091da177e4SLinus Torvalds atomic_inc(&skb->users); 3101da177e4SLinus Torvalds netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 3111da177e4SLinus Torvalds if (echo) 3121da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 3131da177e4SLinus Torvalds return err; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3162942e900SThomas Graf int rtnl_unicast(struct sk_buff *skb, u32 pid) 3172942e900SThomas Graf { 3182942e900SThomas Graf return nlmsg_unicast(rtnl, skb, pid); 3192942e900SThomas Graf } 3202942e900SThomas Graf 32197676b6bSThomas Graf int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, 32297676b6bSThomas Graf struct nlmsghdr *nlh, gfp_t flags) 32397676b6bSThomas Graf { 32497676b6bSThomas Graf int report = 0; 32597676b6bSThomas Graf 32697676b6bSThomas Graf if (nlh) 32797676b6bSThomas Graf report = nlmsg_report(nlh); 32897676b6bSThomas Graf 32997676b6bSThomas Graf return nlmsg_notify(rtnl, skb, pid, group, report, flags); 33097676b6bSThomas Graf } 33197676b6bSThomas Graf 33297676b6bSThomas Graf void rtnl_set_sk_err(u32 group, int error) 33397676b6bSThomas Graf { 33497676b6bSThomas Graf netlink_set_err(rtnl, 0, group, error); 33597676b6bSThomas Graf } 33697676b6bSThomas Graf 3371da177e4SLinus Torvalds int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 3381da177e4SLinus Torvalds { 3392d7202bfSThomas Graf struct nlattr *mx; 3402d7202bfSThomas Graf int i, valid = 0; 3411da177e4SLinus Torvalds 3422d7202bfSThomas Graf mx = nla_nest_start(skb, RTA_METRICS); 3432d7202bfSThomas Graf if (mx == NULL) 3442d7202bfSThomas Graf return -ENOBUFS; 3452d7202bfSThomas Graf 3461da177e4SLinus Torvalds for (i = 0; i < RTAX_MAX; i++) { 3472d7202bfSThomas Graf if (metrics[i]) { 3482d7202bfSThomas Graf valid++; 3492d7202bfSThomas Graf NLA_PUT_U32(skb, i+1, metrics[i]); 3501da177e4SLinus Torvalds } 3512d7202bfSThomas Graf } 3521da177e4SLinus Torvalds 353a57d27fcSDavid S. Miller if (!valid) { 354a57d27fcSDavid S. Miller nla_nest_cancel(skb, mx); 355a57d27fcSDavid S. Miller return 0; 356a57d27fcSDavid S. Miller } 3572d7202bfSThomas Graf 3582d7202bfSThomas Graf return nla_nest_end(skb, mx); 3592d7202bfSThomas Graf 3602d7202bfSThomas Graf nla_put_failure: 3612d7202bfSThomas Graf return nla_nest_cancel(skb, mx); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 364e3703b3dSThomas Graf int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 365e3703b3dSThomas Graf u32 ts, u32 tsage, long expires, u32 error) 366e3703b3dSThomas Graf { 367e3703b3dSThomas Graf struct rta_cacheinfo ci = { 368e3703b3dSThomas Graf .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 369e3703b3dSThomas Graf .rta_used = dst->__use, 370e3703b3dSThomas Graf .rta_clntref = atomic_read(&(dst->__refcnt)), 371e3703b3dSThomas Graf .rta_error = error, 372e3703b3dSThomas Graf .rta_id = id, 373e3703b3dSThomas Graf .rta_ts = ts, 374e3703b3dSThomas Graf .rta_tsage = tsage, 375e3703b3dSThomas Graf }; 376e3703b3dSThomas Graf 377e3703b3dSThomas Graf if (expires) 378e3703b3dSThomas Graf ci.rta_expires = jiffies_to_clock_t(expires); 379e3703b3dSThomas Graf 380e3703b3dSThomas Graf return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 381e3703b3dSThomas Graf } 382e3703b3dSThomas Graf 383e3703b3dSThomas Graf EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 3841da177e4SLinus Torvalds 385b00055aaSStefan Rompf static void set_operstate(struct net_device *dev, unsigned char transition) 386b00055aaSStefan Rompf { 387b00055aaSStefan Rompf unsigned char operstate = dev->operstate; 388b00055aaSStefan Rompf 389b00055aaSStefan Rompf switch(transition) { 390b00055aaSStefan Rompf case IF_OPER_UP: 391b00055aaSStefan Rompf if ((operstate == IF_OPER_DORMANT || 392b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) && 393b00055aaSStefan Rompf !netif_dormant(dev)) 394b00055aaSStefan Rompf operstate = IF_OPER_UP; 395b00055aaSStefan Rompf break; 396b00055aaSStefan Rompf 397b00055aaSStefan Rompf case IF_OPER_DORMANT: 398b00055aaSStefan Rompf if (operstate == IF_OPER_UP || 399b00055aaSStefan Rompf operstate == IF_OPER_UNKNOWN) 400b00055aaSStefan Rompf operstate = IF_OPER_DORMANT; 401b00055aaSStefan Rompf break; 402b00055aaSStefan Rompf }; 403b00055aaSStefan Rompf 404b00055aaSStefan Rompf if (dev->operstate != operstate) { 405b00055aaSStefan Rompf write_lock_bh(&dev_base_lock); 406b00055aaSStefan Rompf dev->operstate = operstate; 407b00055aaSStefan Rompf write_unlock_bh(&dev_base_lock); 408b00055aaSStefan Rompf netdev_state_change(dev); 409b00055aaSStefan Rompf } 410b00055aaSStefan Rompf } 411b00055aaSStefan Rompf 412b60c5115SThomas Graf static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 413b60c5115SThomas Graf struct net_device_stats *b) 4141da177e4SLinus Torvalds { 415b60c5115SThomas Graf a->rx_packets = b->rx_packets; 416b60c5115SThomas Graf a->tx_packets = b->tx_packets; 417b60c5115SThomas Graf a->rx_bytes = b->rx_bytes; 418b60c5115SThomas Graf a->tx_bytes = b->tx_bytes; 419b60c5115SThomas Graf a->rx_errors = b->rx_errors; 420b60c5115SThomas Graf a->tx_errors = b->tx_errors; 421b60c5115SThomas Graf a->rx_dropped = b->rx_dropped; 422b60c5115SThomas Graf a->tx_dropped = b->tx_dropped; 423b60c5115SThomas Graf 424b60c5115SThomas Graf a->multicast = b->multicast; 425b60c5115SThomas Graf a->collisions = b->collisions; 426b60c5115SThomas Graf 427b60c5115SThomas Graf a->rx_length_errors = b->rx_length_errors; 428b60c5115SThomas Graf a->rx_over_errors = b->rx_over_errors; 429b60c5115SThomas Graf a->rx_crc_errors = b->rx_crc_errors; 430b60c5115SThomas Graf a->rx_frame_errors = b->rx_frame_errors; 431b60c5115SThomas Graf a->rx_fifo_errors = b->rx_fifo_errors; 432b60c5115SThomas Graf a->rx_missed_errors = b->rx_missed_errors; 433b60c5115SThomas Graf 434b60c5115SThomas Graf a->tx_aborted_errors = b->tx_aborted_errors; 435b60c5115SThomas Graf a->tx_carrier_errors = b->tx_carrier_errors; 436b60c5115SThomas Graf a->tx_fifo_errors = b->tx_fifo_errors; 437b60c5115SThomas Graf a->tx_heartbeat_errors = b->tx_heartbeat_errors; 438b60c5115SThomas Graf a->tx_window_errors = b->tx_window_errors; 439b60c5115SThomas Graf 440b60c5115SThomas Graf a->rx_compressed = b->rx_compressed; 441b60c5115SThomas Graf a->tx_compressed = b->tx_compressed; 442b60c5115SThomas Graf }; 443b60c5115SThomas Graf 444339bf98fSThomas Graf static inline size_t if_nlmsg_size(int iwbuflen) 445339bf98fSThomas Graf { 446339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 447339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 448339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 449339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_ifmap)) 450339bf98fSThomas Graf + nla_total_size(sizeof(struct rtnl_link_stats)) 451339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 452339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 453339bf98fSThomas Graf + nla_total_size(4) /* IFLA_TXQLEN */ 454339bf98fSThomas Graf + nla_total_size(4) /* IFLA_WEIGHT */ 455339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 456339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 457339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MASTER */ 458339bf98fSThomas Graf + nla_total_size(1) /* IFLA_OPERSTATE */ 459339bf98fSThomas Graf + nla_total_size(1) /* IFLA_LINKMODE */ 460339bf98fSThomas Graf + nla_total_size(iwbuflen); 461339bf98fSThomas Graf } 462339bf98fSThomas Graf 463b60c5115SThomas Graf static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 464b60c5115SThomas Graf void *iwbuf, int iwbuflen, int type, u32 pid, 465b60c5115SThomas Graf u32 seq, u32 change, unsigned int flags) 466b60c5115SThomas Graf { 467b60c5115SThomas Graf struct ifinfomsg *ifm; 4681da177e4SLinus Torvalds struct nlmsghdr *nlh; 4691da177e4SLinus Torvalds 470b60c5115SThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 471b60c5115SThomas Graf if (nlh == NULL) 47226932566SPatrick McHardy return -EMSGSIZE; 4731da177e4SLinus Torvalds 474b60c5115SThomas Graf ifm = nlmsg_data(nlh); 475b60c5115SThomas Graf ifm->ifi_family = AF_UNSPEC; 476b60c5115SThomas Graf ifm->__ifi_pad = 0; 477b60c5115SThomas Graf ifm->ifi_type = dev->type; 478b60c5115SThomas Graf ifm->ifi_index = dev->ifindex; 479b60c5115SThomas Graf ifm->ifi_flags = dev_get_flags(dev); 480b60c5115SThomas Graf ifm->ifi_change = change; 4811da177e4SLinus Torvalds 482b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 483b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 484b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight); 485b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_OPERSTATE, 486b60c5115SThomas Graf netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 487b60c5115SThomas Graf NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 488b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 4891da177e4SLinus Torvalds 490b60c5115SThomas Graf if (dev->ifindex != dev->iflink) 491b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 4921da177e4SLinus Torvalds 493b60c5115SThomas Graf if (dev->master) 494b60c5115SThomas Graf NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 495b60c5115SThomas Graf 496b60c5115SThomas Graf if (dev->qdisc_sleeping) 497b60c5115SThomas Graf NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); 498b00055aaSStefan Rompf 499b00055aaSStefan Rompf if (1) { 5001da177e4SLinus Torvalds struct rtnl_link_ifmap map = { 5011da177e4SLinus Torvalds .mem_start = dev->mem_start, 5021da177e4SLinus Torvalds .mem_end = dev->mem_end, 5031da177e4SLinus Torvalds .base_addr = dev->base_addr, 5041da177e4SLinus Torvalds .irq = dev->irq, 5051da177e4SLinus Torvalds .dma = dev->dma, 5061da177e4SLinus Torvalds .port = dev->if_port, 5071da177e4SLinus Torvalds }; 508b60c5115SThomas Graf NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (dev->addr_len) { 512b60c5115SThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 513b60c5115SThomas Graf NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds if (dev->get_stats) { 517b60c5115SThomas Graf struct net_device_stats *stats = dev->get_stats(dev); 5181da177e4SLinus Torvalds if (stats) { 519b60c5115SThomas Graf struct nlattr *attr; 5201da177e4SLinus Torvalds 521b60c5115SThomas Graf attr = nla_reserve(skb, IFLA_STATS, 522b60c5115SThomas Graf sizeof(struct rtnl_link_stats)); 523b60c5115SThomas Graf if (attr == NULL) 524b60c5115SThomas Graf goto nla_put_failure; 525b60c5115SThomas Graf 526b60c5115SThomas Graf copy_rtnl_link_stats(nla_data(attr), stats); 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530b60c5115SThomas Graf if (iwbuf) 531b60c5115SThomas Graf NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf); 532b60c5115SThomas Graf 533b60c5115SThomas Graf return nlmsg_end(skb, nlh); 534b60c5115SThomas Graf 535b60c5115SThomas Graf nla_put_failure: 53626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 53726932566SPatrick McHardy return -EMSGSIZE; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 540b60c5115SThomas Graf static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds int idx; 5431da177e4SLinus Torvalds int s_idx = cb->args[0]; 5441da177e4SLinus Torvalds struct net_device *dev; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds read_lock(&dev_base_lock); 5471da177e4SLinus Torvalds for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) { 5481da177e4SLinus Torvalds if (idx < s_idx) 5491da177e4SLinus Torvalds continue; 550b60c5115SThomas Graf if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK, 551b6544c0bSJamal Hadi Salim NETLINK_CB(cb->skb).pid, 552b60c5115SThomas Graf cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) 5531da177e4SLinus Torvalds break; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds read_unlock(&dev_base_lock); 5561da177e4SLinus Torvalds cb->args[0] = idx; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds return skb->len; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 561da5e0494SThomas Graf static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = { 5625176f91eSThomas Graf [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 5635176f91eSThomas Graf [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 564da5e0494SThomas Graf [IFLA_MTU] = { .type = NLA_U32 }, 565da5e0494SThomas Graf [IFLA_TXQLEN] = { .type = NLA_U32 }, 566da5e0494SThomas Graf [IFLA_WEIGHT] = { .type = NLA_U32 }, 567da5e0494SThomas Graf [IFLA_OPERSTATE] = { .type = NLA_U8 }, 568da5e0494SThomas Graf [IFLA_LINKMODE] = { .type = NLA_U8 }, 569da5e0494SThomas Graf }; 5701da177e4SLinus Torvalds 571da5e0494SThomas Graf static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 572da5e0494SThomas Graf { 573da5e0494SThomas Graf struct ifinfomsg *ifm; 574da5e0494SThomas Graf struct net_device *dev; 575da5e0494SThomas Graf int err, send_addr_notify = 0, modified = 0; 576da5e0494SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 5771da177e4SLinus Torvalds char ifname[IFNAMSIZ]; 5781da177e4SLinus Torvalds 579da5e0494SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 580da5e0494SThomas Graf if (err < 0) 581da5e0494SThomas Graf goto errout; 5821da177e4SLinus Torvalds 5835176f91eSThomas Graf if (tb[IFLA_IFNAME]) 5845176f91eSThomas Graf nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 58578e5b891SPatrick McHardy else 58678e5b891SPatrick McHardy ifname[0] = '\0'; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds err = -EINVAL; 589da5e0494SThomas Graf ifm = nlmsg_data(nlh); 590da5e0494SThomas Graf if (ifm->ifi_index >= 0) 591da5e0494SThomas Graf dev = dev_get_by_index(ifm->ifi_index); 592da5e0494SThomas Graf else if (tb[IFLA_IFNAME]) 593da5e0494SThomas Graf dev = dev_get_by_name(ifname); 594da5e0494SThomas Graf else 595da5e0494SThomas Graf goto errout; 5961da177e4SLinus Torvalds 597da5e0494SThomas Graf if (dev == NULL) { 598da5e0494SThomas Graf err = -ENODEV; 599da5e0494SThomas Graf goto errout; 600da5e0494SThomas Graf } 6011da177e4SLinus Torvalds 602da5e0494SThomas Graf if (tb[IFLA_ADDRESS] && 603da5e0494SThomas Graf nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 604da5e0494SThomas Graf goto errout_dev; 605da5e0494SThomas Graf 606da5e0494SThomas Graf if (tb[IFLA_BROADCAST] && 607da5e0494SThomas Graf nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 608da5e0494SThomas Graf goto errout_dev; 609da5e0494SThomas Graf 610da5e0494SThomas Graf if (tb[IFLA_MAP]) { 6111da177e4SLinus Torvalds struct rtnl_link_ifmap *u_map; 6121da177e4SLinus Torvalds struct ifmap k_map; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds if (!dev->set_config) { 6151da177e4SLinus Torvalds err = -EOPNOTSUPP; 616da5e0494SThomas Graf goto errout_dev; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds if (!netif_device_present(dev)) { 6201da177e4SLinus Torvalds err = -ENODEV; 621da5e0494SThomas Graf goto errout_dev; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 624da5e0494SThomas Graf u_map = nla_data(tb[IFLA_MAP]); 6251da177e4SLinus Torvalds k_map.mem_start = (unsigned long) u_map->mem_start; 6261da177e4SLinus Torvalds k_map.mem_end = (unsigned long) u_map->mem_end; 6271da177e4SLinus Torvalds k_map.base_addr = (unsigned short) u_map->base_addr; 6281da177e4SLinus Torvalds k_map.irq = (unsigned char) u_map->irq; 6291da177e4SLinus Torvalds k_map.dma = (unsigned char) u_map->dma; 6301da177e4SLinus Torvalds k_map.port = (unsigned char) u_map->port; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds err = dev->set_config(dev, &k_map); 633da5e0494SThomas Graf if (err < 0) 634da5e0494SThomas Graf goto errout_dev; 6351da177e4SLinus Torvalds 636da5e0494SThomas Graf modified = 1; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 639da5e0494SThomas Graf if (tb[IFLA_ADDRESS]) { 64070f8e78eSDavid S. Miller struct sockaddr *sa; 64170f8e78eSDavid S. Miller int len; 64270f8e78eSDavid S. Miller 6431da177e4SLinus Torvalds if (!dev->set_mac_address) { 6441da177e4SLinus Torvalds err = -EOPNOTSUPP; 645da5e0494SThomas Graf goto errout_dev; 6461da177e4SLinus Torvalds } 647da5e0494SThomas Graf 6481da177e4SLinus Torvalds if (!netif_device_present(dev)) { 6491da177e4SLinus Torvalds err = -ENODEV; 650da5e0494SThomas Graf goto errout_dev; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 65370f8e78eSDavid S. Miller len = sizeof(sa_family_t) + dev->addr_len; 65470f8e78eSDavid S. Miller sa = kmalloc(len, GFP_KERNEL); 65570f8e78eSDavid S. Miller if (!sa) { 65670f8e78eSDavid S. Miller err = -ENOMEM; 657da5e0494SThomas Graf goto errout_dev; 65870f8e78eSDavid S. Miller } 65970f8e78eSDavid S. Miller sa->sa_family = dev->type; 660da5e0494SThomas Graf memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 66170f8e78eSDavid S. Miller dev->addr_len); 66270f8e78eSDavid S. Miller err = dev->set_mac_address(dev, sa); 66370f8e78eSDavid S. Miller kfree(sa); 6641da177e4SLinus Torvalds if (err) 665da5e0494SThomas Graf goto errout_dev; 6661da177e4SLinus Torvalds send_addr_notify = 1; 667da5e0494SThomas Graf modified = 1; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 670da5e0494SThomas Graf if (tb[IFLA_MTU]) { 671da5e0494SThomas Graf err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 672da5e0494SThomas Graf if (err < 0) 673da5e0494SThomas Graf goto errout_dev; 674da5e0494SThomas Graf modified = 1; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 677da5e0494SThomas Graf /* 678da5e0494SThomas Graf * Interface selected by interface index but interface 679da5e0494SThomas Graf * name provided implies that a name change has been 680da5e0494SThomas Graf * requested. 681da5e0494SThomas Graf */ 682da5e0494SThomas Graf if (ifm->ifi_index >= 0 && ifname[0]) { 6831da177e4SLinus Torvalds err = dev_change_name(dev, ifname); 684da5e0494SThomas Graf if (err < 0) 685da5e0494SThomas Graf goto errout_dev; 686da5e0494SThomas Graf modified = 1; 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 689711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK 690da5e0494SThomas Graf if (tb[IFLA_WIRELESS]) { 691711e2c33SJean Tourrilhes /* Call Wireless Extensions. 692711e2c33SJean Tourrilhes * Various stuff checked in there... */ 693da5e0494SThomas Graf err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]), 694da5e0494SThomas Graf nla_len(tb[IFLA_WIRELESS])); 695da5e0494SThomas Graf if (err < 0) 696da5e0494SThomas Graf goto errout_dev; 697711e2c33SJean Tourrilhes } 698711e2c33SJean Tourrilhes #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ 699711e2c33SJean Tourrilhes 700da5e0494SThomas Graf if (tb[IFLA_BROADCAST]) { 701da5e0494SThomas Graf nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 702da5e0494SThomas Graf send_addr_notify = 1; 703da5e0494SThomas Graf } 704da5e0494SThomas Graf 705da5e0494SThomas Graf 706da5e0494SThomas Graf if (ifm->ifi_flags) 707da5e0494SThomas Graf dev_change_flags(dev, ifm->ifi_flags); 708da5e0494SThomas Graf 709da5e0494SThomas Graf if (tb[IFLA_TXQLEN]) 710da5e0494SThomas Graf dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 711da5e0494SThomas Graf 712da5e0494SThomas Graf if (tb[IFLA_WEIGHT]) 713da5e0494SThomas Graf dev->weight = nla_get_u32(tb[IFLA_WEIGHT]); 714da5e0494SThomas Graf 715da5e0494SThomas Graf if (tb[IFLA_OPERSTATE]) 716da5e0494SThomas Graf set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 717da5e0494SThomas Graf 718da5e0494SThomas Graf if (tb[IFLA_LINKMODE]) { 719da5e0494SThomas Graf write_lock_bh(&dev_base_lock); 720da5e0494SThomas Graf dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 721da5e0494SThomas Graf write_unlock_bh(&dev_base_lock); 722da5e0494SThomas Graf } 723da5e0494SThomas Graf 7241da177e4SLinus Torvalds err = 0; 7251da177e4SLinus Torvalds 726da5e0494SThomas Graf errout_dev: 727da5e0494SThomas Graf if (err < 0 && modified && net_ratelimit()) 728da5e0494SThomas Graf printk(KERN_WARNING "A link change request failed with " 729da5e0494SThomas Graf "some changes comitted already. Interface %s may " 730da5e0494SThomas Graf "have been left with an inconsistent configuration, " 731da5e0494SThomas Graf "please check.\n", dev->name); 732da5e0494SThomas Graf 7331da177e4SLinus Torvalds if (send_addr_notify) 7341da177e4SLinus Torvalds call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds dev_put(dev); 737da5e0494SThomas Graf errout: 7381da177e4SLinus Torvalds return err; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 741b60c5115SThomas Graf static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 742711e2c33SJean Tourrilhes { 743b60c5115SThomas Graf struct ifinfomsg *ifm; 744b60c5115SThomas Graf struct nlattr *tb[IFLA_MAX+1]; 745b60c5115SThomas Graf struct net_device *dev = NULL; 746b60c5115SThomas Graf struct sk_buff *nskb; 747b60c5115SThomas Graf char *iw_buf = NULL, *iw = NULL; 748711e2c33SJean Tourrilhes int iw_buf_len = 0; 749339bf98fSThomas Graf int err; 750711e2c33SJean Tourrilhes 751b60c5115SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 752b60c5115SThomas Graf if (err < 0) 7539918f230SEric Sesterhenn return err; 754b60c5115SThomas Graf 755b60c5115SThomas Graf ifm = nlmsg_data(nlh); 756b60c5115SThomas Graf if (ifm->ifi_index >= 0) { 757711e2c33SJean Tourrilhes dev = dev_get_by_index(ifm->ifi_index); 758b60c5115SThomas Graf if (dev == NULL) 759711e2c33SJean Tourrilhes return -ENODEV; 760b60c5115SThomas Graf } else 761b60c5115SThomas Graf return -EINVAL; 762b60c5115SThomas Graf 763711e2c33SJean Tourrilhes 764711e2c33SJean Tourrilhes #ifdef CONFIG_NET_WIRELESS_RTNETLINK 765b60c5115SThomas Graf if (tb[IFLA_WIRELESS]) { 766711e2c33SJean Tourrilhes /* Call Wireless Extensions. We need to know the size before 767711e2c33SJean Tourrilhes * we can alloc. Various stuff checked in there... */ 768b60c5115SThomas Graf err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]), 769b60c5115SThomas Graf nla_len(tb[IFLA_WIRELESS]), 770b60c5115SThomas Graf &iw_buf, &iw_buf_len); 771b60c5115SThomas Graf if (err < 0) 772b60c5115SThomas Graf goto errout; 773b60c5115SThomas Graf 774c2805fbbSJean Tourrilhes /* Payload is at an offset in buffer */ 775c2805fbbSJean Tourrilhes iw = iw_buf + IW_EV_POINT_OFF; 776711e2c33SJean Tourrilhes } 777711e2c33SJean Tourrilhes #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ 778711e2c33SJean Tourrilhes 779339bf98fSThomas Graf nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL); 780b60c5115SThomas Graf if (nskb == NULL) { 781b60c5115SThomas Graf err = -ENOBUFS; 782b60c5115SThomas Graf goto errout; 783b60c5115SThomas Graf } 784711e2c33SJean Tourrilhes 785b60c5115SThomas Graf err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK, 786b60c5115SThomas Graf NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0); 78726932566SPatrick McHardy if (err < 0) { 78826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size */ 78926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 79026932566SPatrick McHardy kfree_skb(nskb); 79126932566SPatrick McHardy goto errout; 79226932566SPatrick McHardy } 793b974179aSPatrick McHardy err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); 794b60c5115SThomas Graf errout: 795711e2c33SJean Tourrilhes kfree(iw_buf); 796711e2c33SJean Tourrilhes dev_put(dev); 797b60c5115SThomas Graf 798711e2c33SJean Tourrilhes return err; 799711e2c33SJean Tourrilhes } 800711e2c33SJean Tourrilhes 801e2849863SThomas Graf int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds int idx; 8041da177e4SLinus Torvalds int s_idx = cb->family; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds if (s_idx == 0) 8071da177e4SLinus Torvalds s_idx = 1; 8081da177e4SLinus Torvalds for (idx=1; idx<NPROTO; idx++) { 8091da177e4SLinus Torvalds int type = cb->nlh->nlmsg_type-RTM_BASE; 8101da177e4SLinus Torvalds if (idx < s_idx || idx == PF_PACKET) 8111da177e4SLinus Torvalds continue; 812e2849863SThomas Graf if (rtnl_msg_handlers[idx] == NULL || 813e2849863SThomas Graf rtnl_msg_handlers[idx][type].dumpit == NULL) 8141da177e4SLinus Torvalds continue; 8151da177e4SLinus Torvalds if (idx > s_idx) 8161da177e4SLinus Torvalds memset(&cb->args[0], 0, sizeof(cb->args)); 817e2849863SThomas Graf if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 8181da177e4SLinus Torvalds break; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds cb->family = idx; 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds return skb->len; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 825e2849863SThomas Graf EXPORT_SYMBOL_GPL(rtnl_dump_all); 826e2849863SThomas Graf 8271da177e4SLinus Torvalds void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 8281da177e4SLinus Torvalds { 8291da177e4SLinus Torvalds struct sk_buff *skb; 8300ec6d3f4SThomas Graf int err = -ENOBUFS; 8311da177e4SLinus Torvalds 832339bf98fSThomas Graf skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL); 8330ec6d3f4SThomas Graf if (skb == NULL) 8340ec6d3f4SThomas Graf goto errout; 8351da177e4SLinus Torvalds 8360ec6d3f4SThomas Graf err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0); 83726932566SPatrick McHardy if (err < 0) { 83826932566SPatrick McHardy /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 83926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 84026932566SPatrick McHardy kfree_skb(skb); 84126932566SPatrick McHardy goto errout; 84226932566SPatrick McHardy } 8430ec6d3f4SThomas Graf err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 8440ec6d3f4SThomas Graf errout: 8450ec6d3f4SThomas Graf if (err < 0) 8460ec6d3f4SThomas Graf rtnl_set_sk_err(RTNLGRP_LINK, err); 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* Protected by RTNL sempahore. */ 8501da177e4SLinus Torvalds static struct rtattr **rta_buf; 8511da177e4SLinus Torvalds static int rtattr_max; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /* Process one rtnetlink message. */ 8541da177e4SLinus Torvalds 8551d00a4ebSThomas Graf static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 8561da177e4SLinus Torvalds { 857e2849863SThomas Graf rtnl_doit_func doit; 8581da177e4SLinus Torvalds int sz_idx, kind; 8591da177e4SLinus Torvalds int min_len; 8601da177e4SLinus Torvalds int family; 8611da177e4SLinus Torvalds int type; 862*1c2d670fSPatrick McHardy int err; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds type = nlh->nlmsg_type; 8651da177e4SLinus Torvalds if (type > RTM_MAX) 866038890feSThomas Graf return -EOPNOTSUPP; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds type -= RTM_BASE; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* All the messages must have at least 1 byte length */ 8711da177e4SLinus Torvalds if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 8721da177e4SLinus Torvalds return 0; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; 8751d00a4ebSThomas Graf if (family >= NPROTO) 8761d00a4ebSThomas Graf return -EAFNOSUPPORT; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds sz_idx = type>>2; 8791da177e4SLinus Torvalds kind = type&3; 8801da177e4SLinus Torvalds 8811d00a4ebSThomas Graf if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) 8821d00a4ebSThomas Graf return -EPERM; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 885e2849863SThomas Graf rtnl_dumpit_func dumpit; 8861da177e4SLinus Torvalds 887e2849863SThomas Graf dumpit = rtnl_get_dumpit(family, type); 888e2849863SThomas Graf if (dumpit == NULL) 889038890feSThomas Graf return -EOPNOTSUPP; 8901da177e4SLinus Torvalds 891*1c2d670fSPatrick McHardy __rtnl_unlock(); 892*1c2d670fSPatrick McHardy err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); 893*1c2d670fSPatrick McHardy rtnl_lock(); 894*1c2d670fSPatrick McHardy return err; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds min_len = rtm_min[sz_idx]; 9001da177e4SLinus Torvalds if (nlh->nlmsg_len < min_len) 9011d00a4ebSThomas Graf return -EINVAL; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (nlh->nlmsg_len > min_len) { 9041da177e4SLinus Torvalds int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 9051da177e4SLinus Torvalds struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 9081da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 9091da177e4SLinus Torvalds if (flavor) { 9101da177e4SLinus Torvalds if (flavor > rta_max[sz_idx]) 9111d00a4ebSThomas Graf return -EINVAL; 9121da177e4SLinus Torvalds rta_buf[flavor-1] = attr; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918e2849863SThomas Graf doit = rtnl_get_doit(family, type); 919e2849863SThomas Graf if (doit == NULL) 920038890feSThomas Graf return -EOPNOTSUPP; 9211da177e4SLinus Torvalds 9221d00a4ebSThomas Graf return doit(skb, nlh, (void *)&rta_buf[0]); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds static void rtnetlink_rcv(struct sock *sk, int len) 9261da177e4SLinus Torvalds { 9279ac4a169SThomas Graf unsigned int qlen = 0; 9282a0a6ebeSHerbert Xu 9291da177e4SLinus Torvalds do { 9306756ae4bSStephen Hemminger mutex_lock(&rtnl_mutex); 9319ac4a169SThomas Graf netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); 9326756ae4bSStephen Hemminger mutex_unlock(&rtnl_mutex); 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds netdev_run_todo(); 9352a0a6ebeSHerbert Xu } while (qlen); 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds struct net_device *dev = ptr; 9411da177e4SLinus Torvalds switch (event) { 9421da177e4SLinus Torvalds case NETDEV_UNREGISTER: 9431da177e4SLinus Torvalds rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 9441da177e4SLinus Torvalds break; 9451da177e4SLinus Torvalds case NETDEV_REGISTER: 9461da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 9471da177e4SLinus Torvalds break; 9481da177e4SLinus Torvalds case NETDEV_UP: 9491da177e4SLinus Torvalds case NETDEV_DOWN: 9501da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 9511da177e4SLinus Torvalds break; 9521da177e4SLinus Torvalds case NETDEV_CHANGE: 9531da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 9541da177e4SLinus Torvalds break; 9551da177e4SLinus Torvalds default: 9561da177e4SLinus Torvalds rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 9571da177e4SLinus Torvalds break; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds return NOTIFY_DONE; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static struct notifier_block rtnetlink_dev_notifier = { 9631da177e4SLinus Torvalds .notifier_call = rtnetlink_event, 9641da177e4SLinus Torvalds }; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds void __init rtnetlink_init(void) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds int i; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds rtattr_max = 0; 9711da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(rta_max); i++) 9721da177e4SLinus Torvalds if (rta_max[i] > rtattr_max) 9731da177e4SLinus Torvalds rtattr_max = rta_max[i]; 9741da177e4SLinus Torvalds rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 9751da177e4SLinus Torvalds if (!rta_buf) 9761da177e4SLinus Torvalds panic("rtnetlink_init: cannot allocate rta_buf\n"); 9771da177e4SLinus Torvalds 97806628607SPatrick McHardy rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, 979*1c2d670fSPatrick McHardy &rtnl_mutex, THIS_MODULE); 9801da177e4SLinus Torvalds if (rtnl == NULL) 9811da177e4SLinus Torvalds panic("rtnetlink_init: cannot initialize rtnetlink\n"); 9821da177e4SLinus Torvalds netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 9831da177e4SLinus Torvalds register_netdevice_notifier(&rtnetlink_dev_notifier); 984340d17fcSThomas Graf 985340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); 986340d17fcSThomas Graf rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); 987687ad8ccSThomas Graf 988687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); 989687ad8ccSThomas Graf rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds EXPORT_SYMBOL(__rta_fill); 9931da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_strlcpy); 9941da177e4SLinus Torvalds EXPORT_SYMBOL(rtattr_parse); 9951da177e4SLinus Torvalds EXPORT_SYMBOL(rtnetlink_put_metrics); 9961da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_lock); 9976756ae4bSStephen Hemminger EXPORT_SYMBOL(rtnl_trylock); 9981da177e4SLinus Torvalds EXPORT_SYMBOL(rtnl_unlock); 9992942e900SThomas Graf EXPORT_SYMBOL(rtnl_unicast); 100097676b6bSThomas Graf EXPORT_SYMBOL(rtnl_notify); 100197676b6bSThomas Graf EXPORT_SYMBOL(rtnl_set_sk_err); 1002