Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * Routing netlink socket interface: protocol independent part. |
| 7 | * |
| 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * Fixes: |
| 16 | * Vitaly E. Lavrov RTA_OK arithmetics was wrong. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/config.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/socket.h> |
| 24 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/timer.h> |
| 27 | #include <linux/string.h> |
| 28 | #include <linux/sockios.h> |
| 29 | #include <linux/net.h> |
| 30 | #include <linux/fcntl.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <linux/capability.h> |
| 35 | #include <linux/skbuff.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/security.h> |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 38 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/uaccess.h> |
| 41 | #include <asm/system.h> |
| 42 | #include <asm/string.h> |
| 43 | |
| 44 | #include <linux/inet.h> |
| 45 | #include <linux/netdevice.h> |
| 46 | #include <net/ip.h> |
| 47 | #include <net/protocol.h> |
| 48 | #include <net/arp.h> |
| 49 | #include <net/route.h> |
| 50 | #include <net/udp.h> |
| 51 | #include <net/sock.h> |
| 52 | #include <net/pkt_sched.h> |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 53 | #include <net/netlink.h> |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 54 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
| 55 | #include <linux/wireless.h> |
| 56 | #include <net/iw_handler.h> |
| 57 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 59 | static DEFINE_MUTEX(rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | void rtnl_lock(void) |
| 62 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 63 | mutex_lock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 66 | void __rtnl_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 68 | mutex_unlock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | void rtnl_unlock(void) |
| 72 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 73 | mutex_unlock(&rtnl_mutex); |
| 74 | if (rtnl && rtnl->sk_receive_queue.qlen) |
| 75 | rtnl->sk_data_ready(rtnl, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | netdev_run_todo(); |
| 77 | } |
| 78 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 79 | int rtnl_trylock(void) |
| 80 | { |
| 81 | return mutex_trylock(&rtnl_mutex); |
| 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) |
| 85 | { |
| 86 | memset(tb, 0, sizeof(struct rtattr*)*maxattr); |
| 87 | |
| 88 | while (RTA_OK(rta, len)) { |
| 89 | unsigned flavor = rta->rta_type; |
| 90 | if (flavor && flavor <= maxattr) |
| 91 | tb[flavor-1] = rta; |
| 92 | rta = RTA_NEXT(rta, len); |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | struct sock *rtnl; |
| 98 | |
| 99 | struct rtnetlink_link * rtnetlink_links[NPROTO]; |
| 100 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 101 | static const int rtm_min[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 103 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
| 104 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), |
| 105 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), |
| 106 | [RTM_FAM(RTM_NEWNEIGH)] = NLMSG_LENGTH(sizeof(struct ndmsg)), |
| 107 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), |
| 108 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 109 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 110 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
| 111 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), |
| 112 | [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 113 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
| 114 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 115 | [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 118 | static const int rta_max[RTM_NR_FAMILIES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Thomas Graf | f90a0a7 | 2005-05-03 14:29:00 -0700 | [diff] [blame] | 120 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
| 121 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, |
| 122 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, |
| 123 | [RTM_FAM(RTM_NEWNEIGH)] = NDA_MAX, |
| 124 | [RTM_FAM(RTM_NEWRULE)] = RTA_MAX, |
| 125 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
| 126 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, |
| 127 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, |
| 128 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 129 | [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 133 | { |
| 134 | struct rtattr *rta; |
| 135 | int size = RTA_LENGTH(attrlen); |
| 136 | |
| 137 | rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); |
| 138 | rta->rta_type = attrtype; |
| 139 | rta->rta_len = size; |
| 140 | memcpy(RTA_DATA(rta), data, attrlen); |
Patrick McHardy | b3563c4 | 2005-06-28 12:54:43 -0700 | [diff] [blame] | 141 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) |
| 145 | { |
| 146 | size_t ret = RTA_PAYLOAD(rta); |
| 147 | char *src = RTA_DATA(rta); |
| 148 | |
| 149 | if (ret > 0 && src[ret - 1] == '\0') |
| 150 | ret--; |
| 151 | if (size > 0) { |
| 152 | size_t len = (ret >= size) ? size - 1 : ret; |
| 153 | memset(dest, 0, size); |
| 154 | memcpy(dest, src, len); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) |
| 160 | { |
| 161 | int err = 0; |
| 162 | |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 163 | NETLINK_CB(skb).dst_group = group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (echo) |
| 165 | atomic_inc(&skb->users); |
| 166 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); |
| 167 | if (echo) |
| 168 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); |
| 169 | return err; |
| 170 | } |
| 171 | |
| 172 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
| 173 | { |
| 174 | struct rtattr *mx = (struct rtattr*)skb->tail; |
| 175 | int i; |
| 176 | |
| 177 | RTA_PUT(skb, RTA_METRICS, 0, NULL); |
| 178 | for (i=0; i<RTAX_MAX; i++) { |
| 179 | if (metrics[i]) |
| 180 | RTA_PUT(skb, i+1, sizeof(u32), metrics+i); |
| 181 | } |
| 182 | mx->rta_len = skb->tail - (u8*)mx; |
| 183 | if (mx->rta_len == RTA_LENGTH(0)) |
| 184 | skb_trim(skb, (u8*)mx - skb->data); |
| 185 | return 0; |
| 186 | |
| 187 | rtattr_failure: |
| 188 | skb_trim(skb, (u8*)mx - skb->data); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 193 | static void set_operstate(struct net_device *dev, unsigned char transition) |
| 194 | { |
| 195 | unsigned char operstate = dev->operstate; |
| 196 | |
| 197 | switch(transition) { |
| 198 | case IF_OPER_UP: |
| 199 | if ((operstate == IF_OPER_DORMANT || |
| 200 | operstate == IF_OPER_UNKNOWN) && |
| 201 | !netif_dormant(dev)) |
| 202 | operstate = IF_OPER_UP; |
| 203 | break; |
| 204 | |
| 205 | case IF_OPER_DORMANT: |
| 206 | if (operstate == IF_OPER_UP || |
| 207 | operstate == IF_OPER_UNKNOWN) |
| 208 | operstate = IF_OPER_DORMANT; |
| 209 | break; |
| 210 | }; |
| 211 | |
| 212 | if (dev->operstate != operstate) { |
| 213 | write_lock_bh(&dev_base_lock); |
| 214 | dev->operstate = operstate; |
| 215 | write_unlock_bh(&dev_base_lock); |
| 216 | netdev_state_change(dev); |
| 217 | } |
| 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 221 | int type, u32 pid, u32 seq, u32 change, |
| 222 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | struct ifinfomsg *r; |
| 225 | struct nlmsghdr *nlh; |
| 226 | unsigned char *b = skb->tail; |
| 227 | |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 228 | nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | r = NLMSG_DATA(nlh); |
| 230 | r->ifi_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 231 | r->__ifi_pad = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | r->ifi_type = dev->type; |
| 233 | r->ifi_index = dev->ifindex; |
| 234 | r->ifi_flags = dev_get_flags(dev); |
| 235 | r->ifi_change = change; |
| 236 | |
| 237 | RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name); |
| 238 | |
| 239 | if (1) { |
| 240 | u32 txqlen = dev->tx_queue_len; |
| 241 | RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen); |
| 242 | } |
| 243 | |
| 244 | if (1) { |
| 245 | u32 weight = dev->weight; |
| 246 | RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight); |
| 247 | } |
| 248 | |
| 249 | if (1) { |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 250 | u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN; |
| 251 | u8 link_mode = dev->link_mode; |
| 252 | RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate); |
| 253 | RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode); |
| 254 | } |
| 255 | |
| 256 | if (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | struct rtnl_link_ifmap map = { |
| 258 | .mem_start = dev->mem_start, |
| 259 | .mem_end = dev->mem_end, |
| 260 | .base_addr = dev->base_addr, |
| 261 | .irq = dev->irq, |
| 262 | .dma = dev->dma, |
| 263 | .port = dev->if_port, |
| 264 | }; |
| 265 | RTA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
| 266 | } |
| 267 | |
| 268 | if (dev->addr_len) { |
| 269 | RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
| 270 | RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); |
| 271 | } |
| 272 | |
| 273 | if (1) { |
| 274 | u32 mtu = dev->mtu; |
| 275 | RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu); |
| 276 | } |
| 277 | |
| 278 | if (dev->ifindex != dev->iflink) { |
| 279 | u32 iflink = dev->iflink; |
| 280 | RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink); |
| 281 | } |
| 282 | |
| 283 | if (dev->qdisc_sleeping) |
| 284 | RTA_PUT(skb, IFLA_QDISC, |
| 285 | strlen(dev->qdisc_sleeping->ops->id) + 1, |
| 286 | dev->qdisc_sleeping->ops->id); |
| 287 | |
| 288 | if (dev->master) { |
| 289 | u32 master = dev->master->ifindex; |
| 290 | RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master); |
| 291 | } |
| 292 | |
| 293 | if (dev->get_stats) { |
| 294 | unsigned long *stats = (unsigned long*)dev->get_stats(dev); |
| 295 | if (stats) { |
| 296 | struct rtattr *a; |
| 297 | __u32 *s; |
| 298 | int i; |
| 299 | int n = sizeof(struct rtnl_link_stats)/4; |
| 300 | |
| 301 | a = __RTA_PUT(skb, IFLA_STATS, n*4); |
| 302 | s = RTA_DATA(a); |
| 303 | for (i=0; i<n; i++) |
| 304 | s[i] = stats[i]; |
| 305 | } |
| 306 | } |
| 307 | nlh->nlmsg_len = skb->tail - b; |
| 308 | return skb->len; |
| 309 | |
| 310 | nlmsg_failure: |
| 311 | rtattr_failure: |
| 312 | skb_trim(skb, b - skb->data); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
| 317 | { |
| 318 | int idx; |
| 319 | int s_idx = cb->args[0]; |
| 320 | struct net_device *dev; |
| 321 | |
| 322 | read_lock(&dev_base_lock); |
| 323 | for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) { |
| 324 | if (idx < s_idx) |
| 325 | continue; |
Jamal Hadi Salim | b6544c0 | 2005-06-18 22:54:12 -0700 | [diff] [blame] | 326 | if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, |
| 327 | NETLINK_CB(cb->skb).pid, |
| 328 | cb->nlh->nlmsg_seq, 0, |
| 329 | NLM_F_MULTI) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | read_unlock(&dev_base_lock); |
| 333 | cb->args[0] = idx; |
| 334 | |
| 335 | return skb->len; |
| 336 | } |
| 337 | |
| 338 | static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
| 339 | { |
| 340 | struct ifinfomsg *ifm = NLMSG_DATA(nlh); |
| 341 | struct rtattr **ida = arg; |
| 342 | struct net_device *dev; |
| 343 | int err, send_addr_notify = 0; |
| 344 | |
| 345 | if (ifm->ifi_index >= 0) |
| 346 | dev = dev_get_by_index(ifm->ifi_index); |
| 347 | else if (ida[IFLA_IFNAME - 1]) { |
| 348 | char ifname[IFNAMSIZ]; |
| 349 | |
| 350 | if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1], |
| 351 | IFNAMSIZ) >= IFNAMSIZ) |
| 352 | return -EINVAL; |
| 353 | dev = dev_get_by_name(ifname); |
| 354 | } else |
| 355 | return -EINVAL; |
| 356 | |
| 357 | if (!dev) |
| 358 | return -ENODEV; |
| 359 | |
| 360 | err = -EINVAL; |
| 361 | |
| 362 | if (ifm->ifi_flags) |
| 363 | dev_change_flags(dev, ifm->ifi_flags); |
| 364 | |
| 365 | if (ida[IFLA_MAP - 1]) { |
| 366 | struct rtnl_link_ifmap *u_map; |
| 367 | struct ifmap k_map; |
| 368 | |
| 369 | if (!dev->set_config) { |
| 370 | err = -EOPNOTSUPP; |
| 371 | goto out; |
| 372 | } |
| 373 | |
| 374 | if (!netif_device_present(dev)) { |
| 375 | err = -ENODEV; |
| 376 | goto out; |
| 377 | } |
| 378 | |
| 379 | if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map))) |
| 380 | goto out; |
| 381 | |
| 382 | u_map = RTA_DATA(ida[IFLA_MAP - 1]); |
| 383 | |
| 384 | k_map.mem_start = (unsigned long) u_map->mem_start; |
| 385 | k_map.mem_end = (unsigned long) u_map->mem_end; |
| 386 | k_map.base_addr = (unsigned short) u_map->base_addr; |
| 387 | k_map.irq = (unsigned char) u_map->irq; |
| 388 | k_map.dma = (unsigned char) u_map->dma; |
| 389 | k_map.port = (unsigned char) u_map->port; |
| 390 | |
| 391 | err = dev->set_config(dev, &k_map); |
| 392 | |
| 393 | if (err) |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | if (ida[IFLA_ADDRESS - 1]) { |
| 398 | if (!dev->set_mac_address) { |
| 399 | err = -EOPNOTSUPP; |
| 400 | goto out; |
| 401 | } |
| 402 | if (!netif_device_present(dev)) { |
| 403 | err = -ENODEV; |
| 404 | goto out; |
| 405 | } |
| 406 | if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len)) |
| 407 | goto out; |
| 408 | |
| 409 | err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1])); |
| 410 | if (err) |
| 411 | goto out; |
| 412 | send_addr_notify = 1; |
| 413 | } |
| 414 | |
| 415 | if (ida[IFLA_BROADCAST - 1]) { |
| 416 | if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len)) |
| 417 | goto out; |
| 418 | memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]), |
| 419 | dev->addr_len); |
| 420 | send_addr_notify = 1; |
| 421 | } |
| 422 | |
| 423 | if (ida[IFLA_MTU - 1]) { |
| 424 | if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32))) |
| 425 | goto out; |
| 426 | err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1]))); |
| 427 | |
| 428 | if (err) |
| 429 | goto out; |
| 430 | |
| 431 | } |
| 432 | |
| 433 | if (ida[IFLA_TXQLEN - 1]) { |
| 434 | if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32))) |
| 435 | goto out; |
| 436 | |
| 437 | dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1])); |
| 438 | } |
| 439 | |
| 440 | if (ida[IFLA_WEIGHT - 1]) { |
| 441 | if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32))) |
| 442 | goto out; |
| 443 | |
| 444 | dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1])); |
| 445 | } |
| 446 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 447 | if (ida[IFLA_OPERSTATE - 1]) { |
| 448 | if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8))) |
| 449 | goto out; |
| 450 | |
| 451 | set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1]))); |
| 452 | } |
| 453 | |
| 454 | if (ida[IFLA_LINKMODE - 1]) { |
| 455 | if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8))) |
| 456 | goto out; |
| 457 | |
| 458 | write_lock_bh(&dev_base_lock); |
| 459 | dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1])); |
| 460 | write_unlock_bh(&dev_base_lock); |
| 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) { |
| 464 | char ifname[IFNAMSIZ]; |
| 465 | |
| 466 | if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1], |
| 467 | IFNAMSIZ) >= IFNAMSIZ) |
| 468 | goto out; |
| 469 | err = dev_change_name(dev, ifname); |
| 470 | if (err) |
| 471 | goto out; |
| 472 | } |
| 473 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 474 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
| 475 | if (ida[IFLA_WIRELESS - 1]) { |
| 476 | |
| 477 | /* Call Wireless Extensions. |
| 478 | * Various stuff checked in there... */ |
| 479 | err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len); |
| 480 | if (err) |
| 481 | goto out; |
| 482 | } |
| 483 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ |
| 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | err = 0; |
| 486 | |
| 487 | out: |
| 488 | if (send_addr_notify) |
| 489 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
| 490 | |
| 491 | dev_put(dev); |
| 492 | return err; |
| 493 | } |
| 494 | |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 495 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
| 496 | static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg) |
| 497 | { |
| 498 | struct ifinfomsg *ifm = NLMSG_DATA(in_nlh); |
| 499 | struct rtattr **ida = arg; |
| 500 | struct net_device *dev; |
| 501 | struct ifinfomsg *r; |
| 502 | struct nlmsghdr *nlh; |
| 503 | int err = -ENOBUFS; |
| 504 | struct sk_buff *skb; |
| 505 | unsigned char *b; |
| 506 | char *iw_buf = NULL; |
| 507 | int iw_buf_len = 0; |
| 508 | |
| 509 | if (ifm->ifi_index >= 0) |
| 510 | dev = dev_get_by_index(ifm->ifi_index); |
| 511 | else |
| 512 | return -EINVAL; |
| 513 | if (!dev) |
| 514 | return -ENODEV; |
| 515 | |
| 516 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
| 517 | if (ida[IFLA_WIRELESS - 1]) { |
| 518 | |
| 519 | /* Call Wireless Extensions. We need to know the size before |
| 520 | * we can alloc. Various stuff checked in there... */ |
| 521 | err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len); |
| 522 | if (err) |
| 523 | goto out; |
| 524 | } |
| 525 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ |
| 526 | |
| 527 | /* Create a skb big enough to include all the data. |
| 528 | * Some requests are way bigger than 4k... Jean II */ |
| 529 | skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)), |
| 530 | GFP_KERNEL); |
| 531 | if (!skb) |
| 532 | goto out; |
| 533 | b = skb->tail; |
| 534 | |
| 535 | /* Put in the message the usual good stuff */ |
| 536 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq, |
| 537 | RTM_NEWLINK, sizeof(*r)); |
| 538 | r = NLMSG_DATA(nlh); |
| 539 | r->ifi_family = AF_UNSPEC; |
| 540 | r->__ifi_pad = 0; |
| 541 | r->ifi_type = dev->type; |
| 542 | r->ifi_index = dev->ifindex; |
| 543 | r->ifi_flags = dev->flags; |
| 544 | r->ifi_change = 0; |
| 545 | |
| 546 | /* Put the wireless payload if it exist */ |
| 547 | if(iw_buf != NULL) |
| 548 | RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len, |
| 549 | iw_buf + IW_EV_POINT_OFF); |
| 550 | |
| 551 | nlh->nlmsg_len = skb->tail - b; |
| 552 | |
| 553 | /* Needed ? */ |
| 554 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; |
| 555 | |
| 556 | err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); |
| 557 | if (err > 0) |
| 558 | err = 0; |
| 559 | out: |
| 560 | if(iw_buf != NULL) |
| 561 | kfree(iw_buf); |
| 562 | dev_put(dev); |
| 563 | return err; |
| 564 | |
| 565 | rtattr_failure: |
| 566 | nlmsg_failure: |
| 567 | kfree_skb(skb); |
| 568 | goto out; |
| 569 | } |
| 570 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
| 573 | { |
| 574 | int idx; |
| 575 | int s_idx = cb->family; |
| 576 | |
| 577 | if (s_idx == 0) |
| 578 | s_idx = 1; |
| 579 | for (idx=1; idx<NPROTO; idx++) { |
| 580 | int type = cb->nlh->nlmsg_type-RTM_BASE; |
| 581 | if (idx < s_idx || idx == PF_PACKET) |
| 582 | continue; |
| 583 | if (rtnetlink_links[idx] == NULL || |
| 584 | rtnetlink_links[idx][type].dumpit == NULL) |
| 585 | continue; |
| 586 | if (idx > s_idx) |
| 587 | memset(&cb->args[0], 0, sizeof(cb->args)); |
| 588 | if (rtnetlink_links[idx][type].dumpit(skb, cb)) |
| 589 | break; |
| 590 | } |
| 591 | cb->family = idx; |
| 592 | |
| 593 | return skb->len; |
| 594 | } |
| 595 | |
| 596 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) |
| 597 | { |
| 598 | struct sk_buff *skb; |
| 599 | int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + |
| 600 | sizeof(struct rtnl_link_ifmap) + |
| 601 | sizeof(struct rtnl_link_stats) + 128); |
| 602 | |
| 603 | skb = alloc_skb(size, GFP_KERNEL); |
| 604 | if (!skb) |
| 605 | return; |
| 606 | |
Alexey Kuznetsov | 2863351 | 2006-02-09 16:40:58 -0800 | [diff] [blame] | 607 | if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | kfree_skb(skb); |
| 609 | return; |
| 610 | } |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 611 | NETLINK_CB(skb).dst_group = RTNLGRP_LINK; |
| 612 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | /* Protected by RTNL sempahore. */ |
| 616 | static struct rtattr **rta_buf; |
| 617 | static int rtattr_max; |
| 618 | |
| 619 | /* Process one rtnetlink message. */ |
| 620 | |
| 621 | static __inline__ int |
| 622 | rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) |
| 623 | { |
| 624 | struct rtnetlink_link *link; |
| 625 | struct rtnetlink_link *link_tab; |
| 626 | int sz_idx, kind; |
| 627 | int min_len; |
| 628 | int family; |
| 629 | int type; |
| 630 | int err; |
| 631 | |
| 632 | /* Only requests are handled by kernel now */ |
| 633 | if (!(nlh->nlmsg_flags&NLM_F_REQUEST)) |
| 634 | return 0; |
| 635 | |
| 636 | type = nlh->nlmsg_type; |
| 637 | |
| 638 | /* A control message: ignore them */ |
| 639 | if (type < RTM_BASE) |
| 640 | return 0; |
| 641 | |
| 642 | /* Unknown message: reply with EINVAL */ |
| 643 | if (type > RTM_MAX) |
| 644 | goto err_inval; |
| 645 | |
| 646 | type -= RTM_BASE; |
| 647 | |
| 648 | /* All the messages must have at least 1 byte length */ |
| 649 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) |
| 650 | return 0; |
| 651 | |
| 652 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; |
| 653 | if (family >= NPROTO) { |
| 654 | *errp = -EAFNOSUPPORT; |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | link_tab = rtnetlink_links[family]; |
| 659 | if (link_tab == NULL) |
| 660 | link_tab = rtnetlink_links[PF_UNSPEC]; |
| 661 | link = &link_tab[type]; |
| 662 | |
| 663 | sz_idx = type>>2; |
| 664 | kind = type&3; |
| 665 | |
| 666 | if (kind != 2 && security_netlink_recv(skb)) { |
| 667 | *errp = -EPERM; |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (link->dumpit == NULL) |
| 673 | link = &(rtnetlink_links[PF_UNSPEC][type]); |
| 674 | |
| 675 | if (link->dumpit == NULL) |
| 676 | goto err_inval; |
| 677 | |
| 678 | if ((*errp = netlink_dump_start(rtnl, skb, nlh, |
Thomas Graf | a8f74b2 | 2005-11-10 02:25:52 +0100 | [diff] [blame] | 679 | link->dumpit, NULL)) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return -1; |
| 681 | } |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 682 | |
| 683 | netlink_queue_skip(nlh, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return -1; |
| 685 | } |
| 686 | |
| 687 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); |
| 688 | |
| 689 | min_len = rtm_min[sz_idx]; |
| 690 | if (nlh->nlmsg_len < min_len) |
| 691 | goto err_inval; |
| 692 | |
| 693 | if (nlh->nlmsg_len > min_len) { |
| 694 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
| 695 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); |
| 696 | |
| 697 | while (RTA_OK(attr, attrlen)) { |
| 698 | unsigned flavor = attr->rta_type; |
| 699 | if (flavor) { |
| 700 | if (flavor > rta_max[sz_idx]) |
| 701 | goto err_inval; |
| 702 | rta_buf[flavor-1] = attr; |
| 703 | } |
| 704 | attr = RTA_NEXT(attr, attrlen); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (link->doit == NULL) |
| 709 | link = &(rtnetlink_links[PF_UNSPEC][type]); |
| 710 | if (link->doit == NULL) |
| 711 | goto err_inval; |
| 712 | err = link->doit(skb, nlh, (void *)&rta_buf[0]); |
| 713 | |
| 714 | *errp = err; |
| 715 | return err; |
| 716 | |
| 717 | err_inval: |
| 718 | *errp = -EINVAL; |
| 719 | return -1; |
| 720 | } |
| 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | static void rtnetlink_rcv(struct sock *sk, int len) |
| 723 | { |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 724 | unsigned int qlen = 0; |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | do { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 727 | mutex_lock(&rtnl_mutex); |
Thomas Graf | 9ac4a16 | 2005-11-10 02:25:55 +0100 | [diff] [blame] | 728 | netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 729 | mutex_unlock(&rtnl_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | netdev_run_todo(); |
Herbert Xu | 2a0a6eb | 2005-05-03 14:55:09 -0700 | [diff] [blame] | 732 | } while (qlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Thomas Graf | db46edc | 2005-05-03 14:29:39 -0700 | [diff] [blame] | 735 | static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Jean Tourrilhes | 711e2c3 | 2006-02-22 15:10:56 -0800 | [diff] [blame] | 737 | [RTM_GETLINK - RTM_BASE] = { |
| 738 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
| 739 | .doit = do_getlink, |
| 740 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ |
| 741 | .dumpit = rtnetlink_dump_ifinfo }, |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 742 | [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink }, |
| 743 | [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, |
| 744 | [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, |
| 745 | [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add }, |
| 746 | [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete }, |
| 747 | [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info }, |
| 748 | [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, |
| 749 | [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info }, |
| 750 | [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | }; |
| 752 | |
| 753 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 754 | { |
| 755 | struct net_device *dev = ptr; |
| 756 | switch (event) { |
| 757 | case NETDEV_UNREGISTER: |
| 758 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
| 759 | break; |
| 760 | case NETDEV_REGISTER: |
| 761 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); |
| 762 | break; |
| 763 | case NETDEV_UP: |
| 764 | case NETDEV_DOWN: |
| 765 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
| 766 | break; |
| 767 | case NETDEV_CHANGE: |
| 768 | case NETDEV_GOING_DOWN: |
| 769 | break; |
| 770 | default: |
| 771 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
| 772 | break; |
| 773 | } |
| 774 | return NOTIFY_DONE; |
| 775 | } |
| 776 | |
| 777 | static struct notifier_block rtnetlink_dev_notifier = { |
| 778 | .notifier_call = rtnetlink_event, |
| 779 | }; |
| 780 | |
| 781 | void __init rtnetlink_init(void) |
| 782 | { |
| 783 | int i; |
| 784 | |
| 785 | rtattr_max = 0; |
| 786 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) |
| 787 | if (rta_max[i] > rtattr_max) |
| 788 | rtattr_max = rta_max[i]; |
| 789 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); |
| 790 | if (!rta_buf) |
| 791 | panic("rtnetlink_init: cannot allocate rta_buf\n"); |
| 792 | |
Patrick McHardy | 0662860 | 2005-08-15 12:33:26 -0700 | [diff] [blame] | 793 | rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, |
| 794 | THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | if (rtnl == NULL) |
| 796 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); |
| 797 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); |
| 798 | register_netdevice_notifier(&rtnetlink_dev_notifier); |
| 799 | rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table; |
| 800 | rtnetlink_links[PF_PACKET] = link_rtnetlink_table; |
| 801 | } |
| 802 | |
| 803 | EXPORT_SYMBOL(__rta_fill); |
| 804 | EXPORT_SYMBOL(rtattr_strlcpy); |
| 805 | EXPORT_SYMBOL(rtattr_parse); |
| 806 | EXPORT_SYMBOL(rtnetlink_links); |
| 807 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
| 808 | EXPORT_SYMBOL(rtnl); |
| 809 | EXPORT_SYMBOL(rtnl_lock); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 810 | EXPORT_SYMBOL(rtnl_trylock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | EXPORT_SYMBOL(rtnl_unlock); |