blob: 2adc966d981e34a2857c4da6b795b54380de40c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
25#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080037#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070038#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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 Graf14c0b972006-08-04 03:38:38 -070053#include <net/fib_rules.h>
Thomas Graf9ac4a162005-11-10 02:25:55 +010054#include <net/netlink.h>
Jean Tourrilhes711e2c32006-02-22 15:10:56 -080055#ifdef CONFIG_NET_WIRELESS_RTNETLINK
56#include <linux/wireless.h>
57#include <net/iw_handler.h>
58#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Stephen Hemminger6756ae42006-03-20 22:23:58 -080060static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62void rtnl_lock(void)
63{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080064 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Stephen Hemminger6756ae42006-03-20 22:23:58 -080067void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080069 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072void rtnl_unlock(void)
73{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074 mutex_unlock(&rtnl_mutex);
75 if (rtnl && rtnl->sk_receive_queue.qlen)
76 rtnl->sk_data_ready(rtnl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 netdev_run_todo();
78}
79
Stephen Hemminger6756ae42006-03-20 22:23:58 -080080int rtnl_trylock(void)
81{
82 return mutex_trylock(&rtnl_mutex);
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
86{
87 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
88
89 while (RTA_OK(rta, len)) {
90 unsigned flavor = rta->rta_type;
91 if (flavor && flavor <= maxattr)
92 tb[flavor-1] = rta;
93 rta = RTA_NEXT(rta, len);
94 }
95 return 0;
96}
97
98struct sock *rtnl;
99
100struct rtnetlink_link * rtnetlink_links[NPROTO];
101
Thomas Grafdb46edc2005-05-03 14:29:39 -0700102static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Thomas Graff90a0a72005-05-03 14:29:00 -0700104 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
105 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
106 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
107 [RTM_FAM(RTM_NEWNEIGH)] = NLMSG_LENGTH(sizeof(struct ndmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700108 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700109 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
110 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
111 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
112 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
113 [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
114 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
115 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700116 [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Thomas Grafdb46edc2005-05-03 14:29:39 -0700119static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Thomas Graff90a0a72005-05-03 14:29:00 -0700121 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
122 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
123 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
124 [RTM_FAM(RTM_NEWNEIGH)] = NDA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700125 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700126 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
127 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
128 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
129 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700130 [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
134{
135 struct rtattr *rta;
136 int size = RTA_LENGTH(attrlen);
137
138 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
139 rta->rta_type = attrtype;
140 rta->rta_len = size;
141 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700142 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
146{
147 size_t ret = RTA_PAYLOAD(rta);
148 char *src = RTA_DATA(rta);
149
150 if (ret > 0 && src[ret - 1] == '\0')
151 ret--;
152 if (size > 0) {
153 size_t len = (ret >= size) ? size - 1 : ret;
154 memset(dest, 0, size);
155 memcpy(dest, src, len);
156 }
157 return ret;
158}
159
160int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
161{
162 int err = 0;
163
Patrick McHardyac6d4392005-08-14 19:29:52 -0700164 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (echo)
166 atomic_inc(&skb->users);
167 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
168 if (echo)
169 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
170 return err;
171}
172
173int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
174{
175 struct rtattr *mx = (struct rtattr*)skb->tail;
176 int i;
177
178 RTA_PUT(skb, RTA_METRICS, 0, NULL);
179 for (i=0; i<RTAX_MAX; i++) {
180 if (metrics[i])
181 RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
182 }
183 mx->rta_len = skb->tail - (u8*)mx;
184 if (mx->rta_len == RTA_LENGTH(0))
185 skb_trim(skb, (u8*)mx - skb->data);
186 return 0;
187
188rtattr_failure:
189 skb_trim(skb, (u8*)mx - skb->data);
190 return -1;
191}
192
193
Stefan Rompfb00055a2006-03-20 17:09:11 -0800194static void set_operstate(struct net_device *dev, unsigned char transition)
195{
196 unsigned char operstate = dev->operstate;
197
198 switch(transition) {
199 case IF_OPER_UP:
200 if ((operstate == IF_OPER_DORMANT ||
201 operstate == IF_OPER_UNKNOWN) &&
202 !netif_dormant(dev))
203 operstate = IF_OPER_UP;
204 break;
205
206 case IF_OPER_DORMANT:
207 if (operstate == IF_OPER_UP ||
208 operstate == IF_OPER_UNKNOWN)
209 operstate = IF_OPER_DORMANT;
210 break;
211 };
212
213 if (dev->operstate != operstate) {
214 write_lock_bh(&dev_base_lock);
215 dev->operstate = operstate;
216 write_unlock_bh(&dev_base_lock);
217 netdev_state_change(dev);
218 }
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700222 int type, u32 pid, u32 seq, u32 change,
223 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct ifinfomsg *r;
226 struct nlmsghdr *nlh;
227 unsigned char *b = skb->tail;
228
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700229 nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 r = NLMSG_DATA(nlh);
231 r->ifi_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700232 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 r->ifi_type = dev->type;
234 r->ifi_index = dev->ifindex;
235 r->ifi_flags = dev_get_flags(dev);
236 r->ifi_change = change;
237
238 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
239
240 if (1) {
241 u32 txqlen = dev->tx_queue_len;
242 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
243 }
244
245 if (1) {
246 u32 weight = dev->weight;
247 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
248 }
249
250 if (1) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800251 u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN;
252 u8 link_mode = dev->link_mode;
253 RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate);
254 RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode);
255 }
256
257 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct rtnl_link_ifmap map = {
259 .mem_start = dev->mem_start,
260 .mem_end = dev->mem_end,
261 .base_addr = dev->base_addr,
262 .irq = dev->irq,
263 .dma = dev->dma,
264 .port = dev->if_port,
265 };
266 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
267 }
268
269 if (dev->addr_len) {
270 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
271 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
272 }
273
274 if (1) {
275 u32 mtu = dev->mtu;
276 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
277 }
278
279 if (dev->ifindex != dev->iflink) {
280 u32 iflink = dev->iflink;
281 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
282 }
283
284 if (dev->qdisc_sleeping)
285 RTA_PUT(skb, IFLA_QDISC,
286 strlen(dev->qdisc_sleeping->ops->id) + 1,
287 dev->qdisc_sleeping->ops->id);
288
289 if (dev->master) {
290 u32 master = dev->master->ifindex;
291 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
292 }
293
294 if (dev->get_stats) {
295 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
296 if (stats) {
297 struct rtattr *a;
298 __u32 *s;
299 int i;
300 int n = sizeof(struct rtnl_link_stats)/4;
301
302 a = __RTA_PUT(skb, IFLA_STATS, n*4);
303 s = RTA_DATA(a);
304 for (i=0; i<n; i++)
305 s[i] = stats[i];
306 }
307 }
308 nlh->nlmsg_len = skb->tail - b;
309 return skb->len;
310
311nlmsg_failure:
312rtattr_failure:
313 skb_trim(skb, b - skb->data);
314 return -1;
315}
316
317static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
318{
319 int idx;
320 int s_idx = cb->args[0];
321 struct net_device *dev;
322
323 read_lock(&dev_base_lock);
324 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
325 if (idx < s_idx)
326 continue;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700327 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
328 NETLINK_CB(cb->skb).pid,
329 cb->nlh->nlmsg_seq, 0,
330 NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332 }
333 read_unlock(&dev_base_lock);
334 cb->args[0] = idx;
335
336 return skb->len;
337}
338
Thomas Grafda5e0492006-08-10 21:17:37 -0700339static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
340 [IFLA_IFNAME] = { .type = NLA_STRING },
341 [IFLA_MAP] = { .minlen = sizeof(struct rtnl_link_ifmap) },
342 [IFLA_MTU] = { .type = NLA_U32 },
343 [IFLA_TXQLEN] = { .type = NLA_U32 },
344 [IFLA_WEIGHT] = { .type = NLA_U32 },
345 [IFLA_OPERSTATE] = { .type = NLA_U8 },
346 [IFLA_LINKMODE] = { .type = NLA_U8 },
347};
348
349static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Thomas Grafda5e0492006-08-10 21:17:37 -0700351 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct net_device *dev;
Thomas Grafda5e0492006-08-10 21:17:37 -0700353 int err, send_addr_notify = 0, modified = 0;
354 struct nlattr *tb[IFLA_MAX+1];
355 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Thomas Grafda5e0492006-08-10 21:17:37 -0700357 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
358 if (err < 0)
359 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Thomas Grafda5e0492006-08-10 21:17:37 -0700361 if (tb[IFLA_IFNAME] &&
362 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -EINVAL;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700366 ifm = nlmsg_data(nlh);
367 if (ifm->ifi_index >= 0)
368 dev = dev_get_by_index(ifm->ifi_index);
369 else if (tb[IFLA_IFNAME])
370 dev = dev_get_by_name(ifname);
371 else
372 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Thomas Grafda5e0492006-08-10 21:17:37 -0700374 if (dev == NULL) {
375 err = -ENODEV;
376 goto errout;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Thomas Grafda5e0492006-08-10 21:17:37 -0700379 if (tb[IFLA_ADDRESS] &&
380 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
381 goto errout_dev;
382
383 if (tb[IFLA_BROADCAST] &&
384 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
385 goto errout_dev;
386
387 if (tb[IFLA_MAP]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct rtnl_link_ifmap *u_map;
389 struct ifmap k_map;
390
391 if (!dev->set_config) {
392 err = -EOPNOTSUPP;
Thomas Grafda5e0492006-08-10 21:17:37 -0700393 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 if (!netif_device_present(dev)) {
397 err = -ENODEV;
Thomas Grafda5e0492006-08-10 21:17:37 -0700398 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Thomas Grafda5e0492006-08-10 21:17:37 -0700401 u_map = nla_data(tb[IFLA_MAP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 k_map.mem_start = (unsigned long) u_map->mem_start;
403 k_map.mem_end = (unsigned long) u_map->mem_end;
404 k_map.base_addr = (unsigned short) u_map->base_addr;
405 k_map.irq = (unsigned char) u_map->irq;
406 k_map.dma = (unsigned char) u_map->dma;
407 k_map.port = (unsigned char) u_map->port;
408
409 err = dev->set_config(dev, &k_map);
Thomas Grafda5e0492006-08-10 21:17:37 -0700410 if (err < 0)
411 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Thomas Grafda5e0492006-08-10 21:17:37 -0700413 modified = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
Thomas Grafda5e0492006-08-10 21:17:37 -0700416 if (tb[IFLA_ADDRESS]) {
David S. Miller70f8e782006-08-08 16:47:37 -0700417 struct sockaddr *sa;
418 int len;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!dev->set_mac_address) {
421 err = -EOPNOTSUPP;
Thomas Grafda5e0492006-08-10 21:17:37 -0700422 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Thomas Grafda5e0492006-08-10 21:17:37 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!netif_device_present(dev)) {
426 err = -ENODEV;
Thomas Grafda5e0492006-08-10 21:17:37 -0700427 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
David S. Miller70f8e782006-08-08 16:47:37 -0700430 len = sizeof(sa_family_t) + dev->addr_len;
431 sa = kmalloc(len, GFP_KERNEL);
432 if (!sa) {
433 err = -ENOMEM;
Thomas Grafda5e0492006-08-10 21:17:37 -0700434 goto errout_dev;
David S. Miller70f8e782006-08-08 16:47:37 -0700435 }
436 sa->sa_family = dev->type;
Thomas Grafda5e0492006-08-10 21:17:37 -0700437 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
David S. Miller70f8e782006-08-08 16:47:37 -0700438 dev->addr_len);
439 err = dev->set_mac_address(dev, sa);
440 kfree(sa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (err)
Thomas Grafda5e0492006-08-10 21:17:37 -0700442 goto errout_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 send_addr_notify = 1;
Thomas Grafda5e0492006-08-10 21:17:37 -0700444 modified = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Thomas Grafda5e0492006-08-10 21:17:37 -0700447 if (tb[IFLA_MTU]) {
448 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
449 if (err < 0)
450 goto errout_dev;
451 modified = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Thomas Grafda5e0492006-08-10 21:17:37 -0700454 /*
455 * Interface selected by interface index but interface
456 * name provided implies that a name change has been
457 * requested.
458 */
459 if (ifm->ifi_index >= 0 && ifname[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 err = dev_change_name(dev, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -0700461 if (err < 0)
462 goto errout_dev;
463 modified = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800466#ifdef CONFIG_NET_WIRELESS_RTNETLINK
Thomas Grafda5e0492006-08-10 21:17:37 -0700467 if (tb[IFLA_WIRELESS]) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800468 /* Call Wireless Extensions.
469 * Various stuff checked in there... */
Thomas Grafda5e0492006-08-10 21:17:37 -0700470 err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
471 nla_len(tb[IFLA_WIRELESS]));
472 if (err < 0)
473 goto errout_dev;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800474 }
475#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
476
Thomas Grafda5e0492006-08-10 21:17:37 -0700477 if (tb[IFLA_BROADCAST]) {
478 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
479 send_addr_notify = 1;
480 }
481
482
483 if (ifm->ifi_flags)
484 dev_change_flags(dev, ifm->ifi_flags);
485
486 if (tb[IFLA_TXQLEN])
487 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
488
489 if (tb[IFLA_WEIGHT])
490 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
491
492 if (tb[IFLA_OPERSTATE])
493 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
494
495 if (tb[IFLA_LINKMODE]) {
496 write_lock_bh(&dev_base_lock);
497 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
498 write_unlock_bh(&dev_base_lock);
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 err = 0;
502
Thomas Grafda5e0492006-08-10 21:17:37 -0700503errout_dev:
504 if (err < 0 && modified && net_ratelimit())
505 printk(KERN_WARNING "A link change request failed with "
506 "some changes comitted already. Interface %s may "
507 "have been left with an inconsistent configuration, "
508 "please check.\n", dev->name);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (send_addr_notify)
511 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
512
513 dev_put(dev);
Thomas Grafda5e0492006-08-10 21:17:37 -0700514errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return err;
516}
517
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800518#ifdef CONFIG_NET_WIRELESS_RTNETLINK
519static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
520{
521 struct ifinfomsg *ifm = NLMSG_DATA(in_nlh);
522 struct rtattr **ida = arg;
523 struct net_device *dev;
524 struct ifinfomsg *r;
525 struct nlmsghdr *nlh;
526 int err = -ENOBUFS;
527 struct sk_buff *skb;
528 unsigned char *b;
529 char *iw_buf = NULL;
530 int iw_buf_len = 0;
531
532 if (ifm->ifi_index >= 0)
533 dev = dev_get_by_index(ifm->ifi_index);
534 else
535 return -EINVAL;
536 if (!dev)
537 return -ENODEV;
538
539#ifdef CONFIG_NET_WIRELESS_RTNETLINK
540 if (ida[IFLA_WIRELESS - 1]) {
541
542 /* Call Wireless Extensions. We need to know the size before
543 * we can alloc. Various stuff checked in there... */
544 err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
545 if (err)
546 goto out;
547 }
548#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
549
550 /* Create a skb big enough to include all the data.
551 * Some requests are way bigger than 4k... Jean II */
552 skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
553 GFP_KERNEL);
554 if (!skb)
555 goto out;
556 b = skb->tail;
557
558 /* Put in the message the usual good stuff */
559 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
560 RTM_NEWLINK, sizeof(*r));
561 r = NLMSG_DATA(nlh);
562 r->ifi_family = AF_UNSPEC;
563 r->__ifi_pad = 0;
564 r->ifi_type = dev->type;
565 r->ifi_index = dev->ifindex;
566 r->ifi_flags = dev->flags;
567 r->ifi_change = 0;
568
569 /* Put the wireless payload if it exist */
570 if(iw_buf != NULL)
571 RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
572 iw_buf + IW_EV_POINT_OFF);
573
574 nlh->nlmsg_len = skb->tail - b;
575
576 /* Needed ? */
577 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
578
579 err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
580 if (err > 0)
581 err = 0;
582out:
583 if(iw_buf != NULL)
584 kfree(iw_buf);
585 dev_put(dev);
586 return err;
587
588rtattr_failure:
589nlmsg_failure:
590 kfree_skb(skb);
591 goto out;
592}
593#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
596{
597 int idx;
598 int s_idx = cb->family;
599
600 if (s_idx == 0)
601 s_idx = 1;
602 for (idx=1; idx<NPROTO; idx++) {
603 int type = cb->nlh->nlmsg_type-RTM_BASE;
604 if (idx < s_idx || idx == PF_PACKET)
605 continue;
606 if (rtnetlink_links[idx] == NULL ||
607 rtnetlink_links[idx][type].dumpit == NULL)
608 continue;
609 if (idx > s_idx)
610 memset(&cb->args[0], 0, sizeof(cb->args));
611 if (rtnetlink_links[idx][type].dumpit(skb, cb))
612 break;
613 }
614 cb->family = idx;
615
616 return skb->len;
617}
618
619void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
620{
621 struct sk_buff *skb;
622 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
623 sizeof(struct rtnl_link_ifmap) +
624 sizeof(struct rtnl_link_stats) + 128);
625
626 skb = alloc_skb(size, GFP_KERNEL);
627 if (!skb)
628 return;
629
Alexey Kuznetsov28633512006-02-09 16:40:58 -0800630 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 kfree_skb(skb);
632 return;
633 }
Patrick McHardyac6d4392005-08-14 19:29:52 -0700634 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
635 netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* Protected by RTNL sempahore. */
639static struct rtattr **rta_buf;
640static int rtattr_max;
641
642/* Process one rtnetlink message. */
643
644static __inline__ int
645rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
646{
647 struct rtnetlink_link *link;
648 struct rtnetlink_link *link_tab;
649 int sz_idx, kind;
650 int min_len;
651 int family;
652 int type;
653 int err;
654
655 /* Only requests are handled by kernel now */
656 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
657 return 0;
658
659 type = nlh->nlmsg_type;
660
661 /* A control message: ignore them */
662 if (type < RTM_BASE)
663 return 0;
664
665 /* Unknown message: reply with EINVAL */
666 if (type > RTM_MAX)
667 goto err_inval;
668
669 type -= RTM_BASE;
670
671 /* All the messages must have at least 1 byte length */
672 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
673 return 0;
674
675 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
676 if (family >= NPROTO) {
677 *errp = -EAFNOSUPPORT;
678 return -1;
679 }
680
681 link_tab = rtnetlink_links[family];
682 if (link_tab == NULL)
683 link_tab = rtnetlink_links[PF_UNSPEC];
684 link = &link_tab[type];
685
686 sz_idx = type>>2;
687 kind = type&3;
688
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700689 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *errp = -EPERM;
691 return -1;
692 }
693
694 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (link->dumpit == NULL)
696 link = &(rtnetlink_links[PF_UNSPEC][type]);
697
698 if (link->dumpit == NULL)
699 goto err_inval;
700
701 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +0100702 link->dumpit, NULL)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -1;
704 }
Thomas Graf9ac4a162005-11-10 02:25:55 +0100705
706 netlink_queue_skip(nlh, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -1;
708 }
709
710 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
711
712 min_len = rtm_min[sz_idx];
713 if (nlh->nlmsg_len < min_len)
714 goto err_inval;
715
716 if (nlh->nlmsg_len > min_len) {
717 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
718 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
719
720 while (RTA_OK(attr, attrlen)) {
721 unsigned flavor = attr->rta_type;
722 if (flavor) {
723 if (flavor > rta_max[sz_idx])
724 goto err_inval;
725 rta_buf[flavor-1] = attr;
726 }
727 attr = RTA_NEXT(attr, attrlen);
728 }
729 }
730
731 if (link->doit == NULL)
732 link = &(rtnetlink_links[PF_UNSPEC][type]);
733 if (link->doit == NULL)
734 goto err_inval;
735 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
736
737 *errp = err;
738 return err;
739
740err_inval:
741 *errp = -EINVAL;
742 return -1;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static void rtnetlink_rcv(struct sock *sk, int len)
746{
Thomas Graf9ac4a162005-11-10 02:25:55 +0100747 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 do {
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800750 mutex_lock(&rtnl_mutex);
Thomas Graf9ac4a162005-11-10 02:25:55 +0100751 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800752 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700755 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Thomas Grafdb46edc2005-05-03 14:29:39 -0700758static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800760 [RTM_GETLINK - RTM_BASE] = {
761#ifdef CONFIG_NET_WIRELESS_RTNETLINK
762 .doit = do_getlink,
763#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
764 .dumpit = rtnetlink_dump_ifinfo },
Thomas Grafda5e0492006-08-10 21:17:37 -0700765 [RTM_SETLINK - RTM_BASE] = { .doit = rtnl_setlink },
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700766 [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
767 [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
768 [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
769 [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
770 [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
Thomas Graf14c0b972006-08-04 03:38:38 -0700771#ifdef CONFIG_FIB_RULES
772 [RTM_NEWRULE - RTM_BASE] = { .doit = fib_nl_newrule },
773 [RTM_DELRULE - RTM_BASE] = { .doit = fib_nl_delrule },
774#endif
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700775 [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
776 [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
777 [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
780static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
781{
782 struct net_device *dev = ptr;
783 switch (event) {
784 case NETDEV_UNREGISTER:
785 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
786 break;
787 case NETDEV_REGISTER:
788 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
789 break;
790 case NETDEV_UP:
791 case NETDEV_DOWN:
792 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
793 break;
794 case NETDEV_CHANGE:
795 case NETDEV_GOING_DOWN:
796 break;
797 default:
798 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
799 break;
800 }
801 return NOTIFY_DONE;
802}
803
804static struct notifier_block rtnetlink_dev_notifier = {
805 .notifier_call = rtnetlink_event,
806};
807
808void __init rtnetlink_init(void)
809{
810 int i;
811
812 rtattr_max = 0;
813 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
814 if (rta_max[i] > rtattr_max)
815 rtattr_max = rta_max[i];
816 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
817 if (!rta_buf)
818 panic("rtnetlink_init: cannot allocate rta_buf\n");
819
Patrick McHardy06628602005-08-15 12:33:26 -0700820 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
821 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (rtnl == NULL)
823 panic("rtnetlink_init: cannot initialize rtnetlink\n");
824 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
825 register_netdevice_notifier(&rtnetlink_dev_notifier);
826 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
827 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
828}
829
830EXPORT_SYMBOL(__rta_fill);
831EXPORT_SYMBOL(rtattr_strlcpy);
832EXPORT_SYMBOL(rtattr_parse);
833EXPORT_SYMBOL(rtnetlink_links);
834EXPORT_SYMBOL(rtnetlink_put_metrics);
835EXPORT_SYMBOL(rtnl);
836EXPORT_SYMBOL(rtnl_lock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800837EXPORT_SYMBOL(rtnl_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838EXPORT_SYMBOL(rtnl_unlock);