blob: 1b9c32d79917457aeec83669a96f5d667bae8930 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080036#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070037#include <linux/if_addr.h>
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +020038#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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>
Thomas Graf14c0b972006-08-04 03:38:38 -070053#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070054#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Thomas Grafe2849862007-03-22 11:48:11 -070056struct rtnl_link
57{
58 rtnl_doit_func doit;
59 rtnl_dumpit_func dumpit;
60};
61
Stephen Hemminger6756ae42006-03-20 22:23:58 -080062static DEFINE_MUTEX(rtnl_mutex);
Thomas Graf56fc85a2006-08-15 00:37:29 -070063static struct sock *rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65void rtnl_lock(void)
66{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080067 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075void rtnl_unlock(void)
76{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080077 mutex_unlock(&rtnl_mutex);
78 if (rtnl && rtnl->sk_receive_queue.qlen)
79 rtnl->sk_data_ready(rtnl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 netdev_run_todo();
81}
82
Stephen Hemminger6756ae42006-03-20 22:23:58 -080083int rtnl_trylock(void)
84{
85 return mutex_trylock(&rtnl_mutex);
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
89{
90 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
91
92 while (RTA_OK(rta, len)) {
93 unsigned flavor = rta->rta_type;
94 if (flavor && flavor <= maxattr)
95 tb[flavor-1] = rta;
96 rta = RTA_NEXT(rta, len);
97 }
98 return 0;
99}
100
Patrick McHardy2371baa2007-06-26 03:23:44 -0700101int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
YOSHIFUJI Hideaki40b77c92007-07-19 10:43:23 +0900102 struct rtattr *rta, int len)
Patrick McHardyafdc3232007-06-25 14:30:16 -0700103{
104 if (RTA_PAYLOAD(rta) < len)
105 return -1;
Patrick McHardyafdc3232007-06-25 14:30:16 -0700106 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
107 rta = RTA_DATA(rta) + RTA_ALIGN(len);
108 return rtattr_parse_nested(tb, maxattr, rta);
109 }
110 memset(tb, 0, sizeof(struct rtattr *) * maxattr);
111 return 0;
112}
113
Adrian Bunk42bad1d2007-04-26 00:57:41 -0700114static struct rtnl_link *rtnl_msg_handlers[NPROTO];
Thomas Grafe2849862007-03-22 11:48:11 -0700115
116static inline int rtm_msgindex(int msgtype)
117{
118 int msgindex = msgtype - RTM_BASE;
119
120 /*
121 * msgindex < 0 implies someone tried to register a netlink
122 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
123 * the message type has not been added to linux/rtnetlink.h
124 */
125 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
126
127 return msgindex;
128}
129
130static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
131{
132 struct rtnl_link *tab;
133
134 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700135 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700136 tab = rtnl_msg_handlers[PF_UNSPEC];
137
Thomas Graf51057f22007-03-22 21:41:06 -0700138 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700139}
140
141static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
142{
143 struct rtnl_link *tab;
144
145 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700146 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700147 tab = rtnl_msg_handlers[PF_UNSPEC];
148
Thomas Graf51057f22007-03-22 21:41:06 -0700149 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700150}
151
152/**
153 * __rtnl_register - Register a rtnetlink message type
154 * @protocol: Protocol family or PF_UNSPEC
155 * @msgtype: rtnetlink message type
156 * @doit: Function pointer called for each request message
157 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
158 *
159 * Registers the specified function pointers (at least one of them has
160 * to be non-NULL) to be called whenever a request message for the
161 * specified protocol family and message type is received.
162 *
163 * The special protocol family PF_UNSPEC may be used to define fallback
164 * function pointers for the case when no entry for the specific protocol
165 * family exists.
166 *
167 * Returns 0 on success or a negative error code.
168 */
169int __rtnl_register(int protocol, int msgtype,
170 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
171{
172 struct rtnl_link *tab;
173 int msgindex;
174
175 BUG_ON(protocol < 0 || protocol >= NPROTO);
176 msgindex = rtm_msgindex(msgtype);
177
178 tab = rtnl_msg_handlers[protocol];
179 if (tab == NULL) {
180 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
181 if (tab == NULL)
182 return -ENOBUFS;
183
184 rtnl_msg_handlers[protocol] = tab;
185 }
186
187 if (doit)
188 tab[msgindex].doit = doit;
189
190 if (dumpit)
191 tab[msgindex].dumpit = dumpit;
192
193 return 0;
194}
195
196EXPORT_SYMBOL_GPL(__rtnl_register);
197
198/**
199 * rtnl_register - Register a rtnetlink message type
200 *
201 * Identical to __rtnl_register() but panics on failure. This is useful
202 * as failure of this function is very unlikely, it can only happen due
203 * to lack of memory when allocating the chain to store all message
204 * handlers for a protocol. Meant for use in init functions where lack
205 * of memory implies no sense in continueing.
206 */
207void rtnl_register(int protocol, int msgtype,
208 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
209{
210 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
211 panic("Unable to register rtnetlink message handler, "
212 "protocol = %d, message type = %d\n",
213 protocol, msgtype);
214}
215
216EXPORT_SYMBOL_GPL(rtnl_register);
217
218/**
219 * rtnl_unregister - Unregister a rtnetlink message type
220 * @protocol: Protocol family or PF_UNSPEC
221 * @msgtype: rtnetlink message type
222 *
223 * Returns 0 on success or a negative error code.
224 */
225int rtnl_unregister(int protocol, int msgtype)
226{
227 int msgindex;
228
229 BUG_ON(protocol < 0 || protocol >= NPROTO);
230 msgindex = rtm_msgindex(msgtype);
231
232 if (rtnl_msg_handlers[protocol] == NULL)
233 return -ENOENT;
234
235 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
236 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
237
238 return 0;
239}
240
241EXPORT_SYMBOL_GPL(rtnl_unregister);
242
243/**
244 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
245 * @protocol : Protocol family or PF_UNSPEC
246 *
247 * Identical to calling rtnl_unregster() for all registered message types
248 * of a certain protocol family.
249 */
250void rtnl_unregister_all(int protocol)
251{
252 BUG_ON(protocol < 0 || protocol >= NPROTO);
253
254 kfree(rtnl_msg_handlers[protocol]);
255 rtnl_msg_handlers[protocol] = NULL;
256}
257
258EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick McHardy38f7b872007-06-13 12:03:51 -0700260static LIST_HEAD(link_ops);
261
262/**
263 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
264 * @ops: struct rtnl_link_ops * to register
265 *
266 * The caller must hold the rtnl_mutex. This function should be used
267 * by drivers that create devices during module initialization. It
268 * must be called before registering the devices.
269 *
270 * Returns 0 on success or a negative error code.
271 */
272int __rtnl_link_register(struct rtnl_link_ops *ops)
273{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700274 if (!ops->dellink)
275 ops->dellink = unregister_netdevice;
276
Patrick McHardy38f7b872007-06-13 12:03:51 -0700277 list_add_tail(&ops->list, &link_ops);
278 return 0;
279}
280
281EXPORT_SYMBOL_GPL(__rtnl_link_register);
282
283/**
284 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
285 * @ops: struct rtnl_link_ops * to register
286 *
287 * Returns 0 on success or a negative error code.
288 */
289int rtnl_link_register(struct rtnl_link_ops *ops)
290{
291 int err;
292
293 rtnl_lock();
294 err = __rtnl_link_register(ops);
295 rtnl_unlock();
296 return err;
297}
298
299EXPORT_SYMBOL_GPL(rtnl_link_register);
300
301/**
302 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
303 * @ops: struct rtnl_link_ops * to unregister
304 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700305 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700306 */
307void __rtnl_link_unregister(struct rtnl_link_ops *ops)
308{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700309 struct net_device *dev, *n;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700310 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700311
Eric W. Biederman881d9662007-09-17 11:56:21 -0700312 for_each_net(net) {
313 for_each_netdev_safe(net, dev, n) {
314 if (dev->rtnl_link_ops == ops)
315 ops->dellink(dev);
316 }
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700317 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700318 list_del(&ops->list);
319}
320
321EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
322
323/**
324 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
325 * @ops: struct rtnl_link_ops * to unregister
326 */
327void rtnl_link_unregister(struct rtnl_link_ops *ops)
328{
329 rtnl_lock();
330 __rtnl_link_unregister(ops);
331 rtnl_unlock();
332}
333
334EXPORT_SYMBOL_GPL(rtnl_link_unregister);
335
336static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
337{
338 const struct rtnl_link_ops *ops;
339
340 list_for_each_entry(ops, &link_ops, list) {
341 if (!strcmp(ops->kind, kind))
342 return ops;
343 }
344 return NULL;
345}
346
347static size_t rtnl_link_get_size(const struct net_device *dev)
348{
349 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
350 size_t size;
351
352 if (!ops)
353 return 0;
354
355 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
356 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
357
358 if (ops->get_size)
359 /* IFLA_INFO_DATA + nested data */
360 size += nlmsg_total_size(sizeof(struct nlattr)) +
361 ops->get_size(dev);
362
363 if (ops->get_xstats_size)
364 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
365
366 return size;
367}
368
369static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
370{
371 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
372 struct nlattr *linkinfo, *data;
373 int err = -EMSGSIZE;
374
375 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
376 if (linkinfo == NULL)
377 goto out;
378
379 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
380 goto err_cancel_link;
381 if (ops->fill_xstats) {
382 err = ops->fill_xstats(skb, dev);
383 if (err < 0)
384 goto err_cancel_link;
385 }
386 if (ops->fill_info) {
387 data = nla_nest_start(skb, IFLA_INFO_DATA);
388 if (data == NULL)
389 goto err_cancel_link;
390 err = ops->fill_info(skb, dev);
391 if (err < 0)
392 goto err_cancel_data;
393 nla_nest_end(skb, data);
394 }
395
396 nla_nest_end(skb, linkinfo);
397 return 0;
398
399err_cancel_data:
400 nla_nest_cancel(skb, data);
401err_cancel_link:
402 nla_nest_cancel(skb, linkinfo);
403out:
404 return err;
405}
406
Thomas Grafdb46edc2005-05-03 14:29:39 -0700407static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Thomas Graff90a0a72005-05-03 14:29:00 -0700409 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
410 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
411 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700412 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700413 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
414 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
415 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
416 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700417 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
418 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
420
Thomas Grafdb46edc2005-05-03 14:29:39 -0700421static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Thomas Graff90a0a72005-05-03 14:29:00 -0700423 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
424 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
425 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700426 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700427 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
428 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
429 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
430 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
433void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
434{
435 struct rtattr *rta;
436 int size = RTA_LENGTH(attrlen);
437
438 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
439 rta->rta_type = attrtype;
440 rta->rta_len = size;
441 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700442 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
446{
447 size_t ret = RTA_PAYLOAD(rta);
448 char *src = RTA_DATA(rta);
449
450 if (ret > 0 && src[ret - 1] == '\0')
451 ret--;
452 if (size > 0) {
453 size_t len = (ret >= size) ? size - 1 : ret;
454 memset(dest, 0, size);
455 memcpy(dest, src, len);
456 }
457 return ret;
458}
459
460int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
461{
462 int err = 0;
463
Patrick McHardyac6d4392005-08-14 19:29:52 -0700464 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (echo)
466 atomic_inc(&skb->users);
467 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
468 if (echo)
469 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
470 return err;
471}
472
Thomas Graf2942e902006-08-15 00:30:25 -0700473int rtnl_unicast(struct sk_buff *skb, u32 pid)
474{
475 return nlmsg_unicast(rtnl, skb, pid);
476}
477
Thomas Graf97676b62006-08-15 00:31:41 -0700478int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
479 struct nlmsghdr *nlh, gfp_t flags)
480{
481 int report = 0;
482
483 if (nlh)
484 report = nlmsg_report(nlh);
485
486 return nlmsg_notify(rtnl, skb, pid, group, report, flags);
487}
488
489void rtnl_set_sk_err(u32 group, int error)
490{
491 netlink_set_err(rtnl, 0, group, error);
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
495{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700496 struct nlattr *mx;
497 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Thomas Graf2d7202b2006-08-22 00:01:27 -0700499 mx = nla_nest_start(skb, RTA_METRICS);
500 if (mx == NULL)
501 return -ENOBUFS;
502
503 for (i = 0; i < RTAX_MAX; i++) {
504 if (metrics[i]) {
505 valid++;
506 NLA_PUT_U32(skb, i+1, metrics[i]);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
David S. Millera57d27f2006-08-22 22:20:14 -0700510 if (!valid) {
511 nla_nest_cancel(skb, mx);
512 return 0;
513 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700514
515 return nla_nest_end(skb, mx);
516
517nla_put_failure:
518 return nla_nest_cancel(skb, mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Thomas Grafe3703b32006-11-27 09:27:07 -0800521int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
522 u32 ts, u32 tsage, long expires, u32 error)
523{
524 struct rta_cacheinfo ci = {
525 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
526 .rta_used = dst->__use,
527 .rta_clntref = atomic_read(&(dst->__refcnt)),
528 .rta_error = error,
529 .rta_id = id,
530 .rta_ts = ts,
531 .rta_tsage = tsage,
532 };
533
534 if (expires)
535 ci.rta_expires = jiffies_to_clock_t(expires);
536
537 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
538}
539
540EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Stefan Rompfb00055a2006-03-20 17:09:11 -0800542static void set_operstate(struct net_device *dev, unsigned char transition)
543{
544 unsigned char operstate = dev->operstate;
545
546 switch(transition) {
547 case IF_OPER_UP:
548 if ((operstate == IF_OPER_DORMANT ||
549 operstate == IF_OPER_UNKNOWN) &&
550 !netif_dormant(dev))
551 operstate = IF_OPER_UP;
552 break;
553
554 case IF_OPER_DORMANT:
555 if (operstate == IF_OPER_UP ||
556 operstate == IF_OPER_UNKNOWN)
557 operstate = IF_OPER_DORMANT;
558 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700559 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800560
561 if (dev->operstate != operstate) {
562 write_lock_bh(&dev_base_lock);
563 dev->operstate = operstate;
564 write_unlock_bh(&dev_base_lock);
565 netdev_state_change(dev);
566 }
567}
568
Thomas Grafb60c5112006-08-04 23:05:34 -0700569static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
570 struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Thomas Grafb60c5112006-08-04 23:05:34 -0700572 a->rx_packets = b->rx_packets;
573 a->tx_packets = b->tx_packets;
574 a->rx_bytes = b->rx_bytes;
575 a->tx_bytes = b->tx_bytes;
576 a->rx_errors = b->rx_errors;
577 a->tx_errors = b->tx_errors;
578 a->rx_dropped = b->rx_dropped;
579 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Thomas Grafb60c5112006-08-04 23:05:34 -0700581 a->multicast = b->multicast;
582 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Thomas Grafb60c5112006-08-04 23:05:34 -0700584 a->rx_length_errors = b->rx_length_errors;
585 a->rx_over_errors = b->rx_over_errors;
586 a->rx_crc_errors = b->rx_crc_errors;
587 a->rx_frame_errors = b->rx_frame_errors;
588 a->rx_fifo_errors = b->rx_fifo_errors;
589 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Thomas Grafb60c5112006-08-04 23:05:34 -0700591 a->tx_aborted_errors = b->tx_aborted_errors;
592 a->tx_carrier_errors = b->tx_carrier_errors;
593 a->tx_fifo_errors = b->tx_fifo_errors;
594 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
595 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Thomas Grafb60c5112006-08-04 23:05:34 -0700597 a->rx_compressed = b->rx_compressed;
598 a->tx_compressed = b->tx_compressed;
599};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Patrick McHardy38f7b872007-06-13 12:03:51 -0700601static inline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800602{
603 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
604 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
605 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
606 + nla_total_size(sizeof(struct rtnl_link_ifmap))
607 + nla_total_size(sizeof(struct rtnl_link_stats))
608 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
609 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
610 + nla_total_size(4) /* IFLA_TXQLEN */
611 + nla_total_size(4) /* IFLA_WEIGHT */
612 + nla_total_size(4) /* IFLA_MTU */
613 + nla_total_size(4) /* IFLA_LINK */
614 + nla_total_size(4) /* IFLA_MASTER */
615 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700616 + nla_total_size(1) /* IFLA_LINKMODE */
617 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
Thomas Graf339bf982006-11-10 14:10:15 -0800618}
619
Thomas Grafb60c5112006-08-04 23:05:34 -0700620static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700621 int type, u32 pid, u32 seq, u32 change,
622 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700623{
624 struct ifinfomsg *ifm;
625 struct nlmsghdr *nlh;
626
627 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
628 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800629 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700630
631 ifm = nlmsg_data(nlh);
632 ifm->ifi_family = AF_UNSPEC;
633 ifm->__ifi_pad = 0;
634 ifm->ifi_type = dev->type;
635 ifm->ifi_index = dev->ifindex;
636 ifm->ifi_flags = dev_get_flags(dev);
637 ifm->ifi_change = change;
638
639 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
640 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700641 NLA_PUT_U8(skb, IFLA_OPERSTATE,
642 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
643 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
644 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
645
646 if (dev->ifindex != dev->iflink)
647 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
648
649 if (dev->master)
650 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
651
652 if (dev->qdisc_sleeping)
653 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800654
655 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct rtnl_link_ifmap map = {
657 .mem_start = dev->mem_start,
658 .mem_end = dev->mem_end,
659 .base_addr = dev->base_addr,
660 .irq = dev->irq,
661 .dma = dev->dma,
662 .port = dev->if_port,
663 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700664 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700668 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
669 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 if (dev->get_stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700673 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700675 struct nlattr *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Thomas Grafb60c5112006-08-04 23:05:34 -0700677 attr = nla_reserve(skb, IFLA_STATS,
678 sizeof(struct rtnl_link_stats));
679 if (attr == NULL)
680 goto nla_put_failure;
681
682 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick McHardy38f7b872007-06-13 12:03:51 -0700686 if (dev->rtnl_link_ops) {
687 if (rtnl_link_fill(skb, dev) < 0)
688 goto nla_put_failure;
689 }
690
Thomas Grafb60c5112006-08-04 23:05:34 -0700691 return nlmsg_end(skb, nlh);
692
693nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800694 nlmsg_cancel(skb, nlh);
695 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Thomas Grafb60c5112006-08-04 23:05:34 -0700698static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700700 struct net *net = skb->sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int idx;
702 int s_idx = cb->args[0];
703 struct net_device *dev;
704
Pavel Emelianov7562f872007-05-03 15:13:45 -0700705 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700706 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700708 goto cont;
Patrick McHardy575c3e22007-05-22 17:00:49 -0700709 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
Thomas Grafb60c5112006-08-04 23:05:34 -0700710 NETLINK_CB(cb->skb).pid,
711 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700713cont:
714 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 cb->args[0] = idx;
717
718 return skb->len;
719}
720
Pavel Emelianove7199282007-08-08 22:16:38 -0700721const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700722 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -0700723 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
724 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -0700725 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700726 [IFLA_MTU] = { .type = NLA_U32 },
727 [IFLA_TXQLEN] = { .type = NLA_U32 },
728 [IFLA_WEIGHT] = { .type = NLA_U32 },
729 [IFLA_OPERSTATE] = { .type = NLA_U8 },
730 [IFLA_LINKMODE] = { .type = NLA_U8 },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200731 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -0700732};
733
Patrick McHardy38f7b872007-06-13 12:03:51 -0700734static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
735 [IFLA_INFO_KIND] = { .type = NLA_STRING },
736 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
737};
738
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200739static struct net *get_net_ns_by_pid(pid_t pid)
740{
741 struct task_struct *tsk;
742 struct net *net;
743
744 /* Lookup the network namespace */
745 net = ERR_PTR(-ESRCH);
746 rcu_read_lock();
747 tsk = find_task_by_pid(pid);
748 if (tsk) {
749 task_lock(tsk);
750 if (tsk->nsproxy)
751 net = get_net(tsk->nsproxy->net_ns);
752 task_unlock(tsk);
753 }
754 rcu_read_unlock();
755 return net;
756}
757
Patrick McHardy0157f602007-06-13 12:03:36 -0700758static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -0700759 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -0700760{
Patrick McHardy38f7b872007-06-13 12:03:51 -0700761 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -0700762 int err;
763
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200764 if (tb[IFLA_NET_NS_PID]) {
765 struct net *net;
766 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
767 if (IS_ERR(net)) {
768 err = PTR_ERR(net);
769 goto errout;
770 }
771 err = dev_change_net_namespace(dev, net, ifname);
772 put_net(net);
773 if (err)
774 goto errout;
775 modified = 1;
776 }
777
Patrick McHardy0157f602007-06-13 12:03:36 -0700778 if (tb[IFLA_MAP]) {
779 struct rtnl_link_ifmap *u_map;
780 struct ifmap k_map;
781
782 if (!dev->set_config) {
783 err = -EOPNOTSUPP;
784 goto errout;
785 }
786
787 if (!netif_device_present(dev)) {
788 err = -ENODEV;
789 goto errout;
790 }
791
792 u_map = nla_data(tb[IFLA_MAP]);
793 k_map.mem_start = (unsigned long) u_map->mem_start;
794 k_map.mem_end = (unsigned long) u_map->mem_end;
795 k_map.base_addr = (unsigned short) u_map->base_addr;
796 k_map.irq = (unsigned char) u_map->irq;
797 k_map.dma = (unsigned char) u_map->dma;
798 k_map.port = (unsigned char) u_map->port;
799
800 err = dev->set_config(dev, &k_map);
801 if (err < 0)
802 goto errout;
803
804 modified = 1;
805 }
806
807 if (tb[IFLA_ADDRESS]) {
808 struct sockaddr *sa;
809 int len;
810
811 if (!dev->set_mac_address) {
812 err = -EOPNOTSUPP;
813 goto errout;
814 }
815
816 if (!netif_device_present(dev)) {
817 err = -ENODEV;
818 goto errout;
819 }
820
821 len = sizeof(sa_family_t) + dev->addr_len;
822 sa = kmalloc(len, GFP_KERNEL);
823 if (!sa) {
824 err = -ENOMEM;
825 goto errout;
826 }
827 sa->sa_family = dev->type;
828 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
829 dev->addr_len);
830 err = dev->set_mac_address(dev, sa);
831 kfree(sa);
832 if (err)
833 goto errout;
834 send_addr_notify = 1;
835 modified = 1;
836 }
837
838 if (tb[IFLA_MTU]) {
839 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
840 if (err < 0)
841 goto errout;
842 modified = 1;
843 }
844
845 /*
846 * Interface selected by interface index but interface
847 * name provided implies that a name change has been
848 * requested.
849 */
850 if (ifm->ifi_index > 0 && ifname[0]) {
851 err = dev_change_name(dev, ifname);
852 if (err < 0)
853 goto errout;
854 modified = 1;
855 }
856
857 if (tb[IFLA_BROADCAST]) {
858 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
859 send_addr_notify = 1;
860 }
861
862 if (ifm->ifi_flags || ifm->ifi_change) {
863 unsigned int flags = ifm->ifi_flags;
864
865 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
866 if (ifm->ifi_change)
867 flags = (flags & ifm->ifi_change) |
868 (dev->flags & ~ifm->ifi_change);
869 dev_change_flags(dev, flags);
870 }
871
872 if (tb[IFLA_TXQLEN])
873 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
874
Patrick McHardy0157f602007-06-13 12:03:36 -0700875 if (tb[IFLA_OPERSTATE])
876 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
877
878 if (tb[IFLA_LINKMODE]) {
879 write_lock_bh(&dev_base_lock);
880 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
881 write_unlock_bh(&dev_base_lock);
882 }
883
884 err = 0;
885
886errout:
887 if (err < 0 && modified && net_ratelimit())
888 printk(KERN_WARNING "A link change request failed with "
889 "some changes comitted already. Interface %s may "
890 "have been left with an inconsistent configuration, "
891 "please check.\n", dev->name);
892
893 if (send_addr_notify)
894 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
895 return err;
896}
897
Thomas Grafda5e0492006-08-10 21:17:37 -0700898static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700900 struct net *net = skb->sk->sk_net;
Thomas Grafda5e0492006-08-10 21:17:37 -0700901 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700903 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700904 struct nlattr *tb[IFLA_MAX+1];
905 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Thomas Grafda5e0492006-08-10 21:17:37 -0700907 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
908 if (err < 0)
909 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Thomas Graf5176f912006-08-26 20:13:18 -0700911 if (tb[IFLA_IFNAME])
912 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700913 else
914 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700917 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700918 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700919 dev = dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -0700920 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -0700921 dev = dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -0700922 else
923 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Thomas Grafda5e0492006-08-10 21:17:37 -0700925 if (dev == NULL) {
926 err = -ENODEV;
927 goto errout;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Thomas Grafda5e0492006-08-10 21:17:37 -0700930 if (tb[IFLA_ADDRESS] &&
931 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
932 goto errout_dev;
933
934 if (tb[IFLA_BROADCAST] &&
935 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
936 goto errout_dev;
937
Patrick McHardy38f7b872007-06-13 12:03:51 -0700938 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -0700939errout_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dev_put(dev);
Thomas Grafda5e0492006-08-10 21:17:37 -0700941errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return err;
943}
944
Patrick McHardy38f7b872007-06-13 12:03:51 -0700945static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
946{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700947 struct net *net = skb->sk->sk_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700948 const struct rtnl_link_ops *ops;
949 struct net_device *dev;
950 struct ifinfomsg *ifm;
951 char ifname[IFNAMSIZ];
952 struct nlattr *tb[IFLA_MAX+1];
953 int err;
954
955 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
956 if (err < 0)
957 return err;
958
959 if (tb[IFLA_IFNAME])
960 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
961
962 ifm = nlmsg_data(nlh);
963 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700964 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700965 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -0700966 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700967 else
968 return -EINVAL;
969
970 if (!dev)
971 return -ENODEV;
972
973 ops = dev->rtnl_link_ops;
974 if (!ops)
975 return -EOPNOTSUPP;
976
977 ops->dellink(dev);
978 return 0;
979}
980
Eric W. Biederman881d9662007-09-17 11:56:21 -0700981struct net_device *rtnl_create_link(struct net *net, char *ifname,
Pavel Emelianove7199282007-08-08 22:16:38 -0700982 const struct rtnl_link_ops *ops, struct nlattr *tb[])
983{
984 int err;
985 struct net_device *dev;
986
987 err = -ENOMEM;
988 dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
989 if (!dev)
990 goto err;
991
992 if (strchr(dev->name, '%')) {
993 err = dev_alloc_name(dev, dev->name);
994 if (err < 0)
995 goto err_free;
996 }
997
Eric W. Biederman881d9662007-09-17 11:56:21 -0700998 dev->nd_net = net;
Pavel Emelianove7199282007-08-08 22:16:38 -0700999 dev->rtnl_link_ops = ops;
1000
1001 if (tb[IFLA_MTU])
1002 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1003 if (tb[IFLA_ADDRESS])
1004 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1005 nla_len(tb[IFLA_ADDRESS]));
1006 if (tb[IFLA_BROADCAST])
1007 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1008 nla_len(tb[IFLA_BROADCAST]));
1009 if (tb[IFLA_TXQLEN])
1010 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1011 if (tb[IFLA_OPERSTATE])
1012 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1013 if (tb[IFLA_LINKMODE])
1014 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1015
1016 return dev;
1017
1018err_free:
1019 free_netdev(dev);
1020err:
1021 return ERR_PTR(err);
1022}
1023
Patrick McHardy38f7b872007-06-13 12:03:51 -07001024static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1025{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001026 struct net *net = skb->sk->sk_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001027 const struct rtnl_link_ops *ops;
1028 struct net_device *dev;
1029 struct ifinfomsg *ifm;
1030 char kind[MODULE_NAME_LEN];
1031 char ifname[IFNAMSIZ];
1032 struct nlattr *tb[IFLA_MAX+1];
1033 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1034 int err;
1035
Thomas Graf8072f082007-07-31 14:13:50 -07001036#ifdef CONFIG_KMOD
Patrick McHardy38f7b872007-06-13 12:03:51 -07001037replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001038#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001039 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1040 if (err < 0)
1041 return err;
1042
1043 if (tb[IFLA_IFNAME])
1044 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1045 else
1046 ifname[0] = '\0';
1047
1048 ifm = nlmsg_data(nlh);
1049 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001050 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001051 else if (ifname[0])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001052 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001053 else
1054 dev = NULL;
1055
1056 if (tb[IFLA_LINKINFO]) {
1057 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1058 tb[IFLA_LINKINFO], ifla_info_policy);
1059 if (err < 0)
1060 return err;
1061 } else
1062 memset(linkinfo, 0, sizeof(linkinfo));
1063
1064 if (linkinfo[IFLA_INFO_KIND]) {
1065 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1066 ops = rtnl_link_ops_get(kind);
1067 } else {
1068 kind[0] = '\0';
1069 ops = NULL;
1070 }
1071
1072 if (1) {
1073 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1074
1075 if (ops) {
1076 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1077 err = nla_parse_nested(attr, ops->maxtype,
1078 linkinfo[IFLA_INFO_DATA],
1079 ops->policy);
1080 if (err < 0)
1081 return err;
1082 data = attr;
1083 }
1084 if (ops->validate) {
1085 err = ops->validate(tb, data);
1086 if (err < 0)
1087 return err;
1088 }
1089 }
1090
1091 if (dev) {
1092 int modified = 0;
1093
1094 if (nlh->nlmsg_flags & NLM_F_EXCL)
1095 return -EEXIST;
1096 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1097 return -EOPNOTSUPP;
1098
1099 if (linkinfo[IFLA_INFO_DATA]) {
1100 if (!ops || ops != dev->rtnl_link_ops ||
1101 !ops->changelink)
1102 return -EOPNOTSUPP;
1103
1104 err = ops->changelink(dev, tb, data);
1105 if (err < 0)
1106 return err;
1107 modified = 1;
1108 }
1109
1110 return do_setlink(dev, ifm, tb, ifname, modified);
1111 }
1112
1113 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1114 return -ENODEV;
1115
1116 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1117 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001118 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001119 return -EOPNOTSUPP;
1120
1121 if (!ops) {
1122#ifdef CONFIG_KMOD
1123 if (kind[0]) {
1124 __rtnl_unlock();
1125 request_module("rtnl-link-%s", kind);
1126 rtnl_lock();
1127 ops = rtnl_link_ops_get(kind);
1128 if (ops)
1129 goto replay;
1130 }
1131#endif
1132 return -EOPNOTSUPP;
1133 }
1134
1135 if (!ifname[0])
1136 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001137
Eric W. Biederman881d9662007-09-17 11:56:21 -07001138 dev = rtnl_create_link(net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001139
Pavel Emelianove7199282007-08-08 22:16:38 -07001140 if (IS_ERR(dev))
1141 err = PTR_ERR(dev);
1142 else if (ops->newlink)
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001143 err = ops->newlink(dev, tb, data);
1144 else
1145 err = register_netdevice(dev);
Pavel Emelianove7199282007-08-08 22:16:38 -07001146
1147 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001148 free_netdev(dev);
1149 return err;
1150 }
1151}
1152
Thomas Grafb60c5112006-08-04 23:05:34 -07001153static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001154{
Eric W. Biederman881d9662007-09-17 11:56:21 -07001155 struct net *net = skb->sk->sk_net;
Thomas Grafb60c5112006-08-04 23:05:34 -07001156 struct ifinfomsg *ifm;
1157 struct nlattr *tb[IFLA_MAX+1];
1158 struct net_device *dev = NULL;
1159 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001160 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001161
Thomas Grafb60c5112006-08-04 23:05:34 -07001162 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1163 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001164 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001165
1166 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001167 if (ifm->ifi_index > 0) {
Eric W. Biederman881d9662007-09-17 11:56:21 -07001168 dev = dev_get_by_index(net, ifm->ifi_index);
Thomas Grafb60c5112006-08-04 23:05:34 -07001169 if (dev == NULL)
1170 return -ENODEV;
1171 } else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001172 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001173
Patrick McHardy38f7b872007-06-13 12:03:51 -07001174 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Grafb60c5112006-08-04 23:05:34 -07001175 if (nskb == NULL) {
1176 err = -ENOBUFS;
1177 goto errout;
1178 }
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001179
Patrick McHardy575c3e22007-05-22 17:00:49 -07001180 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1181 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001182 if (err < 0) {
1183 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1184 WARN_ON(err == -EMSGSIZE);
1185 kfree_skb(nskb);
1186 goto errout;
1187 }
Patrick McHardyb9741792006-10-12 01:50:30 -07001188 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001189errout:
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001190 dev_put(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001191
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001192 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001193}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001194
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001195static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int idx;
1198 int s_idx = cb->family;
1199
1200 if (s_idx == 0)
1201 s_idx = 1;
1202 for (idx=1; idx<NPROTO; idx++) {
1203 int type = cb->nlh->nlmsg_type-RTM_BASE;
1204 if (idx < s_idx || idx == PF_PACKET)
1205 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001206 if (rtnl_msg_handlers[idx] == NULL ||
1207 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 continue;
1209 if (idx > s_idx)
1210 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001211 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
1213 }
1214 cb->family = idx;
1215
1216 return skb->len;
1217}
1218
1219void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1220{
1221 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001222 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Patrick McHardy38f7b872007-06-13 12:03:51 -07001224 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001225 if (skb == NULL)
1226 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Patrick McHardy575c3e22007-05-22 17:00:49 -07001228 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001229 if (err < 0) {
1230 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1231 WARN_ON(err == -EMSGSIZE);
1232 kfree_skb(skb);
1233 goto errout;
1234 }
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001235 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1236errout:
1237 if (err < 0)
1238 rtnl_set_sk_err(RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/* Protected by RTNL sempahore. */
1242static struct rtattr **rta_buf;
1243static int rtattr_max;
1244
1245/* Process one rtnetlink message. */
1246
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001247static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Thomas Grafe2849862007-03-22 11:48:11 -07001249 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int sz_idx, kind;
1251 int min_len;
1252 int family;
1253 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001254 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001258 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 type -= RTM_BASE;
1261
1262 /* All the messages must have at least 1 byte length */
1263 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1264 return 0;
1265
1266 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001267 if (family >= NPROTO)
1268 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sz_idx = type>>2;
1271 kind = type&3;
1272
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001273 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1274 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Thomas Grafe2849862007-03-22 11:48:11 -07001277 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Thomas Grafe2849862007-03-22 11:48:11 -07001279 dumpit = rtnl_get_dumpit(family, type);
1280 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001281 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001283 __rtnl_unlock();
1284 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1285 rtnl_lock();
1286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
1289 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1290
1291 min_len = rtm_min[sz_idx];
1292 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (nlh->nlmsg_len > min_len) {
1296 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1297 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
1298
1299 while (RTA_OK(attr, attrlen)) {
1300 unsigned flavor = attr->rta_type;
1301 if (flavor) {
1302 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 rta_buf[flavor-1] = attr;
1305 }
1306 attr = RTA_NEXT(attr, attrlen);
1307 }
1308 }
1309
Thomas Grafe2849862007-03-22 11:48:11 -07001310 doit = rtnl_get_doit(family, type);
1311 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001312 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001314 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static void rtnetlink_rcv(struct sock *sk, int len)
1318{
Thomas Graf9ac4a162005-11-10 02:25:55 +01001319 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 do {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001322 mutex_lock(&rtnl_mutex);
Thomas Graf9ac4a162005-11-10 02:25:55 +01001323 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001324 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001327 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1331{
1332 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001333
1334 if (dev->nd_net != &init_net)
1335 return NOTIFY_DONE;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 switch (event) {
1338 case NETDEV_UNREGISTER:
1339 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1340 break;
1341 case NETDEV_REGISTER:
1342 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1343 break;
1344 case NETDEV_UP:
1345 case NETDEV_DOWN:
1346 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1347 break;
1348 case NETDEV_CHANGE:
1349 case NETDEV_GOING_DOWN:
1350 break;
1351 default:
1352 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1353 break;
1354 }
1355 return NOTIFY_DONE;
1356}
1357
1358static struct notifier_block rtnetlink_dev_notifier = {
1359 .notifier_call = rtnetlink_event,
1360};
1361
1362void __init rtnetlink_init(void)
1363{
1364 int i;
1365
1366 rtattr_max = 0;
1367 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1368 if (rta_max[i] > rtattr_max)
1369 rtattr_max = rta_max[i];
1370 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1371 if (!rta_buf)
1372 panic("rtnetlink_init: cannot allocate rta_buf\n");
1373
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001374 rtnl = netlink_kernel_create(&init_net, NETLINK_ROUTE, RTNLGRP_MAX,
1375 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (rtnl == NULL)
1377 panic("rtnetlink_init: cannot initialize rtnetlink\n");
1378 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1379 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07001380
1381 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1382 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001383 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1384 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07001385
1386 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1387 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390EXPORT_SYMBOL(__rta_fill);
1391EXPORT_SYMBOL(rtattr_strlcpy);
1392EXPORT_SYMBOL(rtattr_parse);
Patrick McHardy2371baa2007-06-26 03:23:44 -07001393EXPORT_SYMBOL(__rtattr_parse_nested_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395EXPORT_SYMBOL(rtnl_lock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001396EXPORT_SYMBOL(rtnl_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397EXPORT_SYMBOL(rtnl_unlock);
Thomas Graf2942e902006-08-15 00:30:25 -07001398EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf97676b62006-08-15 00:31:41 -07001399EXPORT_SYMBOL(rtnl_notify);
1400EXPORT_SYMBOL(rtnl_set_sk_err);
Pavel Emelianove7199282007-08-08 22:16:38 -07001401EXPORT_SYMBOL(rtnl_create_link);
1402EXPORT_SYMBOL(ifla_policy);