]>
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/timer.h> |
25 | #include <linux/string.h> | |
26 | #include <linux/sockios.h> | |
27 | #include <linux/net.h> | |
28 | #include <linux/fcntl.h> | |
29 | #include <linux/mm.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/capability.h> | |
33 | #include <linux/skbuff.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/security.h> | |
6756ae4b | 36 | #include <linux/mutex.h> |
1823730f | 37 | #include <linux/if_addr.h> |
d8a5ec67 | 38 | #include <linux/nsproxy.h> |
1da177e4 LT |
39 | |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/system.h> | |
42 | #include <asm/string.h> | |
43 | ||
44 | #include <linux/inet.h> | |
45 | #include <linux/netdevice.h> | |
46 | #include <net/ip.h> | |
47 | #include <net/protocol.h> | |
48 | #include <net/arp.h> | |
49 | #include <net/route.h> | |
50 | #include <net/udp.h> | |
51 | #include <net/sock.h> | |
52 | #include <net/pkt_sched.h> | |
14c0b97d | 53 | #include <net/fib_rules.h> |
e2849863 | 54 | #include <net/rtnetlink.h> |
1da177e4 | 55 | |
e2849863 TG |
56 | struct rtnl_link |
57 | { | |
58 | rtnl_doit_func doit; | |
59 | rtnl_dumpit_func dumpit; | |
60 | }; | |
61 | ||
6756ae4b | 62 | static DEFINE_MUTEX(rtnl_mutex); |
56fc85ac | 63 | static struct sock *rtnl; |
1da177e4 LT |
64 | |
65 | void rtnl_lock(void) | |
66 | { | |
6756ae4b | 67 | mutex_lock(&rtnl_mutex); |
1da177e4 LT |
68 | } |
69 | ||
6756ae4b | 70 | void __rtnl_unlock(void) |
1da177e4 | 71 | { |
6756ae4b | 72 | mutex_unlock(&rtnl_mutex); |
1da177e4 | 73 | } |
6756ae4b | 74 | |
1da177e4 LT |
75 | void rtnl_unlock(void) |
76 | { | |
6756ae4b | 77 | mutex_unlock(&rtnl_mutex); |
1da177e4 LT |
78 | netdev_run_todo(); |
79 | } | |
80 | ||
6756ae4b SH |
81 | int rtnl_trylock(void) |
82 | { | |
83 | return mutex_trylock(&rtnl_mutex); | |
84 | } | |
85 | ||
1da177e4 LT |
86 | int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len) |
87 | { | |
88 | memset(tb, 0, sizeof(struct rtattr*)*maxattr); | |
89 | ||
90 | while (RTA_OK(rta, len)) { | |
91 | unsigned flavor = rta->rta_type; | |
92 | if (flavor && flavor <= maxattr) | |
93 | tb[flavor-1] = rta; | |
94 | rta = RTA_NEXT(rta, len); | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
2371baa4 | 99 | int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr, |
40b77c94 | 100 | struct rtattr *rta, int len) |
afdc3238 PM |
101 | { |
102 | if (RTA_PAYLOAD(rta) < len) | |
103 | return -1; | |
afdc3238 PM |
104 | if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) { |
105 | rta = RTA_DATA(rta) + RTA_ALIGN(len); | |
106 | return rtattr_parse_nested(tb, maxattr, rta); | |
107 | } | |
108 | memset(tb, 0, sizeof(struct rtattr *) * maxattr); | |
109 | return 0; | |
110 | } | |
111 | ||
42bad1da | 112 | static struct rtnl_link *rtnl_msg_handlers[NPROTO]; |
e2849863 TG |
113 | |
114 | static inline int rtm_msgindex(int msgtype) | |
115 | { | |
116 | int msgindex = msgtype - RTM_BASE; | |
117 | ||
118 | /* | |
119 | * msgindex < 0 implies someone tried to register a netlink | |
120 | * control code. msgindex >= RTM_NR_MSGTYPES may indicate that | |
121 | * the message type has not been added to linux/rtnetlink.h | |
122 | */ | |
123 | BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); | |
124 | ||
125 | return msgindex; | |
126 | } | |
127 | ||
128 | static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) | |
129 | { | |
130 | struct rtnl_link *tab; | |
131 | ||
132 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 133 | if (tab == NULL || tab[msgindex].doit == NULL) |
e2849863 TG |
134 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
135 | ||
51057f2f | 136 | return tab ? tab[msgindex].doit : NULL; |
e2849863 TG |
137 | } |
138 | ||
139 | static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) | |
140 | { | |
141 | struct rtnl_link *tab; | |
142 | ||
143 | tab = rtnl_msg_handlers[protocol]; | |
51057f2f | 144 | if (tab == NULL || tab[msgindex].dumpit == NULL) |
e2849863 TG |
145 | tab = rtnl_msg_handlers[PF_UNSPEC]; |
146 | ||
51057f2f | 147 | return tab ? tab[msgindex].dumpit : NULL; |
e2849863 TG |
148 | } |
149 | ||
150 | /** | |
151 | * __rtnl_register - Register a rtnetlink message type | |
152 | * @protocol: Protocol family or PF_UNSPEC | |
153 | * @msgtype: rtnetlink message type | |
154 | * @doit: Function pointer called for each request message | |
155 | * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message | |
156 | * | |
157 | * Registers the specified function pointers (at least one of them has | |
158 | * to be non-NULL) to be called whenever a request message for the | |
159 | * specified protocol family and message type is received. | |
160 | * | |
161 | * The special protocol family PF_UNSPEC may be used to define fallback | |
162 | * function pointers for the case when no entry for the specific protocol | |
163 | * family exists. | |
164 | * | |
165 | * Returns 0 on success or a negative error code. | |
166 | */ | |
167 | int __rtnl_register(int protocol, int msgtype, | |
168 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) | |
169 | { | |
170 | struct rtnl_link *tab; | |
171 | int msgindex; | |
172 | ||
173 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
174 | msgindex = rtm_msgindex(msgtype); | |
175 | ||
176 | tab = rtnl_msg_handlers[protocol]; | |
177 | if (tab == NULL) { | |
178 | tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); | |
179 | if (tab == NULL) | |
180 | return -ENOBUFS; | |
181 | ||
182 | rtnl_msg_handlers[protocol] = tab; | |
183 | } | |
184 | ||
185 | if (doit) | |
186 | tab[msgindex].doit = doit; | |
187 | ||
188 | if (dumpit) | |
189 | tab[msgindex].dumpit = dumpit; | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | EXPORT_SYMBOL_GPL(__rtnl_register); | |
195 | ||
196 | /** | |
197 | * rtnl_register - Register a rtnetlink message type | |
198 | * | |
199 | * Identical to __rtnl_register() but panics on failure. This is useful | |
200 | * as failure of this function is very unlikely, it can only happen due | |
201 | * to lack of memory when allocating the chain to store all message | |
202 | * handlers for a protocol. Meant for use in init functions where lack | |
203 | * of memory implies no sense in continueing. | |
204 | */ | |
205 | void rtnl_register(int protocol, int msgtype, | |
206 | rtnl_doit_func doit, rtnl_dumpit_func dumpit) | |
207 | { | |
208 | if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0) | |
209 | panic("Unable to register rtnetlink message handler, " | |
210 | "protocol = %d, message type = %d\n", | |
211 | protocol, msgtype); | |
212 | } | |
213 | ||
214 | EXPORT_SYMBOL_GPL(rtnl_register); | |
215 | ||
216 | /** | |
217 | * rtnl_unregister - Unregister a rtnetlink message type | |
218 | * @protocol: Protocol family or PF_UNSPEC | |
219 | * @msgtype: rtnetlink message type | |
220 | * | |
221 | * Returns 0 on success or a negative error code. | |
222 | */ | |
223 | int rtnl_unregister(int protocol, int msgtype) | |
224 | { | |
225 | int msgindex; | |
226 | ||
227 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
228 | msgindex = rtm_msgindex(msgtype); | |
229 | ||
230 | if (rtnl_msg_handlers[protocol] == NULL) | |
231 | return -ENOENT; | |
232 | ||
233 | rtnl_msg_handlers[protocol][msgindex].doit = NULL; | |
234 | rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | EXPORT_SYMBOL_GPL(rtnl_unregister); | |
240 | ||
241 | /** | |
242 | * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol | |
243 | * @protocol : Protocol family or PF_UNSPEC | |
244 | * | |
245 | * Identical to calling rtnl_unregster() for all registered message types | |
246 | * of a certain protocol family. | |
247 | */ | |
248 | void rtnl_unregister_all(int protocol) | |
249 | { | |
250 | BUG_ON(protocol < 0 || protocol >= NPROTO); | |
251 | ||
252 | kfree(rtnl_msg_handlers[protocol]); | |
253 | rtnl_msg_handlers[protocol] = NULL; | |
254 | } | |
255 | ||
256 | EXPORT_SYMBOL_GPL(rtnl_unregister_all); | |
1da177e4 | 257 | |
38f7b870 PM |
258 | static LIST_HEAD(link_ops); |
259 | ||
260 | /** | |
261 | * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
262 | * @ops: struct rtnl_link_ops * to register | |
263 | * | |
264 | * The caller must hold the rtnl_mutex. This function should be used | |
265 | * by drivers that create devices during module initialization. It | |
266 | * must be called before registering the devices. | |
267 | * | |
268 | * Returns 0 on success or a negative error code. | |
269 | */ | |
270 | int __rtnl_link_register(struct rtnl_link_ops *ops) | |
271 | { | |
2d85cba2 PM |
272 | if (!ops->dellink) |
273 | ops->dellink = unregister_netdevice; | |
274 | ||
38f7b870 PM |
275 | list_add_tail(&ops->list, &link_ops); |
276 | return 0; | |
277 | } | |
278 | ||
279 | EXPORT_SYMBOL_GPL(__rtnl_link_register); | |
280 | ||
281 | /** | |
282 | * rtnl_link_register - Register rtnl_link_ops with rtnetlink. | |
283 | * @ops: struct rtnl_link_ops * to register | |
284 | * | |
285 | * Returns 0 on success or a negative error code. | |
286 | */ | |
287 | int rtnl_link_register(struct rtnl_link_ops *ops) | |
288 | { | |
289 | int err; | |
290 | ||
291 | rtnl_lock(); | |
292 | err = __rtnl_link_register(ops); | |
293 | rtnl_unlock(); | |
294 | return err; | |
295 | } | |
296 | ||
297 | EXPORT_SYMBOL_GPL(rtnl_link_register); | |
298 | ||
299 | /** | |
300 | * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
301 | * @ops: struct rtnl_link_ops * to unregister | |
302 | * | |
2d85cba2 | 303 | * The caller must hold the rtnl_mutex. |
38f7b870 PM |
304 | */ |
305 | void __rtnl_link_unregister(struct rtnl_link_ops *ops) | |
306 | { | |
2d85cba2 | 307 | struct net_device *dev, *n; |
881d966b | 308 | struct net *net; |
2d85cba2 | 309 | |
881d966b EB |
310 | for_each_net(net) { |
311 | for_each_netdev_safe(net, dev, n) { | |
312 | if (dev->rtnl_link_ops == ops) | |
313 | ops->dellink(dev); | |
314 | } | |
2d85cba2 | 315 | } |
38f7b870 PM |
316 | list_del(&ops->list); |
317 | } | |
318 | ||
319 | EXPORT_SYMBOL_GPL(__rtnl_link_unregister); | |
320 | ||
321 | /** | |
322 | * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. | |
323 | * @ops: struct rtnl_link_ops * to unregister | |
324 | */ | |
325 | void rtnl_link_unregister(struct rtnl_link_ops *ops) | |
326 | { | |
327 | rtnl_lock(); | |
328 | __rtnl_link_unregister(ops); | |
329 | rtnl_unlock(); | |
330 | } | |
331 | ||
332 | EXPORT_SYMBOL_GPL(rtnl_link_unregister); | |
333 | ||
334 | static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) | |
335 | { | |
336 | const struct rtnl_link_ops *ops; | |
337 | ||
338 | list_for_each_entry(ops, &link_ops, list) { | |
339 | if (!strcmp(ops->kind, kind)) | |
340 | return ops; | |
341 | } | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | static size_t rtnl_link_get_size(const struct net_device *dev) | |
346 | { | |
347 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
348 | size_t size; | |
349 | ||
350 | if (!ops) | |
351 | return 0; | |
352 | ||
353 | size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ | |
354 | nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ | |
355 | ||
356 | if (ops->get_size) | |
357 | /* IFLA_INFO_DATA + nested data */ | |
358 | size += nlmsg_total_size(sizeof(struct nlattr)) + | |
359 | ops->get_size(dev); | |
360 | ||
361 | if (ops->get_xstats_size) | |
362 | size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */ | |
363 | ||
364 | return size; | |
365 | } | |
366 | ||
367 | static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) | |
368 | { | |
369 | const struct rtnl_link_ops *ops = dev->rtnl_link_ops; | |
370 | struct nlattr *linkinfo, *data; | |
371 | int err = -EMSGSIZE; | |
372 | ||
373 | linkinfo = nla_nest_start(skb, IFLA_LINKINFO); | |
374 | if (linkinfo == NULL) | |
375 | goto out; | |
376 | ||
377 | if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) | |
378 | goto err_cancel_link; | |
379 | if (ops->fill_xstats) { | |
380 | err = ops->fill_xstats(skb, dev); | |
381 | if (err < 0) | |
382 | goto err_cancel_link; | |
383 | } | |
384 | if (ops->fill_info) { | |
385 | data = nla_nest_start(skb, IFLA_INFO_DATA); | |
386 | if (data == NULL) | |
387 | goto err_cancel_link; | |
388 | err = ops->fill_info(skb, dev); | |
389 | if (err < 0) | |
390 | goto err_cancel_data; | |
391 | nla_nest_end(skb, data); | |
392 | } | |
393 | ||
394 | nla_nest_end(skb, linkinfo); | |
395 | return 0; | |
396 | ||
397 | err_cancel_data: | |
398 | nla_nest_cancel(skb, data); | |
399 | err_cancel_link: | |
400 | nla_nest_cancel(skb, linkinfo); | |
401 | out: | |
402 | return err; | |
403 | } | |
404 | ||
db46edc6 | 405 | static const int rtm_min[RTM_NR_FAMILIES] = |
1da177e4 | 406 | { |
f90a0a74 TG |
407 | [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), |
408 | [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), | |
409 | [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), | |
14c0b97d | 410 | [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), |
f90a0a74 TG |
411 | [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), |
412 | [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
413 | [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
414 | [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), | |
f90a0a74 TG |
415 | [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), |
416 | [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), | |
1da177e4 LT |
417 | }; |
418 | ||
db46edc6 | 419 | static const int rta_max[RTM_NR_FAMILIES] = |
1da177e4 | 420 | { |
f90a0a74 TG |
421 | [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, |
422 | [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, | |
423 | [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, | |
14c0b97d | 424 | [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, |
f90a0a74 TG |
425 | [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, |
426 | [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, | |
427 | [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, | |
428 | [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, | |
1da177e4 LT |
429 | }; |
430 | ||
431 | void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) | |
432 | { | |
433 | struct rtattr *rta; | |
434 | int size = RTA_LENGTH(attrlen); | |
435 | ||
436 | rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size)); | |
437 | rta->rta_type = attrtype; | |
438 | rta->rta_len = size; | |
439 | memcpy(RTA_DATA(rta), data, attrlen); | |
b3563c4f | 440 | memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); |
1da177e4 LT |
441 | } |
442 | ||
443 | size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size) | |
444 | { | |
445 | size_t ret = RTA_PAYLOAD(rta); | |
446 | char *src = RTA_DATA(rta); | |
447 | ||
448 | if (ret > 0 && src[ret - 1] == '\0') | |
449 | ret--; | |
450 | if (size > 0) { | |
451 | size_t len = (ret >= size) ? size - 1 : ret; | |
452 | memset(dest, 0, size); | |
453 | memcpy(dest, src, len); | |
454 | } | |
455 | return ret; | |
456 | } | |
457 | ||
458 | int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo) | |
459 | { | |
460 | int err = 0; | |
461 | ||
ac6d439d | 462 | NETLINK_CB(skb).dst_group = group; |
1da177e4 LT |
463 | if (echo) |
464 | atomic_inc(&skb->users); | |
465 | netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); | |
466 | if (echo) | |
467 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
468 | return err; | |
469 | } | |
470 | ||
2942e900 TG |
471 | int rtnl_unicast(struct sk_buff *skb, u32 pid) |
472 | { | |
473 | return nlmsg_unicast(rtnl, skb, pid); | |
474 | } | |
475 | ||
97676b6b TG |
476 | int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group, |
477 | struct nlmsghdr *nlh, gfp_t flags) | |
478 | { | |
479 | int report = 0; | |
480 | ||
481 | if (nlh) | |
482 | report = nlmsg_report(nlh); | |
483 | ||
484 | return nlmsg_notify(rtnl, skb, pid, group, report, flags); | |
485 | } | |
486 | ||
487 | void rtnl_set_sk_err(u32 group, int error) | |
488 | { | |
489 | netlink_set_err(rtnl, 0, group, error); | |
490 | } | |
491 | ||
1da177e4 LT |
492 | int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) |
493 | { | |
2d7202bf TG |
494 | struct nlattr *mx; |
495 | int i, valid = 0; | |
496 | ||
497 | mx = nla_nest_start(skb, RTA_METRICS); | |
498 | if (mx == NULL) | |
499 | return -ENOBUFS; | |
500 | ||
501 | for (i = 0; i < RTAX_MAX; i++) { | |
502 | if (metrics[i]) { | |
503 | valid++; | |
504 | NLA_PUT_U32(skb, i+1, metrics[i]); | |
505 | } | |
1da177e4 | 506 | } |
1da177e4 | 507 | |
a57d27fc DM |
508 | if (!valid) { |
509 | nla_nest_cancel(skb, mx); | |
510 | return 0; | |
511 | } | |
2d7202bf TG |
512 | |
513 | return nla_nest_end(skb, mx); | |
514 | ||
515 | nla_put_failure: | |
516 | return nla_nest_cancel(skb, mx); | |
1da177e4 LT |
517 | } |
518 | ||
e3703b3d TG |
519 | int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, |
520 | u32 ts, u32 tsage, long expires, u32 error) | |
521 | { | |
522 | struct rta_cacheinfo ci = { | |
523 | .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), | |
524 | .rta_used = dst->__use, | |
525 | .rta_clntref = atomic_read(&(dst->__refcnt)), | |
526 | .rta_error = error, | |
527 | .rta_id = id, | |
528 | .rta_ts = ts, | |
529 | .rta_tsage = tsage, | |
530 | }; | |
531 | ||
532 | if (expires) | |
533 | ci.rta_expires = jiffies_to_clock_t(expires); | |
534 | ||
535 | return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); | |
536 | } | |
537 | ||
538 | EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); | |
1da177e4 | 539 | |
b00055aa SR |
540 | static void set_operstate(struct net_device *dev, unsigned char transition) |
541 | { | |
542 | unsigned char operstate = dev->operstate; | |
543 | ||
544 | switch(transition) { | |
545 | case IF_OPER_UP: | |
546 | if ((operstate == IF_OPER_DORMANT || | |
547 | operstate == IF_OPER_UNKNOWN) && | |
548 | !netif_dormant(dev)) | |
549 | operstate = IF_OPER_UP; | |
550 | break; | |
551 | ||
552 | case IF_OPER_DORMANT: | |
553 | if (operstate == IF_OPER_UP || | |
554 | operstate == IF_OPER_UNKNOWN) | |
555 | operstate = IF_OPER_DORMANT; | |
556 | break; | |
3ff50b79 | 557 | } |
b00055aa SR |
558 | |
559 | if (dev->operstate != operstate) { | |
560 | write_lock_bh(&dev_base_lock); | |
561 | dev->operstate = operstate; | |
562 | write_unlock_bh(&dev_base_lock); | |
563 | netdev_state_change(dev); | |
564 | } | |
565 | } | |
566 | ||
b60c5115 TG |
567 | static void copy_rtnl_link_stats(struct rtnl_link_stats *a, |
568 | struct net_device_stats *b) | |
1da177e4 | 569 | { |
b60c5115 TG |
570 | a->rx_packets = b->rx_packets; |
571 | a->tx_packets = b->tx_packets; | |
572 | a->rx_bytes = b->rx_bytes; | |
573 | a->tx_bytes = b->tx_bytes; | |
574 | a->rx_errors = b->rx_errors; | |
575 | a->tx_errors = b->tx_errors; | |
576 | a->rx_dropped = b->rx_dropped; | |
577 | a->tx_dropped = b->tx_dropped; | |
578 | ||
579 | a->multicast = b->multicast; | |
580 | a->collisions = b->collisions; | |
581 | ||
582 | a->rx_length_errors = b->rx_length_errors; | |
583 | a->rx_over_errors = b->rx_over_errors; | |
584 | a->rx_crc_errors = b->rx_crc_errors; | |
585 | a->rx_frame_errors = b->rx_frame_errors; | |
586 | a->rx_fifo_errors = b->rx_fifo_errors; | |
587 | a->rx_missed_errors = b->rx_missed_errors; | |
588 | ||
589 | a->tx_aborted_errors = b->tx_aborted_errors; | |
590 | a->tx_carrier_errors = b->tx_carrier_errors; | |
591 | a->tx_fifo_errors = b->tx_fifo_errors; | |
592 | a->tx_heartbeat_errors = b->tx_heartbeat_errors; | |
593 | a->tx_window_errors = b->tx_window_errors; | |
594 | ||
595 | a->rx_compressed = b->rx_compressed; | |
596 | a->tx_compressed = b->tx_compressed; | |
597 | }; | |
1da177e4 | 598 | |
38f7b870 | 599 | static inline size_t if_nlmsg_size(const struct net_device *dev) |
339bf98f TG |
600 | { |
601 | return NLMSG_ALIGN(sizeof(struct ifinfomsg)) | |
602 | + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ | |
603 | + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ | |
604 | + nla_total_size(sizeof(struct rtnl_link_ifmap)) | |
605 | + nla_total_size(sizeof(struct rtnl_link_stats)) | |
606 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ | |
607 | + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ | |
608 | + nla_total_size(4) /* IFLA_TXQLEN */ | |
609 | + nla_total_size(4) /* IFLA_WEIGHT */ | |
610 | + nla_total_size(4) /* IFLA_MTU */ | |
611 | + nla_total_size(4) /* IFLA_LINK */ | |
612 | + nla_total_size(4) /* IFLA_MASTER */ | |
613 | + nla_total_size(1) /* IFLA_OPERSTATE */ | |
38f7b870 PM |
614 | + nla_total_size(1) /* IFLA_LINKMODE */ |
615 | + rtnl_link_get_size(dev); /* IFLA_LINKINFO */ | |
339bf98f TG |
616 | } |
617 | ||
b60c5115 | 618 | static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, |
575c3e2a PM |
619 | int type, u32 pid, u32 seq, u32 change, |
620 | unsigned int flags) | |
b60c5115 TG |
621 | { |
622 | struct ifinfomsg *ifm; | |
623 | struct nlmsghdr *nlh; | |
1da177e4 | 624 | |
b60c5115 TG |
625 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); |
626 | if (nlh == NULL) | |
26932566 | 627 | return -EMSGSIZE; |
1da177e4 | 628 | |
b60c5115 TG |
629 | ifm = nlmsg_data(nlh); |
630 | ifm->ifi_family = AF_UNSPEC; | |
631 | ifm->__ifi_pad = 0; | |
632 | ifm->ifi_type = dev->type; | |
633 | ifm->ifi_index = dev->ifindex; | |
634 | ifm->ifi_flags = dev_get_flags(dev); | |
635 | ifm->ifi_change = change; | |
636 | ||
637 | NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); | |
638 | NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); | |
b60c5115 TG |
639 | NLA_PUT_U8(skb, IFLA_OPERSTATE, |
640 | netif_running(dev) ? dev->operstate : IF_OPER_DOWN); | |
641 | NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); | |
642 | NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); | |
643 | ||
644 | if (dev->ifindex != dev->iflink) | |
645 | NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); | |
646 | ||
647 | if (dev->master) | |
648 | NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); | |
1da177e4 | 649 | |
b60c5115 TG |
650 | if (dev->qdisc_sleeping) |
651 | NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id); | |
b00055aa | 652 | |
1da177e4 LT |
653 | if (1) { |
654 | struct rtnl_link_ifmap map = { | |
655 | .mem_start = dev->mem_start, | |
656 | .mem_end = dev->mem_end, | |
657 | .base_addr = dev->base_addr, | |
658 | .irq = dev->irq, | |
659 | .dma = dev->dma, | |
660 | .port = dev->if_port, | |
661 | }; | |
b60c5115 | 662 | NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); |
1da177e4 LT |
663 | } |
664 | ||
665 | if (dev->addr_len) { | |
b60c5115 TG |
666 | NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); |
667 | NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); | |
1da177e4 LT |
668 | } |
669 | ||
670 | if (dev->get_stats) { | |
b60c5115 | 671 | struct net_device_stats *stats = dev->get_stats(dev); |
1da177e4 | 672 | if (stats) { |
b60c5115 TG |
673 | struct nlattr *attr; |
674 | ||
675 | attr = nla_reserve(skb, IFLA_STATS, | |
676 | sizeof(struct rtnl_link_stats)); | |
677 | if (attr == NULL) | |
678 | goto nla_put_failure; | |
679 | ||
680 | copy_rtnl_link_stats(nla_data(attr), stats); | |
1da177e4 LT |
681 | } |
682 | } | |
1da177e4 | 683 | |
38f7b870 PM |
684 | if (dev->rtnl_link_ops) { |
685 | if (rtnl_link_fill(skb, dev) < 0) | |
686 | goto nla_put_failure; | |
687 | } | |
688 | ||
b60c5115 TG |
689 | return nlmsg_end(skb, nlh); |
690 | ||
691 | nla_put_failure: | |
26932566 PM |
692 | nlmsg_cancel(skb, nlh); |
693 | return -EMSGSIZE; | |
1da177e4 LT |
694 | } |
695 | ||
b60c5115 | 696 | static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 | 697 | { |
881d966b | 698 | struct net *net = skb->sk->sk_net; |
1da177e4 LT |
699 | int idx; |
700 | int s_idx = cb->args[0]; | |
701 | struct net_device *dev; | |
702 | ||
7562f876 | 703 | idx = 0; |
881d966b | 704 | for_each_netdev(net, dev) { |
1da177e4 | 705 | if (idx < s_idx) |
7562f876 | 706 | goto cont; |
575c3e2a | 707 | if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, |
b60c5115 TG |
708 | NETLINK_CB(cb->skb).pid, |
709 | cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0) | |
1da177e4 | 710 | break; |
7562f876 PE |
711 | cont: |
712 | idx++; | |
1da177e4 | 713 | } |
1da177e4 LT |
714 | cb->args[0] = idx; |
715 | ||
716 | return skb->len; | |
717 | } | |
718 | ||
e7199288 | 719 | const struct nla_policy ifla_policy[IFLA_MAX+1] = { |
5176f91e | 720 | [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, |
38f7b870 PM |
721 | [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, |
722 | [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, | |
5176f91e | 723 | [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, |
da5e0494 TG |
724 | [IFLA_MTU] = { .type = NLA_U32 }, |
725 | [IFLA_TXQLEN] = { .type = NLA_U32 }, | |
726 | [IFLA_WEIGHT] = { .type = NLA_U32 }, | |
727 | [IFLA_OPERSTATE] = { .type = NLA_U8 }, | |
728 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | |
d8a5ec67 | 729 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
da5e0494 TG |
730 | }; |
731 | ||
38f7b870 PM |
732 | static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { |
733 | [IFLA_INFO_KIND] = { .type = NLA_STRING }, | |
734 | [IFLA_INFO_DATA] = { .type = NLA_NESTED }, | |
735 | }; | |
736 | ||
d8a5ec67 EB |
737 | static struct net *get_net_ns_by_pid(pid_t pid) |
738 | { | |
739 | struct task_struct *tsk; | |
740 | struct net *net; | |
741 | ||
742 | /* Lookup the network namespace */ | |
743 | net = ERR_PTR(-ESRCH); | |
744 | rcu_read_lock(); | |
745 | tsk = find_task_by_pid(pid); | |
746 | if (tsk) { | |
747 | task_lock(tsk); | |
748 | if (tsk->nsproxy) | |
749 | net = get_net(tsk->nsproxy->net_ns); | |
750 | task_unlock(tsk); | |
751 | } | |
752 | rcu_read_unlock(); | |
753 | return net; | |
754 | } | |
755 | ||
0157f60c | 756 | static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, |
38f7b870 | 757 | struct nlattr **tb, char *ifname, int modified) |
1da177e4 | 758 | { |
38f7b870 | 759 | int send_addr_notify = 0; |
0157f60c | 760 | int err; |
1da177e4 | 761 | |
d8a5ec67 EB |
762 | if (tb[IFLA_NET_NS_PID]) { |
763 | struct net *net; | |
764 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | |
765 | if (IS_ERR(net)) { | |
766 | err = PTR_ERR(net); | |
767 | goto errout; | |
768 | } | |
769 | err = dev_change_net_namespace(dev, net, ifname); | |
770 | put_net(net); | |
771 | if (err) | |
772 | goto errout; | |
773 | modified = 1; | |
774 | } | |
775 | ||
da5e0494 | 776 | if (tb[IFLA_MAP]) { |
1da177e4 LT |
777 | struct rtnl_link_ifmap *u_map; |
778 | struct ifmap k_map; | |
779 | ||
780 | if (!dev->set_config) { | |
781 | err = -EOPNOTSUPP; | |
0157f60c | 782 | goto errout; |
1da177e4 LT |
783 | } |
784 | ||
785 | if (!netif_device_present(dev)) { | |
786 | err = -ENODEV; | |
0157f60c | 787 | goto errout; |
1da177e4 | 788 | } |
1da177e4 | 789 | |
da5e0494 | 790 | u_map = nla_data(tb[IFLA_MAP]); |
1da177e4 LT |
791 | k_map.mem_start = (unsigned long) u_map->mem_start; |
792 | k_map.mem_end = (unsigned long) u_map->mem_end; | |
793 | k_map.base_addr = (unsigned short) u_map->base_addr; | |
794 | k_map.irq = (unsigned char) u_map->irq; | |
795 | k_map.dma = (unsigned char) u_map->dma; | |
796 | k_map.port = (unsigned char) u_map->port; | |
797 | ||
798 | err = dev->set_config(dev, &k_map); | |
da5e0494 | 799 | if (err < 0) |
0157f60c | 800 | goto errout; |
1da177e4 | 801 | |
da5e0494 | 802 | modified = 1; |
1da177e4 LT |
803 | } |
804 | ||
da5e0494 | 805 | if (tb[IFLA_ADDRESS]) { |
70f8e78e DM |
806 | struct sockaddr *sa; |
807 | int len; | |
808 | ||
1da177e4 LT |
809 | if (!dev->set_mac_address) { |
810 | err = -EOPNOTSUPP; | |
0157f60c | 811 | goto errout; |
1da177e4 | 812 | } |
da5e0494 | 813 | |
1da177e4 LT |
814 | if (!netif_device_present(dev)) { |
815 | err = -ENODEV; | |
0157f60c | 816 | goto errout; |
1da177e4 | 817 | } |
1da177e4 | 818 | |
70f8e78e DM |
819 | len = sizeof(sa_family_t) + dev->addr_len; |
820 | sa = kmalloc(len, GFP_KERNEL); | |
821 | if (!sa) { | |
822 | err = -ENOMEM; | |
0157f60c | 823 | goto errout; |
70f8e78e DM |
824 | } |
825 | sa->sa_family = dev->type; | |
da5e0494 | 826 | memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), |
70f8e78e DM |
827 | dev->addr_len); |
828 | err = dev->set_mac_address(dev, sa); | |
829 | kfree(sa); | |
1da177e4 | 830 | if (err) |
0157f60c | 831 | goto errout; |
1da177e4 | 832 | send_addr_notify = 1; |
da5e0494 | 833 | modified = 1; |
1da177e4 LT |
834 | } |
835 | ||
da5e0494 TG |
836 | if (tb[IFLA_MTU]) { |
837 | err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); | |
838 | if (err < 0) | |
0157f60c | 839 | goto errout; |
da5e0494 | 840 | modified = 1; |
1da177e4 LT |
841 | } |
842 | ||
da5e0494 TG |
843 | /* |
844 | * Interface selected by interface index but interface | |
845 | * name provided implies that a name change has been | |
846 | * requested. | |
847 | */ | |
51055be8 | 848 | if (ifm->ifi_index > 0 && ifname[0]) { |
da5e0494 TG |
849 | err = dev_change_name(dev, ifname); |
850 | if (err < 0) | |
0157f60c | 851 | goto errout; |
da5e0494 | 852 | modified = 1; |
1da177e4 LT |
853 | } |
854 | ||
da5e0494 TG |
855 | if (tb[IFLA_BROADCAST]) { |
856 | nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); | |
857 | send_addr_notify = 1; | |
1da177e4 LT |
858 | } |
859 | ||
83b496e9 PM |
860 | if (ifm->ifi_flags || ifm->ifi_change) { |
861 | unsigned int flags = ifm->ifi_flags; | |
862 | ||
863 | /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ | |
864 | if (ifm->ifi_change) | |
865 | flags = (flags & ifm->ifi_change) | | |
866 | (dev->flags & ~ifm->ifi_change); | |
867 | dev_change_flags(dev, flags); | |
868 | } | |
1da177e4 | 869 | |
da5e0494 TG |
870 | if (tb[IFLA_TXQLEN]) |
871 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
b00055aa | 872 | |
da5e0494 TG |
873 | if (tb[IFLA_OPERSTATE]) |
874 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); | |
b00055aa | 875 | |
da5e0494 | 876 | if (tb[IFLA_LINKMODE]) { |
b00055aa | 877 | write_lock_bh(&dev_base_lock); |
da5e0494 | 878 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); |
b00055aa SR |
879 | write_unlock_bh(&dev_base_lock); |
880 | } | |
881 | ||
1da177e4 LT |
882 | err = 0; |
883 | ||
0157f60c | 884 | errout: |
da5e0494 TG |
885 | if (err < 0 && modified && net_ratelimit()) |
886 | printk(KERN_WARNING "A link change request failed with " | |
887 | "some changes comitted already. Interface %s may " | |
888 | "have been left with an inconsistent configuration, " | |
889 | "please check.\n", dev->name); | |
890 | ||
1da177e4 LT |
891 | if (send_addr_notify) |
892 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
0157f60c PM |
893 | return err; |
894 | } | |
1da177e4 | 895 | |
0157f60c PM |
896 | static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
897 | { | |
881d966b | 898 | struct net *net = skb->sk->sk_net; |
0157f60c PM |
899 | struct ifinfomsg *ifm; |
900 | struct net_device *dev; | |
901 | int err; | |
902 | struct nlattr *tb[IFLA_MAX+1]; | |
903 | char ifname[IFNAMSIZ]; | |
904 | ||
905 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
906 | if (err < 0) | |
907 | goto errout; | |
908 | ||
909 | if (tb[IFLA_IFNAME]) | |
910 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
911 | else | |
912 | ifname[0] = '\0'; | |
913 | ||
914 | err = -EINVAL; | |
915 | ifm = nlmsg_data(nlh); | |
916 | if (ifm->ifi_index > 0) | |
881d966b | 917 | dev = dev_get_by_index(net, ifm->ifi_index); |
0157f60c | 918 | else if (tb[IFLA_IFNAME]) |
881d966b | 919 | dev = dev_get_by_name(net, ifname); |
0157f60c PM |
920 | else |
921 | goto errout; | |
922 | ||
923 | if (dev == NULL) { | |
924 | err = -ENODEV; | |
925 | goto errout; | |
926 | } | |
927 | ||
928 | if (tb[IFLA_ADDRESS] && | |
929 | nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) | |
930 | goto errout_dev; | |
931 | ||
932 | if (tb[IFLA_BROADCAST] && | |
933 | nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) | |
934 | goto errout_dev; | |
935 | ||
38f7b870 | 936 | err = do_setlink(dev, ifm, tb, ifname, 0); |
0157f60c | 937 | errout_dev: |
1da177e4 | 938 | dev_put(dev); |
da5e0494 | 939 | errout: |
1da177e4 LT |
940 | return err; |
941 | } | |
942 | ||
38f7b870 PM |
943 | static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
944 | { | |
881d966b | 945 | struct net *net = skb->sk->sk_net; |
38f7b870 PM |
946 | const struct rtnl_link_ops *ops; |
947 | struct net_device *dev; | |
948 | struct ifinfomsg *ifm; | |
949 | char ifname[IFNAMSIZ]; | |
950 | struct nlattr *tb[IFLA_MAX+1]; | |
951 | int err; | |
952 | ||
953 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); | |
954 | if (err < 0) | |
955 | return err; | |
956 | ||
957 | if (tb[IFLA_IFNAME]) | |
958 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
959 | ||
960 | ifm = nlmsg_data(nlh); | |
961 | if (ifm->ifi_index > 0) | |
881d966b | 962 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 963 | else if (tb[IFLA_IFNAME]) |
881d966b | 964 | dev = __dev_get_by_name(net, ifname); |
38f7b870 PM |
965 | else |
966 | return -EINVAL; | |
967 | ||
968 | if (!dev) | |
969 | return -ENODEV; | |
970 | ||
971 | ops = dev->rtnl_link_ops; | |
972 | if (!ops) | |
973 | return -EOPNOTSUPP; | |
974 | ||
975 | ops->dellink(dev); | |
976 | return 0; | |
977 | } | |
978 | ||
881d966b | 979 | struct net_device *rtnl_create_link(struct net *net, char *ifname, |
e7199288 PE |
980 | const struct rtnl_link_ops *ops, struct nlattr *tb[]) |
981 | { | |
982 | int err; | |
983 | struct net_device *dev; | |
984 | ||
985 | err = -ENOMEM; | |
986 | dev = alloc_netdev(ops->priv_size, ifname, ops->setup); | |
987 | if (!dev) | |
988 | goto err; | |
989 | ||
990 | if (strchr(dev->name, '%')) { | |
991 | err = dev_alloc_name(dev, dev->name); | |
992 | if (err < 0) | |
993 | goto err_free; | |
994 | } | |
995 | ||
881d966b | 996 | dev->nd_net = net; |
e7199288 PE |
997 | dev->rtnl_link_ops = ops; |
998 | ||
999 | if (tb[IFLA_MTU]) | |
1000 | dev->mtu = nla_get_u32(tb[IFLA_MTU]); | |
1001 | if (tb[IFLA_ADDRESS]) | |
1002 | memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), | |
1003 | nla_len(tb[IFLA_ADDRESS])); | |
1004 | if (tb[IFLA_BROADCAST]) | |
1005 | memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), | |
1006 | nla_len(tb[IFLA_BROADCAST])); | |
1007 | if (tb[IFLA_TXQLEN]) | |
1008 | dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); | |
1009 | if (tb[IFLA_OPERSTATE]) | |
1010 | set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); | |
1011 | if (tb[IFLA_LINKMODE]) | |
1012 | dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); | |
1013 | ||
1014 | return dev; | |
1015 | ||
1016 | err_free: | |
1017 | free_netdev(dev); | |
1018 | err: | |
1019 | return ERR_PTR(err); | |
1020 | } | |
1021 | ||
38f7b870 PM |
1022 | static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
1023 | { | |
881d966b | 1024 | struct net *net = skb->sk->sk_net; |
38f7b870 PM |
1025 | const struct rtnl_link_ops *ops; |
1026 | struct net_device *dev; | |
1027 | struct ifinfomsg *ifm; | |
1028 | char kind[MODULE_NAME_LEN]; | |
1029 | char ifname[IFNAMSIZ]; | |
1030 | struct nlattr *tb[IFLA_MAX+1]; | |
1031 | struct nlattr *linkinfo[IFLA_INFO_MAX+1]; | |
1032 | int err; | |
1033 | ||
8072f085 | 1034 | #ifdef CONFIG_KMOD |
38f7b870 | 1035 | replay: |
8072f085 | 1036 | #endif |
38f7b870 PM |
1037 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1038 | if (err < 0) | |
1039 | return err; | |
1040 | ||
1041 | if (tb[IFLA_IFNAME]) | |
1042 | nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); | |
1043 | else | |
1044 | ifname[0] = '\0'; | |
1045 | ||
1046 | ifm = nlmsg_data(nlh); | |
1047 | if (ifm->ifi_index > 0) | |
881d966b | 1048 | dev = __dev_get_by_index(net, ifm->ifi_index); |
38f7b870 | 1049 | else if (ifname[0]) |
881d966b | 1050 | dev = __dev_get_by_name(net, ifname); |
38f7b870 PM |
1051 | else |
1052 | dev = NULL; | |
1053 | ||
1054 | if (tb[IFLA_LINKINFO]) { | |
1055 | err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, | |
1056 | tb[IFLA_LINKINFO], ifla_info_policy); | |
1057 | if (err < 0) | |
1058 | return err; | |
1059 | } else | |
1060 | memset(linkinfo, 0, sizeof(linkinfo)); | |
1061 | ||
1062 | if (linkinfo[IFLA_INFO_KIND]) { | |
1063 | nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); | |
1064 | ops = rtnl_link_ops_get(kind); | |
1065 | } else { | |
1066 | kind[0] = '\0'; | |
1067 | ops = NULL; | |
1068 | } | |
1069 | ||
1070 | if (1) { | |
1071 | struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; | |
1072 | ||
1073 | if (ops) { | |
1074 | if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { | |
1075 | err = nla_parse_nested(attr, ops->maxtype, | |
1076 | linkinfo[IFLA_INFO_DATA], | |
1077 | ops->policy); | |
1078 | if (err < 0) | |
1079 | return err; | |
1080 | data = attr; | |
1081 | } | |
1082 | if (ops->validate) { | |
1083 | err = ops->validate(tb, data); | |
1084 | if (err < 0) | |
1085 | return err; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | if (dev) { | |
1090 | int modified = 0; | |
1091 | ||
1092 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
1093 | return -EEXIST; | |
1094 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
1095 | return -EOPNOTSUPP; | |
1096 | ||
1097 | if (linkinfo[IFLA_INFO_DATA]) { | |
1098 | if (!ops || ops != dev->rtnl_link_ops || | |
1099 | !ops->changelink) | |
1100 | return -EOPNOTSUPP; | |
1101 | ||
1102 | err = ops->changelink(dev, tb, data); | |
1103 | if (err < 0) | |
1104 | return err; | |
1105 | modified = 1; | |
1106 | } | |
1107 | ||
1108 | return do_setlink(dev, ifm, tb, ifname, modified); | |
1109 | } | |
1110 | ||
1111 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
1112 | return -ENODEV; | |
1113 | ||
1114 | if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change) | |
1115 | return -EOPNOTSUPP; | |
0e06877c | 1116 | if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) |
38f7b870 PM |
1117 | return -EOPNOTSUPP; |
1118 | ||
1119 | if (!ops) { | |
1120 | #ifdef CONFIG_KMOD | |
1121 | if (kind[0]) { | |
1122 | __rtnl_unlock(); | |
1123 | request_module("rtnl-link-%s", kind); | |
1124 | rtnl_lock(); | |
1125 | ops = rtnl_link_ops_get(kind); | |
1126 | if (ops) | |
1127 | goto replay; | |
1128 | } | |
1129 | #endif | |
1130 | return -EOPNOTSUPP; | |
1131 | } | |
1132 | ||
1133 | if (!ifname[0]) | |
1134 | snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); | |
e7199288 | 1135 | |
881d966b | 1136 | dev = rtnl_create_link(net, ifname, ops, tb); |
e7199288 PE |
1137 | |
1138 | if (IS_ERR(dev)) | |
1139 | err = PTR_ERR(dev); | |
1140 | else if (ops->newlink) | |
2d85cba2 PM |
1141 | err = ops->newlink(dev, tb, data); |
1142 | else | |
1143 | err = register_netdevice(dev); | |
e7199288 PE |
1144 | |
1145 | if (err < 0 && !IS_ERR(dev)) | |
38f7b870 PM |
1146 | free_netdev(dev); |
1147 | return err; | |
1148 | } | |
1149 | } | |
1150 | ||
b60c5115 | 1151 | static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) |
711e2c33 | 1152 | { |
881d966b | 1153 | struct net *net = skb->sk->sk_net; |
b60c5115 TG |
1154 | struct ifinfomsg *ifm; |
1155 | struct nlattr *tb[IFLA_MAX+1]; | |
1156 | struct net_device *dev = NULL; | |
1157 | struct sk_buff *nskb; | |
339bf98f | 1158 | int err; |
711e2c33 | 1159 | |
b60c5115 TG |
1160 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); |
1161 | if (err < 0) | |
9918f230 | 1162 | return err; |
b60c5115 TG |
1163 | |
1164 | ifm = nlmsg_data(nlh); | |
51055be8 | 1165 | if (ifm->ifi_index > 0) { |
881d966b | 1166 | dev = dev_get_by_index(net, ifm->ifi_index); |
b60c5115 TG |
1167 | if (dev == NULL) |
1168 | return -ENODEV; | |
1169 | } else | |
711e2c33 | 1170 | return -EINVAL; |
711e2c33 | 1171 | |
38f7b870 | 1172 | nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
b60c5115 TG |
1173 | if (nskb == NULL) { |
1174 | err = -ENOBUFS; | |
1175 | goto errout; | |
1176 | } | |
1177 | ||
575c3e2a PM |
1178 | err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, |
1179 | nlh->nlmsg_seq, 0, 0); | |
26932566 PM |
1180 | if (err < 0) { |
1181 | /* -EMSGSIZE implies BUG in if_nlmsg_size */ | |
1182 | WARN_ON(err == -EMSGSIZE); | |
1183 | kfree_skb(nskb); | |
1184 | goto errout; | |
1185 | } | |
b974179a | 1186 | err = rtnl_unicast(nskb, NETLINK_CB(skb).pid); |
b60c5115 | 1187 | errout: |
711e2c33 | 1188 | dev_put(dev); |
711e2c33 | 1189 | |
b60c5115 | 1190 | return err; |
711e2c33 | 1191 | } |
711e2c33 | 1192 | |
42bad1da | 1193 | static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 LT |
1194 | { |
1195 | int idx; | |
1196 | int s_idx = cb->family; | |
1197 | ||
1198 | if (s_idx == 0) | |
1199 | s_idx = 1; | |
1200 | for (idx=1; idx<NPROTO; idx++) { | |
1201 | int type = cb->nlh->nlmsg_type-RTM_BASE; | |
1202 | if (idx < s_idx || idx == PF_PACKET) | |
1203 | continue; | |
e2849863 TG |
1204 | if (rtnl_msg_handlers[idx] == NULL || |
1205 | rtnl_msg_handlers[idx][type].dumpit == NULL) | |
1da177e4 LT |
1206 | continue; |
1207 | if (idx > s_idx) | |
1208 | memset(&cb->args[0], 0, sizeof(cb->args)); | |
e2849863 | 1209 | if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) |
1da177e4 LT |
1210 | break; |
1211 | } | |
1212 | cb->family = idx; | |
1213 | ||
1214 | return skb->len; | |
1215 | } | |
1216 | ||
1217 | void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) | |
1218 | { | |
1219 | struct sk_buff *skb; | |
0ec6d3f4 | 1220 | int err = -ENOBUFS; |
1da177e4 | 1221 | |
38f7b870 | 1222 | skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL); |
0ec6d3f4 TG |
1223 | if (skb == NULL) |
1224 | goto errout; | |
1da177e4 | 1225 | |
575c3e2a | 1226 | err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0); |
26932566 PM |
1227 | if (err < 0) { |
1228 | /* -EMSGSIZE implies BUG in if_nlmsg_size() */ | |
1229 | WARN_ON(err == -EMSGSIZE); | |
1230 | kfree_skb(skb); | |
1231 | goto errout; | |
1232 | } | |
0ec6d3f4 TG |
1233 | err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); |
1234 | errout: | |
1235 | if (err < 0) | |
1236 | rtnl_set_sk_err(RTNLGRP_LINK, err); | |
1da177e4 LT |
1237 | } |
1238 | ||
1da177e4 LT |
1239 | /* Protected by RTNL sempahore. */ |
1240 | static struct rtattr **rta_buf; | |
1241 | static int rtattr_max; | |
1242 | ||
1243 | /* Process one rtnetlink message. */ | |
1244 | ||
1d00a4eb | 1245 | static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 1246 | { |
e2849863 | 1247 | rtnl_doit_func doit; |
1da177e4 LT |
1248 | int sz_idx, kind; |
1249 | int min_len; | |
1250 | int family; | |
1251 | int type; | |
1c2d670f | 1252 | int err; |
1da177e4 | 1253 | |
1da177e4 | 1254 | type = nlh->nlmsg_type; |
1da177e4 | 1255 | if (type > RTM_MAX) |
038890fe | 1256 | return -EOPNOTSUPP; |
1da177e4 LT |
1257 | |
1258 | type -= RTM_BASE; | |
1259 | ||
1260 | /* All the messages must have at least 1 byte length */ | |
1261 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) | |
1262 | return 0; | |
1263 | ||
1264 | family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family; | |
1d00a4eb TG |
1265 | if (family >= NPROTO) |
1266 | return -EAFNOSUPPORT; | |
1da177e4 | 1267 | |
1da177e4 LT |
1268 | sz_idx = type>>2; |
1269 | kind = type&3; | |
1270 | ||
1d00a4eb TG |
1271 | if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) |
1272 | return -EPERM; | |
1da177e4 LT |
1273 | |
1274 | if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { | |
e2849863 | 1275 | rtnl_dumpit_func dumpit; |
1da177e4 | 1276 | |
e2849863 TG |
1277 | dumpit = rtnl_get_dumpit(family, type); |
1278 | if (dumpit == NULL) | |
038890fe | 1279 | return -EOPNOTSUPP; |
9ac4a169 | 1280 | |
1c2d670f PM |
1281 | __rtnl_unlock(); |
1282 | err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL); | |
1283 | rtnl_lock(); | |
1284 | return err; | |
1da177e4 LT |
1285 | } |
1286 | ||
1287 | memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); | |
1288 | ||
1289 | min_len = rtm_min[sz_idx]; | |
1290 | if (nlh->nlmsg_len < min_len) | |
1d00a4eb | 1291 | return -EINVAL; |
1da177e4 LT |
1292 | |
1293 | if (nlh->nlmsg_len > min_len) { | |
1294 | int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); | |
1295 | struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len); | |
1296 | ||
1297 | while (RTA_OK(attr, attrlen)) { | |
1298 | unsigned flavor = attr->rta_type; | |
1299 | if (flavor) { | |
1300 | if (flavor > rta_max[sz_idx]) | |
1d00a4eb | 1301 | return -EINVAL; |
1da177e4 LT |
1302 | rta_buf[flavor-1] = attr; |
1303 | } | |
1304 | attr = RTA_NEXT(attr, attrlen); | |
1305 | } | |
1306 | } | |
1307 | ||
e2849863 TG |
1308 | doit = rtnl_get_doit(family, type); |
1309 | if (doit == NULL) | |
038890fe | 1310 | return -EOPNOTSUPP; |
1da177e4 | 1311 | |
1d00a4eb | 1312 | return doit(skb, nlh, (void *)&rta_buf[0]); |
1da177e4 LT |
1313 | } |
1314 | ||
1da177e4 LT |
1315 | static void rtnetlink_rcv(struct sock *sk, int len) |
1316 | { | |
9ac4a169 | 1317 | unsigned int qlen = 0; |
2a0a6ebe | 1318 | |
1da177e4 | 1319 | do { |
1536cc0d | 1320 | rtnl_lock(); |
0cfad075 | 1321 | qlen = netlink_run_queue(sk, qlen, &rtnetlink_rcv_msg); |
1536cc0d | 1322 | rtnl_unlock(); |
2a0a6ebe | 1323 | } while (qlen); |
1da177e4 LT |
1324 | } |
1325 | ||
1da177e4 LT |
1326 | static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) |
1327 | { | |
1328 | struct net_device *dev = ptr; | |
e9dc8653 EB |
1329 | |
1330 | if (dev->nd_net != &init_net) | |
1331 | return NOTIFY_DONE; | |
1332 | ||
1da177e4 LT |
1333 | switch (event) { |
1334 | case NETDEV_UNREGISTER: | |
1335 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); | |
1336 | break; | |
1337 | case NETDEV_REGISTER: | |
1338 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
1339 | break; | |
1340 | case NETDEV_UP: | |
1341 | case NETDEV_DOWN: | |
1342 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); | |
1343 | break; | |
1344 | case NETDEV_CHANGE: | |
1345 | case NETDEV_GOING_DOWN: | |
1346 | break; | |
1347 | default: | |
1348 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); | |
1349 | break; | |
1350 | } | |
1351 | return NOTIFY_DONE; | |
1352 | } | |
1353 | ||
1354 | static struct notifier_block rtnetlink_dev_notifier = { | |
1355 | .notifier_call = rtnetlink_event, | |
1356 | }; | |
1357 | ||
1358 | void __init rtnetlink_init(void) | |
1359 | { | |
1360 | int i; | |
1361 | ||
1362 | rtattr_max = 0; | |
1363 | for (i = 0; i < ARRAY_SIZE(rta_max); i++) | |
1364 | if (rta_max[i] > rtattr_max) | |
1365 | rtattr_max = rta_max[i]; | |
1366 | rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); | |
1367 | if (!rta_buf) | |
1368 | panic("rtnetlink_init: cannot allocate rta_buf\n"); | |
1369 | ||
b4b51029 EB |
1370 | rtnl = netlink_kernel_create(&init_net, NETLINK_ROUTE, RTNLGRP_MAX, |
1371 | rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); | |
1da177e4 LT |
1372 | if (rtnl == NULL) |
1373 | panic("rtnetlink_init: cannot initialize rtnetlink\n"); | |
1374 | netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); | |
1375 | register_netdevice_notifier(&rtnetlink_dev_notifier); | |
340d17fc TG |
1376 | |
1377 | rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo); | |
1378 | rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL); | |
38f7b870 PM |
1379 | rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL); |
1380 | rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL); | |
687ad8cc TG |
1381 | |
1382 | rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all); | |
1383 | rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all); | |
1da177e4 LT |
1384 | } |
1385 | ||
1386 | EXPORT_SYMBOL(__rta_fill); | |
1387 | EXPORT_SYMBOL(rtattr_strlcpy); | |
1388 | EXPORT_SYMBOL(rtattr_parse); | |
2371baa4 | 1389 | EXPORT_SYMBOL(__rtattr_parse_nested_compat); |
1da177e4 | 1390 | EXPORT_SYMBOL(rtnetlink_put_metrics); |
1da177e4 | 1391 | EXPORT_SYMBOL(rtnl_lock); |
6756ae4b | 1392 | EXPORT_SYMBOL(rtnl_trylock); |
1da177e4 | 1393 | EXPORT_SYMBOL(rtnl_unlock); |
2942e900 | 1394 | EXPORT_SYMBOL(rtnl_unicast); |
97676b6b TG |
1395 | EXPORT_SYMBOL(rtnl_notify); |
1396 | EXPORT_SYMBOL(rtnl_set_sk_err); | |
e7199288 PE |
1397 | EXPORT_SYMBOL(rtnl_create_link); |
1398 | EXPORT_SYMBOL(ifla_policy); |