Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 LT |
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> | |
38 | ||
39 | #include <asm/uaccess.h> | |
40 | #include <asm/system.h> | |
41 | #include <asm/string.h> | |
42 | ||
43 | #include <linux/inet.h> | |
44 | #include <linux/netdevice.h> | |
45 | #include <net/ip.h> | |
46 | #include <net/protocol.h> | |
47 | #include <net/arp.h> | |
48 | #include <net/route.h> | |
49 | #include <net/udp.h> | |
50 | #include <net/sock.h> | |
51 | #include <net/pkt_sched.h> | |
52 | ||
53 | DECLARE_MUTEX(rtnl_sem); | |
54 | ||
55 | void rtnl_lock(void) | |
56 | { | |
57 | rtnl_shlock(); | |
58 | } | |
59 | ||
60 | int rtnl_lock_interruptible(void) | |
61 | { | |
62 | return down_interruptible(&rtnl_sem); | |
63 | } | |
64 | ||
65 | void rtnl_unlock(void) | |
66 | { | |
67 | rtnl_shunlock(); | |
68 | ||
69 | netdev_run_todo(); | |
70 | } | |
71 | ||
72 | int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) | |
73 | { | |
74 | memset(tb, 0, sizeof(struct rtattr*)*maxattr); | |
75 | ||
76 | while (RTA_OK(rta, len)) { | |
77 | unsigned flavor = rta->rta_type; | |
78 | if (flavor && flavor <= maxattr) | |
79 | tb[flavor-1] = rta; | |
80 | rta = RTA_NEXT(rta, len); | |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
85 | struct sock *rtnl; | |
86 | ||
87 | struct rtnetlink_link * rtnetlink_links[NPROTO]; | |
88 | ||
89 | static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] = | |
90 | { | |
91 | NLMSG_LENGTH(sizeof(struct ifinfomsg)), | |
92 | NLMSG_LENGTH(sizeof(struct ifaddrmsg)), | |
93 | NLMSG_LENGTH(sizeof(struct rtmsg)), | |
94 | NLMSG_LENGTH(sizeof(struct ndmsg)), | |
95 | NLMSG_LENGTH(sizeof(struct rtmsg)), | |
96 | NLMSG_LENGTH(sizeof(struct tcmsg)), | |
97 | NLMSG_LENGTH(sizeof(struct tcmsg)), | |
98 | NLMSG_LENGTH(sizeof(struct tcmsg)), | |
99 | NLMSG_LENGTH(sizeof(struct tcamsg)) | |
100 | }; | |
101 | ||
102 | static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] = | |
103 | { | |
104 | IFLA_MAX, | |
105 | IFA_MAX, | |
106 | RTA_MAX, | |
107 | NDA_MAX, | |
108 | RTA_MAX, | |
109 | TCA_MAX, | |
110 | TCA_MAX, | |
111 | TCA_MAX, | |
112 | TCAA_MAX | |
113 | }; | |
114 | ||
115 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
116 | { | |
117 | struct rtattr *rta; | |
118 | int size = RTA_LENGTH(attrlen); | |
119 | ||
120 | rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); | |
121 | rta->rta_type = attrtype; | |
122 | rta->rta_len = size; | |
123 | memcpy(RTA_DATA(rta), data, attrlen); | |
124 | } | |
125 | ||
126 | size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) | |
127 | { | |
128 | size_t ret = RTA_PAYLOAD(rta); | |
129 | char *src = RTA_DATA(rta); | |
130 | ||
131 | if (ret > 0 && src[ret - 1] == '\0') | |
132 | ret--; | |
133 | if (size > 0) { | |
134 | size_t len = (ret >= size) ? size - 1 : ret; | |
135 | memset(dest, 0, size); | |
136 | memcpy(dest, src, len); | |
137 | } | |
138 | return ret; | |
139 | } | |
140 | ||
141 | int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) | |
142 | { | |
143 | int err = 0; | |
144 | ||
145 | NETLINK_CB(skb).dst_groups = group; | |
146 | if (echo) | |
147 | atomic_inc(&skb->users); | |
148 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); | |
149 | if (echo) | |
150 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
151 | return err; | |
152 | } | |
153 | ||
154 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) | |
155 | { | |
156 | struct rtattr *mx = (struct rtattr*)skb->tail; | |
157 | int i; | |
158 | ||
159 | RTA_PUT(skb, RTA_METRICS, 0, NULL); | |
160 | for (i=0; i<RTAX_MAX; i++) { | |
161 | if (metrics[i]) | |
162 | RTA_PUT(skb, i+1, sizeof(u32), metrics+i); | |
163 | } | |
164 | mx->rta_len = skb->tail - (u8*)mx; | |
165 | if (mx->rta_len == RTA_LENGTH(0)) | |
166 | skb_trim(skb, (u8*)mx - skb->data); | |
167 | return 0; | |
168 | ||
169 | rtattr_failure: | |
170 | skb_trim(skb, (u8*)mx - skb->data); | |
171 | return -1; | |
172 | } | |
173 | ||
174 | ||
175 | static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, | |
176 | int type, u32 pid, u32 seq, u32 change) | |
177 | { | |
178 | struct ifinfomsg *r; | |
179 | struct nlmsghdr *nlh; | |
180 | unsigned char *b = skb->tail; | |
181 | ||
182 | nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r)); | |
183 | if (pid) nlh->nlmsg_flags |= NLM_F_MULTI; | |
184 | r = NLMSG_DATA(nlh); | |
185 | r->ifi_family = AF_UNSPEC; | |
186 | r->ifi_type = dev->type; | |
187 | r->ifi_index = dev->ifindex; | |
188 | r->ifi_flags = dev_get_flags(dev); | |
189 | r->ifi_change = change; | |
190 | ||
191 | RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name); | |
192 | ||
193 | if (1) { | |
194 | u32 txqlen = dev->tx_queue_len; | |
195 | RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen); | |
196 | } | |
197 | ||
198 | if (1) { | |
199 | u32 weight = dev->weight; | |
200 | RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight); | |
201 | } | |
202 | ||
203 | if (1) { | |
204 | struct rtnl_link_ifmap map = { | |
205 | .mem_start = dev->mem_start, | |
206 | .mem_end = dev->mem_end, | |
207 | .base_addr = dev->base_addr, | |
208 | .irq = dev->irq, | |
209 | .dma = dev->dma, | |
210 | .port = dev->if_port, | |
211 | }; | |
212 | RTA_PUT(skb, IFLA_MAP, sizeof(map), &map); | |
213 | } | |
214 | ||
215 | if (dev->addr_len) { | |
216 | RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); | |
217 | RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); | |
218 | } | |
219 | ||
220 | if (1) { | |
221 | u32 mtu = dev->mtu; | |
222 | RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu); | |
223 | } | |
224 | ||
225 | if (dev->ifindex != dev->iflink) { | |
226 | u32 iflink = dev->iflink; | |
227 | RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink); | |
228 | } | |
229 | ||
230 | if (dev->qdisc_sleeping) | |
231 | RTA_PUT(skb, IFLA_QDISC, | |
232 | strlen(dev->qdisc_sleeping->ops->id) + 1, | |
233 | dev->qdisc_sleeping->ops->id); | |
234 | ||
235 | if (dev->master) { | |
236 | u32 master = dev->master->ifindex; | |
237 | RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master); | |
238 | } | |
239 | ||
240 | if (dev->get_stats) { | |
241 | unsigned long *stats = (unsigned long*)dev->get_stats(dev); | |
242 | if (stats) { | |
243 | struct rtattr *a; | |
244 | __u32 *s; | |
245 | int i; | |
246 | int n = sizeof(struct rtnl_link_stats)/4; | |
247 | ||
248 | a = __RTA_PUT(skb, IFLA_STATS, n*4); | |
249 | s = RTA_DATA(a); | |
250 | for (i=0; i<n; i++) | |
251 | s[i] = stats[i]; | |
252 | } | |
253 | } | |
254 | nlh->nlmsg_len = skb->tail - b; | |
255 | return skb->len; | |
256 | ||
257 | nlmsg_failure: | |
258 | rtattr_failure: | |
259 | skb_trim(skb, b - skb->data); | |
260 | return -1; | |
261 | } | |
262 | ||
263 | static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) | |
264 | { | |
265 | int idx; | |
266 | int s_idx = cb->args[0]; | |
267 | struct net_device *dev; | |
268 | ||
269 | read_lock(&dev_base_lock); | |
270 | for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) { | |
271 | if (idx < s_idx) | |
272 | continue; | |
273 | if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0) | |
274 | break; | |
275 | } | |
276 | read_unlock(&dev_base_lock); | |
277 | cb->args[0] = idx; | |
278 | ||
279 | return skb->len; | |
280 | } | |
281 | ||
282 | static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
283 | { | |
284 | struct ifinfomsg *ifm = NLMSG_DATA(nlh); | |
285 | struct rtattr **ida = arg; | |
286 | struct net_device *dev; | |
287 | int err, send_addr_notify = 0; | |
288 | ||
289 | if (ifm->ifi_index >= 0) | |
290 | dev = dev_get_by_index(ifm->ifi_index); | |
291 | else if (ida[IFLA_IFNAME - 1]) { | |
292 | char ifname[IFNAMSIZ]; | |
293 | ||
294 | if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1], | |
295 | IFNAMSIZ) >= IFNAMSIZ) | |
296 | return -EINVAL; | |
297 | dev = dev_get_by_name(ifname); | |
298 | } else | |
299 | return -EINVAL; | |
300 | ||
301 | if (!dev) | |
302 | return -ENODEV; | |
303 | ||
304 | err = -EINVAL; | |
305 | ||
306 | if (ifm->ifi_flags) | |
307 | dev_change_flags(dev, ifm->ifi_flags); | |
308 | ||
309 | if (ida[IFLA_MAP - 1]) { | |
310 | struct rtnl_link_ifmap *u_map; | |
311 | struct ifmap k_map; | |
312 | ||
313 | if (!dev->set_config) { | |
314 | err = -EOPNOTSUPP; | |
315 | goto out; | |
316 | } | |
317 | ||
318 | if (!netif_device_present(dev)) { | |
319 | err = -ENODEV; | |
320 | goto out; | |
321 | } | |
322 | ||
323 | if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map))) | |
324 | goto out; | |
325 | ||
326 | u_map = RTA_DATA(ida[IFLA_MAP - 1]); | |
327 | ||
328 | k_map.mem_start = (unsigned long) u_map->mem_start; | |
329 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
330 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
331 | k_map.irq = (unsigned char) u_map->irq; | |
332 | k_map.dma = (unsigned char) u_map->dma; | |
333 | k_map.port = (unsigned char) u_map->port; | |
334 | ||
335 | err = dev->set_config(dev, &k_map); | |
336 | ||
337 | if (err) | |
338 | goto out; | |
339 | } | |
340 | ||
341 | if (ida[IFLA_ADDRESS - 1]) { | |
342 | if (!dev->set_mac_address) { | |
343 | err = -EOPNOTSUPP; | |
344 | goto out; | |
345 | } | |
346 | if (!netif_device_present(dev)) { | |
347 | err = -ENODEV; | |
348 | goto out; | |
349 | } | |
350 | if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len)) | |
351 | goto out; | |
352 | ||
353 | err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1])); | |
354 | if (err) | |
355 | goto out; | |
356 | send_addr_notify = 1; | |
357 | } | |
358 | ||
359 | if (ida[IFLA_BROADCAST - 1]) { | |
360 | if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len)) | |
361 | goto out; | |
362 | memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]), | |
363 | dev->addr_len); | |
364 | send_addr_notify = 1; | |
365 | } | |
366 | ||
367 | if (ida[IFLA_MTU - 1]) { | |
368 | if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
369 | goto out; | |
370 | err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1]))); | |
371 | ||
372 | if (err) | |
373 | goto out; | |
374 | ||
375 | } | |
376 | ||
377 | if (ida[IFLA_TXQLEN - 1]) { | |
378 | if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
379 | goto out; | |
380 | ||
381 | dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1])); | |
382 | } | |
383 | ||
384 | if (ida[IFLA_WEIGHT - 1]) { | |
385 | if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
386 | goto out; | |
387 | ||
388 | dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1])); | |
389 | } | |
390 | ||
391 | if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) { | |
392 | char ifname[IFNAMSIZ]; | |
393 | ||
394 | if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1], | |
395 | IFNAMSIZ) >= IFNAMSIZ) | |
396 | goto out; | |
397 | err = dev_change_name(dev, ifname); | |
398 | if (err) | |
399 | goto out; | |
400 | } | |
401 | ||
402 | err = 0; | |
403 | ||
404 | out: | |
405 | if (send_addr_notify) | |
406 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
407 | ||
408 | dev_put(dev); | |
409 | return err; | |
410 | } | |
411 | ||
412 | static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb) | |
413 | { | |
414 | int idx; | |
415 | int s_idx = cb->family; | |
416 | ||
417 | if (s_idx == 0) | |
418 | s_idx = 1; | |
419 | for (idx=1; idx<NPROTO; idx++) { | |
420 | int type = cb->nlh->nlmsg_type-RTM_BASE; | |
421 | if (idx < s_idx || idx == PF_PACKET) | |
422 | continue; | |
423 | if (rtnetlink_links[idx] == NULL || | |
424 | rtnetlink_links[idx][type].dumpit == NULL) | |
425 | continue; | |
426 | if (idx > s_idx) | |
427 | memset(&cb->args[0], 0, sizeof(cb->args)); | |
428 | if (rtnetlink_links[idx][type].dumpit(skb, cb)) | |
429 | break; | |
430 | } | |
431 | cb->family = idx; | |
432 | ||
433 | return skb->len; | |
434 | } | |
435 | ||
436 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) | |
437 | { | |
438 | struct sk_buff *skb; | |
439 | int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + | |
440 | sizeof(struct rtnl_link_ifmap) + | |
441 | sizeof(struct rtnl_link_stats) + 128); | |
442 | ||
443 | skb = alloc_skb(size, GFP_KERNEL); | |
444 | if (!skb) | |
445 | return; | |
446 | ||
447 | if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) { | |
448 | kfree_skb(skb); | |
449 | return; | |
450 | } | |
451 | NETLINK_CB(skb).dst_groups = RTMGRP_LINK; | |
452 | netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL); | |
453 | } | |
454 | ||
455 | static int rtnetlink_done(struct netlink_callback *cb) | |
456 | { | |
457 | return 0; | |
458 | } | |
459 | ||
460 | /* Protected by RTNL sempahore. */ | |
461 | static struct rtattr **rta_buf; | |
462 | static int rtattr_max; | |
463 | ||
464 | /* Process one rtnetlink message. */ | |
465 | ||
466 | static __inline__ int | |
467 | rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) | |
468 | { | |
469 | struct rtnetlink_link *link; | |
470 | struct rtnetlink_link *link_tab; | |
471 | int sz_idx, kind; | |
472 | int min_len; | |
473 | int family; | |
474 | int type; | |
475 | int err; | |
476 | ||
477 | /* Only requests are handled by kernel now */ | |
478 | if (!(nlh->nlmsg_flags&NLM_F_REQUEST)) | |
479 | return 0; | |
480 | ||
481 | type = nlh->nlmsg_type; | |
482 | ||
483 | /* A control message: ignore them */ | |
484 | if (type < RTM_BASE) | |
485 | return 0; | |
486 | ||
487 | /* Unknown message: reply with EINVAL */ | |
488 | if (type > RTM_MAX) | |
489 | goto err_inval; | |
490 | ||
491 | type -= RTM_BASE; | |
492 | ||
493 | /* All the messages must have at least 1 byte length */ | |
494 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) | |
495 | return 0; | |
496 | ||
497 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; | |
498 | if (family >= NPROTO) { | |
499 | *errp = -EAFNOSUPPORT; | |
500 | return -1; | |
501 | } | |
502 | ||
503 | link_tab = rtnetlink_links[family]; | |
504 | if (link_tab == NULL) | |
505 | link_tab = rtnetlink_links[PF_UNSPEC]; | |
506 | link = &link_tab[type]; | |
507 | ||
508 | sz_idx = type>>2; | |
509 | kind = type&3; | |
510 | ||
511 | if (kind != 2 && security_netlink_recv(skb)) { | |
512 | *errp = -EPERM; | |
513 | return -1; | |
514 | } | |
515 | ||
516 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { | |
517 | u32 rlen; | |
518 | ||
519 | if (link->dumpit == NULL) | |
520 | link = &(rtnetlink_links[PF_UNSPEC][type]); | |
521 | ||
522 | if (link->dumpit == NULL) | |
523 | goto err_inval; | |
524 | ||
525 | if ((*errp = netlink_dump_start(rtnl, skb, nlh, | |
526 | link->dumpit, | |
527 | rtnetlink_done)) != 0) { | |
528 | return -1; | |
529 | } | |
530 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); | |
531 | if (rlen > skb->len) | |
532 | rlen = skb->len; | |
533 | skb_pull(skb, rlen); | |
534 | return -1; | |
535 | } | |
536 | ||
537 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); | |
538 | ||
539 | min_len = rtm_min[sz_idx]; | |
540 | if (nlh->nlmsg_len < min_len) | |
541 | goto err_inval; | |
542 | ||
543 | if (nlh->nlmsg_len > min_len) { | |
544 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
545 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); | |
546 | ||
547 | while (RTA_OK(attr, attrlen)) { | |
548 | unsigned flavor = attr->rta_type; | |
549 | if (flavor) { | |
550 | if (flavor > rta_max[sz_idx]) | |
551 | goto err_inval; | |
552 | rta_buf[flavor-1] = attr; | |
553 | } | |
554 | attr = RTA_NEXT(attr, attrlen); | |
555 | } | |
556 | } | |
557 | ||
558 | if (link->doit == NULL) | |
559 | link = &(rtnetlink_links[PF_UNSPEC][type]); | |
560 | if (link->doit == NULL) | |
561 | goto err_inval; | |
562 | err = link->doit(skb, nlh, (void *)&rta_buf[0]); | |
563 | ||
564 | *errp = err; | |
565 | return err; | |
566 | ||
567 | err_inval: | |
568 | *errp = -EINVAL; | |
569 | return -1; | |
570 | } | |
571 | ||
572 | /* | |
573 | * Process one packet of messages. | |
574 | * Malformed skbs with wrong lengths of messages are discarded silently. | |
575 | */ | |
576 | ||
577 | static inline int rtnetlink_rcv_skb(struct sk_buff *skb) | |
578 | { | |
579 | int err; | |
580 | struct nlmsghdr * nlh; | |
581 | ||
582 | while (skb->len >= NLMSG_SPACE(0)) { | |
583 | u32 rlen; | |
584 | ||
585 | nlh = (struct nlmsghdr *)skb->data; | |
586 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
587 | return 0; | |
588 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); | |
589 | if (rlen > skb->len) | |
590 | rlen = skb->len; | |
591 | if (rtnetlink_rcv_msg(skb, nlh, &err)) { | |
592 | /* Not error, but we must interrupt processing here: | |
593 | * Note, that in this case we do not pull message | |
594 | * from skb, it will be processed later. | |
595 | */ | |
596 | if (err == 0) | |
597 | return -1; | |
598 | netlink_ack(skb, nlh, err); | |
599 | } else if (nlh->nlmsg_flags&NLM_F_ACK) | |
600 | netlink_ack(skb, nlh, 0); | |
601 | skb_pull(skb, rlen); | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | /* | |
608 | * rtnetlink input queue processing routine: | |
609 | * - try to acquire shared lock. If it is failed, defer processing. | |
610 | * - feed skbs to rtnetlink_rcv_skb, until it refuse a message, | |
611 | * that will occur, when a dump started and/or acquisition of | |
612 | * exclusive lock failed. | |
613 | */ | |
614 | ||
615 | static void rtnetlink_rcv(struct sock *sk, int len) | |
616 | { | |
617 | do { | |
618 | struct sk_buff *skb; | |
619 | ||
620 | if (rtnl_shlock_nowait()) | |
621 | return; | |
622 | ||
623 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { | |
624 | if (rtnetlink_rcv_skb(skb)) { | |
625 | if (skb->len) | |
626 | skb_queue_head(&sk->sk_receive_queue, | |
627 | skb); | |
628 | else | |
629 | kfree_skb(skb); | |
630 | break; | |
631 | } | |
632 | kfree_skb(skb); | |
633 | } | |
634 | ||
635 | up(&rtnl_sem); | |
636 | ||
637 | netdev_run_todo(); | |
638 | } while (rtnl && rtnl->sk_receive_queue.qlen); | |
639 | } | |
640 | ||
641 | static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] = | |
642 | { | |
643 | [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo }, | |
644 | [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink }, | |
645 | [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, | |
646 | [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, | |
647 | [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add }, | |
648 | [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete }, | |
240eed95 | 649 | [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info }, |
98f245e7 | 650 | [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, |
1da177e4 LT |
651 | }; |
652 | ||
653 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) | |
654 | { | |
655 | struct net_device *dev = ptr; | |
656 | switch (event) { | |
657 | case NETDEV_UNREGISTER: | |
658 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); | |
659 | break; | |
660 | case NETDEV_REGISTER: | |
661 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
662 | break; | |
663 | case NETDEV_UP: | |
664 | case NETDEV_DOWN: | |
665 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); | |
666 | break; | |
667 | case NETDEV_CHANGE: | |
668 | case NETDEV_GOING_DOWN: | |
669 | break; | |
670 | default: | |
671 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
672 | break; | |
673 | } | |
674 | return NOTIFY_DONE; | |
675 | } | |
676 | ||
677 | static struct notifier_block rtnetlink_dev_notifier = { | |
678 | .notifier_call = rtnetlink_event, | |
679 | }; | |
680 | ||
681 | void __init rtnetlink_init(void) | |
682 | { | |
683 | int i; | |
684 | ||
685 | rtattr_max = 0; | |
686 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) | |
687 | if (rta_max[i] > rtattr_max) | |
688 | rtattr_max = rta_max[i]; | |
689 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); | |
690 | if (!rta_buf) | |
691 | panic("rtnetlink_init: cannot allocate rta_buf\n"); | |
692 | ||
693 | rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv); | |
694 | if (rtnl == NULL) | |
695 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); | |
696 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); | |
697 | register_netdevice_notifier(&rtnetlink_dev_notifier); | |
698 | rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table; | |
699 | rtnetlink_links[PF_PACKET] = link_rtnetlink_table; | |
700 | } | |
701 | ||
702 | EXPORT_SYMBOL(__rta_fill); | |
703 | EXPORT_SYMBOL(rtattr_strlcpy); | |
704 | EXPORT_SYMBOL(rtattr_parse); | |
705 | EXPORT_SYMBOL(rtnetlink_links); | |
706 | EXPORT_SYMBOL(rtnetlink_put_metrics); | |
707 | EXPORT_SYMBOL(rtnl); | |
708 | EXPORT_SYMBOL(rtnl_lock); | |
709 | EXPORT_SYMBOL(rtnl_lock_interruptible); | |
710 | EXPORT_SYMBOL(rtnl_sem); | |
711 | EXPORT_SYMBOL(rtnl_unlock); |