blob: 4c7d3f635ba7ab97dbc76b55f000ccb398bd2dbc [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
Paul E. McKenneya898def2010-02-22 17:04:49 -080092#ifdef CONFIG_PROVE_LOCKING
93int lockdep_rtnl_is_held(void)
94{
95 return lockdep_is_held(&rtnl_mutex);
96}
97EXPORT_SYMBOL(lockdep_rtnl_is_held);
98#endif /* #ifdef CONFIG_PROVE_LOCKING */
99
Adrian Bunk42bad1d2007-04-26 00:57:41 -0700100static struct rtnl_link *rtnl_msg_handlers[NPROTO];
Thomas Grafe2849862007-03-22 11:48:11 -0700101
102static inline int rtm_msgindex(int msgtype)
103{
104 int msgindex = msgtype - RTM_BASE;
105
106 /*
107 * msgindex < 0 implies someone tried to register a netlink
108 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
109 * the message type has not been added to linux/rtnetlink.h
110 */
111 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
112
113 return msgindex;
114}
115
116static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
117{
118 struct rtnl_link *tab;
119
120 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700121 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700122 tab = rtnl_msg_handlers[PF_UNSPEC];
123
Thomas Graf51057f22007-03-22 21:41:06 -0700124 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700125}
126
127static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
128{
129 struct rtnl_link *tab;
130
131 tab = rtnl_msg_handlers[protocol];
Thomas Graf51057f22007-03-22 21:41:06 -0700132 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700133 tab = rtnl_msg_handlers[PF_UNSPEC];
134
Thomas Graf51057f22007-03-22 21:41:06 -0700135 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700136}
137
138/**
139 * __rtnl_register - Register a rtnetlink message type
140 * @protocol: Protocol family or PF_UNSPEC
141 * @msgtype: rtnetlink message type
142 * @doit: Function pointer called for each request message
143 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
144 *
145 * Registers the specified function pointers (at least one of them has
146 * to be non-NULL) to be called whenever a request message for the
147 * specified protocol family and message type is received.
148 *
149 * The special protocol family PF_UNSPEC may be used to define fallback
150 * function pointers for the case when no entry for the specific protocol
151 * family exists.
152 *
153 * Returns 0 on success or a negative error code.
154 */
155int __rtnl_register(int protocol, int msgtype,
156 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
157{
158 struct rtnl_link *tab;
159 int msgindex;
160
161 BUG_ON(protocol < 0 || protocol >= NPROTO);
162 msgindex = rtm_msgindex(msgtype);
163
164 tab = rtnl_msg_handlers[protocol];
165 if (tab == NULL) {
166 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
167 if (tab == NULL)
168 return -ENOBUFS;
169
170 rtnl_msg_handlers[protocol] = tab;
171 }
172
173 if (doit)
174 tab[msgindex].doit = doit;
175
176 if (dumpit)
177 tab[msgindex].dumpit = dumpit;
178
179 return 0;
180}
Thomas Grafe2849862007-03-22 11:48:11 -0700181EXPORT_SYMBOL_GPL(__rtnl_register);
182
183/**
184 * rtnl_register - Register a rtnetlink message type
185 *
186 * Identical to __rtnl_register() but panics on failure. This is useful
187 * as failure of this function is very unlikely, it can only happen due
188 * to lack of memory when allocating the chain to store all message
189 * handlers for a protocol. Meant for use in init functions where lack
190 * of memory implies no sense in continueing.
191 */
192void rtnl_register(int protocol, int msgtype,
193 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
194{
195 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
196 panic("Unable to register rtnetlink message handler, "
197 "protocol = %d, message type = %d\n",
198 protocol, msgtype);
199}
Thomas Grafe2849862007-03-22 11:48:11 -0700200EXPORT_SYMBOL_GPL(rtnl_register);
201
202/**
203 * rtnl_unregister - Unregister a rtnetlink message type
204 * @protocol: Protocol family or PF_UNSPEC
205 * @msgtype: rtnetlink message type
206 *
207 * Returns 0 on success or a negative error code.
208 */
209int rtnl_unregister(int protocol, int msgtype)
210{
211 int msgindex;
212
213 BUG_ON(protocol < 0 || protocol >= NPROTO);
214 msgindex = rtm_msgindex(msgtype);
215
216 if (rtnl_msg_handlers[protocol] == NULL)
217 return -ENOENT;
218
219 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
220 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
221
222 return 0;
223}
Thomas Grafe2849862007-03-22 11:48:11 -0700224EXPORT_SYMBOL_GPL(rtnl_unregister);
225
226/**
227 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
228 * @protocol : Protocol family or PF_UNSPEC
229 *
230 * Identical to calling rtnl_unregster() for all registered message types
231 * of a certain protocol family.
232 */
233void rtnl_unregister_all(int protocol)
234{
235 BUG_ON(protocol < 0 || protocol >= NPROTO);
236
237 kfree(rtnl_msg_handlers[protocol]);
238 rtnl_msg_handlers[protocol] = NULL;
239}
Thomas Grafe2849862007-03-22 11:48:11 -0700240EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Patrick McHardy38f7b872007-06-13 12:03:51 -0700242static LIST_HEAD(link_ops);
243
244/**
245 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
246 * @ops: struct rtnl_link_ops * to register
247 *
248 * The caller must hold the rtnl_mutex. This function should be used
249 * by drivers that create devices during module initialization. It
250 * must be called before registering the devices.
251 *
252 * Returns 0 on success or a negative error code.
253 */
254int __rtnl_link_register(struct rtnl_link_ops *ops)
255{
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700256 if (!ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000257 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700258
Patrick McHardy38f7b872007-06-13 12:03:51 -0700259 list_add_tail(&ops->list, &link_ops);
260 return 0;
261}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700262EXPORT_SYMBOL_GPL(__rtnl_link_register);
263
264/**
265 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
266 * @ops: struct rtnl_link_ops * to register
267 *
268 * Returns 0 on success or a negative error code.
269 */
270int rtnl_link_register(struct rtnl_link_ops *ops)
271{
272 int err;
273
274 rtnl_lock();
275 err = __rtnl_link_register(ops);
276 rtnl_unlock();
277 return err;
278}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700279EXPORT_SYMBOL_GPL(rtnl_link_register);
280
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700281static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
282{
283 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000284 LIST_HEAD(list_kill);
285
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700286 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000287 if (dev->rtnl_link_ops == ops)
288 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700289 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000290 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700291}
292
293void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
294{
295 rtnl_lock();
296 __rtnl_kill_links(net, ops);
297 rtnl_unlock();
298}
299EXPORT_SYMBOL_GPL(rtnl_kill_links);
300
Patrick McHardy38f7b872007-06-13 12:03:51 -0700301/**
302 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
303 * @ops: struct rtnl_link_ops * to unregister
304 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700305 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700306 */
307void __rtnl_link_unregister(struct rtnl_link_ops *ops)
308{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700309 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700310
Eric W. Biederman881d9662007-09-17 11:56:21 -0700311 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700312 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700313 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700314 list_del(&ops->list);
315}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700316EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
317
318/**
319 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
320 * @ops: struct rtnl_link_ops * to unregister
321 */
322void rtnl_link_unregister(struct rtnl_link_ops *ops)
323{
324 rtnl_lock();
325 __rtnl_link_unregister(ops);
326 rtnl_unlock();
327}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700328EXPORT_SYMBOL_GPL(rtnl_link_unregister);
329
330static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
331{
332 const struct rtnl_link_ops *ops;
333
334 list_for_each_entry(ops, &link_ops, list) {
335 if (!strcmp(ops->kind, kind))
336 return ops;
337 }
338 return NULL;
339}
340
341static size_t rtnl_link_get_size(const struct net_device *dev)
342{
343 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
344 size_t size;
345
346 if (!ops)
347 return 0;
348
349 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
350 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
351
352 if (ops->get_size)
353 /* IFLA_INFO_DATA + nested data */
354 size += nlmsg_total_size(sizeof(struct nlattr)) +
355 ops->get_size(dev);
356
357 if (ops->get_xstats_size)
358 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
359
360 return size;
361}
362
363static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
364{
365 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
366 struct nlattr *linkinfo, *data;
367 int err = -EMSGSIZE;
368
369 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
370 if (linkinfo == NULL)
371 goto out;
372
373 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
374 goto err_cancel_link;
375 if (ops->fill_xstats) {
376 err = ops->fill_xstats(skb, dev);
377 if (err < 0)
378 goto err_cancel_link;
379 }
380 if (ops->fill_info) {
381 data = nla_nest_start(skb, IFLA_INFO_DATA);
382 if (data == NULL)
383 goto err_cancel_link;
384 err = ops->fill_info(skb, dev);
385 if (err < 0)
386 goto err_cancel_data;
387 nla_nest_end(skb, data);
388 }
389
390 nla_nest_end(skb, linkinfo);
391 return 0;
392
393err_cancel_data:
394 nla_nest_cancel(skb, data);
395err_cancel_link:
396 nla_nest_cancel(skb, linkinfo);
397out:
398 return err;
399}
400
Thomas Grafdb46edc2005-05-03 14:29:39 -0700401static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Thomas Graff90a0a72005-05-03 14:29:00 -0700403 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
404 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
405 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700406 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700407 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
408 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
409 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
410 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700411 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
412 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413};
414
Thomas Grafdb46edc2005-05-03 14:29:39 -0700415static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Thomas Graff90a0a72005-05-03 14:29:00 -0700417 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
418 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
419 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700420 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700421 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
422 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
423 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
424 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
427void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
428{
429 struct rtattr *rta;
430 int size = RTA_LENGTH(attrlen);
431
Eric Dumazete0d087a2009-11-07 01:26:17 -0800432 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 rta->rta_type = attrtype;
434 rta->rta_len = size;
435 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700436 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800438EXPORT_SYMBOL(__rta_fill);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800440int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800442 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int err = 0;
444
Patrick McHardyac6d4392005-08-14 19:29:52 -0700445 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (echo)
447 atomic_inc(&skb->users);
448 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
449 if (echo)
450 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
451 return err;
452}
453
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800454int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700455{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800456 struct sock *rtnl = net->rtnl;
457
Thomas Graf2942e902006-08-15 00:30:25 -0700458 return nlmsg_unicast(rtnl, skb, pid);
459}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800460EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700461
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800462void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
463 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700464{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800465 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700466 int report = 0;
467
468 if (nlh)
469 report = nlmsg_report(nlh);
470
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800471 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700472}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800473EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700474
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800475void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700476{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800477 struct sock *rtnl = net->rtnl;
478
Thomas Graf97676b62006-08-15 00:31:41 -0700479 netlink_set_err(rtnl, 0, group, error);
480}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800481EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
484{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700485 struct nlattr *mx;
486 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Thomas Graf2d7202b2006-08-22 00:01:27 -0700488 mx = nla_nest_start(skb, RTA_METRICS);
489 if (mx == NULL)
490 return -ENOBUFS;
491
492 for (i = 0; i < RTAX_MAX; i++) {
493 if (metrics[i]) {
494 valid++;
495 NLA_PUT_U32(skb, i+1, metrics[i]);
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David S. Millera57d27f2006-08-22 22:20:14 -0700499 if (!valid) {
500 nla_nest_cancel(skb, mx);
501 return 0;
502 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700503
504 return nla_nest_end(skb, mx);
505
506nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700507 nla_nest_cancel(skb, mx);
508 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800510EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Thomas Grafe3703b32006-11-27 09:27:07 -0800512int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
513 u32 ts, u32 tsage, long expires, u32 error)
514{
515 struct rta_cacheinfo ci = {
516 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
517 .rta_used = dst->__use,
518 .rta_clntref = atomic_read(&(dst->__refcnt)),
519 .rta_error = error,
520 .rta_id = id,
521 .rta_ts = ts,
522 .rta_tsage = tsage,
523 };
524
525 if (expires)
526 ci.rta_expires = jiffies_to_clock_t(expires);
527
528 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
529}
Thomas Grafe3703b32006-11-27 09:27:07 -0800530EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David S. Miller93b2d4a2008-02-17 18:35:07 -0800532static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800533{
534 unsigned char operstate = dev->operstate;
535
Eric Dumazete0d087a2009-11-07 01:26:17 -0800536 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800537 case IF_OPER_UP:
538 if ((operstate == IF_OPER_DORMANT ||
539 operstate == IF_OPER_UNKNOWN) &&
540 !netif_dormant(dev))
541 operstate = IF_OPER_UP;
542 break;
543
544 case IF_OPER_DORMANT:
545 if (operstate == IF_OPER_UP ||
546 operstate == IF_OPER_UNKNOWN)
547 operstate = IF_OPER_DORMANT;
548 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700549 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800550
551 if (dev->operstate != operstate) {
552 write_lock_bh(&dev_base_lock);
553 dev->operstate = operstate;
554 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800555 netdev_state_change(dev);
556 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800557}
558
Thomas Grafb60c5112006-08-04 23:05:34 -0700559static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800560 const struct net_device_stats *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Thomas Grafb60c5112006-08-04 23:05:34 -0700562 a->rx_packets = b->rx_packets;
563 a->tx_packets = b->tx_packets;
564 a->rx_bytes = b->rx_bytes;
565 a->tx_bytes = b->tx_bytes;
566 a->rx_errors = b->rx_errors;
567 a->tx_errors = b->tx_errors;
568 a->rx_dropped = b->rx_dropped;
569 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Thomas Grafb60c5112006-08-04 23:05:34 -0700571 a->multicast = b->multicast;
572 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Thomas Grafb60c5112006-08-04 23:05:34 -0700574 a->rx_length_errors = b->rx_length_errors;
575 a->rx_over_errors = b->rx_over_errors;
576 a->rx_crc_errors = b->rx_crc_errors;
577 a->rx_frame_errors = b->rx_frame_errors;
578 a->rx_fifo_errors = b->rx_fifo_errors;
579 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Thomas Grafb60c5112006-08-04 23:05:34 -0700581 a->tx_aborted_errors = b->tx_aborted_errors;
582 a->tx_carrier_errors = b->tx_carrier_errors;
583 a->tx_fifo_errors = b->tx_fifo_errors;
584 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
585 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Thomas Grafb60c5112006-08-04 23:05:34 -0700587 a->rx_compressed = b->rx_compressed;
588 a->tx_compressed = b->tx_compressed;
589};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Patrick McHardy38f7b872007-06-13 12:03:51 -0700591static inline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800592{
593 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
594 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700595 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800596 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
597 + nla_total_size(sizeof(struct rtnl_link_ifmap))
598 + nla_total_size(sizeof(struct rtnl_link_stats))
599 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
600 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
601 + nla_total_size(4) /* IFLA_TXQLEN */
602 + nla_total_size(4) /* IFLA_WEIGHT */
603 + nla_total_size(4) /* IFLA_MTU */
604 + nla_total_size(4) /* IFLA_LINK */
605 + nla_total_size(4) /* IFLA_MASTER */
606 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700607 + nla_total_size(1) /* IFLA_LINKMODE */
608 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
Thomas Graf339bf982006-11-10 14:10:15 -0800609}
610
Thomas Grafb60c5112006-08-04 23:05:34 -0700611static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700612 int type, u32 pid, u32 seq, u32 change,
613 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700614{
615 struct ifinfomsg *ifm;
616 struct nlmsghdr *nlh;
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800617 const struct net_device_stats *stats;
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700618 struct nlattr *attr;
Thomas Grafb60c5112006-08-04 23:05:34 -0700619
620 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
621 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800622 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700623
624 ifm = nlmsg_data(nlh);
625 ifm->ifi_family = AF_UNSPEC;
626 ifm->__ifi_pad = 0;
627 ifm->ifi_type = dev->type;
628 ifm->ifi_index = dev->ifindex;
629 ifm->ifi_flags = dev_get_flags(dev);
630 ifm->ifi_change = change;
631
632 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
633 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700634 NLA_PUT_U8(skb, IFLA_OPERSTATE,
635 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
636 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
637 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
638
639 if (dev->ifindex != dev->iflink)
640 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
641
642 if (dev->master)
643 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
644
Patrick McHardyaf356af2009-09-04 06:41:18 +0000645 if (dev->qdisc)
646 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800647
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700648 if (dev->ifalias)
649 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
650
Stefan Rompfb00055a2006-03-20 17:09:11 -0800651 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct rtnl_link_ifmap map = {
653 .mem_start = dev->mem_start,
654 .mem_end = dev->mem_end,
655 .base_addr = dev->base_addr,
656 .irq = dev->irq,
657 .dma = dev->dma,
658 .port = dev->if_port,
659 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700660 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700664 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
665 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700668 attr = nla_reserve(skb, IFLA_STATS,
669 sizeof(struct rtnl_link_stats));
670 if (attr == NULL)
671 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -0800673 stats = dev_get_stats(dev);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700674 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Patrick McHardy38f7b872007-06-13 12:03:51 -0700676 if (dev->rtnl_link_ops) {
677 if (rtnl_link_fill(skb, dev) < 0)
678 goto nla_put_failure;
679 }
680
Thomas Grafb60c5112006-08-04 23:05:34 -0700681 return nlmsg_end(skb, nlh);
682
683nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800684 nlmsg_cancel(skb, nlh);
685 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Thomas Grafb60c5112006-08-04 23:05:34 -0700688static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900690 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700691 int h, s_h;
692 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700694 struct hlist_head *head;
695 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700697 s_h = cb->args[0];
698 s_idx = cb->args[1];
699
700 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
701 idx = 0;
702 head = &net->dev_index_head[h];
703 hlist_for_each_entry(dev, node, head, index_hlist) {
704 if (idx < s_idx)
705 goto cont;
706 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
707 NETLINK_CB(cb->skb).pid,
708 cb->nlh->nlmsg_seq, 0,
709 NLM_F_MULTI) <= 0)
710 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -0700711cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700712 idx++;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -0700715out:
716 cb->args[1] = idx;
717 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return skb->len;
720}
721
Pavel Emelianove7199282007-08-08 22:16:38 -0700722const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -0700723 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -0700724 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
725 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -0700726 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -0700727 [IFLA_MTU] = { .type = NLA_U32 },
Thomas Graf76e87302008-02-19 16:12:08 -0800728 [IFLA_LINK] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -0700729 [IFLA_TXQLEN] = { .type = NLA_U32 },
730 [IFLA_WEIGHT] = { .type = NLA_U32 },
731 [IFLA_OPERSTATE] = { .type = NLA_U8 },
732 [IFLA_LINKMODE] = { .type = NLA_U8 },
Thomas Graf76e87302008-02-19 16:12:08 -0800733 [IFLA_LINKINFO] = { .type = NLA_NESTED },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200734 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700735 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
Thomas Grafda5e0492006-08-10 21:17:37 -0700736};
Eric Dumazete0d087a2009-11-07 01:26:17 -0800737EXPORT_SYMBOL(ifla_policy);
Thomas Grafda5e0492006-08-10 21:17:37 -0700738
Patrick McHardy38f7b872007-06-13 12:03:51 -0700739static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
740 [IFLA_INFO_KIND] = { .type = NLA_STRING },
741 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
742};
743
Eric W. Biederman81adee42009-11-08 00:53:51 -0800744struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
745{
746 struct net *net;
747 /* Examine the link attributes and figure out which
748 * network namespace we are talking about.
749 */
750 if (tb[IFLA_NET_NS_PID])
751 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
752 else
753 net = get_net(src_net);
754 return net;
755}
756EXPORT_SYMBOL(rtnl_link_get_net);
757
Thomas Graf1840bb12008-02-23 19:54:36 -0800758static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
759{
760 if (dev) {
761 if (tb[IFLA_ADDRESS] &&
762 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
763 return -EINVAL;
764
765 if (tb[IFLA_BROADCAST] &&
766 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
767 return -EINVAL;
768 }
769
770 return 0;
771}
772
Patrick McHardy0157f602007-06-13 12:03:36 -0700773static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -0700774 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -0700775{
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800776 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700777 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -0700778 int err;
779
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200780 if (tb[IFLA_NET_NS_PID]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -0800781 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +0200782 if (IS_ERR(net)) {
783 err = PTR_ERR(net);
784 goto errout;
785 }
786 err = dev_change_net_namespace(dev, net, ifname);
787 put_net(net);
788 if (err)
789 goto errout;
790 modified = 1;
791 }
792
Patrick McHardy0157f602007-06-13 12:03:36 -0700793 if (tb[IFLA_MAP]) {
794 struct rtnl_link_ifmap *u_map;
795 struct ifmap k_map;
796
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800797 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -0700798 err = -EOPNOTSUPP;
799 goto errout;
800 }
801
802 if (!netif_device_present(dev)) {
803 err = -ENODEV;
804 goto errout;
805 }
806
807 u_map = nla_data(tb[IFLA_MAP]);
808 k_map.mem_start = (unsigned long) u_map->mem_start;
809 k_map.mem_end = (unsigned long) u_map->mem_end;
810 k_map.base_addr = (unsigned short) u_map->base_addr;
811 k_map.irq = (unsigned char) u_map->irq;
812 k_map.dma = (unsigned char) u_map->dma;
813 k_map.port = (unsigned char) u_map->port;
814
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800815 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -0700816 if (err < 0)
817 goto errout;
818
819 modified = 1;
820 }
821
822 if (tb[IFLA_ADDRESS]) {
823 struct sockaddr *sa;
824 int len;
825
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800826 if (!ops->ndo_set_mac_address) {
Patrick McHardy0157f602007-06-13 12:03:36 -0700827 err = -EOPNOTSUPP;
828 goto errout;
829 }
830
831 if (!netif_device_present(dev)) {
832 err = -ENODEV;
833 goto errout;
834 }
835
836 len = sizeof(sa_family_t) + dev->addr_len;
837 sa = kmalloc(len, GFP_KERNEL);
838 if (!sa) {
839 err = -ENOMEM;
840 goto errout;
841 }
842 sa->sa_family = dev->type;
843 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
844 dev->addr_len);
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800845 err = ops->ndo_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -0700846 kfree(sa);
847 if (err)
848 goto errout;
849 send_addr_notify = 1;
850 modified = 1;
851 }
852
853 if (tb[IFLA_MTU]) {
854 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
855 if (err < 0)
856 goto errout;
857 modified = 1;
858 }
859
860 /*
861 * Interface selected by interface index but interface
862 * name provided implies that a name change has been
863 * requested.
864 */
865 if (ifm->ifi_index > 0 && ifname[0]) {
866 err = dev_change_name(dev, ifname);
867 if (err < 0)
868 goto errout;
869 modified = 1;
870 }
871
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700872 if (tb[IFLA_IFALIAS]) {
873 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
874 nla_len(tb[IFLA_IFALIAS]));
875 if (err < 0)
876 goto errout;
877 modified = 1;
878 }
879
Patrick McHardy0157f602007-06-13 12:03:36 -0700880 if (tb[IFLA_BROADCAST]) {
881 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
882 send_addr_notify = 1;
883 }
884
885 if (ifm->ifi_flags || ifm->ifi_change) {
886 unsigned int flags = ifm->ifi_flags;
887
888 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
889 if (ifm->ifi_change)
890 flags = (flags & ifm->ifi_change) |
891 (dev->flags & ~ifm->ifi_change);
Johannes Berg5f9021c2008-11-16 23:20:31 -0800892 err = dev_change_flags(dev, flags);
893 if (err < 0)
894 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -0700895 }
896
David S. Miller93b2d4a2008-02-17 18:35:07 -0800897 if (tb[IFLA_TXQLEN])
898 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
Patrick McHardy0157f602007-06-13 12:03:36 -0700899
Patrick McHardy0157f602007-06-13 12:03:36 -0700900 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -0800901 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -0700902
903 if (tb[IFLA_LINKMODE]) {
David S. Miller93b2d4a2008-02-17 18:35:07 -0800904 write_lock_bh(&dev_base_lock);
905 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
906 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -0700907 }
908
909 err = 0;
910
911errout:
912 if (err < 0 && modified && net_ratelimit())
913 printk(KERN_WARNING "A link change request failed with "
914 "some changes comitted already. Interface %s may "
915 "have been left with an inconsistent configuration, "
916 "please check.\n", dev->name);
917
918 if (send_addr_notify)
919 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
920 return err;
921}
922
Thomas Grafda5e0492006-08-10 21:17:37 -0700923static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900925 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -0700926 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -0700928 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -0700929 struct nlattr *tb[IFLA_MAX+1];
930 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Thomas Grafda5e0492006-08-10 21:17:37 -0700932 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
933 if (err < 0)
934 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Thomas Graf5176f912006-08-26 20:13:18 -0700936 if (tb[IFLA_IFNAME])
937 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -0700938 else
939 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -0700942 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -0700943 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +0000944 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -0700945 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +0000946 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -0700947 else
948 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Thomas Grafda5e0492006-08-10 21:17:37 -0700950 if (dev == NULL) {
951 err = -ENODEV;
952 goto errout;
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Eric Dumazete0d087a2009-11-07 01:26:17 -0800955 err = validate_linkmsg(dev, tb);
956 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +0000957 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -0700958
Patrick McHardy38f7b872007-06-13 12:03:51 -0700959 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -0700960errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return err;
962}
963
Patrick McHardy38f7b872007-06-13 12:03:51 -0700964static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
965{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900966 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700967 const struct rtnl_link_ops *ops;
968 struct net_device *dev;
969 struct ifinfomsg *ifm;
970 char ifname[IFNAMSIZ];
971 struct nlattr *tb[IFLA_MAX+1];
972 int err;
973
974 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
975 if (err < 0)
976 return err;
977
978 if (tb[IFLA_IFNAME])
979 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
980
981 ifm = nlmsg_data(nlh);
982 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700983 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700984 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -0700985 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700986 else
987 return -EINVAL;
988
989 if (!dev)
990 return -ENODEV;
991
992 ops = dev->rtnl_link_ops;
993 if (!ops)
994 return -EOPNOTSUPP;
995
Eric Dumazet23289a32009-10-27 07:06:36 +0000996 ops->dellink(dev, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700997 return 0;
998}
999
Eric W. Biederman81adee42009-11-08 00:53:51 -08001000struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1001 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001002{
1003 int err;
1004 struct net_device *dev;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001005 unsigned int num_queues = 1;
1006 unsigned int real_num_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001007
Eric Dumazet2e59af32009-09-02 18:03:00 -07001008 if (ops->get_tx_queues) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001009 err = ops->get_tx_queues(src_net, tb, &num_queues,
Eric Dumazete0d087a2009-11-07 01:26:17 -08001010 &real_num_queues);
Eric Dumazet2e59af32009-09-02 18:03:00 -07001011 if (err)
1012 goto err;
1013 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001014 err = -ENOMEM;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001015 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001016 if (!dev)
1017 goto err;
1018
Eric W. Biederman81adee42009-11-08 00:53:51 -08001019 dev_net_set(dev, net);
1020 dev->rtnl_link_ops = ops;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001021 dev->real_num_tx_queues = real_num_queues;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001022
Pavel Emelianove7199282007-08-08 22:16:38 -07001023 if (strchr(dev->name, '%')) {
1024 err = dev_alloc_name(dev, dev->name);
1025 if (err < 0)
1026 goto err_free;
1027 }
1028
Pavel Emelianove7199282007-08-08 22:16:38 -07001029 if (tb[IFLA_MTU])
1030 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1031 if (tb[IFLA_ADDRESS])
1032 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1033 nla_len(tb[IFLA_ADDRESS]));
1034 if (tb[IFLA_BROADCAST])
1035 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1036 nla_len(tb[IFLA_BROADCAST]));
1037 if (tb[IFLA_TXQLEN])
1038 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1039 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001040 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001041 if (tb[IFLA_LINKMODE])
1042 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1043
1044 return dev;
1045
1046err_free:
1047 free_netdev(dev);
1048err:
1049 return ERR_PTR(err);
1050}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001051EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001052
Patrick McHardy38f7b872007-06-13 12:03:51 -07001053static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1054{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001055 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001056 const struct rtnl_link_ops *ops;
1057 struct net_device *dev;
1058 struct ifinfomsg *ifm;
1059 char kind[MODULE_NAME_LEN];
1060 char ifname[IFNAMSIZ];
1061 struct nlattr *tb[IFLA_MAX+1];
1062 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1063 int err;
1064
Johannes Berg95a5afc2008-10-16 15:24:51 -07001065#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001066replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001067#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001068 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1069 if (err < 0)
1070 return err;
1071
1072 if (tb[IFLA_IFNAME])
1073 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1074 else
1075 ifname[0] = '\0';
1076
1077 ifm = nlmsg_data(nlh);
1078 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001079 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001080 else if (ifname[0])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001081 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001082 else
1083 dev = NULL;
1084
Eric Dumazete0d087a2009-11-07 01:26:17 -08001085 err = validate_linkmsg(dev, tb);
1086 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001087 return err;
1088
Patrick McHardy38f7b872007-06-13 12:03:51 -07001089 if (tb[IFLA_LINKINFO]) {
1090 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1091 tb[IFLA_LINKINFO], ifla_info_policy);
1092 if (err < 0)
1093 return err;
1094 } else
1095 memset(linkinfo, 0, sizeof(linkinfo));
1096
1097 if (linkinfo[IFLA_INFO_KIND]) {
1098 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1099 ops = rtnl_link_ops_get(kind);
1100 } else {
1101 kind[0] = '\0';
1102 ops = NULL;
1103 }
1104
1105 if (1) {
1106 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001107 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001108
1109 if (ops) {
1110 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1111 err = nla_parse_nested(attr, ops->maxtype,
1112 linkinfo[IFLA_INFO_DATA],
1113 ops->policy);
1114 if (err < 0)
1115 return err;
1116 data = attr;
1117 }
1118 if (ops->validate) {
1119 err = ops->validate(tb, data);
1120 if (err < 0)
1121 return err;
1122 }
1123 }
1124
1125 if (dev) {
1126 int modified = 0;
1127
1128 if (nlh->nlmsg_flags & NLM_F_EXCL)
1129 return -EEXIST;
1130 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1131 return -EOPNOTSUPP;
1132
1133 if (linkinfo[IFLA_INFO_DATA]) {
1134 if (!ops || ops != dev->rtnl_link_ops ||
1135 !ops->changelink)
1136 return -EOPNOTSUPP;
1137
1138 err = ops->changelink(dev, tb, data);
1139 if (err < 0)
1140 return err;
1141 modified = 1;
1142 }
1143
1144 return do_setlink(dev, ifm, tb, ifname, modified);
1145 }
1146
1147 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1148 return -ENODEV;
1149
1150 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1151 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001152 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001153 return -EOPNOTSUPP;
1154
1155 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001156#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001157 if (kind[0]) {
1158 __rtnl_unlock();
1159 request_module("rtnl-link-%s", kind);
1160 rtnl_lock();
1161 ops = rtnl_link_ops_get(kind);
1162 if (ops)
1163 goto replay;
1164 }
1165#endif
1166 return -EOPNOTSUPP;
1167 }
1168
1169 if (!ifname[0])
1170 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001171
Eric W. Biederman81adee42009-11-08 00:53:51 -08001172 dest_net = rtnl_link_get_net(net, tb);
1173 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001174
Pavel Emelianove7199282007-08-08 22:16:38 -07001175 if (IS_ERR(dev))
1176 err = PTR_ERR(dev);
1177 else if (ops->newlink)
Eric W. Biederman81adee42009-11-08 00:53:51 -08001178 err = ops->newlink(net, dev, tb, data);
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001179 else
1180 err = register_netdevice(dev);
Pavel Emelianove7199282007-08-08 22:16:38 -07001181 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001182 free_netdev(dev);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001183
1184 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001185 return err;
1186 }
1187}
1188
Thomas Grafb60c5112006-08-04 23:05:34 -07001189static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001190{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001191 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07001192 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00001193 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07001194 struct nlattr *tb[IFLA_MAX+1];
1195 struct net_device *dev = NULL;
1196 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001197 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001198
Thomas Grafb60c5112006-08-04 23:05:34 -07001199 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1200 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001201 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001202
Eric Dumazeta3d12892009-10-21 10:59:31 +00001203 if (tb[IFLA_IFNAME])
1204 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1205
Thomas Grafb60c5112006-08-04 23:05:34 -07001206 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001207 if (ifm->ifi_index > 0)
1208 dev = __dev_get_by_index(net, ifm->ifi_index);
1209 else if (tb[IFLA_IFNAME])
1210 dev = __dev_get_by_name(net, ifname);
1211 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001212 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001213
Eric Dumazeta3d12892009-10-21 10:59:31 +00001214 if (dev == NULL)
1215 return -ENODEV;
1216
Patrick McHardy38f7b872007-06-13 12:03:51 -07001217 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001218 if (nskb == NULL)
1219 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001220
Patrick McHardy575c3e22007-05-22 17:00:49 -07001221 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1222 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001223 if (err < 0) {
1224 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1225 WARN_ON(err == -EMSGSIZE);
1226 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001227 } else
1228 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001229
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001230 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001231}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001232
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001233static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 int idx;
1236 int s_idx = cb->family;
1237
1238 if (s_idx == 0)
1239 s_idx = 1;
Eric Dumazete0d087a2009-11-07 01:26:17 -08001240 for (idx = 1; idx < NPROTO; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int type = cb->nlh->nlmsg_type-RTM_BASE;
1242 if (idx < s_idx || idx == PF_PACKET)
1243 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001244 if (rtnl_msg_handlers[idx] == NULL ||
1245 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 continue;
1247 if (idx > s_idx)
1248 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001249 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251 }
1252 cb->family = idx;
1253
1254 return skb->len;
1255}
1256
1257void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1258{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001259 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001261 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Patrick McHardy38f7b872007-06-13 12:03:51 -07001263 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001264 if (skb == NULL)
1265 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Patrick McHardy575c3e22007-05-22 17:00:49 -07001267 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001268 if (err < 0) {
1269 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1270 WARN_ON(err == -EMSGSIZE);
1271 kfree_skb(skb);
1272 goto errout;
1273 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001274 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1275 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001276errout:
1277 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08001278 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/* Protected by RTNL sempahore. */
1282static struct rtattr **rta_buf;
1283static int rtattr_max;
1284
1285/* Process one rtnetlink message. */
1286
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001287static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001289 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07001290 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int sz_idx, kind;
1292 int min_len;
1293 int family;
1294 int type;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001299 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 type -= RTM_BASE;
1302
1303 /* All the messages must have at least 1 byte length */
1304 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1305 return 0;
1306
Eric Dumazete0d087a2009-11-07 01:26:17 -08001307 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001308 if (family >= NPROTO)
1309 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sz_idx = type>>2;
1312 kind = type&3;
1313
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001314 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1315 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001318 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07001319 rtnl_dumpit_func dumpit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Thomas Grafe2849862007-03-22 11:48:11 -07001321 dumpit = rtnl_get_dumpit(family, type);
1322 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001323 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001325 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001326 rtnl = net->rtnl;
Patrick McHardy1c2d6702007-04-16 16:59:10 -07001327 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1328 rtnl_lock();
1329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
1332 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1333
1334 min_len = rtm_min[sz_idx];
1335 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (nlh->nlmsg_len > min_len) {
1339 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Eric Dumazete0d087a2009-11-07 01:26:17 -08001340 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 while (RTA_OK(attr, attrlen)) {
1343 unsigned flavor = attr->rta_type;
1344 if (flavor) {
1345 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 rta_buf[flavor-1] = attr;
1348 }
1349 attr = RTA_NEXT(attr, attrlen);
1350 }
1351 }
1352
Thomas Grafe2849862007-03-22 11:48:11 -07001353 doit = rtnl_get_doit(family, type);
1354 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001355 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001357 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001360static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001362 rtnl_lock();
1363 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1364 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1368{
1369 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 switch (event) {
1372 case NETDEV_UNREGISTER:
1373 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case NETDEV_UP:
1376 case NETDEV_DOWN:
1377 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1378 break;
Eric W. Biedermand90a9092009-12-12 22:11:15 +00001379 case NETDEV_POST_INIT:
1380 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 case NETDEV_CHANGE:
1382 case NETDEV_GOING_DOWN:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00001383 case NETDEV_UNREGISTER_BATCH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 default:
1386 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1387 break;
1388 }
1389 return NOTIFY_DONE;
1390}
1391
1392static struct notifier_block rtnetlink_dev_notifier = {
1393 .notifier_call = rtnetlink_event,
1394};
1395
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001396
1397static int rtnetlink_net_init(struct net *net)
1398{
1399 struct sock *sk;
1400 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1401 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1402 if (!sk)
1403 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001404 net->rtnl = sk;
1405 return 0;
1406}
1407
1408static void rtnetlink_net_exit(struct net *net)
1409{
Denis V. Lunev775516b2008-01-18 23:55:19 -08001410 netlink_kernel_release(net->rtnl);
1411 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001412}
1413
1414static struct pernet_operations rtnetlink_net_ops = {
1415 .init = rtnetlink_net_init,
1416 .exit = rtnetlink_net_exit,
1417};
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419void __init rtnetlink_init(void)
1420{
1421 int i;
1422
1423 rtattr_max = 0;
1424 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1425 if (rta_max[i] > rtattr_max)
1426 rtattr_max = rta_max[i];
1427 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1428 if (!rta_buf)
1429 panic("rtnetlink_init: cannot allocate rta_buf\n");
1430
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001431 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1435 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07001436
1437 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1438 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001439 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1440 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07001441
1442 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1443 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445