blob: 5c6e4fa006037075a23be84ddfe8683e0a8b5184 [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>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000038#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/uaccess.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;
Greg Rosec7ac8672011-06-10 01:27:09 +000058 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070059};
60
Stephen Hemminger6756ae42006-03-20 22:23:58 -080061static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63void rtnl_lock(void)
64{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080065 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Eric Dumazete0d087a2009-11-07 01:26:17 -080067EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Stephen Hemminger6756ae42006-03-20 22:23:58 -080069void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074void rtnl_unlock(void)
75{
Herbert Xu58ec3b42008-10-07 15:50:03 -070076 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 netdev_run_todo();
78}
Eric Dumazete0d087a2009-11-07 01:26:17 -080079EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Stephen Hemminger6756ae42006-03-20 22:23:58 -080081int rtnl_trylock(void)
82{
83 return mutex_trylock(&rtnl_mutex);
84}
Eric Dumazete0d087a2009-11-07 01:26:17 -080085EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086
Patrick McHardyc9c10142008-04-23 22:10:48 -070087int rtnl_is_locked(void)
88{
89 return mutex_is_locked(&rtnl_mutex);
90}
Eric Dumazete0d087a2009-11-07 01:26:17 -080091EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070092
Paul E. McKenneya898def2010-02-22 17:04:49 -080093#ifdef CONFIG_PROVE_LOCKING
94int lockdep_rtnl_is_held(void)
95{
96 return lockdep_is_held(&rtnl_mutex);
97}
98EXPORT_SYMBOL(lockdep_rtnl_is_held);
99#endif /* #ifdef CONFIG_PROVE_LOCKING */
100
Patrick McHardy25239ce2010-04-26 16:02:05 +0200101static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700102
103static inline int rtm_msgindex(int msgtype)
104{
105 int msgindex = msgtype - RTM_BASE;
106
107 /*
108 * msgindex < 0 implies someone tried to register a netlink
109 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
110 * the message type has not been added to linux/rtnetlink.h
111 */
112 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
113
114 return msgindex;
115}
116
117static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
118{
119 struct rtnl_link *tab;
120
Patrick McHardy25239ce2010-04-26 16:02:05 +0200121 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000122 tab = rtnl_msg_handlers[protocol];
123 else
124 tab = NULL;
125
Thomas Graf51057f22007-03-22 21:41:06 -0700126 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700127 tab = rtnl_msg_handlers[PF_UNSPEC];
128
Thomas Graf51057f22007-03-22 21:41:06 -0700129 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700130}
131
132static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
133{
134 struct rtnl_link *tab;
135
Patrick McHardy25239ce2010-04-26 16:02:05 +0200136 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000137 tab = rtnl_msg_handlers[protocol];
138 else
139 tab = NULL;
140
Thomas Graf51057f22007-03-22 21:41:06 -0700141 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700142 tab = rtnl_msg_handlers[PF_UNSPEC];
143
Thomas Graf51057f22007-03-22 21:41:06 -0700144 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700145}
146
Greg Rosec7ac8672011-06-10 01:27:09 +0000147static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
148{
149 struct rtnl_link *tab;
150
151 if (protocol <= RTNL_FAMILY_MAX)
152 tab = rtnl_msg_handlers[protocol];
153 else
154 tab = NULL;
155
156 if (tab == NULL || tab[msgindex].calcit == NULL)
157 tab = rtnl_msg_handlers[PF_UNSPEC];
158
159 return tab ? tab[msgindex].calcit : NULL;
160}
161
Thomas Grafe2849862007-03-22 11:48:11 -0700162/**
163 * __rtnl_register - Register a rtnetlink message type
164 * @protocol: Protocol family or PF_UNSPEC
165 * @msgtype: rtnetlink message type
166 * @doit: Function pointer called for each request message
167 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000168 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700169 *
170 * Registers the specified function pointers (at least one of them has
171 * to be non-NULL) to be called whenever a request message for the
172 * specified protocol family and message type is received.
173 *
174 * The special protocol family PF_UNSPEC may be used to define fallback
175 * function pointers for the case when no entry for the specific protocol
176 * family exists.
177 *
178 * Returns 0 on success or a negative error code.
179 */
180int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000181 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
182 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700183{
184 struct rtnl_link *tab;
185 int msgindex;
186
Patrick McHardy25239ce2010-04-26 16:02:05 +0200187 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700188 msgindex = rtm_msgindex(msgtype);
189
190 tab = rtnl_msg_handlers[protocol];
191 if (tab == NULL) {
192 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
193 if (tab == NULL)
194 return -ENOBUFS;
195
196 rtnl_msg_handlers[protocol] = tab;
197 }
198
199 if (doit)
200 tab[msgindex].doit = doit;
201
202 if (dumpit)
203 tab[msgindex].dumpit = dumpit;
204
Greg Rosec7ac8672011-06-10 01:27:09 +0000205 if (calcit)
206 tab[msgindex].calcit = calcit;
207
Thomas Grafe2849862007-03-22 11:48:11 -0700208 return 0;
209}
Thomas Grafe2849862007-03-22 11:48:11 -0700210EXPORT_SYMBOL_GPL(__rtnl_register);
211
212/**
213 * rtnl_register - Register a rtnetlink message type
214 *
215 * Identical to __rtnl_register() but panics on failure. This is useful
216 * as failure of this function is very unlikely, it can only happen due
217 * to lack of memory when allocating the chain to store all message
218 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300219 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700220 */
221void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000222 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
223 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700224{
Greg Rosec7ac8672011-06-10 01:27:09 +0000225 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700226 panic("Unable to register rtnetlink message handler, "
227 "protocol = %d, message type = %d\n",
228 protocol, msgtype);
229}
Thomas Grafe2849862007-03-22 11:48:11 -0700230EXPORT_SYMBOL_GPL(rtnl_register);
231
232/**
233 * rtnl_unregister - Unregister a rtnetlink message type
234 * @protocol: Protocol family or PF_UNSPEC
235 * @msgtype: rtnetlink message type
236 *
237 * Returns 0 on success or a negative error code.
238 */
239int rtnl_unregister(int protocol, int msgtype)
240{
241 int msgindex;
242
Patrick McHardy25239ce2010-04-26 16:02:05 +0200243 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700244 msgindex = rtm_msgindex(msgtype);
245
246 if (rtnl_msg_handlers[protocol] == NULL)
247 return -ENOENT;
248
249 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
250 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
251
252 return 0;
253}
Thomas Grafe2849862007-03-22 11:48:11 -0700254EXPORT_SYMBOL_GPL(rtnl_unregister);
255
256/**
257 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
258 * @protocol : Protocol family or PF_UNSPEC
259 *
260 * Identical to calling rtnl_unregster() for all registered message types
261 * of a certain protocol family.
262 */
263void rtnl_unregister_all(int protocol)
264{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200265 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700266
267 kfree(rtnl_msg_handlers[protocol]);
268 rtnl_msg_handlers[protocol] = NULL;
269}
Thomas Grafe2849862007-03-22 11:48:11 -0700270EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Patrick McHardy38f7b872007-06-13 12:03:51 -0700272static LIST_HEAD(link_ops);
273
Eric Dumazetc63044f2011-12-13 11:38:00 +0000274static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
275{
276 const struct rtnl_link_ops *ops;
277
278 list_for_each_entry(ops, &link_ops, list) {
279 if (!strcmp(ops->kind, kind))
280 return ops;
281 }
282 return NULL;
283}
284
Patrick McHardy38f7b872007-06-13 12:03:51 -0700285/**
286 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
287 * @ops: struct rtnl_link_ops * to register
288 *
289 * The caller must hold the rtnl_mutex. This function should be used
290 * by drivers that create devices during module initialization. It
291 * must be called before registering the devices.
292 *
293 * Returns 0 on success or a negative error code.
294 */
295int __rtnl_link_register(struct rtnl_link_ops *ops)
296{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000297 if (rtnl_link_ops_get(ops->kind))
298 return -EEXIST;
299
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700300 if (!ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000301 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700302
Patrick McHardy38f7b872007-06-13 12:03:51 -0700303 list_add_tail(&ops->list, &link_ops);
304 return 0;
305}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700306EXPORT_SYMBOL_GPL(__rtnl_link_register);
307
308/**
309 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
310 * @ops: struct rtnl_link_ops * to register
311 *
312 * Returns 0 on success or a negative error code.
313 */
314int rtnl_link_register(struct rtnl_link_ops *ops)
315{
316 int err;
317
318 rtnl_lock();
319 err = __rtnl_link_register(ops);
320 rtnl_unlock();
321 return err;
322}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700323EXPORT_SYMBOL_GPL(rtnl_link_register);
324
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700325static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
326{
327 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000328 LIST_HEAD(list_kill);
329
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700330 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000331 if (dev->rtnl_link_ops == ops)
332 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700333 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000334 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335}
336
Patrick McHardy38f7b872007-06-13 12:03:51 -0700337/**
338 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
339 * @ops: struct rtnl_link_ops * to unregister
340 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700341 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700342 */
343void __rtnl_link_unregister(struct rtnl_link_ops *ops)
344{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700345 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700346
Eric W. Biederman881d9662007-09-17 11:56:21 -0700347 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700348 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700349 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700350 list_del(&ops->list);
351}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
353
354/**
355 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
356 * @ops: struct rtnl_link_ops * to unregister
357 */
358void rtnl_link_unregister(struct rtnl_link_ops *ops)
359{
360 rtnl_lock();
361 __rtnl_link_unregister(ops);
362 rtnl_unlock();
363}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700364EXPORT_SYMBOL_GPL(rtnl_link_unregister);
365
Patrick McHardy38f7b872007-06-13 12:03:51 -0700366static size_t rtnl_link_get_size(const struct net_device *dev)
367{
368 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
369 size_t size;
370
371 if (!ops)
372 return 0;
373
Thomas Graf369cf772010-11-11 15:47:59 +0000374 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
375 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700376
377 if (ops->get_size)
378 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000379 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700380 ops->get_size(dev);
381
382 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000383 /* IFLA_INFO_XSTATS */
384 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700385
386 return size;
387}
388
Thomas Graff8ff1822010-11-16 04:30:14 +0000389static LIST_HEAD(rtnl_af_ops);
390
391static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
392{
393 const struct rtnl_af_ops *ops;
394
395 list_for_each_entry(ops, &rtnl_af_ops, list) {
396 if (ops->family == family)
397 return ops;
398 }
399
400 return NULL;
401}
402
403/**
404 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
405 * @ops: struct rtnl_af_ops * to register
406 *
407 * The caller must hold the rtnl_mutex.
408 *
409 * Returns 0 on success or a negative error code.
410 */
411int __rtnl_af_register(struct rtnl_af_ops *ops)
412{
413 list_add_tail(&ops->list, &rtnl_af_ops);
414 return 0;
415}
416EXPORT_SYMBOL_GPL(__rtnl_af_register);
417
418/**
419 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
420 * @ops: struct rtnl_af_ops * to register
421 *
422 * Returns 0 on success or a negative error code.
423 */
424int rtnl_af_register(struct rtnl_af_ops *ops)
425{
426 int err;
427
428 rtnl_lock();
429 err = __rtnl_af_register(ops);
430 rtnl_unlock();
431 return err;
432}
433EXPORT_SYMBOL_GPL(rtnl_af_register);
434
435/**
436 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
437 * @ops: struct rtnl_af_ops * to unregister
438 *
439 * The caller must hold the rtnl_mutex.
440 */
441void __rtnl_af_unregister(struct rtnl_af_ops *ops)
442{
443 list_del(&ops->list);
444}
445EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
446
447/**
448 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
449 * @ops: struct rtnl_af_ops * to unregister
450 */
451void rtnl_af_unregister(struct rtnl_af_ops *ops)
452{
453 rtnl_lock();
454 __rtnl_af_unregister(ops);
455 rtnl_unlock();
456}
457EXPORT_SYMBOL_GPL(rtnl_af_unregister);
458
459static size_t rtnl_link_get_af_size(const struct net_device *dev)
460{
461 struct rtnl_af_ops *af_ops;
462 size_t size;
463
464 /* IFLA_AF_SPEC */
465 size = nla_total_size(sizeof(struct nlattr));
466
467 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
468 if (af_ops->get_link_af_size) {
469 /* AF_* + nested data */
470 size += nla_total_size(sizeof(struct nlattr)) +
471 af_ops->get_link_af_size(dev);
472 }
473 }
474
475 return size;
476}
477
Patrick McHardy38f7b872007-06-13 12:03:51 -0700478static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
479{
480 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
481 struct nlattr *linkinfo, *data;
482 int err = -EMSGSIZE;
483
484 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
485 if (linkinfo == NULL)
486 goto out;
487
488 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
489 goto err_cancel_link;
490 if (ops->fill_xstats) {
491 err = ops->fill_xstats(skb, dev);
492 if (err < 0)
493 goto err_cancel_link;
494 }
495 if (ops->fill_info) {
496 data = nla_nest_start(skb, IFLA_INFO_DATA);
Wei Yongjuna36da1e2013-03-27 03:22:45 +0000497 if (data == NULL) {
498 err = -EMSGSIZE;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700499 goto err_cancel_link;
Wei Yongjuna36da1e2013-03-27 03:22:45 +0000500 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700501 err = ops->fill_info(skb, dev);
502 if (err < 0)
503 goto err_cancel_data;
504 nla_nest_end(skb, data);
505 }
506
507 nla_nest_end(skb, linkinfo);
508 return 0;
509
510err_cancel_data:
511 nla_nest_cancel(skb, data);
512err_cancel_link:
513 nla_nest_cancel(skb, linkinfo);
514out:
515 return err;
516}
517
Thomas Grafdb46edc2005-05-03 14:29:39 -0700518static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Thomas Graff90a0a72005-05-03 14:29:00 -0700520 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
521 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
522 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
Thomas Graf14c0b972006-08-04 03:38:38 -0700523 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700524 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
525 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
526 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
527 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
Thomas Graff90a0a72005-05-03 14:29:00 -0700528 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
529 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530};
531
Thomas Grafdb46edc2005-05-03 14:29:39 -0700532static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Thomas Graff90a0a72005-05-03 14:29:00 -0700534 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
535 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
536 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
Thomas Graf14c0b972006-08-04 03:38:38 -0700537 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
Thomas Graff90a0a72005-05-03 14:29:00 -0700538 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
539 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
540 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
541 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
544void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
545{
546 struct rtattr *rta;
547 int size = RTA_LENGTH(attrlen);
548
Eric Dumazete0d087a2009-11-07 01:26:17 -0800549 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 rta->rta_type = attrtype;
551 rta->rta_len = size;
552 memcpy(RTA_DATA(rta), data, attrlen);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700553 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800555EXPORT_SYMBOL(__rta_fill);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800557int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800559 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int err = 0;
561
Patrick McHardyac6d4392005-08-14 19:29:52 -0700562 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (echo)
564 atomic_inc(&skb->users);
565 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
566 if (echo)
567 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
568 return err;
569}
570
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800571int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700572{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800573 struct sock *rtnl = net->rtnl;
574
Thomas Graf2942e902006-08-15 00:30:25 -0700575 return nlmsg_unicast(rtnl, skb, pid);
576}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800577EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700578
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800579void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
580 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700581{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800582 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700583 int report = 0;
584
585 if (nlh)
586 report = nlmsg_report(nlh);
587
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800588 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700589}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800590EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700591
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800592void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700593{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800594 struct sock *rtnl = net->rtnl;
595
Thomas Graf97676b62006-08-15 00:31:41 -0700596 netlink_set_err(rtnl, 0, group, error);
597}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800598EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
601{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700602 struct nlattr *mx;
603 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Thomas Graf2d7202b2006-08-22 00:01:27 -0700605 mx = nla_nest_start(skb, RTA_METRICS);
606 if (mx == NULL)
607 return -ENOBUFS;
608
609 for (i = 0; i < RTAX_MAX; i++) {
610 if (metrics[i]) {
611 valid++;
612 NLA_PUT_U32(skb, i+1, metrics[i]);
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
David S. Millera57d27f2006-08-22 22:20:14 -0700616 if (!valid) {
617 nla_nest_cancel(skb, mx);
618 return 0;
619 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700620
621 return nla_nest_end(skb, mx);
622
623nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700624 nla_nest_cancel(skb, mx);
625 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800627EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Thomas Grafe3703b32006-11-27 09:27:07 -0800629int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
630 u32 ts, u32 tsage, long expires, u32 error)
631{
632 struct rta_cacheinfo ci = {
633 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
634 .rta_used = dst->__use,
635 .rta_clntref = atomic_read(&(dst->__refcnt)),
636 .rta_error = error,
637 .rta_id = id,
638 .rta_ts = ts,
639 .rta_tsage = tsage,
640 };
641
642 if (expires)
643 ci.rta_expires = jiffies_to_clock_t(expires);
644
645 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
646}
Thomas Grafe3703b32006-11-27 09:27:07 -0800647EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
David S. Miller93b2d4a2008-02-17 18:35:07 -0800649static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800650{
651 unsigned char operstate = dev->operstate;
652
Eric Dumazete0d087a2009-11-07 01:26:17 -0800653 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800654 case IF_OPER_UP:
655 if ((operstate == IF_OPER_DORMANT ||
656 operstate == IF_OPER_UNKNOWN) &&
657 !netif_dormant(dev))
658 operstate = IF_OPER_UP;
659 break;
660
661 case IF_OPER_DORMANT:
662 if (operstate == IF_OPER_UP ||
663 operstate == IF_OPER_UNKNOWN)
664 operstate = IF_OPER_DORMANT;
665 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700666 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800667
668 if (dev->operstate != operstate) {
669 write_lock_bh(&dev_base_lock);
670 dev->operstate = operstate;
671 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800672 netdev_state_change(dev);
673 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800674}
675
Jiri Bence7b539a2012-07-27 02:58:22 +0000676static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
677{
678 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
679 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
680}
681
Patrick McHardy3729d502010-02-26 06:34:54 +0000682static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
683 const struct ifinfomsg *ifm)
684{
685 unsigned int flags = ifm->ifi_flags;
686
687 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
688 if (ifm->ifi_change)
689 flags = (flags & ifm->ifi_change) |
Jiri Bence7b539a2012-07-27 02:58:22 +0000690 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000691
692 return flags;
693}
694
Thomas Grafb60c5112006-08-04 23:05:34 -0700695static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000696 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Thomas Grafb60c5112006-08-04 23:05:34 -0700698 a->rx_packets = b->rx_packets;
699 a->tx_packets = b->tx_packets;
700 a->rx_bytes = b->rx_bytes;
701 a->tx_bytes = b->tx_bytes;
702 a->rx_errors = b->rx_errors;
703 a->tx_errors = b->tx_errors;
704 a->rx_dropped = b->rx_dropped;
705 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Thomas Grafb60c5112006-08-04 23:05:34 -0700707 a->multicast = b->multicast;
708 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Thomas Grafb60c5112006-08-04 23:05:34 -0700710 a->rx_length_errors = b->rx_length_errors;
711 a->rx_over_errors = b->rx_over_errors;
712 a->rx_crc_errors = b->rx_crc_errors;
713 a->rx_frame_errors = b->rx_frame_errors;
714 a->rx_fifo_errors = b->rx_fifo_errors;
715 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Thomas Grafb60c5112006-08-04 23:05:34 -0700717 a->tx_aborted_errors = b->tx_aborted_errors;
718 a->tx_carrier_errors = b->tx_carrier_errors;
719 a->tx_fifo_errors = b->tx_fifo_errors;
720 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
721 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Thomas Grafb60c5112006-08-04 23:05:34 -0700723 a->rx_compressed = b->rx_compressed;
724 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000725}
726
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000727static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000728{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000729 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000730}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Chris Wrightc02db8c2010-05-16 01:05:45 -0700732/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500733static inline int rtnl_vfinfo_size(const struct net_device *dev,
734 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000735{
Greg Rose115c9b82012-02-21 16:54:48 -0500736 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
737 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700738 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700739 size_t size = nla_total_size(sizeof(struct nlattr));
740 size += nla_total_size(num_vfs * sizeof(struct nlattr));
741 size += num_vfs *
742 (nla_total_size(sizeof(struct ifla_vf_mac)) +
743 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Greg Rose5f8444a2011-10-08 03:05:24 +0000744 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
745 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700746 return size;
747 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000748 return 0;
749}
750
Scott Feldman57b61082010-05-17 22:49:55 -0700751static size_t rtnl_port_size(const struct net_device *dev)
752{
753 size_t port_size = nla_total_size(4) /* PORT_VF */
754 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
755 + nla_total_size(sizeof(struct ifla_port_vsi))
756 /* PORT_VSI_TYPE */
757 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
758 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
759 + nla_total_size(1) /* PROT_VDP_REQUEST */
760 + nla_total_size(2); /* PORT_VDP_RESPONSE */
761 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
762 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
763 + port_size;
764 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
765 + port_size;
766
767 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
768 return 0;
769 if (dev_num_vf(dev->dev.parent))
770 return port_self_size + vf_ports_size +
771 vf_port_size * dev_num_vf(dev->dev.parent);
772 else
773 return port_self_size;
774}
775
Greg Rose115c9b82012-02-21 16:54:48 -0500776static noinline size_t if_nlmsg_size(const struct net_device *dev,
777 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800778{
779 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
780 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700781 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800782 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
783 + nla_total_size(sizeof(struct rtnl_link_ifmap))
784 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700785 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800786 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
787 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
788 + nla_total_size(4) /* IFLA_TXQLEN */
789 + nla_total_size(4) /* IFLA_WEIGHT */
790 + nla_total_size(4) /* IFLA_MTU */
791 + nla_total_size(4) /* IFLA_LINK */
792 + nla_total_size(4) /* IFLA_MASTER */
793 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700794 + nla_total_size(1) /* IFLA_LINKMODE */
Greg Rose115c9b82012-02-21 16:54:48 -0500795 + nla_total_size(ext_filter_mask
796 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
797 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
Scott Feldman57b61082010-05-17 22:49:55 -0700798 + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000799 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
800 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
Thomas Graf339bf982006-11-10 14:10:15 -0800801}
802
Scott Feldman57b61082010-05-17 22:49:55 -0700803static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
804{
805 struct nlattr *vf_ports;
806 struct nlattr *vf_port;
807 int vf;
808 int err;
809
810 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
811 if (!vf_ports)
812 return -EMSGSIZE;
813
814 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
815 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700816 if (!vf_port)
817 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700818 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
819 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700820 if (err == -EMSGSIZE)
821 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700822 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700823 nla_nest_cancel(skb, vf_port);
824 continue;
825 }
826 nla_nest_end(skb, vf_port);
827 }
828
829 nla_nest_end(skb, vf_ports);
830
831 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700832
833nla_put_failure:
834 nla_nest_cancel(skb, vf_ports);
835 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700836}
837
838static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
839{
840 struct nlattr *port_self;
841 int err;
842
843 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
844 if (!port_self)
845 return -EMSGSIZE;
846
847 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
848 if (err) {
849 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700850 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700851 }
852
853 nla_nest_end(skb, port_self);
854
855 return 0;
856}
857
858static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
859{
860 int err;
861
862 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
863 return 0;
864
865 err = rtnl_port_self_fill(skb, dev);
866 if (err)
867 return err;
868
869 if (dev_num_vf(dev->dev.parent)) {
870 err = rtnl_vf_ports_fill(skb, dev);
871 if (err)
872 return err;
873 }
874
875 return 0;
876}
877
Thomas Grafb60c5112006-08-04 23:05:34 -0700878static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700879 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -0500880 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -0700881{
882 struct ifinfomsg *ifm;
883 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -0700884 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000885 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +0000886 struct nlattr *attr, *af_spec;
887 struct rtnl_af_ops *af_ops;
Thomas Grafb60c5112006-08-04 23:05:34 -0700888
Eric Dumazet2907c352011-05-25 07:34:04 +0000889 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -0700890 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
891 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800892 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700893
894 ifm = nlmsg_data(nlh);
895 ifm->ifi_family = AF_UNSPEC;
896 ifm->__ifi_pad = 0;
897 ifm->ifi_type = dev->type;
898 ifm->ifi_index = dev->ifindex;
899 ifm->ifi_flags = dev_get_flags(dev);
900 ifm->ifi_change = change;
901
902 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
903 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700904 NLA_PUT_U8(skb, IFLA_OPERSTATE,
905 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
906 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
907 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Vlad Dogarucbda10f2011-01-13 23:38:30 +0000908 NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
Thomas Grafb60c5112006-08-04 23:05:34 -0700909
910 if (dev->ifindex != dev->iflink)
911 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
912
913 if (dev->master)
914 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
915
Patrick McHardyaf356af2009-09-04 06:41:18 +0000916 if (dev->qdisc)
917 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800918
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700919 if (dev->ifalias)
920 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
921
Stefan Rompfb00055a2006-03-20 17:09:11 -0800922 if (1) {
Teow Wan Yeeaf9dd6d2016-07-26 16:53:39 +0800923 struct rtnl_link_ifmap map;
924
925 memset(&map, 0, sizeof(map));
926 map.mem_start = dev->mem_start;
927 map.mem_end = dev->mem_end;
928 map.base_addr = dev->base_addr;
929 map.irq = dev->irq;
930 map.dma = dev->dma;
931 map.port = dev->if_port;
932
Thomas Grafb60c5112006-08-04 23:05:34 -0700933 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700937 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
938 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700941 attr = nla_reserve(skb, IFLA_STATS,
942 sizeof(struct rtnl_link_stats));
943 if (attr == NULL)
944 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Eric Dumazet28172732010-07-07 14:58:56 -0700946 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700947 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jan Engelhardt10708f32010-03-11 09:57:29 +0000949 attr = nla_reserve(skb, IFLA_STATS64,
950 sizeof(struct rtnl_link_stats64));
951 if (attr == NULL)
952 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000953 copy_rtnl_link_stats64(nla_data(attr), stats);
954
Greg Rose115c9b82012-02-21 16:54:48 -0500955 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700956 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
957
Greg Rose115c9b82012-02-21 16:54:48 -0500958 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
959 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000960 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000961
Chris Wrightc02db8c2010-05-16 01:05:45 -0700962 struct nlattr *vfinfo, *vf;
963 int num_vfs = dev_num_vf(dev->dev.parent);
964
Chris Wrightc02db8c2010-05-16 01:05:45 -0700965 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
966 if (!vfinfo)
967 goto nla_put_failure;
968 for (i = 0; i < num_vfs; i++) {
969 struct ifla_vf_info ivi;
970 struct ifla_vf_mac vf_mac;
971 struct ifla_vf_vlan vf_vlan;
972 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000973 struct ifla_vf_spoofchk vf_spoofchk;
974
975 /*
976 * Not all SR-IOV capable drivers support the
977 * spoofcheck query. Preset to -1 so the user
978 * space tool can detect that the driver didn't
979 * report anything.
980 */
981 ivi.spoofchk = -1;
Mathias Krause75deb322013-03-09 05:52:20 +0000982 memset(ivi.mac, 0, sizeof(ivi.mac));
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000983 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
984 break;
Greg Rose5f8444a2011-10-08 03:05:24 +0000985 vf_mac.vf =
986 vf_vlan.vf =
987 vf_tx_rate.vf =
988 vf_spoofchk.vf = ivi.vf;
989
Chris Wrightc02db8c2010-05-16 01:05:45 -0700990 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
991 vf_vlan.vlan = ivi.vlan;
992 vf_vlan.qos = ivi.qos;
993 vf_tx_rate.rate = ivi.tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000994 vf_spoofchk.setting = ivi.spoofchk;
Chris Wrightc02db8c2010-05-16 01:05:45 -0700995 vf = nla_nest_start(skb, IFLA_VF_INFO);
996 if (!vf) {
997 nla_nest_cancel(skb, vfinfo);
998 goto nla_put_failure;
999 }
1000 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
1001 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
Greg Rose5f8444a2011-10-08 03:05:24 +00001002 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1003 &vf_tx_rate);
1004 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1005 &vf_spoofchk);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001006 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001007 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001008 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001009 }
Scott Feldman57b61082010-05-17 22:49:55 -07001010
1011 if (rtnl_port_fill(skb, dev))
1012 goto nla_put_failure;
1013
Patrick McHardy38f7b872007-06-13 12:03:51 -07001014 if (dev->rtnl_link_ops) {
1015 if (rtnl_link_fill(skb, dev) < 0)
1016 goto nla_put_failure;
1017 }
1018
Thomas Graff8ff1822010-11-16 04:30:14 +00001019 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1020 goto nla_put_failure;
1021
1022 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1023 if (af_ops->fill_link_af) {
1024 struct nlattr *af;
1025 int err;
1026
1027 if (!(af = nla_nest_start(skb, af_ops->family)))
1028 goto nla_put_failure;
1029
1030 err = af_ops->fill_link_af(skb, dev);
1031
1032 /*
1033 * Caller may return ENODATA to indicate that there
1034 * was no data to be dumped. This is not an error, it
1035 * means we should trim the attribute header and
1036 * continue.
1037 */
1038 if (err == -ENODATA)
1039 nla_nest_cancel(skb, af);
1040 else if (err < 0)
1041 goto nla_put_failure;
1042
1043 nla_nest_end(skb, af);
1044 }
1045 }
1046
1047 nla_nest_end(skb, af_spec);
1048
Thomas Grafb60c5112006-08-04 23:05:34 -07001049 return nlmsg_end(skb, nlh);
1050
1051nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001052 nlmsg_cancel(skb, nlh);
1053 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Thomas Grafb60c5112006-08-04 23:05:34 -07001056static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001058 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001059 int h, s_h;
1060 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001062 struct hlist_head *head;
1063 struct hlist_node *node;
Greg Rose115c9b82012-02-21 16:54:48 -05001064 struct nlattr *tb[IFLA_MAX+1];
1065 u32 ext_filter_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001067 s_h = cb->args[0];
1068 s_idx = cb->args[1];
1069
Eric Dumazete67f88d2011-04-27 22:56:07 +00001070 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001071 cb->seq = net->dev_base_seq;
1072
Michael Rieschb38a2412013-04-08 05:45:26 +00001073 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001074 ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001075
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001076 if (tb[IFLA_EXT_MASK])
1077 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1078 }
Greg Rose115c9b82012-02-21 16:54:48 -05001079
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001080 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1081 idx = 0;
1082 head = &net->dev_index_head[h];
Eric Dumazete67f88d2011-04-27 22:56:07 +00001083 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001084 if (idx < s_idx)
1085 goto cont;
1086 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1087 NETLINK_CB(cb->skb).pid,
1088 cb->nlh->nlmsg_seq, 0,
Greg Rose115c9b82012-02-21 16:54:48 -05001089 NLM_F_MULTI,
1090 ext_filter_mask) <= 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001091 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001092
1093 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001094cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001095 idx++;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001098out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001099 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001100 cb->args[1] = idx;
1101 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 return skb->len;
1104}
1105
Pavel Emelianove7199282007-08-08 22:16:38 -07001106const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07001107 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -07001108 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1109 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -07001110 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -07001111 [IFLA_MTU] = { .type = NLA_U32 },
Thomas Graf76e87302008-02-19 16:12:08 -08001112 [IFLA_LINK] = { .type = NLA_U32 },
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001113 [IFLA_MASTER] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -07001114 [IFLA_TXQLEN] = { .type = NLA_U32 },
1115 [IFLA_WEIGHT] = { .type = NLA_U32 },
1116 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1117 [IFLA_LINKMODE] = { .type = NLA_U8 },
Thomas Graf76e87302008-02-19 16:12:08 -08001118 [IFLA_LINKINFO] = { .type = NLA_NESTED },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001119 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001120 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001121 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
Chris Wrightc02db8c2010-05-16 01:05:45 -07001122 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
Scott Feldman57b61082010-05-17 22:49:55 -07001123 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1124 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
Thomas Graff8ff1822010-11-16 04:30:14 +00001125 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
Greg Rose115c9b82012-02-21 16:54:48 -05001126 [IFLA_EXT_MASK] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -07001127};
Eric Dumazete0d087a2009-11-07 01:26:17 -08001128EXPORT_SYMBOL(ifla_policy);
Thomas Grafda5e0492006-08-10 21:17:37 -07001129
Patrick McHardy38f7b872007-06-13 12:03:51 -07001130static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1131 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1132 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1133};
1134
Chris Wrightc02db8c2010-05-16 01:05:45 -07001135static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1136 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1137};
1138
1139static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1140 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1141 .len = sizeof(struct ifla_vf_mac) },
1142 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1143 .len = sizeof(struct ifla_vf_vlan) },
1144 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1145 .len = sizeof(struct ifla_vf_tx_rate) },
Greg Rose48752f62012-02-08 00:45:00 +00001146 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1147 .len = sizeof(struct ifla_vf_spoofchk) },
Chris Wrightc02db8c2010-05-16 01:05:45 -07001148};
1149
Scott Feldman57b61082010-05-17 22:49:55 -07001150static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1151 [IFLA_PORT_VF] = { .type = NLA_U32 },
1152 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1153 .len = PORT_PROFILE_MAX },
1154 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1155 .len = sizeof(struct ifla_port_vsi)},
1156 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1157 .len = PORT_UUID_MAX },
1158 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1159 .len = PORT_UUID_MAX },
1160 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1161 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1162};
1163
Eric W. Biederman81adee42009-11-08 00:53:51 -08001164struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1165{
1166 struct net *net;
1167 /* Examine the link attributes and figure out which
1168 * network namespace we are talking about.
1169 */
1170 if (tb[IFLA_NET_NS_PID])
1171 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001172 else if (tb[IFLA_NET_NS_FD])
1173 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001174 else
1175 net = get_net(src_net);
1176 return net;
1177}
1178EXPORT_SYMBOL(rtnl_link_get_net);
1179
Thomas Graf1840bb12008-02-23 19:54:36 -08001180static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1181{
1182 if (dev) {
1183 if (tb[IFLA_ADDRESS] &&
1184 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1185 return -EINVAL;
1186
1187 if (tb[IFLA_BROADCAST] &&
1188 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1189 return -EINVAL;
1190 }
1191
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001192 if (tb[IFLA_AF_SPEC]) {
1193 struct nlattr *af;
1194 int rem, err;
1195
1196 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1197 const struct rtnl_af_ops *af_ops;
1198
1199 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1200 return -EAFNOSUPPORT;
1201
1202 if (!af_ops->set_link_af)
1203 return -EOPNOTSUPP;
1204
1205 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001206 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001207 if (err < 0)
1208 return err;
1209 }
1210 }
1211 }
1212
Thomas Graf1840bb12008-02-23 19:54:36 -08001213 return 0;
1214}
1215
Chris Wrightc02db8c2010-05-16 01:05:45 -07001216static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1217{
1218 int rem, err = -EINVAL;
1219 struct nlattr *vf;
1220 const struct net_device_ops *ops = dev->netdev_ops;
1221
1222 nla_for_each_nested(vf, attr, rem) {
1223 switch (nla_type(vf)) {
1224 case IFLA_VF_MAC: {
1225 struct ifla_vf_mac *ivm;
1226 ivm = nla_data(vf);
1227 err = -EOPNOTSUPP;
1228 if (ops->ndo_set_vf_mac)
1229 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1230 ivm->mac);
1231 break;
1232 }
1233 case IFLA_VF_VLAN: {
1234 struct ifla_vf_vlan *ivv;
1235 ivv = nla_data(vf);
1236 err = -EOPNOTSUPP;
1237 if (ops->ndo_set_vf_vlan)
1238 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1239 ivv->vlan,
1240 ivv->qos);
1241 break;
1242 }
1243 case IFLA_VF_TX_RATE: {
1244 struct ifla_vf_tx_rate *ivt;
1245 ivt = nla_data(vf);
1246 err = -EOPNOTSUPP;
1247 if (ops->ndo_set_vf_tx_rate)
1248 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1249 ivt->rate);
1250 break;
1251 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001252 case IFLA_VF_SPOOFCHK: {
1253 struct ifla_vf_spoofchk *ivs;
1254 ivs = nla_data(vf);
1255 err = -EOPNOTSUPP;
1256 if (ops->ndo_set_vf_spoofchk)
1257 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1258 ivs->setting);
1259 break;
1260 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001261 default:
1262 err = -EINVAL;
1263 break;
1264 }
1265 if (err)
1266 break;
1267 }
1268 return err;
1269}
1270
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001271static int do_set_master(struct net_device *dev, int ifindex)
1272{
1273 struct net_device *master_dev;
1274 const struct net_device_ops *ops;
1275 int err;
1276
1277 if (dev->master) {
1278 if (dev->master->ifindex == ifindex)
1279 return 0;
1280 ops = dev->master->netdev_ops;
1281 if (ops->ndo_del_slave) {
1282 err = ops->ndo_del_slave(dev->master, dev);
1283 if (err)
1284 return err;
1285 } else {
1286 return -EOPNOTSUPP;
1287 }
1288 }
1289
1290 if (ifindex) {
1291 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1292 if (!master_dev)
1293 return -EINVAL;
1294 ops = master_dev->netdev_ops;
1295 if (ops->ndo_add_slave) {
1296 err = ops->ndo_add_slave(master_dev, dev);
1297 if (err)
1298 return err;
1299 } else {
1300 return -EOPNOTSUPP;
1301 }
1302 }
1303 return 0;
1304}
1305
Patrick McHardy0157f602007-06-13 12:03:36 -07001306static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -07001307 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -07001308{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001309 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001310 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -07001311 int err;
1312
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001313 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001314 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001315 if (IS_ERR(net)) {
1316 err = PTR_ERR(net);
1317 goto errout;
1318 }
1319 err = dev_change_net_namespace(dev, net, ifname);
1320 put_net(net);
1321 if (err)
1322 goto errout;
1323 modified = 1;
1324 }
1325
Patrick McHardy0157f602007-06-13 12:03:36 -07001326 if (tb[IFLA_MAP]) {
1327 struct rtnl_link_ifmap *u_map;
1328 struct ifmap k_map;
1329
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001330 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001331 err = -EOPNOTSUPP;
1332 goto errout;
1333 }
1334
1335 if (!netif_device_present(dev)) {
1336 err = -ENODEV;
1337 goto errout;
1338 }
1339
1340 u_map = nla_data(tb[IFLA_MAP]);
1341 k_map.mem_start = (unsigned long) u_map->mem_start;
1342 k_map.mem_end = (unsigned long) u_map->mem_end;
1343 k_map.base_addr = (unsigned short) u_map->base_addr;
1344 k_map.irq = (unsigned char) u_map->irq;
1345 k_map.dma = (unsigned char) u_map->dma;
1346 k_map.port = (unsigned char) u_map->port;
1347
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001348 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001349 if (err < 0)
1350 goto errout;
1351
1352 modified = 1;
1353 }
1354
1355 if (tb[IFLA_ADDRESS]) {
1356 struct sockaddr *sa;
1357 int len;
1358
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001359 if (!ops->ndo_set_mac_address) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001360 err = -EOPNOTSUPP;
1361 goto errout;
1362 }
1363
1364 if (!netif_device_present(dev)) {
1365 err = -ENODEV;
1366 goto errout;
1367 }
1368
1369 len = sizeof(sa_family_t) + dev->addr_len;
1370 sa = kmalloc(len, GFP_KERNEL);
1371 if (!sa) {
1372 err = -ENOMEM;
1373 goto errout;
1374 }
1375 sa->sa_family = dev->type;
1376 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1377 dev->addr_len);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001378 err = ops->ndo_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001379 kfree(sa);
1380 if (err)
1381 goto errout;
1382 send_addr_notify = 1;
1383 modified = 1;
Theodore Ts'o7044edd2012-07-04 21:23:25 -04001384 add_device_randomness(dev->dev_addr, dev->addr_len);
Patrick McHardy0157f602007-06-13 12:03:36 -07001385 }
1386
1387 if (tb[IFLA_MTU]) {
1388 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1389 if (err < 0)
1390 goto errout;
1391 modified = 1;
1392 }
1393
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001394 if (tb[IFLA_GROUP]) {
1395 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1396 modified = 1;
1397 }
1398
Patrick McHardy0157f602007-06-13 12:03:36 -07001399 /*
1400 * Interface selected by interface index but interface
1401 * name provided implies that a name change has been
1402 * requested.
1403 */
1404 if (ifm->ifi_index > 0 && ifname[0]) {
1405 err = dev_change_name(dev, ifname);
1406 if (err < 0)
1407 goto errout;
1408 modified = 1;
1409 }
1410
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001411 if (tb[IFLA_IFALIAS]) {
1412 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1413 nla_len(tb[IFLA_IFALIAS]));
1414 if (err < 0)
1415 goto errout;
1416 modified = 1;
1417 }
1418
Patrick McHardy0157f602007-06-13 12:03:36 -07001419 if (tb[IFLA_BROADCAST]) {
1420 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1421 send_addr_notify = 1;
1422 }
1423
1424 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001425 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001426 if (err < 0)
1427 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001428 }
1429
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001430 if (tb[IFLA_MASTER]) {
1431 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1432 if (err)
1433 goto errout;
1434 modified = 1;
1435 }
1436
David S. Miller93b2d4a2008-02-17 18:35:07 -08001437 if (tb[IFLA_TXQLEN])
1438 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
Patrick McHardy0157f602007-06-13 12:03:36 -07001439
Patrick McHardy0157f602007-06-13 12:03:36 -07001440 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001441 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001442
1443 if (tb[IFLA_LINKMODE]) {
David S. Miller93b2d4a2008-02-17 18:35:07 -08001444 write_lock_bh(&dev_base_lock);
1445 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1446 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001447 }
1448
Chris Wrightc02db8c2010-05-16 01:05:45 -07001449 if (tb[IFLA_VFINFO_LIST]) {
1450 struct nlattr *attr;
1451 int rem;
1452 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001453 if (nla_type(attr) != IFLA_VF_INFO) {
1454 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001455 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001456 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001457 err = do_setvfinfo(dev, attr);
1458 if (err < 0)
1459 goto errout;
1460 modified = 1;
1461 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001462 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001463 err = 0;
1464
Scott Feldman57b61082010-05-17 22:49:55 -07001465 if (tb[IFLA_VF_PORTS]) {
1466 struct nlattr *port[IFLA_PORT_MAX+1];
1467 struct nlattr *attr;
1468 int vf;
1469 int rem;
1470
1471 err = -EOPNOTSUPP;
1472 if (!ops->ndo_set_vf_port)
1473 goto errout;
1474
1475 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1476 if (nla_type(attr) != IFLA_VF_PORT)
1477 continue;
1478 err = nla_parse_nested(port, IFLA_PORT_MAX,
1479 attr, ifla_port_policy);
1480 if (err < 0)
1481 goto errout;
1482 if (!port[IFLA_PORT_VF]) {
1483 err = -EOPNOTSUPP;
1484 goto errout;
1485 }
1486 vf = nla_get_u32(port[IFLA_PORT_VF]);
1487 err = ops->ndo_set_vf_port(dev, vf, port);
1488 if (err < 0)
1489 goto errout;
1490 modified = 1;
1491 }
1492 }
1493 err = 0;
1494
1495 if (tb[IFLA_PORT_SELF]) {
1496 struct nlattr *port[IFLA_PORT_MAX+1];
1497
1498 err = nla_parse_nested(port, IFLA_PORT_MAX,
1499 tb[IFLA_PORT_SELF], ifla_port_policy);
1500 if (err < 0)
1501 goto errout;
1502
1503 err = -EOPNOTSUPP;
1504 if (ops->ndo_set_vf_port)
1505 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1506 if (err < 0)
1507 goto errout;
1508 modified = 1;
1509 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001510
1511 if (tb[IFLA_AF_SPEC]) {
1512 struct nlattr *af;
1513 int rem;
1514
1515 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1516 const struct rtnl_af_ops *af_ops;
1517
1518 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001519 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001520
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001521 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001522 if (err < 0)
1523 goto errout;
1524
1525 modified = 1;
1526 }
1527 }
Scott Feldman57b61082010-05-17 22:49:55 -07001528 err = 0;
1529
Patrick McHardy0157f602007-06-13 12:03:36 -07001530errout:
1531 if (err < 0 && modified && net_ratelimit())
1532 printk(KERN_WARNING "A link change request failed with "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001533 "some changes committed already. Interface %s may "
Patrick McHardy0157f602007-06-13 12:03:36 -07001534 "have been left with an inconsistent configuration, "
1535 "please check.\n", dev->name);
1536
1537 if (send_addr_notify)
1538 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Stefan Gulaf18da142012-01-26 11:01:06 +00001539
Patrick McHardy0157f602007-06-13 12:03:36 -07001540 return err;
1541}
1542
Thomas Grafda5e0492006-08-10 21:17:37 -07001543static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001545 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001546 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001548 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001549 struct nlattr *tb[IFLA_MAX+1];
1550 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Thomas Grafda5e0492006-08-10 21:17:37 -07001552 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1553 if (err < 0)
1554 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Thomas Graf5176f912006-08-26 20:13:18 -07001556 if (tb[IFLA_IFNAME])
1557 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001558 else
1559 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001562 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001563 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001564 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001565 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001566 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001567 else
1568 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Grafda5e0492006-08-10 21:17:37 -07001570 if (dev == NULL) {
1571 err = -ENODEV;
1572 goto errout;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Eric Dumazete0d087a2009-11-07 01:26:17 -08001575 err = validate_linkmsg(dev, tb);
1576 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001577 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001578
Patrick McHardy38f7b872007-06-13 12:03:51 -07001579 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001580errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return err;
1582}
1583
Patrick McHardy38f7b872007-06-13 12:03:51 -07001584static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1585{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001586 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001587 const struct rtnl_link_ops *ops;
1588 struct net_device *dev;
1589 struct ifinfomsg *ifm;
1590 char ifname[IFNAMSIZ];
1591 struct nlattr *tb[IFLA_MAX+1];
1592 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001593 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001594
1595 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1596 if (err < 0)
1597 return err;
1598
1599 if (tb[IFLA_IFNAME])
1600 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1601
1602 ifm = nlmsg_data(nlh);
1603 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001604 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001605 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001606 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001607 else
1608 return -EINVAL;
1609
1610 if (!dev)
1611 return -ENODEV;
1612
1613 ops = dev->rtnl_link_ops;
1614 if (!ops)
1615 return -EOPNOTSUPP;
1616
Eric Dumazet226bd342011-05-08 23:17:57 +00001617 ops->dellink(dev, &list_kill);
1618 unregister_netdevice_many(&list_kill);
1619 list_del(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001620 return 0;
1621}
1622
Patrick McHardy3729d502010-02-26 06:34:54 +00001623int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1624{
1625 unsigned int old_flags;
1626 int err;
1627
1628 old_flags = dev->flags;
1629 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1630 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1631 if (err < 0)
1632 return err;
1633 }
1634
1635 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1636 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1637
1638 __dev_notify_flags(dev, old_flags);
1639 return 0;
1640}
1641EXPORT_SYMBOL(rtnl_configure_link);
1642
Eric W. Biederman81adee42009-11-08 00:53:51 -08001643struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1644 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001645{
1646 int err;
1647 struct net_device *dev;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001648 unsigned int num_queues = 1;
1649 unsigned int real_num_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001650
Eric Dumazet2e59af32009-09-02 18:03:00 -07001651 if (ops->get_tx_queues) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001652 err = ops->get_tx_queues(src_net, tb, &num_queues,
Eric Dumazete0d087a2009-11-07 01:26:17 -08001653 &real_num_queues);
Eric Dumazet2e59af32009-09-02 18:03:00 -07001654 if (err)
1655 goto err;
1656 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001657 err = -ENOMEM;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001658 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001659 if (!dev)
1660 goto err;
1661
Eric W. Biederman81adee42009-11-08 00:53:51 -08001662 dev_net_set(dev, net);
1663 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001664 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001665
Pavel Emelianove7199282007-08-08 22:16:38 -07001666 if (tb[IFLA_MTU])
1667 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1668 if (tb[IFLA_ADDRESS])
1669 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1670 nla_len(tb[IFLA_ADDRESS]));
1671 if (tb[IFLA_BROADCAST])
1672 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1673 nla_len(tb[IFLA_BROADCAST]));
1674 if (tb[IFLA_TXQLEN])
1675 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1676 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001677 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001678 if (tb[IFLA_LINKMODE])
1679 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001680 if (tb[IFLA_GROUP])
1681 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001682
1683 return dev;
1684
Pavel Emelianove7199282007-08-08 22:16:38 -07001685err:
1686 return ERR_PTR(err);
1687}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001688EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001689
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001690static int rtnl_group_changelink(struct net *net, int group,
1691 struct ifinfomsg *ifm,
1692 struct nlattr **tb)
1693{
1694 struct net_device *dev;
1695 int err;
1696
1697 for_each_netdev(net, dev) {
1698 if (dev->group == group) {
1699 err = do_setlink(dev, ifm, tb, NULL, 0);
1700 if (err < 0)
1701 return err;
1702 }
1703 }
1704
1705 return 0;
1706}
1707
Patrick McHardy38f7b872007-06-13 12:03:51 -07001708static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1709{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001710 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001711 const struct rtnl_link_ops *ops;
1712 struct net_device *dev;
1713 struct ifinfomsg *ifm;
1714 char kind[MODULE_NAME_LEN];
1715 char ifname[IFNAMSIZ];
1716 struct nlattr *tb[IFLA_MAX+1];
1717 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1718 int err;
1719
Johannes Berg95a5afc2008-10-16 15:24:51 -07001720#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001721replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001722#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001723 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1724 if (err < 0)
1725 return err;
1726
1727 if (tb[IFLA_IFNAME])
1728 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1729 else
1730 ifname[0] = '\0';
1731
1732 ifm = nlmsg_data(nlh);
1733 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001734 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001735 else {
1736 if (ifname[0])
1737 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001738 else
1739 dev = NULL;
1740 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001741
Eric Dumazete0d087a2009-11-07 01:26:17 -08001742 err = validate_linkmsg(dev, tb);
1743 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001744 return err;
1745
Patrick McHardy38f7b872007-06-13 12:03:51 -07001746 if (tb[IFLA_LINKINFO]) {
1747 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1748 tb[IFLA_LINKINFO], ifla_info_policy);
1749 if (err < 0)
1750 return err;
1751 } else
1752 memset(linkinfo, 0, sizeof(linkinfo));
1753
1754 if (linkinfo[IFLA_INFO_KIND]) {
1755 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1756 ops = rtnl_link_ops_get(kind);
1757 } else {
1758 kind[0] = '\0';
1759 ops = NULL;
1760 }
1761
1762 if (1) {
1763 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001764 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001765
1766 if (ops) {
1767 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1768 err = nla_parse_nested(attr, ops->maxtype,
1769 linkinfo[IFLA_INFO_DATA],
1770 ops->policy);
1771 if (err < 0)
1772 return err;
1773 data = attr;
1774 }
1775 if (ops->validate) {
1776 err = ops->validate(tb, data);
1777 if (err < 0)
1778 return err;
1779 }
1780 }
1781
1782 if (dev) {
1783 int modified = 0;
1784
1785 if (nlh->nlmsg_flags & NLM_F_EXCL)
1786 return -EEXIST;
1787 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1788 return -EOPNOTSUPP;
1789
1790 if (linkinfo[IFLA_INFO_DATA]) {
1791 if (!ops || ops != dev->rtnl_link_ops ||
1792 !ops->changelink)
1793 return -EOPNOTSUPP;
1794
1795 err = ops->changelink(dev, tb, data);
1796 if (err < 0)
1797 return err;
1798 modified = 1;
1799 }
1800
1801 return do_setlink(dev, ifm, tb, ifname, modified);
1802 }
1803
Patrick McHardyffa934f2011-01-20 03:00:42 +00001804 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1805 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1806 return rtnl_group_changelink(net,
1807 nla_get_u32(tb[IFLA_GROUP]),
1808 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001809 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00001810 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001811
Patrick McHardy3729d502010-02-26 06:34:54 +00001812 if (ifm->ifi_index)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001813 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001814 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001815 return -EOPNOTSUPP;
1816
1817 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001818#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001819 if (kind[0]) {
1820 __rtnl_unlock();
1821 request_module("rtnl-link-%s", kind);
1822 rtnl_lock();
1823 ops = rtnl_link_ops_get(kind);
1824 if (ops)
1825 goto replay;
1826 }
1827#endif
1828 return -EOPNOTSUPP;
1829 }
1830
1831 if (!ifname[0])
1832 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001833
Eric W. Biederman81adee42009-11-08 00:53:51 -08001834 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00001835 if (IS_ERR(dest_net))
1836 return PTR_ERR(dest_net);
1837
Eric W. Biederman81adee42009-11-08 00:53:51 -08001838 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001839
Pavel Emelianove7199282007-08-08 22:16:38 -07001840 if (IS_ERR(dev))
1841 err = PTR_ERR(dev);
1842 else if (ops->newlink)
Eric W. Biederman81adee42009-11-08 00:53:51 -08001843 err = ops->newlink(net, dev, tb, data);
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001844 else
1845 err = register_netdevice(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001846
1847 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001848 free_netdev(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001849 if (err < 0)
Patrick McHardy3729d502010-02-26 06:34:54 +00001850 goto out;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001851
Patrick McHardy3729d502010-02-26 06:34:54 +00001852 err = rtnl_configure_link(dev, ifm);
1853 if (err < 0)
1854 unregister_netdevice(dev);
1855out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08001856 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001857 return err;
1858 }
1859}
1860
Thomas Grafb60c5112006-08-04 23:05:34 -07001861static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001862{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001863 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07001864 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00001865 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07001866 struct nlattr *tb[IFLA_MAX+1];
1867 struct net_device *dev = NULL;
1868 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001869 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05001870 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001871
Thomas Grafb60c5112006-08-04 23:05:34 -07001872 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1873 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001874 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001875
Eric Dumazeta3d12892009-10-21 10:59:31 +00001876 if (tb[IFLA_IFNAME])
1877 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1878
Greg Rose115c9b82012-02-21 16:54:48 -05001879 if (tb[IFLA_EXT_MASK])
1880 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1881
Thomas Grafb60c5112006-08-04 23:05:34 -07001882 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001883 if (ifm->ifi_index > 0)
1884 dev = __dev_get_by_index(net, ifm->ifi_index);
1885 else if (tb[IFLA_IFNAME])
1886 dev = __dev_get_by_name(net, ifname);
1887 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001888 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001889
Eric Dumazeta3d12892009-10-21 10:59:31 +00001890 if (dev == NULL)
1891 return -ENODEV;
1892
Greg Rose115c9b82012-02-21 16:54:48 -05001893 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001894 if (nskb == NULL)
1895 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001896
Patrick McHardy575c3e22007-05-22 17:00:49 -07001897 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
Greg Rose115c9b82012-02-21 16:54:48 -05001898 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08001899 if (err < 0) {
1900 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1901 WARN_ON(err == -EMSGSIZE);
1902 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001903 } else
1904 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001905
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001906 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001907}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001908
Greg Rose115c9b82012-02-21 16:54:48 -05001909static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00001910{
Greg Rose115c9b82012-02-21 16:54:48 -05001911 struct net *net = sock_net(skb->sk);
1912 struct net_device *dev;
1913 struct nlattr *tb[IFLA_MAX+1];
1914 u32 ext_filter_mask = 0;
1915 u16 min_ifinfo_dump_size = 0;
1916
Michael Rieschb38a2412013-04-08 05:45:26 +00001917 if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001918 ifla_policy) >= 0) {
1919 if (tb[IFLA_EXT_MASK])
1920 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1921 }
Greg Rose115c9b82012-02-21 16:54:48 -05001922
1923 if (!ext_filter_mask)
1924 return NLMSG_GOODSIZE;
1925 /*
1926 * traverse the list of net devices and compute the minimum
1927 * buffer size based upon the filter mask.
1928 */
1929 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1930 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1931 if_nlmsg_size(dev,
1932 ext_filter_mask));
1933 }
1934
Greg Rosec7ac8672011-06-10 01:27:09 +00001935 return min_ifinfo_dump_size;
1936}
1937
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001938static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int idx;
1941 int s_idx = cb->family;
1942
1943 if (s_idx == 0)
1944 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02001945 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 int type = cb->nlh->nlmsg_type-RTM_BASE;
1947 if (idx < s_idx || idx == PF_PACKET)
1948 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001949 if (rtnl_msg_handlers[idx] == NULL ||
1950 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 continue;
1952 if (idx > s_idx)
1953 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001954 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956 }
1957 cb->family = idx;
1958
1959 return skb->len;
1960}
1961
1962void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1963{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001964 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001966 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001967 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Greg Rose115c9b82012-02-21 16:54:48 -05001969 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001970 if (skb == NULL)
1971 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Greg Rose115c9b82012-02-21 16:54:48 -05001973 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001974 if (err < 0) {
1975 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1976 WARN_ON(err == -EMSGSIZE);
1977 kfree_skb(skb);
1978 goto errout;
1979 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001980 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1981 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001982errout:
1983 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08001984 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987/* Protected by RTNL sempahore. */
1988static struct rtattr **rta_buf;
1989static int rtattr_max;
1990
1991/* Process one rtnetlink message. */
1992
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001993static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001995 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07001996 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int sz_idx, kind;
1998 int min_len;
1999 int family;
2000 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00002001 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07002005 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 type -= RTM_BASE;
2008
2009 /* All the messages must have at least 1 byte length */
2010 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2011 return 0;
2012
Eric Dumazete0d087a2009-11-07 01:26:17 -08002013 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 sz_idx = type>>2;
2015 kind = type&3;
2016
Eric Parisfd778462012-01-03 12:25:16 -05002017 if (kind != 2 && !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002018 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
David S. Millerb8f3ab42011-01-18 12:40:38 -08002020 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002021 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07002022 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00002023 rtnl_calcit_func calcit;
2024 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Thomas Grafe2849862007-03-22 11:48:11 -07002026 dumpit = rtnl_get_dumpit(family, type);
2027 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002028 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00002029 calcit = rtnl_get_calcit(family, type);
2030 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05002031 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Eric Dumazet2907c352011-05-25 07:34:04 +00002033 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002034 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002035 {
2036 struct netlink_dump_control c = {
2037 .dump = dumpit,
2038 .min_dump_alloc = min_dump_alloc,
2039 };
2040 err = netlink_dump_start(rtnl, skb, nlh, &c);
2041 }
Eric Dumazet2907c352011-05-25 07:34:04 +00002042 rtnl_lock();
2043 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
2046 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2047
2048 min_len = rtm_min[sz_idx];
2049 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 if (nlh->nlmsg_len > min_len) {
2053 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Eric Dumazete0d087a2009-11-07 01:26:17 -08002054 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 while (RTA_OK(attr, attrlen)) {
Vlad Yasevich40abddfd2013-03-13 04:18:58 +00002057 unsigned int flavor = attr->rta_type & NLA_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (flavor) {
2059 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 rta_buf[flavor-1] = attr;
2062 }
2063 attr = RTA_NEXT(attr, attrlen);
2064 }
2065 }
2066
Thomas Grafe2849862007-03-22 11:48:11 -07002067 doit = rtnl_get_doit(family, type);
2068 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002069 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002071 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002074static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002076 rtnl_lock();
2077 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2078 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2082{
2083 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 case NETDEV_UP:
2087 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00002088 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002089 case NETDEV_POST_INIT:
2090 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00002092 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00002094 case NETDEV_UNREGISTER:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002095 case NETDEV_UNREGISTER_BATCH:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00002096 case NETDEV_RELEASE:
2097 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
2099 default:
2100 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2101 break;
2102 }
2103 return NOTIFY_DONE;
2104}
2105
2106static struct notifier_block rtnetlink_dev_notifier = {
2107 .notifier_call = rtnetlink_event,
2108};
2109
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002110
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002111static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002112{
2113 struct sock *sk;
2114 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
Eric Dumazet2907c352011-05-25 07:34:04 +00002115 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002116 if (!sk)
2117 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002118 net->rtnl = sk;
2119 return 0;
2120}
2121
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002122static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002123{
Denis V. Lunev775516b2008-01-18 23:55:19 -08002124 netlink_kernel_release(net->rtnl);
2125 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002126}
2127
2128static struct pernet_operations rtnetlink_net_ops = {
2129 .init = rtnetlink_net_init,
2130 .exit = rtnetlink_net_exit,
2131};
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133void __init rtnetlink_init(void)
2134{
2135 int i;
2136
2137 rtattr_max = 0;
2138 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2139 if (rta_max[i] > rtattr_max)
2140 rtattr_max = rta_max[i];
2141 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2142 if (!rta_buf)
2143 panic("rtnetlink_init: cannot allocate rta_buf\n");
2144
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002145 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2149 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07002150
Greg Rosec7ac8672011-06-10 01:27:09 +00002151 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2152 rtnl_dump_ifinfo, rtnl_calcit);
2153 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2154 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2155 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07002156
Greg Rosec7ac8672011-06-10 01:27:09 +00002157 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2158 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160