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