]>
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 | ||
1da177e4 LT |
19 | #include <linux/errno.h> |
20 | #include <linux/module.h> | |
21 | #include <linux/types.h> | |
22 | #include <linux/socket.h> | |
23 | #include <linux/kernel.h> | |
1da177e4 LT |
24 | #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> | |
6756ae4b | 37 | #include <linux/mutex.h> |
1da177e4 LT |
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> | |
14c0b97d | 52 | #include <net/fib_rules.h> |
9ac4a169 | 53 | #include <net/netlink.h> |
711e2c33 JT |
54 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
55 | #include <linux/wireless.h> | |
56 | #include <net/iw_handler.h> | |
57 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
1da177e4 | 58 | |
6756ae4b | 59 | static DEFINE_MUTEX(rtnl_mutex); |
1da177e4 LT |
60 | |
61 | void rtnl_lock(void) | |
62 | { | |
6756ae4b | 63 | mutex_lock(&rtnl_mutex); |
1da177e4 LT |
64 | } |
65 | ||
6756ae4b | 66 | void __rtnl_unlock(void) |
1da177e4 | 67 | { |
6756ae4b | 68 | mutex_unlock(&rtnl_mutex); |
1da177e4 | 69 | } |
6756ae4b | 70 | |
1da177e4 LT |
71 | void rtnl_unlock(void) |
72 | { | |
6756ae4b SH |
73 | mutex_unlock(&rtnl_mutex); |
74 | if (rtnl && rtnl->sk_receive_queue.qlen) | |
75 | rtnl->sk_data_ready(rtnl, 0); | |
1da177e4 LT |
76 | netdev_run_todo(); |
77 | } | |
78 | ||
6756ae4b SH |
79 | int rtnl_trylock(void) |
80 | { | |
81 | return mutex_trylock(&rtnl_mutex); | |
82 | } | |
83 | ||
1da177e4 LT |
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 | ||
db46edc6 | 101 | static const int rtm_min[RTM_NR_FAMILIES] = |
1da177e4 | 102 | { |
f90a0a74 TG |
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)), | |
14c0b97d | 107 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
f90a0a74 TG |
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)), | |
c7fb64db | 115 | [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)), |
1da177e4 LT |
116 | }; |
117 | ||
db46edc6 | 118 | static const int rta_max[RTM_NR_FAMILIES] = |
1da177e4 | 119 | { |
f90a0a74 TG |
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, | |
14c0b97d | 124 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
f90a0a74 TG |
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, | |
c7fb64db | 129 | [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX, |
1da177e4 LT |
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); | |
b3563c4f | 141 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
1da177e4 LT |
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 | ||
ac6d439d | 163 | NETLINK_CB(skb).dst_group = group; |
1da177e4 LT |
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 | ||
b00055aa SR |
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 | ||
1da177e4 | 220 | static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
b6544c0b JHS |
221 | int type, u32 pid, u32 seq, u32 change, |
222 | unsigned int flags) | |
1da177e4 LT |
223 | { |
224 | struct ifinfomsg *r; | |
225 | struct nlmsghdr *nlh; | |
226 | unsigned char *b = skb->tail; | |
227 | ||
b6544c0b | 228 | nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags); |
1da177e4 LT |
229 | r = NLMSG_DATA(nlh); |
230 | r->ifi_family = AF_UNSPEC; | |
9ef1d4c7 | 231 | r->__ifi_pad = 0; |
1da177e4 LT |
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 | ||
b00055aa SR |
249 | if (1) { |
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 | ||
1da177e4 LT |
256 | if (1) { |
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; | |
b6544c0b JHS |
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) | |
1da177e4 LT |
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]) { | |
70f8e78e DM |
398 | struct sockaddr *sa; |
399 | int len; | |
400 | ||
1da177e4 LT |
401 | if (!dev->set_mac_address) { |
402 | err = -EOPNOTSUPP; | |
403 | goto out; | |
404 | } | |
405 | if (!netif_device_present(dev)) { | |
406 | err = -ENODEV; | |
407 | goto out; | |
408 | } | |
409 | if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len)) | |
410 | goto out; | |
411 | ||
70f8e78e DM |
412 | len = sizeof(sa_family_t) + dev->addr_len; |
413 | sa = kmalloc(len, GFP_KERNEL); | |
414 | if (!sa) { | |
415 | err = -ENOMEM; | |
416 | goto out; | |
417 | } | |
418 | sa->sa_family = dev->type; | |
419 | memcpy(sa->sa_data, RTA_DATA(ida[IFLA_ADDRESS - 1]), | |
420 | dev->addr_len); | |
421 | err = dev->set_mac_address(dev, sa); | |
422 | kfree(sa); | |
1da177e4 LT |
423 | if (err) |
424 | goto out; | |
425 | send_addr_notify = 1; | |
426 | } | |
427 | ||
428 | if (ida[IFLA_BROADCAST - 1]) { | |
429 | if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len)) | |
430 | goto out; | |
431 | memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]), | |
432 | dev->addr_len); | |
433 | send_addr_notify = 1; | |
434 | } | |
435 | ||
436 | if (ida[IFLA_MTU - 1]) { | |
437 | if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
438 | goto out; | |
439 | err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1]))); | |
440 | ||
441 | if (err) | |
442 | goto out; | |
443 | ||
444 | } | |
445 | ||
446 | if (ida[IFLA_TXQLEN - 1]) { | |
447 | if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
448 | goto out; | |
449 | ||
450 | dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1])); | |
451 | } | |
452 | ||
453 | if (ida[IFLA_WEIGHT - 1]) { | |
454 | if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32))) | |
455 | goto out; | |
456 | ||
457 | dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1])); | |
458 | } | |
459 | ||
b00055aa SR |
460 | if (ida[IFLA_OPERSTATE - 1]) { |
461 | if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8))) | |
462 | goto out; | |
463 | ||
464 | set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1]))); | |
465 | } | |
466 | ||
467 | if (ida[IFLA_LINKMODE - 1]) { | |
468 | if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8))) | |
469 | goto out; | |
470 | ||
471 | write_lock_bh(&dev_base_lock); | |
472 | dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1])); | |
473 | write_unlock_bh(&dev_base_lock); | |
474 | } | |
475 | ||
1da177e4 LT |
476 | if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) { |
477 | char ifname[IFNAMSIZ]; | |
478 | ||
479 | if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1], | |
480 | IFNAMSIZ) >= IFNAMSIZ) | |
481 | goto out; | |
482 | err = dev_change_name(dev, ifname); | |
483 | if (err) | |
484 | goto out; | |
485 | } | |
486 | ||
711e2c33 JT |
487 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
488 | if (ida[IFLA_WIRELESS - 1]) { | |
489 | ||
490 | /* Call Wireless Extensions. | |
491 | * Various stuff checked in there... */ | |
492 | err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len); | |
493 | if (err) | |
494 | goto out; | |
495 | } | |
496 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
497 | ||
1da177e4 LT |
498 | err = 0; |
499 | ||
500 | out: | |
501 | if (send_addr_notify) | |
502 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
503 | ||
504 | dev_put(dev); | |
505 | return err; | |
506 | } | |
507 | ||
711e2c33 JT |
508 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK |
509 | static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg) | |
510 | { | |
511 | struct ifinfomsg *ifm = NLMSG_DATA(in_nlh); | |
512 | struct rtattr **ida = arg; | |
513 | struct net_device *dev; | |
514 | struct ifinfomsg *r; | |
515 | struct nlmsghdr *nlh; | |
516 | int err = -ENOBUFS; | |
517 | struct sk_buff *skb; | |
518 | unsigned char *b; | |
519 | char *iw_buf = NULL; | |
520 | int iw_buf_len = 0; | |
521 | ||
522 | if (ifm->ifi_index >= 0) | |
523 | dev = dev_get_by_index(ifm->ifi_index); | |
524 | else | |
525 | return -EINVAL; | |
526 | if (!dev) | |
527 | return -ENODEV; | |
528 | ||
529 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK | |
530 | if (ida[IFLA_WIRELESS - 1]) { | |
531 | ||
532 | /* Call Wireless Extensions. We need to know the size before | |
533 | * we can alloc. Various stuff checked in there... */ | |
534 | err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len); | |
535 | if (err) | |
536 | goto out; | |
537 | } | |
538 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
539 | ||
540 | /* Create a skb big enough to include all the data. | |
541 | * Some requests are way bigger than 4k... Jean II */ | |
542 | skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)), | |
543 | GFP_KERNEL); | |
544 | if (!skb) | |
545 | goto out; | |
546 | b = skb->tail; | |
547 | ||
548 | /* Put in the message the usual good stuff */ | |
549 | nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq, | |
550 | RTM_NEWLINK, sizeof(*r)); | |
551 | r = NLMSG_DATA(nlh); | |
552 | r->ifi_family = AF_UNSPEC; | |
553 | r->__ifi_pad = 0; | |
554 | r->ifi_type = dev->type; | |
555 | r->ifi_index = dev->ifindex; | |
556 | r->ifi_flags = dev->flags; | |
557 | r->ifi_change = 0; | |
558 | ||
559 | /* Put the wireless payload if it exist */ | |
560 | if(iw_buf != NULL) | |
561 | RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len, | |
562 | iw_buf + IW_EV_POINT_OFF); | |
563 | ||
564 | nlh->nlmsg_len = skb->tail - b; | |
565 | ||
566 | /* Needed ? */ | |
567 | NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; | |
568 | ||
569 | err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); | |
570 | if (err > 0) | |
571 | err = 0; | |
572 | out: | |
573 | if(iw_buf != NULL) | |
574 | kfree(iw_buf); | |
575 | dev_put(dev); | |
576 | return err; | |
577 | ||
578 | rtattr_failure: | |
579 | nlmsg_failure: | |
580 | kfree_skb(skb); | |
581 | goto out; | |
582 | } | |
583 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
584 | ||
1da177e4 LT |
585 | static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
586 | { | |
587 | int idx; | |
588 | int s_idx = cb->family; | |
589 | ||
590 | if (s_idx == 0) | |
591 | s_idx = 1; | |
592 | for (idx=1; idx<NPROTO; idx++) { | |
593 | int type = cb->nlh->nlmsg_type-RTM_BASE; | |
594 | if (idx < s_idx || idx == PF_PACKET) | |
595 | continue; | |
596 | if (rtnetlink_links[idx] == NULL || | |
597 | rtnetlink_links[idx][type].dumpit == NULL) | |
598 | continue; | |
599 | if (idx > s_idx) | |
600 | memset(&cb->args[0], 0, sizeof(cb->args)); | |
601 | if (rtnetlink_links[idx][type].dumpit(skb, cb)) | |
602 | break; | |
603 | } | |
604 | cb->family = idx; | |
605 | ||
606 | return skb->len; | |
607 | } | |
608 | ||
609 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) | |
610 | { | |
611 | struct sk_buff *skb; | |
612 | int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + | |
613 | sizeof(struct rtnl_link_ifmap) + | |
614 | sizeof(struct rtnl_link_stats) + 128); | |
615 | ||
616 | skb = alloc_skb(size, GFP_KERNEL); | |
617 | if (!skb) | |
618 | return; | |
619 | ||
28633514 | 620 | if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) { |
1da177e4 LT |
621 | kfree_skb(skb); |
622 | return; | |
623 | } | |
ac6d439d PM |
624 | NETLINK_CB(skb).dst_group = RTNLGRP_LINK; |
625 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL); | |
1da177e4 LT |
626 | } |
627 | ||
1da177e4 LT |
628 | /* Protected by RTNL sempahore. */ |
629 | static struct rtattr **rta_buf; | |
630 | static int rtattr_max; | |
631 | ||
632 | /* Process one rtnetlink message. */ | |
633 | ||
634 | static __inline__ int | |
635 | rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp) | |
636 | { | |
637 | struct rtnetlink_link *link; | |
638 | struct rtnetlink_link *link_tab; | |
639 | int sz_idx, kind; | |
640 | int min_len; | |
641 | int family; | |
642 | int type; | |
643 | int err; | |
644 | ||
645 | /* Only requests are handled by kernel now */ | |
646 | if (!(nlh->nlmsg_flags&NLM_F_REQUEST)) | |
647 | return 0; | |
648 | ||
649 | type = nlh->nlmsg_type; | |
650 | ||
651 | /* A control message: ignore them */ | |
652 | if (type < RTM_BASE) | |
653 | return 0; | |
654 | ||
655 | /* Unknown message: reply with EINVAL */ | |
656 | if (type > RTM_MAX) | |
657 | goto err_inval; | |
658 | ||
659 | type -= RTM_BASE; | |
660 | ||
661 | /* All the messages must have at least 1 byte length */ | |
662 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) | |
663 | return 0; | |
664 | ||
665 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; | |
666 | if (family >= NPROTO) { | |
667 | *errp = -EAFNOSUPPORT; | |
668 | return -1; | |
669 | } | |
670 | ||
671 | link_tab = rtnetlink_links[family]; | |
672 | if (link_tab == NULL) | |
673 | link_tab = rtnetlink_links[PF_UNSPEC]; | |
674 | link = &link_tab[type]; | |
675 | ||
676 | sz_idx = type>>2; | |
677 | kind = type&3; | |
678 | ||
c7bdb545 | 679 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) { |
1da177e4 LT |
680 | *errp = -EPERM; |
681 | return -1; | |
682 | } | |
683 | ||
684 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { | |
1da177e4 LT |
685 | if (link->dumpit == NULL) |
686 | link = &(rtnetlink_links[PF_UNSPEC][type]); | |
687 | ||
688 | if (link->dumpit == NULL) | |
689 | goto err_inval; | |
690 | ||
691 | if ((*errp = netlink_dump_start(rtnl, skb, nlh, | |
a8f74b22 | 692 | link->dumpit, NULL)) != 0) { |
1da177e4 LT |
693 | return -1; |
694 | } | |
9ac4a169 TG |
695 | |
696 | netlink_queue_skip(nlh, skb); | |
1da177e4 LT |
697 | return -1; |
698 | } | |
699 | ||
700 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); | |
701 | ||
702 | min_len = rtm_min[sz_idx]; | |
703 | if (nlh->nlmsg_len < min_len) | |
704 | goto err_inval; | |
705 | ||
706 | if (nlh->nlmsg_len > min_len) { | |
707 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
708 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); | |
709 | ||
710 | while (RTA_OK(attr, attrlen)) { | |
711 | unsigned flavor = attr->rta_type; | |
712 | if (flavor) { | |
713 | if (flavor > rta_max[sz_idx]) | |
714 | goto err_inval; | |
715 | rta_buf[flavor-1] = attr; | |
716 | } | |
717 | attr = RTA_NEXT(attr, attrlen); | |
718 | } | |
719 | } | |
720 | ||
721 | if (link->doit == NULL) | |
722 | link = &(rtnetlink_links[PF_UNSPEC][type]); | |
723 | if (link->doit == NULL) | |
724 | goto err_inval; | |
725 | err = link->doit(skb, nlh, (void *)&rta_buf[0]); | |
726 | ||
727 | *errp = err; | |
728 | return err; | |
729 | ||
730 | err_inval: | |
731 | *errp = -EINVAL; | |
732 | return -1; | |
733 | } | |
734 | ||
1da177e4 LT |
735 | static void rtnetlink_rcv(struct sock *sk, int len) |
736 | { | |
9ac4a169 | 737 | unsigned int qlen = 0; |
2a0a6ebe | 738 | |
1da177e4 | 739 | do { |
6756ae4b | 740 | mutex_lock(&rtnl_mutex); |
9ac4a169 | 741 | netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg); |
6756ae4b | 742 | mutex_unlock(&rtnl_mutex); |
1da177e4 LT |
743 | |
744 | netdev_run_todo(); | |
2a0a6ebe | 745 | } while (qlen); |
1da177e4 LT |
746 | } |
747 | ||
db46edc6 | 748 | static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] = |
1da177e4 | 749 | { |
711e2c33 JT |
750 | [RTM_GETLINK - RTM_BASE] = { |
751 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK | |
752 | .doit = do_getlink, | |
753 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
754 | .dumpit = rtnetlink_dump_ifinfo }, | |
c7fb64db TG |
755 | [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink }, |
756 | [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, | |
757 | [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, | |
758 | [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add }, | |
759 | [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete }, | |
760 | [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info }, | |
14c0b97d TG |
761 | #ifdef CONFIG_FIB_RULES |
762 | [RTM_NEWRULE - RTM_BASE] = { .doit = fib_nl_newrule }, | |
763 | [RTM_DELRULE - RTM_BASE] = { .doit = fib_nl_delrule }, | |
764 | #endif | |
c7fb64db TG |
765 | [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all }, |
766 | [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info }, | |
767 | [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set }, | |
1da177e4 LT |
768 | }; |
769 | ||
770 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) | |
771 | { | |
772 | struct net_device *dev = ptr; | |
773 | switch (event) { | |
774 | case NETDEV_UNREGISTER: | |
775 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); | |
776 | break; | |
777 | case NETDEV_REGISTER: | |
778 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
779 | break; | |
780 | case NETDEV_UP: | |
781 | case NETDEV_DOWN: | |
782 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); | |
783 | break; | |
784 | case NETDEV_CHANGE: | |
785 | case NETDEV_GOING_DOWN: | |
786 | break; | |
787 | default: | |
788 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
789 | break; | |
790 | } | |
791 | return NOTIFY_DONE; | |
792 | } | |
793 | ||
794 | static struct notifier_block rtnetlink_dev_notifier = { | |
795 | .notifier_call = rtnetlink_event, | |
796 | }; | |
797 | ||
798 | void __init rtnetlink_init(void) | |
799 | { | |
800 | int i; | |
801 | ||
802 | rtattr_max = 0; | |
803 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) | |
804 | if (rta_max[i] > rtattr_max) | |
805 | rtattr_max = rta_max[i]; | |
806 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); | |
807 | if (!rta_buf) | |
808 | panic("rtnetlink_init: cannot allocate rta_buf\n"); | |
809 | ||
06628607 PM |
810 | rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv, |
811 | THIS_MODULE); | |
1da177e4 LT |
812 | if (rtnl == NULL) |
813 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); | |
814 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); | |
815 | register_netdevice_notifier(&rtnetlink_dev_notifier); | |
816 | rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table; | |
817 | rtnetlink_links[PF_PACKET] = link_rtnetlink_table; | |
818 | } | |
819 | ||
820 | EXPORT_SYMBOL(__rta_fill); | |
821 | EXPORT_SYMBOL(rtattr_strlcpy); | |
822 | EXPORT_SYMBOL(rtattr_parse); | |
823 | EXPORT_SYMBOL(rtnetlink_links); | |
824 | EXPORT_SYMBOL(rtnetlink_put_metrics); | |
825 | EXPORT_SYMBOL(rtnl); | |
826 | EXPORT_SYMBOL(rtnl_lock); | |
6756ae4b | 827 | EXPORT_SYMBOL(rtnl_trylock); |
1da177e4 | 828 | EXPORT_SYMBOL(rtnl_unlock); |