blob: 864cbdf31ed7f49b2ab73c44b1de818db70b505d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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>
Thomas Graf14c0b972006-08-04 03:38:38 -070052#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070053#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Thomas Grafe2849862007-03-22 11:48:11 -070055struct rtnl_link
56{
57 rtnl_doit_func doit;
58 rtnl_dumpit_func dumpit;
59};
60
Stephen Hemminger6756ae42006-03-20 22:23:58 -080061static DEFINE_MUTEX(rtnl_mutex);
Thomas Graf56fc85a2006-08-15 00:37:29 -070062static struct sock *rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64void rtnl_lock(void)
65{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Stephen Hemminger6756ae42006-03-20 22:23:58 -080069void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void rtnl_unlock(void)
75{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_unlock(&rtnl_mutex);
77 if (rtnl && rtnl->sk_receive_queue.qlen)
78 rtnl->sk_data_ready(rtnl, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 netdev_run_todo();
80}
81
Stephen Hemminger6756ae42006-03-20 22:23:58 -080082int rtnl_trylock(void)
83{
84 return mutex_trylock(&rtnl_mutex);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087int 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
Patrick McHardy2371baa2007-06-26 03:23:44 -0700100int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
101 struct rtattr *rta, int len)
Patrick McHardyafdc3232007-06-25 14:30:16 -0700102{
103 if (RTA_PAYLOAD(rta) < len)
104 return -1;
Patrick McHardyafdc3232007-06-25 14:30:16 -0700105 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
Adrian Bunk42bad1d2007-04-26 00:57:41 -0700113static struct rtnl_link *rtnl_msg_handlers[NPROTO];
Thomas Grafe2849862007-03-22 11:48:11 -0700114
115static 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
129static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
130{
131 struct rtnl_link *tab;
132
133 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700134 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700135 tab = rtnl_msg_handlers[PF_UNSPEC];
136
Thomas Graf51057f22007-03-22 21:41:06 -0700137 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700138}
139
140static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
141{
142 struct rtnl_link *tab;
143
144 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700145 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700146 tab = rtnl_msg_handlers[PF_UNSPEC];
147
Thomas Graf51057f22007-03-22 21:41:06 -0700148 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700149}
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 */
168int __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
195EXPORT_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 */
206void 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
215EXPORT_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 */
224int 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
240EXPORT_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 */
249void 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
257EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Patrick McHardy38f7b872007-06-13 12:03:51 -0700259static 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 */
271int __rtnl_link_register(struct rtnl_link_ops *ops)
272{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700273 if (!ops->dellink)
274 ops->dellink = unregister_netdevice;
275
Patrick McHardy38f7b872007-06-13 12:03:51 -0700276 list_add_tail(&ops->list, &link_ops);
277 return 0;
278}
279
280EXPORT_SYMBOL_GPL(__rtnl_link_register);
281
282/**
283 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
284 * @ops: struct rtnl_link_ops * to register
285 *
286 * Returns 0 on success or a negative error code.
287 */
288int rtnl_link_register(struct rtnl_link_ops *ops)
289{
290 int err;
291
292 rtnl_lock();
293 err = __rtnl_link_register(ops);
294 rtnl_unlock();
295 return err;
296}
297
298EXPORT_SYMBOL_GPL(rtnl_link_register);
299
300/**
301 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
302 * @ops: struct rtnl_link_ops * to unregister
303 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700304 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700305 */
306void __rtnl_link_unregister(struct rtnl_link_ops *ops)
307{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700308 struct net_device *dev, *n;
309
310 for_each_netdev_safe(dev, n) {
311 if (dev->rtnl_link_ops == ops)
312 ops->dellink(dev);
313 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700314 list_del(&ops->list);
315}
316
317EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
318
319/**
320 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
321 * @ops: struct rtnl_link_ops * to unregister
322 */
323void rtnl_link_unregister(struct rtnl_link_ops *ops)
324{
325 rtnl_lock();
326 __rtnl_link_unregister(ops);
327 rtnl_unlock();
328}
329
330EXPORT_SYMBOL_GPL(rtnl_link_unregister);
331
332static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
333{
334 const struct rtnl_link_ops *ops;
335
336 list_for_each_entry(ops, &link_ops, list) {
337 if (!strcmp(ops->kind, kind))
338 return ops;
339 }
340 return NULL;
341}
342
343static size_t rtnl_link_get_size(const struct net_device *dev)
344{
345 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
346 size_t size;
347
348 if (!ops)
349 return 0;
350
351 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
352 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
353
354 if (ops->get_size)
355 /* IFLA_INFO_DATA + nested data */
356 size += nlmsg_total_size(sizeof(struct nlattr)) +
357 ops->get_size(dev);
358
359 if (ops->get_xstats_size)
360 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
361
362 return size;
363}
364
365static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
366{
367 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
368 struct nlattr *linkinfo, *data;
369 int err = -EMSGSIZE;
370
371 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
372 if (linkinfo == NULL)
373 goto out;
374
375 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
376 goto err_cancel_link;
377 if (ops->fill_xstats) {
378 err = ops->fill_xstats(skb, dev);
379 if (err < 0)
380 goto err_cancel_link;
381 }
382 if (ops->fill_info) {
383 data = nla_nest_start(skb, IFLA_INFO_DATA);
384 if (data == NULL)
385 goto err_cancel_link;
386 err = ops->fill_info(skb, dev);
387 if (err < 0)
388 goto err_cancel_data;
389 nla_nest_end(skb, data);
390 }
391
392 nla_nest_end(skb, linkinfo);
393 return 0;
394
395err_cancel_data:
396 nla_nest_cancel(skb, data);
397err_cancel_link:
398 nla_nest_cancel(skb, linkinfo);
399out:
400 return err;
401}
402
Thomas Grafdb46edc2005-05-03 14:29:39 -0700403static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Thomas Graff90a0a72005-05-03 14:29:00 -0700405 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
406 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
407 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700408 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700409 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
410 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
411 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
412 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700413 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
414 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
Thomas Grafdb46edc2005-05-03 14:29:39 -0700417static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Thomas Graff90a0a72005-05-03 14:29:00 -0700419 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
420 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
421 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700422 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700423 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
424 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
425 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
426 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
429void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
430{
431 struct rtattr *rta;
432 int size = RTA_LENGTH(attrlen);
433
434 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
435 rta->rta_type = attrtype;
436 rta->rta_len = size;
437 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700438 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
442{
443 size_t ret = RTA_PAYLOAD(rta);
444 char *src = RTA_DATA(rta);
445
446 if (ret > 0 && src[ret - 1] == '\0')
447 ret--;
448 if (size > 0) {
449 size_t len = (ret >= size) ? size - 1 : ret;
450 memset(dest, 0, size);
451 memcpy(dest, src, len);
452 }
453 return ret;
454}
455
456int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
457{
458 int err = 0;
459
Patrick McHardyac6d4392005-08-14 19:29:52 -0700460 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (echo)
462 atomic_inc(&skb->users);
463 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
464 if (echo)
465 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
466 return err;
467}
468
Thomas Graf2942e902006-08-15 00:30:25 -0700469int rtnl_unicast(struct sk_buff *skb, u32 pid)
470{
471 return nlmsg_unicast(rtnl, skb, pid);
472}
473
Thomas Graf97676b62006-08-15 00:31:41 -0700474int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
475 struct nlmsghdr *nlh, gfp_t flags)
476{
477 int report = 0;
478
479 if (nlh)
480 report = nlmsg_report(nlh);
481
482 return nlmsg_notify(rtnl, skb, pid, group, report, flags);
483}
484
485void rtnl_set_sk_err(u32 group, int error)
486{
487 netlink_set_err(rtnl, 0, group, error);
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
491{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700492 struct nlattr *mx;
493 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Thomas Graf2d7202b2006-08-22 00:01:27 -0700495 mx = nla_nest_start(skb, RTA_METRICS);
496 if (mx == NULL)
497 return -ENOBUFS;
498
499 for (i = 0; i < RTAX_MAX; i++) {
500 if (metrics[i]) {
501 valid++;
502 NLA_PUT_U32(skb, i+1, metrics[i]);
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David S. Millera57d27f2006-08-22 22:20:14 -0700506 if (!valid) {
507 nla_nest_cancel(skb, mx);
508 return 0;
509 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700510
511 return nla_nest_end(skb, mx);
512
513nla_put_failure:
514 return nla_nest_cancel(skb, mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Thomas Grafe3703b32006-11-27 09:27:07 -0800517int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
518 u32 ts, u32 tsage, long expires, u32 error)
519{
520 struct rta_cacheinfo ci = {
521 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
522 .rta_used = dst->__use,
523 .rta_clntref = atomic_read(&(dst->__refcnt)),
524 .rta_error = error,
525 .rta_id = id,
526 .rta_ts = ts,
527 .rta_tsage = tsage,
528 };
529
530 if (expires)
531 ci.rta_expires = jiffies_to_clock_t(expires);
532
533 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
534}
535
536EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Stefan Rompfb00055a2006-03-20 17:09:11 -0800538static void set_operstate(struct net_device *dev, unsigned char transition)
539{
540 unsigned char operstate = dev->operstate;
541
542 switch(transition) {
543 case IF_OPER_UP:
544 if ((operstate == IF_OPER_DORMANT ||
545 operstate == IF_OPER_UNKNOWN) &&
546 !netif_dormant(dev))
547 operstate = IF_OPER_UP;
548 break;
549
550 case IF_OPER_DORMANT:
551 if (operstate == IF_OPER_UP ||
552 operstate == IF_OPER_UNKNOWN)
553 operstate = IF_OPER_DORMANT;
554 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700555 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800556
557 if (dev->operstate != operstate) {
558 write_lock_bh(&dev_base_lock);
559 dev->operstate = operstate;
560 write_unlock_bh(&dev_base_lock);
561 netdev_state_change(dev);
562 }
563}
564
Thomas Grafb60c5112006-08-04 23:05:34 -0700565static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
566 struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Thomas Grafb60c5112006-08-04 23:05:34 -0700568 a->rx_packets = b->rx_packets;
569 a->tx_packets = b->tx_packets;
570 a->rx_bytes = b->rx_bytes;
571 a->tx_bytes = b->tx_bytes;
572 a->rx_errors = b->rx_errors;
573 a->tx_errors = b->tx_errors;
574 a->rx_dropped = b->rx_dropped;
575 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Thomas Grafb60c5112006-08-04 23:05:34 -0700577 a->multicast = b->multicast;
578 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Thomas Grafb60c5112006-08-04 23:05:34 -0700580 a->rx_length_errors = b->rx_length_errors;
581 a->rx_over_errors = b->rx_over_errors;
582 a->rx_crc_errors = b->rx_crc_errors;
583 a->rx_frame_errors = b->rx_frame_errors;
584 a->rx_fifo_errors = b->rx_fifo_errors;
585 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Thomas Grafb60c5112006-08-04 23:05:34 -0700587 a->tx_aborted_errors = b->tx_aborted_errors;
588 a->tx_carrier_errors = b->tx_carrier_errors;
589 a->tx_fifo_errors = b->tx_fifo_errors;
590 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
591 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Thomas Grafb60c5112006-08-04 23:05:34 -0700593 a->rx_compressed = b->rx_compressed;
594 a->tx_compressed = b->tx_compressed;
595};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Patrick McHardy38f7b872007-06-13 12:03:51 -0700597static inline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800598{
599 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
600 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
601 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
602 + nla_total_size(sizeof(struct rtnl_link_ifmap))
603 + nla_total_size(sizeof(struct rtnl_link_stats))
604 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
605 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
606 + nla_total_size(4) /* IFLA_TXQLEN */
607 + nla_total_size(4) /* IFLA_WEIGHT */
608 + nla_total_size(4) /* IFLA_MTU */
609 + nla_total_size(4) /* IFLA_LINK */
610 + nla_total_size(4) /* IFLA_MASTER */
611 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612 + nla_total_size(1) /* IFLA_LINKMODE */
613 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
Thomas Graf339bf982006-11-10 14:10:15 -0800614}
615
Thomas Grafb60c5112006-08-04 23:05:34 -0700616static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700617 int type, u32 pid, u32 seq, u32 change,
618 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700619{
620 struct ifinfomsg *ifm;
621 struct nlmsghdr *nlh;
622
623 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
624 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800625 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700626
627 ifm = nlmsg_data(nlh);
628 ifm->ifi_family = AF_UNSPEC;
629 ifm->__ifi_pad = 0;
630 ifm->ifi_type = dev->type;
631 ifm->ifi_index = dev->ifindex;
632 ifm->ifi_flags = dev_get_flags(dev);
633 ifm->ifi_change = change;
634
635 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
636 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
637 NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
638 NLA_PUT_U8(skb, IFLA_OPERSTATE,
639 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
640 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
641 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
642
643 if (dev->ifindex != dev->iflink)
644 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
645
646 if (dev->master)
647 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
648
649 if (dev->qdisc_sleeping)
650 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800651
652 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct rtnl_link_ifmap map = {
654 .mem_start = dev->mem_start,
655 .mem_end = dev->mem_end,
656 .base_addr = dev->base_addr,
657 .irq = dev->irq,
658 .dma = dev->dma,
659 .port = dev->if_port,
660 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700661 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700665 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
666 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 if (dev->get_stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700670 struct net_device_stats *stats = dev->get_stats(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (stats) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700672 struct nlattr *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Thomas Grafb60c5112006-08-04 23:05:34 -0700674 attr = nla_reserve(skb, IFLA_STATS,
675 sizeof(struct rtnl_link_stats));
676 if (attr == NULL)
677 goto nla_put_failure;
678
679 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Patrick McHardy38f7b872007-06-13 12:03:51 -0700683 if (dev->rtnl_link_ops) {
684 if (rtnl_link_fill(skb, dev) < 0)
685 goto nla_put_failure;
686 }
687
Thomas Grafb60c5112006-08-04 23:05:34 -0700688 return nlmsg_end(skb, nlh);
689
690nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800691 nlmsg_cancel(skb, nlh);
692 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Thomas Grafb60c5112006-08-04 23:05:34 -0700695static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int idx;
698 int s_idx = cb->args[0];
699 struct net_device *dev;
700
Pavel Emelianov7562f872007-05-03 15:13:45 -0700701 idx = 0;
702 for_each_netdev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (idx < s_idx)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700704 goto cont;
Patrick McHardy575c3e22007-05-22 17:00:49 -0700705 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
Thomas Grafb60c5112006-08-04 23:05:34 -0700706 NETLINK_CB(cb->skb).pid,
707 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700709cont:
710 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 cb->args[0] = idx;
713
714 return skb->len;
715}
716
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700717static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700718 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -0700719 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
720 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -0700721 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700722 [IFLA_MTU] = { .type = NLA_U32 },
723 [IFLA_TXQLEN] = { .type = NLA_U32 },
724 [IFLA_WEIGHT] = { .type = NLA_U32 },
725 [IFLA_OPERSTATE] = { .type = NLA_U8 },
726 [IFLA_LINKMODE] = { .type = NLA_U8 },
727};
728
Patrick McHardy38f7b872007-06-13 12:03:51 -0700729static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
730 [IFLA_INFO_KIND] = { .type = NLA_STRING },
731 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
732};
733
Patrick McHardy0157f602007-06-13 12:03:36 -0700734static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -0700735 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -0700736{
Patrick McHardy38f7b872007-06-13 12:03:51 -0700737 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -0700738 int err;
739
740 if (tb[IFLA_MAP]) {
741 struct rtnl_link_ifmap *u_map;
742 struct ifmap k_map;
743
744 if (!dev->set_config) {
745 err = -EOPNOTSUPP;
746 goto errout;
747 }
748
749 if (!netif_device_present(dev)) {
750 err = -ENODEV;
751 goto errout;
752 }
753
754 u_map = nla_data(tb[IFLA_MAP]);
755 k_map.mem_start = (unsigned long) u_map->mem_start;
756 k_map.mem_end = (unsigned long) u_map->mem_end;
757 k_map.base_addr = (unsigned short) u_map->base_addr;
758 k_map.irq = (unsigned char) u_map->irq;
759 k_map.dma = (unsigned char) u_map->dma;
760 k_map.port = (unsigned char) u_map->port;
761
762 err = dev->set_config(dev, &k_map);
763 if (err < 0)
764 goto errout;
765
766 modified = 1;
767 }
768
769 if (tb[IFLA_ADDRESS]) {
770 struct sockaddr *sa;
771 int len;
772
773 if (!dev->set_mac_address) {
774 err = -EOPNOTSUPP;
775 goto errout;
776 }
777
778 if (!netif_device_present(dev)) {
779 err = -ENODEV;
780 goto errout;
781 }
782
783 len = sizeof(sa_family_t) + dev->addr_len;
784 sa = kmalloc(len, GFP_KERNEL);
785 if (!sa) {
786 err = -ENOMEM;
787 goto errout;
788 }
789 sa->sa_family = dev->type;
790 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
791 dev->addr_len);
792 err = dev->set_mac_address(dev, sa);
793 kfree(sa);
794 if (err)
795 goto errout;
796 send_addr_notify = 1;
797 modified = 1;
798 }
799
800 if (tb[IFLA_MTU]) {
801 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
802 if (err < 0)
803 goto errout;
804 modified = 1;
805 }
806
807 /*
808 * Interface selected by interface index but interface
809 * name provided implies that a name change has been
810 * requested.
811 */
812 if (ifm->ifi_index > 0 && ifname[0]) {
813 err = dev_change_name(dev, ifname);
814 if (err < 0)
815 goto errout;
816 modified = 1;
817 }
818
819 if (tb[IFLA_BROADCAST]) {
820 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
821 send_addr_notify = 1;
822 }
823
824 if (ifm->ifi_flags || ifm->ifi_change) {
825 unsigned int flags = ifm->ifi_flags;
826
827 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
828 if (ifm->ifi_change)
829 flags = (flags & ifm->ifi_change) |
830 (dev->flags & ~ifm->ifi_change);
831 dev_change_flags(dev, flags);
832 }
833
834 if (tb[IFLA_TXQLEN])
835 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
836
837 if (tb[IFLA_WEIGHT])
838 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
839
840 if (tb[IFLA_OPERSTATE])
841 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
842
843 if (tb[IFLA_LINKMODE]) {
844 write_lock_bh(&dev_base_lock);
845 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
846 write_unlock_bh(&dev_base_lock);
847 }
848
849 err = 0;
850
851errout:
852 if (err < 0 && modified && net_ratelimit())
853 printk(KERN_WARNING "A link change request failed with "
854 "some changes comitted already. Interface %s may "
855 "have been left with an inconsistent configuration, "
856 "please check.\n", dev->name);
857
858 if (send_addr_notify)
859 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
860 return err;
861}
862
Thomas Grafda5e0492006-08-10 21:17:37 -0700863static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Thomas Grafda5e0492006-08-10 21:17:37 -0700865 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700867 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700868 struct nlattr *tb[IFLA_MAX+1];
869 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Thomas Grafda5e0492006-08-10 21:17:37 -0700871 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
872 if (err < 0)
873 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Thomas Graf5176f912006-08-26 20:13:18 -0700875 if (tb[IFLA_IFNAME])
876 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700877 else
878 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700881 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700882 if (ifm->ifi_index > 0)
Thomas Grafda5e0492006-08-10 21:17:37 -0700883 dev = dev_get_by_index(ifm->ifi_index);
884 else if (tb[IFLA_IFNAME])
885 dev = dev_get_by_name(ifname);
886 else
887 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Thomas Grafda5e0492006-08-10 21:17:37 -0700889 if (dev == NULL) {
890 err = -ENODEV;
891 goto errout;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Thomas Grafda5e0492006-08-10 21:17:37 -0700894 if (tb[IFLA_ADDRESS] &&
895 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
896 goto errout_dev;
897
898 if (tb[IFLA_BROADCAST] &&
899 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
900 goto errout_dev;
901
Patrick McHardy38f7b872007-06-13 12:03:51 -0700902 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -0700903errout_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dev_put(dev);
Thomas Grafda5e0492006-08-10 21:17:37 -0700905errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return err;
907}
908
Patrick McHardy38f7b872007-06-13 12:03:51 -0700909static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
910{
911 const struct rtnl_link_ops *ops;
912 struct net_device *dev;
913 struct ifinfomsg *ifm;
914 char ifname[IFNAMSIZ];
915 struct nlattr *tb[IFLA_MAX+1];
916 int err;
917
918 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
919 if (err < 0)
920 return err;
921
922 if (tb[IFLA_IFNAME])
923 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
924
925 ifm = nlmsg_data(nlh);
926 if (ifm->ifi_index > 0)
927 dev = __dev_get_by_index(ifm->ifi_index);
928 else if (tb[IFLA_IFNAME])
929 dev = __dev_get_by_name(ifname);
930 else
931 return -EINVAL;
932
933 if (!dev)
934 return -ENODEV;
935
936 ops = dev->rtnl_link_ops;
937 if (!ops)
938 return -EOPNOTSUPP;
939
940 ops->dellink(dev);
941 return 0;
942}
943
944static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
945{
946 const struct rtnl_link_ops *ops;
947 struct net_device *dev;
948 struct ifinfomsg *ifm;
949 char kind[MODULE_NAME_LEN];
950 char ifname[IFNAMSIZ];
951 struct nlattr *tb[IFLA_MAX+1];
952 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
953 int err;
954
955replay:
956 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
957 if (err < 0)
958 return err;
959
960 if (tb[IFLA_IFNAME])
961 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
962 else
963 ifname[0] = '\0';
964
965 ifm = nlmsg_data(nlh);
966 if (ifm->ifi_index > 0)
967 dev = __dev_get_by_index(ifm->ifi_index);
968 else if (ifname[0])
969 dev = __dev_get_by_name(ifname);
970 else
971 dev = NULL;
972
973 if (tb[IFLA_LINKINFO]) {
974 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
975 tb[IFLA_LINKINFO], ifla_info_policy);
976 if (err < 0)
977 return err;
978 } else
979 memset(linkinfo, 0, sizeof(linkinfo));
980
981 if (linkinfo[IFLA_INFO_KIND]) {
982 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
983 ops = rtnl_link_ops_get(kind);
984 } else {
985 kind[0] = '\0';
986 ops = NULL;
987 }
988
989 if (1) {
990 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
991
992 if (ops) {
993 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
994 err = nla_parse_nested(attr, ops->maxtype,
995 linkinfo[IFLA_INFO_DATA],
996 ops->policy);
997 if (err < 0)
998 return err;
999 data = attr;
1000 }
1001 if (ops->validate) {
1002 err = ops->validate(tb, data);
1003 if (err < 0)
1004 return err;
1005 }
1006 }
1007
1008 if (dev) {
1009 int modified = 0;
1010
1011 if (nlh->nlmsg_flags & NLM_F_EXCL)
1012 return -EEXIST;
1013 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1014 return -EOPNOTSUPP;
1015
1016 if (linkinfo[IFLA_INFO_DATA]) {
1017 if (!ops || ops != dev->rtnl_link_ops ||
1018 !ops->changelink)
1019 return -EOPNOTSUPP;
1020
1021 err = ops->changelink(dev, tb, data);
1022 if (err < 0)
1023 return err;
1024 modified = 1;
1025 }
1026
1027 return do_setlink(dev, ifm, tb, ifname, modified);
1028 }
1029
1030 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1031 return -ENODEV;
1032
1033 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1034 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001035 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001036 return -EOPNOTSUPP;
1037
1038 if (!ops) {
1039#ifdef CONFIG_KMOD
1040 if (kind[0]) {
1041 __rtnl_unlock();
1042 request_module("rtnl-link-%s", kind);
1043 rtnl_lock();
1044 ops = rtnl_link_ops_get(kind);
1045 if (ops)
1046 goto replay;
1047 }
1048#endif
1049 return -EOPNOTSUPP;
1050 }
1051
1052 if (!ifname[0])
1053 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1054 dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
1055 if (!dev)
1056 return -ENOMEM;
1057
1058 if (strchr(dev->name, '%')) {
1059 err = dev_alloc_name(dev, dev->name);
1060 if (err < 0)
1061 goto err_free;
1062 }
1063 dev->rtnl_link_ops = ops;
1064
1065 if (tb[IFLA_MTU])
1066 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Patrick McHardy0e068772007-07-11 19:42:31 -07001067 if (tb[IFLA_ADDRESS])
1068 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1069 nla_len(tb[IFLA_ADDRESS]));
1070 if (tb[IFLA_BROADCAST])
1071 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1072 nla_len(tb[IFLA_BROADCAST]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07001073 if (tb[IFLA_TXQLEN])
1074 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1075 if (tb[IFLA_WEIGHT])
1076 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
1077 if (tb[IFLA_OPERSTATE])
1078 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1079 if (tb[IFLA_LINKMODE])
1080 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1081
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001082 if (ops->newlink)
1083 err = ops->newlink(dev, tb, data);
1084 else
1085 err = register_netdevice(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001086err_free:
1087 if (err < 0)
1088 free_netdev(dev);
1089 return err;
1090 }
1091}
1092
Thomas Grafb60c5112006-08-04 23:05:34 -07001093static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001094{
Thomas Grafb60c5112006-08-04 23:05:34 -07001095 struct ifinfomsg *ifm;
1096 struct nlattr *tb[IFLA_MAX+1];
1097 struct net_device *dev = NULL;
1098 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001099 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001100
Thomas Grafb60c5112006-08-04 23:05:34 -07001101 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1102 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001103 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001104
1105 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001106 if (ifm->ifi_index > 0) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001107 dev = dev_get_by_index(ifm->ifi_index);
Thomas Grafb60c5112006-08-04 23:05:34 -07001108 if (dev == NULL)
1109 return -ENODEV;
1110 } else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001111 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001112
Patrick McHardy38f7b872007-06-13 12:03:51 -07001113 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Grafb60c5112006-08-04 23:05:34 -07001114 if (nskb == NULL) {
1115 err = -ENOBUFS;
1116 goto errout;
1117 }
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001118
Patrick McHardy575c3e22007-05-22 17:00:49 -07001119 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1120 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001121 if (err < 0) {
1122 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1123 WARN_ON(err == -EMSGSIZE);
1124 kfree_skb(nskb);
1125 goto errout;
1126 }
Patrick McHardyb9741792006-10-12 01:50:30 -07001127 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001128errout:
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001129 dev_put(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001130
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001131 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001132}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001133
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001134static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 int idx;
1137 int s_idx = cb->family;
1138
1139 if (s_idx == 0)
1140 s_idx = 1;
1141 for (idx=1; idx<NPROTO; idx++) {
1142 int type = cb->nlh->nlmsg_type-RTM_BASE;
1143 if (idx < s_idx || idx == PF_PACKET)
1144 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001145 if (rtnl_msg_handlers[idx] == NULL ||
1146 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 continue;
1148 if (idx > s_idx)
1149 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001150 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 }
1153 cb->family = idx;
1154
1155 return skb->len;
1156}
1157
1158void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1159{
1160 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001161 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Patrick McHardy38f7b872007-06-13 12:03:51 -07001163 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001164 if (skb == NULL)
1165 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Patrick McHardy575c3e22007-05-22 17:00:49 -07001167 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001168 if (err < 0) {
1169 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1170 WARN_ON(err == -EMSGSIZE);
1171 kfree_skb(skb);
1172 goto errout;
1173 }
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001174 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1175errout:
1176 if (err < 0)
1177 rtnl_set_sk_err(RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180/* Protected by RTNL sempahore. */
1181static struct rtattr **rta_buf;
1182static int rtattr_max;
1183
1184/* Process one rtnetlink message. */
1185
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001186static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Thomas Grafe2849862007-03-22 11:48:11 -07001188 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int sz_idx, kind;
1190 int min_len;
1191 int family;
1192 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001193 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001197 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 type -= RTM_BASE;
1200
1201 /* All the messages must have at least 1 byte length */
1202 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1203 return 0;
1204
1205 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001206 if (family >= NPROTO)
1207 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 sz_idx = type>>2;
1210 kind = type&3;
1211
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001212 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1213 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Thomas Grafe2849862007-03-22 11:48:11 -07001216 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Thomas Grafe2849862007-03-22 11:48:11 -07001218 dumpit = rtnl_get_dumpit(family, type);
1219 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001220 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001222 __rtnl_unlock();
1223 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1224 rtnl_lock();
1225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1229
1230 min_len = rtm_min[sz_idx];
1231 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (nlh->nlmsg_len > min_len) {
1235 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1236 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
1237
1238 while (RTA_OK(attr, attrlen)) {
1239 unsigned flavor = attr->rta_type;
1240 if (flavor) {
1241 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 rta_buf[flavor-1] = attr;
1244 }
1245 attr = RTA_NEXT(attr, attrlen);
1246 }
1247 }
1248
Thomas Grafe2849862007-03-22 11:48:11 -07001249 doit = rtnl_get_doit(family, type);
1250 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001251 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001253 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256static void rtnetlink_rcv(struct sock *sk, int len)
1257{
Thomas Graf9ac4a162005-11-10 02:25:55 +01001258 unsigned int qlen = 0;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 do {
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001261 mutex_lock(&rtnl_mutex);
Thomas Graf9ac4a162005-11-10 02:25:55 +01001262 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001263 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -07001266 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1270{
1271 struct net_device *dev = ptr;
1272 switch (event) {
1273 case NETDEV_UNREGISTER:
1274 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1275 break;
1276 case NETDEV_REGISTER:
1277 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1278 break;
1279 case NETDEV_UP:
1280 case NETDEV_DOWN:
1281 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1282 break;
1283 case NETDEV_CHANGE:
1284 case NETDEV_GOING_DOWN:
1285 break;
1286 default:
1287 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1288 break;
1289 }
1290 return NOTIFY_DONE;
1291}
1292
1293static struct notifier_block rtnetlink_dev_notifier = {
1294 .notifier_call = rtnetlink_event,
1295};
1296
1297void __init rtnetlink_init(void)
1298{
1299 int i;
1300
1301 rtattr_max = 0;
1302 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1303 if (rta_max[i] > rtattr_max)
1304 rtattr_max = rta_max[i];
1305 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1306 if (!rta_buf)
1307 panic("rtnetlink_init: cannot allocate rta_buf\n");
1308
Patrick McHardy06628602005-08-15 12:33:26 -07001309 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001310 &rtnl_mutex, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (rtnl == NULL)
1312 panic("rtnetlink_init: cannot initialize rtnetlink\n");
1313 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1314 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07001315
1316 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1317 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001318 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1319 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07001320
1321 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1322 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325EXPORT_SYMBOL(__rta_fill);
1326EXPORT_SYMBOL(rtattr_strlcpy);
1327EXPORT_SYMBOL(rtattr_parse);
Patrick McHardy2371baa2007-06-26 03:23:44 -07001328EXPORT_SYMBOL(__rtattr_parse_nested_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330EXPORT_SYMBOL(rtnl_lock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001331EXPORT_SYMBOL(rtnl_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332EXPORT_SYMBOL(rtnl_unlock);
Thomas Graf2942e902006-08-15 00:30:25 -07001333EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf97676b62006-08-15 00:31:41 -07001334EXPORT_SYMBOL(rtnl_notify);
1335EXPORT_SYMBOL(rtnl_set_sk_err);