blob: e2f3317f290f95b171ee64da160f1b3d3079f817 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/inet.h>
43#include <linux/netdevice.h>
44#include <net/ip.h>
45#include <net/protocol.h>
46#include <net/arp.h>
47#include <net/route.h>
48#include <net/udp.h>
49#include <net/sock.h>
50#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070051#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070052#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000053#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Eric Dumazete0d087a2009-11-07 01:26:17 -080055struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070056 rtnl_doit_func doit;
57 rtnl_dumpit_func dumpit;
58};
59
Stephen Hemminger6756ae42006-03-20 22:23:58 -080060static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62void rtnl_lock(void)
63{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080064 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Eric Dumazete0d087a2009-11-07 01:26:17 -080066EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Stephen Hemminger6756ae42006-03-20 22:23:58 -080068void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073void rtnl_unlock(void)
74{
Herbert Xu58ec3b42008-10-07 15:50:03 -070075 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 netdev_run_todo();
77}
Eric Dumazete0d087a2009-11-07 01:26:17 -080078EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Stephen Hemminger6756ae42006-03-20 22:23:58 -080080int rtnl_trylock(void)
81{
82 return mutex_trylock(&rtnl_mutex);
83}
Eric Dumazete0d087a2009-11-07 01:26:17 -080084EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080085
Patrick McHardyc9c10142008-04-23 22:10:48 -070086int rtnl_is_locked(void)
87{
88 return mutex_is_locked(&rtnl_mutex);
89}
Eric Dumazete0d087a2009-11-07 01:26:17 -080090EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070091
Adrian Bunk42bad1d2007-04-26 00:57:41 -070092static struct rtnl_link *rtnl_msg_handlers[NPROTO];
Thomas Grafe2849862007-03-22 11:48:11 -070093
94static inline int rtm_msgindex(int msgtype)
95{
96 int msgindex = msgtype - RTM_BASE;
97
98 /*
99 * msgindex < 0 implies someone tried to register a netlink
100 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
101 * the message type has not been added to linux/rtnetlink.h
102 */
103 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
104
105 return msgindex;
106}
107
108static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
109{
110 struct rtnl_link *tab;
111
112 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700113 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700114 tab = rtnl_msg_handlers[PF_UNSPEC];
115
Thomas Graf51057f22007-03-22 21:41:06 -0700116 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700117}
118
119static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
120{
121 struct rtnl_link *tab;
122
123 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700124 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700125 tab = rtnl_msg_handlers[PF_UNSPEC];
126
Thomas Graf51057f22007-03-22 21:41:06 -0700127 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700128}
129
130/**
131 * __rtnl_register - Register a rtnetlink message type
132 * @protocol: Protocol family or PF_UNSPEC
133 * @msgtype: rtnetlink message type
134 * @doit: Function pointer called for each request message
135 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
136 *
137 * Registers the specified function pointers (at least one of them has
138 * to be non-NULL) to be called whenever a request message for the
139 * specified protocol family and message type is received.
140 *
141 * The special protocol family PF_UNSPEC may be used to define fallback
142 * function pointers for the case when no entry for the specific protocol
143 * family exists.
144 *
145 * Returns 0 on success or a negative error code.
146 */
147int __rtnl_register(int protocol, int msgtype,
148 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
149{
150 struct rtnl_link *tab;
151 int msgindex;
152
153 BUG_ON(protocol < 0 || protocol >= NPROTO);
154 msgindex = rtm_msgindex(msgtype);
155
156 tab = rtnl_msg_handlers[protocol];
157 if (tab == NULL) {
158 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
159 if (tab == NULL)
160 return -ENOBUFS;
161
162 rtnl_msg_handlers[protocol] = tab;
163 }
164
165 if (doit)
166 tab[msgindex].doit = doit;
167
168 if (dumpit)
169 tab[msgindex].dumpit = dumpit;
170
171 return 0;
172}
Thomas Grafe2849862007-03-22 11:48:11 -0700173EXPORT_SYMBOL_GPL(__rtnl_register);
174
175/**
176 * rtnl_register - Register a rtnetlink message type
177 *
178 * Identical to __rtnl_register() but panics on failure. This is useful
179 * as failure of this function is very unlikely, it can only happen due
180 * to lack of memory when allocating the chain to store all message
181 * handlers for a protocol. Meant for use in init functions where lack
182 * of memory implies no sense in continueing.
183 */
184void rtnl_register(int protocol, int msgtype,
185 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
186{
187 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
188 panic("Unable to register rtnetlink message handler, "
189 "protocol = %d, message type = %d\n",
190 protocol, msgtype);
191}
Thomas Grafe2849862007-03-22 11:48:11 -0700192EXPORT_SYMBOL_GPL(rtnl_register);
193
194/**
195 * rtnl_unregister - Unregister a rtnetlink message type
196 * @protocol: Protocol family or PF_UNSPEC
197 * @msgtype: rtnetlink message type
198 *
199 * Returns 0 on success or a negative error code.
200 */
201int rtnl_unregister(int protocol, int msgtype)
202{
203 int msgindex;
204
205 BUG_ON(protocol < 0 || protocol >= NPROTO);
206 msgindex = rtm_msgindex(msgtype);
207
208 if (rtnl_msg_handlers[protocol] == NULL)
209 return -ENOENT;
210
211 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
212 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
213
214 return 0;
215}
Thomas Grafe2849862007-03-22 11:48:11 -0700216EXPORT_SYMBOL_GPL(rtnl_unregister);
217
218/**
219 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
220 * @protocol : Protocol family or PF_UNSPEC
221 *
222 * Identical to calling rtnl_unregster() for all registered message types
223 * of a certain protocol family.
224 */
225void rtnl_unregister_all(int protocol)
226{
227 BUG_ON(protocol < 0 || protocol >= NPROTO);
228
229 kfree(rtnl_msg_handlers[protocol]);
230 rtnl_msg_handlers[protocol] = NULL;
231}
Thomas Grafe2849862007-03-22 11:48:11 -0700232EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Patrick McHardy38f7b872007-06-13 12:03:51 -0700234static LIST_HEAD(link_ops);
235
236/**
237 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
238 * @ops: struct rtnl_link_ops * to register
239 *
240 * The caller must hold the rtnl_mutex. This function should be used
241 * by drivers that create devices during module initialization. It
242 * must be called before registering the devices.
243 *
244 * Returns 0 on success or a negative error code.
245 */
246int __rtnl_link_register(struct rtnl_link_ops *ops)
247{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700248 if (!ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000249 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700250
Patrick McHardy38f7b872007-06-13 12:03:51 -0700251 list_add_tail(&ops->list, &link_ops);
252 return 0;
253}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700254EXPORT_SYMBOL_GPL(__rtnl_link_register);
255
256/**
257 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
258 * @ops: struct rtnl_link_ops * to register
259 *
260 * Returns 0 on success or a negative error code.
261 */
262int rtnl_link_register(struct rtnl_link_ops *ops)
263{
264 int err;
265
266 rtnl_lock();
267 err = __rtnl_link_register(ops);
268 rtnl_unlock();
269 return err;
270}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700271EXPORT_SYMBOL_GPL(rtnl_link_register);
272
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700273static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
274{
275 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000276 LIST_HEAD(list_kill);
277
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700278 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000279 if (dev->rtnl_link_ops == ops)
280 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700281 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000282 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700283}
284
285void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
286{
287 rtnl_lock();
288 __rtnl_kill_links(net, ops);
289 rtnl_unlock();
290}
291EXPORT_SYMBOL_GPL(rtnl_kill_links);
292
Patrick McHardy38f7b872007-06-13 12:03:51 -0700293/**
294 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
295 * @ops: struct rtnl_link_ops * to unregister
296 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700297 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700298 */
299void __rtnl_link_unregister(struct rtnl_link_ops *ops)
300{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700301 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700302
Eric W. Biederman881d9662007-09-17 11:56:21 -0700303 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700304 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700305 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700306 list_del(&ops->list);
307}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700308EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
309
310/**
311 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
312 * @ops: struct rtnl_link_ops * to unregister
313 */
314void rtnl_link_unregister(struct rtnl_link_ops *ops)
315{
316 rtnl_lock();
317 __rtnl_link_unregister(ops);
318 rtnl_unlock();
319}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700320EXPORT_SYMBOL_GPL(rtnl_link_unregister);
321
322static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
323{
324 const struct rtnl_link_ops *ops;
325
326 list_for_each_entry(ops, &link_ops, list) {
327 if (!strcmp(ops->kind, kind))
328 return ops;
329 }
330 return NULL;
331}
332
333static size_t rtnl_link_get_size(const struct net_device *dev)
334{
335 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
336 size_t size;
337
338 if (!ops)
339 return 0;
340
341 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
342 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
343
344 if (ops->get_size)
345 /* IFLA_INFO_DATA + nested data */
346 size += nlmsg_total_size(sizeof(struct nlattr)) +
347 ops->get_size(dev);
348
349 if (ops->get_xstats_size)
350 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
351
352 return size;
353}
354
355static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
356{
357 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
358 struct nlattr *linkinfo, *data;
359 int err = -EMSGSIZE;
360
361 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
362 if (linkinfo == NULL)
363 goto out;
364
365 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
366 goto err_cancel_link;
367 if (ops->fill_xstats) {
368 err = ops->fill_xstats(skb, dev);
369 if (err < 0)
370 goto err_cancel_link;
371 }
372 if (ops->fill_info) {
373 data = nla_nest_start(skb, IFLA_INFO_DATA);
374 if (data == NULL)
375 goto err_cancel_link;
376 err = ops->fill_info(skb, dev);
377 if (err < 0)
378 goto err_cancel_data;
379 nla_nest_end(skb, data);
380 }
381
382 nla_nest_end(skb, linkinfo);
383 return 0;
384
385err_cancel_data:
386 nla_nest_cancel(skb, data);
387err_cancel_link:
388 nla_nest_cancel(skb, linkinfo);
389out:
390 return err;
391}
392
Thomas Grafdb46edc2005-05-03 14:29:39 -0700393static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Thomas Graff90a0a72005-05-03 14:29:00 -0700395 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
396 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
397 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700398 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700399 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
400 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
401 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
402 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700403 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
404 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405};
406
Thomas Grafdb46edc2005-05-03 14:29:39 -0700407static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Thomas Graff90a0a72005-05-03 14:29:00 -0700409 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
410 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
411 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700412 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700413 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
414 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
415 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
416 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417};
418
419void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
420{
421 struct rtattr *rta;
422 int size = RTA_LENGTH(attrlen);
423
Eric Dumazete0d087a2009-11-07 01:26:17 -0800424 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rta->rta_type = attrtype;
426 rta->rta_len = size;
427 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700428 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800430EXPORT_SYMBOL(__rta_fill);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800432int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800434 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int err = 0;
436
Patrick McHardyac6d4392005-08-14 19:29:52 -0700437 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (echo)
439 atomic_inc(&skb->users);
440 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
441 if (echo)
442 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
443 return err;
444}
445
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800446int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700447{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800448 struct sock *rtnl = net->rtnl;
449
Thomas Graf2942e902006-08-15 00:30:25 -0700450 return nlmsg_unicast(rtnl, skb, pid);
451}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800452EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700453
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800454void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
455 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700456{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800457 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700458 int report = 0;
459
460 if (nlh)
461 report = nlmsg_report(nlh);
462
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800463 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700464}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800465EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700466
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800467void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700468{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800469 struct sock *rtnl = net->rtnl;
470
Thomas Graf97676b62006-08-15 00:31:41 -0700471 netlink_set_err(rtnl, 0, group, error);
472}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800473EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
476{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700477 struct nlattr *mx;
478 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Thomas Graf2d7202b2006-08-22 00:01:27 -0700480 mx = nla_nest_start(skb, RTA_METRICS);
481 if (mx == NULL)
482 return -ENOBUFS;
483
484 for (i = 0; i < RTAX_MAX; i++) {
485 if (metrics[i]) {
486 valid++;
487 NLA_PUT_U32(skb, i+1, metrics[i]);
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David S. Millera57d27f2006-08-22 22:20:14 -0700491 if (!valid) {
492 nla_nest_cancel(skb, mx);
493 return 0;
494 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700495
496 return nla_nest_end(skb, mx);
497
498nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700499 nla_nest_cancel(skb, mx);
500 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800502EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Thomas Grafe3703b32006-11-27 09:27:07 -0800504int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
505 u32 ts, u32 tsage, long expires, u32 error)
506{
507 struct rta_cacheinfo ci = {
508 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
509 .rta_used = dst->__use,
510 .rta_clntref = atomic_read(&(dst->__refcnt)),
511 .rta_error = error,
512 .rta_id = id,
513 .rta_ts = ts,
514 .rta_tsage = tsage,
515 };
516
517 if (expires)
518 ci.rta_expires = jiffies_to_clock_t(expires);
519
520 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
521}
Thomas Grafe3703b32006-11-27 09:27:07 -0800522EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Miller93b2d4a2008-02-17 18:35:07 -0800524static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800525{
526 unsigned char operstate = dev->operstate;
527
Eric Dumazete0d087a2009-11-07 01:26:17 -0800528 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800529 case IF_OPER_UP:
530 if ((operstate == IF_OPER_DORMANT ||
531 operstate == IF_OPER_UNKNOWN) &&
532 !netif_dormant(dev))
533 operstate = IF_OPER_UP;
534 break;
535
536 case IF_OPER_DORMANT:
537 if (operstate == IF_OPER_UP ||
538 operstate == IF_OPER_UNKNOWN)
539 operstate = IF_OPER_DORMANT;
540 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700541 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800542
543 if (dev->operstate != operstate) {
544 write_lock_bh(&dev_base_lock);
545 dev->operstate = operstate;
546 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800547 netdev_state_change(dev);
548 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800549}
550
Thomas Grafb60c5112006-08-04 23:05:34 -0700551static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800552 const struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Thomas Grafb60c5112006-08-04 23:05:34 -0700554 a->rx_packets = b->rx_packets;
555 a->tx_packets = b->tx_packets;
556 a->rx_bytes = b->rx_bytes;
557 a->tx_bytes = b->tx_bytes;
558 a->rx_errors = b->rx_errors;
559 a->tx_errors = b->tx_errors;
560 a->rx_dropped = b->rx_dropped;
561 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Thomas Grafb60c5112006-08-04 23:05:34 -0700563 a->multicast = b->multicast;
564 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Thomas Grafb60c5112006-08-04 23:05:34 -0700566 a->rx_length_errors = b->rx_length_errors;
567 a->rx_over_errors = b->rx_over_errors;
568 a->rx_crc_errors = b->rx_crc_errors;
569 a->rx_frame_errors = b->rx_frame_errors;
570 a->rx_fifo_errors = b->rx_fifo_errors;
571 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Thomas Grafb60c5112006-08-04 23:05:34 -0700573 a->tx_aborted_errors = b->tx_aborted_errors;
574 a->tx_carrier_errors = b->tx_carrier_errors;
575 a->tx_fifo_errors = b->tx_fifo_errors;
576 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
577 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Thomas Grafb60c5112006-08-04 23:05:34 -0700579 a->rx_compressed = b->rx_compressed;
580 a->tx_compressed = b->tx_compressed;
581};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Patrick McHardy38f7b872007-06-13 12:03:51 -0700583static inline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800584{
585 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
586 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700587 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800588 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
589 + nla_total_size(sizeof(struct rtnl_link_ifmap))
590 + nla_total_size(sizeof(struct rtnl_link_stats))
591 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
592 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
593 + nla_total_size(4) /* IFLA_TXQLEN */
594 + nla_total_size(4) /* IFLA_WEIGHT */
595 + nla_total_size(4) /* IFLA_MTU */
596 + nla_total_size(4) /* IFLA_LINK */
597 + nla_total_size(4) /* IFLA_MASTER */
598 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700599 + nla_total_size(1) /* IFLA_LINKMODE */
600 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
Thomas Graf339bf982006-11-10 14:10:15 -0800601}
602
Thomas Grafb60c5112006-08-04 23:05:34 -0700603static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700604 int type, u32 pid, u32 seq, u32 change,
605 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700606{
607 struct ifinfomsg *ifm;
608 struct nlmsghdr *nlh;
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800609 const struct net_device_stats *stats;
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700610 struct nlattr *attr;
Thomas Grafb60c5112006-08-04 23:05:34 -0700611
612 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
613 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800614 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700615
616 ifm = nlmsg_data(nlh);
617 ifm->ifi_family = AF_UNSPEC;
618 ifm->__ifi_pad = 0;
619 ifm->ifi_type = dev->type;
620 ifm->ifi_index = dev->ifindex;
621 ifm->ifi_flags = dev_get_flags(dev);
622 ifm->ifi_change = change;
623
624 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
625 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700626 NLA_PUT_U8(skb, IFLA_OPERSTATE,
627 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
628 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
629 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
630
631 if (dev->ifindex != dev->iflink)
632 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
633
634 if (dev->master)
635 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
636
Patrick McHardyaf356af2009-09-04 06:41:18 +0000637 if (dev->qdisc)
638 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800639
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700640 if (dev->ifalias)
641 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
642
Stefan Rompfb00055a2006-03-20 17:09:11 -0800643 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct rtnl_link_ifmap map = {
645 .mem_start = dev->mem_start,
646 .mem_end = dev->mem_end,
647 .base_addr = dev->base_addr,
648 .irq = dev->irq,
649 .dma = dev->dma,
650 .port = dev->if_port,
651 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700652 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700656 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
657 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700660 attr = nla_reserve(skb, IFLA_STATS,
661 sizeof(struct rtnl_link_stats));
662 if (attr == NULL)
663 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800665 stats = dev_get_stats(dev);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700666 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick McHardy38f7b872007-06-13 12:03:51 -0700668 if (dev->rtnl_link_ops) {
669 if (rtnl_link_fill(skb, dev) < 0)
670 goto nla_put_failure;
671 }
672
Thomas Grafb60c5112006-08-04 23:05:34 -0700673 return nlmsg_end(skb, nlh);
674
675nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800676 nlmsg_cancel(skb, nlh);
677 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Thomas Grafb60c5112006-08-04 23:05:34 -0700680static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900682 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700683 int h, s_h;
684 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700686 struct hlist_head *head;
687 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700689 s_h = cb->args[0];
690 s_idx = cb->args[1];
691
692 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
693 idx = 0;
694 head = &net->dev_index_head[h];
695 hlist_for_each_entry(dev, node, head, index_hlist) {
696 if (idx < s_idx)
697 goto cont;
698 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
699 NETLINK_CB(cb->skb).pid,
700 cb->nlh->nlmsg_seq, 0,
701 NLM_F_MULTI) <= 0)
702 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700703cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700704 idx++;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700707out:
708 cb->args[1] = idx;
709 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 return skb->len;
712}
713
Pavel Emelianove7199282007-08-08 22:16:38 -0700714const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700715 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -0700716 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
717 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -0700718 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700719 [IFLA_MTU] = { .type = NLA_U32 },
Thomas Graf76e87302008-02-19 16:12:08 -0800720 [IFLA_LINK] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -0700721 [IFLA_TXQLEN] = { .type = NLA_U32 },
722 [IFLA_WEIGHT] = { .type = NLA_U32 },
723 [IFLA_OPERSTATE] = { .type = NLA_U8 },
724 [IFLA_LINKMODE] = { .type = NLA_U8 },
Thomas Graf76e87302008-02-19 16:12:08 -0800725 [IFLA_LINKINFO] = { .type = NLA_NESTED },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200726 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700727 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
Thomas Grafda5e0492006-08-10 21:17:37 -0700728};
Eric Dumazete0d087a2009-11-07 01:26:17 -0800729EXPORT_SYMBOL(ifla_policy);
Thomas Grafda5e0492006-08-10 21:17:37 -0700730
Patrick McHardy38f7b872007-06-13 12:03:51 -0700731static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
732 [IFLA_INFO_KIND] = { .type = NLA_STRING },
733 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
734};
735
Thomas Graf1840bb12008-02-23 19:54:36 -0800736static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
737{
738 if (dev) {
739 if (tb[IFLA_ADDRESS] &&
740 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
741 return -EINVAL;
742
743 if (tb[IFLA_BROADCAST] &&
744 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
745 return -EINVAL;
746 }
747
748 return 0;
749}
750
Patrick McHardy0157f602007-06-13 12:03:36 -0700751static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -0700752 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -0700753{
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800754 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700755 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -0700756 int err;
757
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200758 if (tb[IFLA_NET_NS_PID]) {
759 struct net *net;
760 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
761 if (IS_ERR(net)) {
762 err = PTR_ERR(net);
763 goto errout;
764 }
765 err = dev_change_net_namespace(dev, net, ifname);
766 put_net(net);
767 if (err)
768 goto errout;
769 modified = 1;
770 }
771
Patrick McHardy0157f602007-06-13 12:03:36 -0700772 if (tb[IFLA_MAP]) {
773 struct rtnl_link_ifmap *u_map;
774 struct ifmap k_map;
775
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800776 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -0700777 err = -EOPNOTSUPP;
778 goto errout;
779 }
780
781 if (!netif_device_present(dev)) {
782 err = -ENODEV;
783 goto errout;
784 }
785
786 u_map = nla_data(tb[IFLA_MAP]);
787 k_map.mem_start = (unsigned long) u_map->mem_start;
788 k_map.mem_end = (unsigned long) u_map->mem_end;
789 k_map.base_addr = (unsigned short) u_map->base_addr;
790 k_map.irq = (unsigned char) u_map->irq;
791 k_map.dma = (unsigned char) u_map->dma;
792 k_map.port = (unsigned char) u_map->port;
793
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800794 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -0700795 if (err < 0)
796 goto errout;
797
798 modified = 1;
799 }
800
801 if (tb[IFLA_ADDRESS]) {
802 struct sockaddr *sa;
803 int len;
804
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800805 if (!ops->ndo_set_mac_address) {
Patrick McHardy0157f602007-06-13 12:03:36 -0700806 err = -EOPNOTSUPP;
807 goto errout;
808 }
809
810 if (!netif_device_present(dev)) {
811 err = -ENODEV;
812 goto errout;
813 }
814
815 len = sizeof(sa_family_t) + dev->addr_len;
816 sa = kmalloc(len, GFP_KERNEL);
817 if (!sa) {
818 err = -ENOMEM;
819 goto errout;
820 }
821 sa->sa_family = dev->type;
822 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
823 dev->addr_len);
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800824 err = ops->ndo_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -0700825 kfree(sa);
826 if (err)
827 goto errout;
828 send_addr_notify = 1;
829 modified = 1;
830 }
831
832 if (tb[IFLA_MTU]) {
833 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
834 if (err < 0)
835 goto errout;
836 modified = 1;
837 }
838
839 /*
840 * Interface selected by interface index but interface
841 * name provided implies that a name change has been
842 * requested.
843 */
844 if (ifm->ifi_index > 0 && ifname[0]) {
845 err = dev_change_name(dev, ifname);
846 if (err < 0)
847 goto errout;
848 modified = 1;
849 }
850
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700851 if (tb[IFLA_IFALIAS]) {
852 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
853 nla_len(tb[IFLA_IFALIAS]));
854 if (err < 0)
855 goto errout;
856 modified = 1;
857 }
858
Patrick McHardy0157f602007-06-13 12:03:36 -0700859 if (tb[IFLA_BROADCAST]) {
860 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
861 send_addr_notify = 1;
862 }
863
864 if (ifm->ifi_flags || ifm->ifi_change) {
865 unsigned int flags = ifm->ifi_flags;
866
867 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
868 if (ifm->ifi_change)
869 flags = (flags & ifm->ifi_change) |
870 (dev->flags & ~ifm->ifi_change);
Johannes Berg5f9021c2008-11-16 23:20:31 -0800871 err = dev_change_flags(dev, flags);
872 if (err < 0)
873 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -0700874 }
875
David S. Miller93b2d4a2008-02-17 18:35:07 -0800876 if (tb[IFLA_TXQLEN])
877 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
Patrick McHardy0157f602007-06-13 12:03:36 -0700878
Patrick McHardy0157f602007-06-13 12:03:36 -0700879 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -0800880 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -0700881
882 if (tb[IFLA_LINKMODE]) {
David S. Miller93b2d4a2008-02-17 18:35:07 -0800883 write_lock_bh(&dev_base_lock);
884 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
885 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -0700886 }
887
888 err = 0;
889
890errout:
891 if (err < 0 && modified && net_ratelimit())
892 printk(KERN_WARNING "A link change request failed with "
893 "some changes comitted already. Interface %s may "
894 "have been left with an inconsistent configuration, "
895 "please check.\n", dev->name);
896
897 if (send_addr_notify)
898 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
899 return err;
900}
901
Thomas Grafda5e0492006-08-10 21:17:37 -0700902static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900904 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -0700905 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700907 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700908 struct nlattr *tb[IFLA_MAX+1];
909 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Thomas Grafda5e0492006-08-10 21:17:37 -0700911 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
912 if (err < 0)
913 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Thomas Graf5176f912006-08-26 20:13:18 -0700915 if (tb[IFLA_IFNAME])
916 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700917 else
918 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700921 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700922 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +0000923 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -0700924 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +0000925 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -0700926 else
927 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Thomas Grafda5e0492006-08-10 21:17:37 -0700929 if (dev == NULL) {
930 err = -ENODEV;
931 goto errout;
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Dumazete0d087a2009-11-07 01:26:17 -0800934 err = validate_linkmsg(dev, tb);
935 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +0000936 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -0700937
Patrick McHardy38f7b872007-06-13 12:03:51 -0700938 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -0700939errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return err;
941}
942
Patrick McHardy38f7b872007-06-13 12:03:51 -0700943static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
944{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900945 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700946 const struct rtnl_link_ops *ops;
947 struct net_device *dev;
948 struct ifinfomsg *ifm;
949 char ifname[IFNAMSIZ];
950 struct nlattr *tb[IFLA_MAX+1];
951 int err;
952
953 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
954 if (err < 0)
955 return err;
956
957 if (tb[IFLA_IFNAME])
958 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
959
960 ifm = nlmsg_data(nlh);
961 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700962 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700963 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -0700964 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700965 else
966 return -EINVAL;
967
968 if (!dev)
969 return -ENODEV;
970
971 ops = dev->rtnl_link_ops;
972 if (!ops)
973 return -EOPNOTSUPP;
974
Eric Dumazet23289a32009-10-27 07:06:36 +0000975 ops->dellink(dev, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700976 return 0;
977}
978
Eric W. Biederman881d9662007-09-17 11:56:21 -0700979struct net_device *rtnl_create_link(struct net *net, char *ifname,
Pavel Emelianove7199282007-08-08 22:16:38 -0700980 const struct rtnl_link_ops *ops, struct nlattr *tb[])
981{
982 int err;
983 struct net_device *dev;
Eric Dumazet2e59af32009-09-02 18:03:00 -0700984 unsigned int num_queues = 1;
985 unsigned int real_num_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -0700986
Eric Dumazet2e59af32009-09-02 18:03:00 -0700987 if (ops->get_tx_queues) {
Eric Dumazete0d087a2009-11-07 01:26:17 -0800988 err = ops->get_tx_queues(net, tb, &num_queues,
989 &real_num_queues);
Eric Dumazet2e59af32009-09-02 18:03:00 -0700990 if (err)
991 goto err;
992 }
Pavel Emelianove7199282007-08-08 22:16:38 -0700993 err = -ENOMEM;
Eric Dumazet2e59af32009-09-02 18:03:00 -0700994 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -0700995 if (!dev)
996 goto err;
997
Eric Dumazet2e59af32009-09-02 18:03:00 -0700998 dev->real_num_tx_queues = real_num_queues;
Pavel Emelianove7199282007-08-08 22:16:38 -0700999 if (strchr(dev->name, '%')) {
1000 err = dev_alloc_name(dev, dev->name);
1001 if (err < 0)
1002 goto err_free;
1003 }
1004
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001005 dev_net_set(dev, net);
Pavel Emelianove7199282007-08-08 22:16:38 -07001006 dev->rtnl_link_ops = ops;
1007
1008 if (tb[IFLA_MTU])
1009 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1010 if (tb[IFLA_ADDRESS])
1011 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1012 nla_len(tb[IFLA_ADDRESS]));
1013 if (tb[IFLA_BROADCAST])
1014 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1015 nla_len(tb[IFLA_BROADCAST]));
1016 if (tb[IFLA_TXQLEN])
1017 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1018 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001019 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001020 if (tb[IFLA_LINKMODE])
1021 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1022
1023 return dev;
1024
1025err_free:
1026 free_netdev(dev);
1027err:
1028 return ERR_PTR(err);
1029}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001030EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001031
Patrick McHardy38f7b872007-06-13 12:03:51 -07001032static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1033{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001034 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001035 const struct rtnl_link_ops *ops;
1036 struct net_device *dev;
1037 struct ifinfomsg *ifm;
1038 char kind[MODULE_NAME_LEN];
1039 char ifname[IFNAMSIZ];
1040 struct nlattr *tb[IFLA_MAX+1];
1041 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1042 int err;
1043
Johannes Berg95a5afc2008-10-16 15:24:51 -07001044#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001045replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001046#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001047 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1048 if (err < 0)
1049 return err;
1050
1051 if (tb[IFLA_IFNAME])
1052 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1053 else
1054 ifname[0] = '\0';
1055
1056 ifm = nlmsg_data(nlh);
1057 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001058 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001059 else if (ifname[0])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001060 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001061 else
1062 dev = NULL;
1063
Eric Dumazete0d087a2009-11-07 01:26:17 -08001064 err = validate_linkmsg(dev, tb);
1065 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001066 return err;
1067
Patrick McHardy38f7b872007-06-13 12:03:51 -07001068 if (tb[IFLA_LINKINFO]) {
1069 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1070 tb[IFLA_LINKINFO], ifla_info_policy);
1071 if (err < 0)
1072 return err;
1073 } else
1074 memset(linkinfo, 0, sizeof(linkinfo));
1075
1076 if (linkinfo[IFLA_INFO_KIND]) {
1077 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1078 ops = rtnl_link_ops_get(kind);
1079 } else {
1080 kind[0] = '\0';
1081 ops = NULL;
1082 }
1083
1084 if (1) {
1085 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1086
1087 if (ops) {
1088 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1089 err = nla_parse_nested(attr, ops->maxtype,
1090 linkinfo[IFLA_INFO_DATA],
1091 ops->policy);
1092 if (err < 0)
1093 return err;
1094 data = attr;
1095 }
1096 if (ops->validate) {
1097 err = ops->validate(tb, data);
1098 if (err < 0)
1099 return err;
1100 }
1101 }
1102
1103 if (dev) {
1104 int modified = 0;
1105
1106 if (nlh->nlmsg_flags & NLM_F_EXCL)
1107 return -EEXIST;
1108 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1109 return -EOPNOTSUPP;
1110
1111 if (linkinfo[IFLA_INFO_DATA]) {
1112 if (!ops || ops != dev->rtnl_link_ops ||
1113 !ops->changelink)
1114 return -EOPNOTSUPP;
1115
1116 err = ops->changelink(dev, tb, data);
1117 if (err < 0)
1118 return err;
1119 modified = 1;
1120 }
1121
1122 return do_setlink(dev, ifm, tb, ifname, modified);
1123 }
1124
1125 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1126 return -ENODEV;
1127
1128 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1129 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001130 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001131 return -EOPNOTSUPP;
1132
1133 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001134#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001135 if (kind[0]) {
1136 __rtnl_unlock();
1137 request_module("rtnl-link-%s", kind);
1138 rtnl_lock();
1139 ops = rtnl_link_ops_get(kind);
1140 if (ops)
1141 goto replay;
1142 }
1143#endif
1144 return -EOPNOTSUPP;
1145 }
1146
1147 if (!ifname[0])
1148 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001149
Eric W. Biederman881d9662007-09-17 11:56:21 -07001150 dev = rtnl_create_link(net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001151
Pavel Emelianove7199282007-08-08 22:16:38 -07001152 if (IS_ERR(dev))
1153 err = PTR_ERR(dev);
1154 else if (ops->newlink)
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001155 err = ops->newlink(dev, tb, data);
1156 else
1157 err = register_netdevice(dev);
Pavel Emelianove7199282007-08-08 22:16:38 -07001158
1159 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001160 free_netdev(dev);
1161 return err;
1162 }
1163}
1164
Thomas Grafb60c5112006-08-04 23:05:34 -07001165static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001166{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001167 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07001168 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00001169 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07001170 struct nlattr *tb[IFLA_MAX+1];
1171 struct net_device *dev = NULL;
1172 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001173 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001174
Thomas Grafb60c5112006-08-04 23:05:34 -07001175 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1176 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001177 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001178
Eric Dumazeta3d12892009-10-21 10:59:31 +00001179 if (tb[IFLA_IFNAME])
1180 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1181
Thomas Grafb60c5112006-08-04 23:05:34 -07001182 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001183 if (ifm->ifi_index > 0)
1184 dev = __dev_get_by_index(net, ifm->ifi_index);
1185 else if (tb[IFLA_IFNAME])
1186 dev = __dev_get_by_name(net, ifname);
1187 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001188 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001189
Eric Dumazeta3d12892009-10-21 10:59:31 +00001190 if (dev == NULL)
1191 return -ENODEV;
1192
Patrick McHardy38f7b872007-06-13 12:03:51 -07001193 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001194 if (nskb == NULL)
1195 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001196
Patrick McHardy575c3e22007-05-22 17:00:49 -07001197 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1198 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001199 if (err < 0) {
1200 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1201 WARN_ON(err == -EMSGSIZE);
1202 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001203 } else
1204 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001205
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001206 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001207}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001208
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001209static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 int idx;
1212 int s_idx = cb->family;
1213
1214 if (s_idx == 0)
1215 s_idx = 1;
Eric Dumazete0d087a2009-11-07 01:26:17 -08001216 for (idx = 1; idx < NPROTO; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int type = cb->nlh->nlmsg_type-RTM_BASE;
1218 if (idx < s_idx || idx == PF_PACKET)
1219 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001220 if (rtnl_msg_handlers[idx] == NULL ||
1221 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 continue;
1223 if (idx > s_idx)
1224 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001225 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
1227 }
1228 cb->family = idx;
1229
1230 return skb->len;
1231}
1232
1233void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1234{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001235 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001237 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Patrick McHardy38f7b872007-06-13 12:03:51 -07001239 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001240 if (skb == NULL)
1241 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Patrick McHardy575c3e22007-05-22 17:00:49 -07001243 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001244 if (err < 0) {
1245 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1246 WARN_ON(err == -EMSGSIZE);
1247 kfree_skb(skb);
1248 goto errout;
1249 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001250 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1251 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001252errout:
1253 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08001254 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/* Protected by RTNL sempahore. */
1258static struct rtattr **rta_buf;
1259static int rtattr_max;
1260
1261/* Process one rtnetlink message. */
1262
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001263static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001265 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07001266 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int sz_idx, kind;
1268 int min_len;
1269 int family;
1270 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001271 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001275 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 type -= RTM_BASE;
1278
1279 /* All the messages must have at least 1 byte length */
1280 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1281 return 0;
1282
Eric Dumazete0d087a2009-11-07 01:26:17 -08001283 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001284 if (family >= NPROTO)
1285 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 sz_idx = type>>2;
1288 kind = type&3;
1289
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001290 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1291 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001294 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07001295 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Thomas Grafe2849862007-03-22 11:48:11 -07001297 dumpit = rtnl_get_dumpit(family, type);
1298 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001299 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001301 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001302 rtnl = net->rtnl;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001303 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1304 rtnl_lock();
1305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1309
1310 min_len = rtm_min[sz_idx];
1311 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 if (nlh->nlmsg_len > min_len) {
1315 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Eric Dumazete0d087a2009-11-07 01:26:17 -08001316 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 while (RTA_OK(attr, attrlen)) {
1319 unsigned flavor = attr->rta_type;
1320 if (flavor) {
1321 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 rta_buf[flavor-1] = attr;
1324 }
1325 attr = RTA_NEXT(attr, attrlen);
1326 }
1327 }
1328
Thomas Grafe2849862007-03-22 11:48:11 -07001329 doit = rtnl_get_doit(family, type);
1330 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001331 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001333 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001336static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001338 rtnl_lock();
1339 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1340 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1344{
1345 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 switch (event) {
1348 case NETDEV_UNREGISTER:
1349 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1350 break;
1351 case NETDEV_REGISTER:
1352 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1353 break;
1354 case NETDEV_UP:
1355 case NETDEV_DOWN:
1356 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1357 break;
1358 case NETDEV_CHANGE:
1359 case NETDEV_GOING_DOWN:
1360 break;
1361 default:
1362 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1363 break;
1364 }
1365 return NOTIFY_DONE;
1366}
1367
1368static struct notifier_block rtnetlink_dev_notifier = {
1369 .notifier_call = rtnetlink_event,
1370};
1371
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001372
1373static int rtnetlink_net_init(struct net *net)
1374{
1375 struct sock *sk;
1376 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1377 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1378 if (!sk)
1379 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001380 net->rtnl = sk;
1381 return 0;
1382}
1383
1384static void rtnetlink_net_exit(struct net *net)
1385{
Denis V. Lunev775516b2008-01-18 23:55:19 -08001386 netlink_kernel_release(net->rtnl);
1387 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001388}
1389
1390static struct pernet_operations rtnetlink_net_ops = {
1391 .init = rtnetlink_net_init,
1392 .exit = rtnetlink_net_exit,
1393};
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395void __init rtnetlink_init(void)
1396{
1397 int i;
1398
1399 rtattr_max = 0;
1400 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1401 if (rta_max[i] > rtattr_max)
1402 rtattr_max = rta_max[i];
1403 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1404 if (!rta_buf)
1405 panic("rtnetlink_init: cannot allocate rta_buf\n");
1406
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001407 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1411 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07001412
1413 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1414 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001415 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1416 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07001417
1418 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1419 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421