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