blob: 782ab9d1b806926c9edf06b8d02ffc0fa115b106 [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
David Gibson76eb96e2014-04-24 10:22:36 +1000751static size_t rtnl_port_size(const struct net_device *dev,
752 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700753{
754 size_t port_size = nla_total_size(4) /* PORT_VF */
755 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
756 + nla_total_size(sizeof(struct ifla_port_vsi))
757 /* PORT_VSI_TYPE */
758 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
759 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
760 + nla_total_size(1) /* PROT_VDP_REQUEST */
761 + nla_total_size(2); /* PORT_VDP_RESPONSE */
762 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
763 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
764 + port_size;
765 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
766 + port_size;
767
David Gibson76eb96e2014-04-24 10:22:36 +1000768 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
769 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700770 return 0;
771 if (dev_num_vf(dev->dev.parent))
772 return port_self_size + vf_ports_size +
773 vf_port_size * dev_num_vf(dev->dev.parent);
774 else
775 return port_self_size;
776}
777
Greg Rose115c9b82012-02-21 16:54:48 -0500778static noinline size_t if_nlmsg_size(const struct net_device *dev,
779 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800780{
781 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
782 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700783 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800784 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
785 + nla_total_size(sizeof(struct rtnl_link_ifmap))
786 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700787 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800788 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
789 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
790 + nla_total_size(4) /* IFLA_TXQLEN */
791 + nla_total_size(4) /* IFLA_WEIGHT */
792 + nla_total_size(4) /* IFLA_MTU */
793 + nla_total_size(4) /* IFLA_LINK */
794 + nla_total_size(4) /* IFLA_MASTER */
795 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700796 + nla_total_size(1) /* IFLA_LINKMODE */
Greg Rose115c9b82012-02-21 16:54:48 -0500797 + nla_total_size(ext_filter_mask
798 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
799 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibson76eb96e2014-04-24 10:22:36 +1000800 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000801 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
802 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
Thomas Graf339bf982006-11-10 14:10:15 -0800803}
804
Scott Feldman57b61082010-05-17 22:49:55 -0700805static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
806{
807 struct nlattr *vf_ports;
808 struct nlattr *vf_port;
809 int vf;
810 int err;
811
812 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
813 if (!vf_ports)
814 return -EMSGSIZE;
815
816 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
817 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700818 if (!vf_port)
819 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700820 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
821 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700822 if (err == -EMSGSIZE)
823 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700824 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700825 nla_nest_cancel(skb, vf_port);
826 continue;
827 }
828 nla_nest_end(skb, vf_port);
829 }
830
831 nla_nest_end(skb, vf_ports);
832
833 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700834
835nla_put_failure:
836 nla_nest_cancel(skb, vf_ports);
837 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700838}
839
840static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
841{
842 struct nlattr *port_self;
843 int err;
844
845 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
846 if (!port_self)
847 return -EMSGSIZE;
848
849 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
850 if (err) {
851 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700852 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700853 }
854
855 nla_nest_end(skb, port_self);
856
857 return 0;
858}
859
David Gibson76eb96e2014-04-24 10:22:36 +1000860static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
861 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700862{
863 int err;
864
David Gibson76eb96e2014-04-24 10:22:36 +1000865 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
866 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700867 return 0;
868
869 err = rtnl_port_self_fill(skb, dev);
870 if (err)
871 return err;
872
873 if (dev_num_vf(dev->dev.parent)) {
874 err = rtnl_vf_ports_fill(skb, dev);
875 if (err)
876 return err;
877 }
878
879 return 0;
880}
881
Thomas Grafb60c5112006-08-04 23:05:34 -0700882static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700883 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -0500884 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -0700885{
886 struct ifinfomsg *ifm;
887 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -0700888 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000889 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +0000890 struct nlattr *attr, *af_spec;
891 struct rtnl_af_ops *af_ops;
Thomas Grafb60c5112006-08-04 23:05:34 -0700892
Eric Dumazet2907c352011-05-25 07:34:04 +0000893 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -0700894 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
895 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800896 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700897
898 ifm = nlmsg_data(nlh);
899 ifm->ifi_family = AF_UNSPEC;
900 ifm->__ifi_pad = 0;
901 ifm->ifi_type = dev->type;
902 ifm->ifi_index = dev->ifindex;
903 ifm->ifi_flags = dev_get_flags(dev);
904 ifm->ifi_change = change;
905
906 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
907 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700908 NLA_PUT_U8(skb, IFLA_OPERSTATE,
909 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
910 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
911 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Vlad Dogarucbda10f2011-01-13 23:38:30 +0000912 NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
Thomas Grafb60c5112006-08-04 23:05:34 -0700913
914 if (dev->ifindex != dev->iflink)
915 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
916
917 if (dev->master)
918 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
919
Patrick McHardyaf356af2009-09-04 06:41:18 +0000920 if (dev->qdisc)
921 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800922
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700923 if (dev->ifalias)
924 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
925
Stefan Rompfb00055a2006-03-20 17:09:11 -0800926 if (1) {
Teow Wan Yeeaf9dd6d2016-07-26 16:53:39 +0800927 struct rtnl_link_ifmap map;
928
929 memset(&map, 0, sizeof(map));
930 map.mem_start = dev->mem_start;
931 map.mem_end = dev->mem_end;
932 map.base_addr = dev->base_addr;
933 map.irq = dev->irq;
934 map.dma = dev->dma;
935 map.port = dev->if_port;
936
Thomas Grafb60c5112006-08-04 23:05:34 -0700937 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700941 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
942 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700945 attr = nla_reserve(skb, IFLA_STATS,
946 sizeof(struct rtnl_link_stats));
947 if (attr == NULL)
948 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Dumazet28172732010-07-07 14:58:56 -0700950 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700951 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Jan Engelhardt10708f32010-03-11 09:57:29 +0000953 attr = nla_reserve(skb, IFLA_STATS64,
954 sizeof(struct rtnl_link_stats64));
955 if (attr == NULL)
956 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000957 copy_rtnl_link_stats64(nla_data(attr), stats);
958
Greg Rose115c9b82012-02-21 16:54:48 -0500959 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700960 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
961
Greg Rose115c9b82012-02-21 16:54:48 -0500962 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
963 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000964 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000965
Chris Wrightc02db8c2010-05-16 01:05:45 -0700966 struct nlattr *vfinfo, *vf;
967 int num_vfs = dev_num_vf(dev->dev.parent);
968
Chris Wrightc02db8c2010-05-16 01:05:45 -0700969 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
970 if (!vfinfo)
971 goto nla_put_failure;
972 for (i = 0; i < num_vfs; i++) {
973 struct ifla_vf_info ivi;
974 struct ifla_vf_mac vf_mac;
975 struct ifla_vf_vlan vf_vlan;
976 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000977 struct ifla_vf_spoofchk vf_spoofchk;
978
979 /*
980 * Not all SR-IOV capable drivers support the
981 * spoofcheck query. Preset to -1 so the user
982 * space tool can detect that the driver didn't
983 * report anything.
984 */
985 ivi.spoofchk = -1;
Mathias Krause75deb322013-03-09 05:52:20 +0000986 memset(ivi.mac, 0, sizeof(ivi.mac));
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000987 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
988 break;
Greg Rose5f8444a2011-10-08 03:05:24 +0000989 vf_mac.vf =
990 vf_vlan.vf =
991 vf_tx_rate.vf =
992 vf_spoofchk.vf = ivi.vf;
993
Chris Wrightc02db8c2010-05-16 01:05:45 -0700994 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
995 vf_vlan.vlan = ivi.vlan;
996 vf_vlan.qos = ivi.qos;
997 vf_tx_rate.rate = ivi.tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000998 vf_spoofchk.setting = ivi.spoofchk;
Chris Wrightc02db8c2010-05-16 01:05:45 -0700999 vf = nla_nest_start(skb, IFLA_VF_INFO);
1000 if (!vf) {
1001 nla_nest_cancel(skb, vfinfo);
1002 goto nla_put_failure;
1003 }
1004 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
1005 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
Greg Rose5f8444a2011-10-08 03:05:24 +00001006 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1007 &vf_tx_rate);
1008 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1009 &vf_spoofchk);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001010 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001011 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001012 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001013 }
Scott Feldman57b61082010-05-17 22:49:55 -07001014
David Gibson76eb96e2014-04-24 10:22:36 +10001015 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001016 goto nla_put_failure;
1017
Patrick McHardy38f7b872007-06-13 12:03:51 -07001018 if (dev->rtnl_link_ops) {
1019 if (rtnl_link_fill(skb, dev) < 0)
1020 goto nla_put_failure;
1021 }
1022
Thomas Graff8ff1822010-11-16 04:30:14 +00001023 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1024 goto nla_put_failure;
1025
1026 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1027 if (af_ops->fill_link_af) {
1028 struct nlattr *af;
1029 int err;
1030
1031 if (!(af = nla_nest_start(skb, af_ops->family)))
1032 goto nla_put_failure;
1033
1034 err = af_ops->fill_link_af(skb, dev);
1035
1036 /*
1037 * Caller may return ENODATA to indicate that there
1038 * was no data to be dumped. This is not an error, it
1039 * means we should trim the attribute header and
1040 * continue.
1041 */
1042 if (err == -ENODATA)
1043 nla_nest_cancel(skb, af);
1044 else if (err < 0)
1045 goto nla_put_failure;
1046
1047 nla_nest_end(skb, af);
1048 }
1049 }
1050
1051 nla_nest_end(skb, af_spec);
1052
Thomas Grafb60c5112006-08-04 23:05:34 -07001053 return nlmsg_end(skb, nlh);
1054
1055nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001056 nlmsg_cancel(skb, nlh);
1057 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Thomas Grafb60c5112006-08-04 23:05:34 -07001060static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001062 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001063 int h, s_h;
1064 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001066 struct hlist_head *head;
1067 struct hlist_node *node;
Greg Rose115c9b82012-02-21 16:54:48 -05001068 struct nlattr *tb[IFLA_MAX+1];
1069 u32 ext_filter_mask = 0;
David Gibson6771e082014-04-24 10:22:35 +10001070 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001072 s_h = cb->args[0];
1073 s_idx = cb->args[1];
1074
Eric Dumazete67f88d2011-04-27 22:56:07 +00001075 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001076 cb->seq = net->dev_base_seq;
1077
Michael Rieschb38a2412013-04-08 05:45:26 +00001078 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001079 ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001080
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001081 if (tb[IFLA_EXT_MASK])
1082 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1083 }
Greg Rose115c9b82012-02-21 16:54:48 -05001084
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001085 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1086 idx = 0;
1087 head = &net->dev_index_head[h];
Eric Dumazete67f88d2011-04-27 22:56:07 +00001088 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001089 if (idx < s_idx)
1090 goto cont;
David Gibson6771e082014-04-24 10:22:35 +10001091 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1092 NETLINK_CB(cb->skb).pid,
1093 cb->nlh->nlmsg_seq, 0,
1094 NLM_F_MULTI,
1095 ext_filter_mask);
1096 /* If we ran out of room on the first message,
1097 * we're in trouble
1098 */
1099 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1100
1101 if (err <= 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001102 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001103
1104 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001105cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001106 idx++;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001109out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001110 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001111 cb->args[1] = idx;
1112 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return skb->len;
1115}
1116
Pavel Emelianove7199282007-08-08 22:16:38 -07001117const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07001118 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -07001119 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1120 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -07001121 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -07001122 [IFLA_MTU] = { .type = NLA_U32 },
Thomas Graf76e87302008-02-19 16:12:08 -08001123 [IFLA_LINK] = { .type = NLA_U32 },
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001124 [IFLA_MASTER] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -07001125 [IFLA_TXQLEN] = { .type = NLA_U32 },
1126 [IFLA_WEIGHT] = { .type = NLA_U32 },
1127 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1128 [IFLA_LINKMODE] = { .type = NLA_U8 },
Thomas Graf76e87302008-02-19 16:12:08 -08001129 [IFLA_LINKINFO] = { .type = NLA_NESTED },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001130 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001131 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001132 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
Chris Wrightc02db8c2010-05-16 01:05:45 -07001133 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
Scott Feldman57b61082010-05-17 22:49:55 -07001134 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1135 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
Thomas Graff8ff1822010-11-16 04:30:14 +00001136 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
Greg Rose115c9b82012-02-21 16:54:48 -05001137 [IFLA_EXT_MASK] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -07001138};
Eric Dumazete0d087a2009-11-07 01:26:17 -08001139EXPORT_SYMBOL(ifla_policy);
Thomas Grafda5e0492006-08-10 21:17:37 -07001140
Patrick McHardy38f7b872007-06-13 12:03:51 -07001141static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1142 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1143 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1144};
1145
Chris Wrightc02db8c2010-05-16 01:05:45 -07001146static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1147 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1148};
1149
1150static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1151 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1152 .len = sizeof(struct ifla_vf_mac) },
1153 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1154 .len = sizeof(struct ifla_vf_vlan) },
1155 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1156 .len = sizeof(struct ifla_vf_tx_rate) },
Greg Rose48752f62012-02-08 00:45:00 +00001157 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1158 .len = sizeof(struct ifla_vf_spoofchk) },
Chris Wrightc02db8c2010-05-16 01:05:45 -07001159};
1160
Scott Feldman57b61082010-05-17 22:49:55 -07001161static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1162 [IFLA_PORT_VF] = { .type = NLA_U32 },
1163 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1164 .len = PORT_PROFILE_MAX },
1165 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1166 .len = sizeof(struct ifla_port_vsi)},
1167 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1168 .len = PORT_UUID_MAX },
1169 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1170 .len = PORT_UUID_MAX },
1171 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1172 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1173};
1174
Eric W. Biederman81adee42009-11-08 00:53:51 -08001175struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1176{
1177 struct net *net;
1178 /* Examine the link attributes and figure out which
1179 * network namespace we are talking about.
1180 */
1181 if (tb[IFLA_NET_NS_PID])
1182 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001183 else if (tb[IFLA_NET_NS_FD])
1184 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001185 else
1186 net = get_net(src_net);
1187 return net;
1188}
1189EXPORT_SYMBOL(rtnl_link_get_net);
1190
Thomas Graf1840bb12008-02-23 19:54:36 -08001191static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1192{
1193 if (dev) {
1194 if (tb[IFLA_ADDRESS] &&
1195 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1196 return -EINVAL;
1197
1198 if (tb[IFLA_BROADCAST] &&
1199 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1200 return -EINVAL;
1201 }
1202
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001203 if (tb[IFLA_AF_SPEC]) {
1204 struct nlattr *af;
1205 int rem, err;
1206
1207 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1208 const struct rtnl_af_ops *af_ops;
1209
1210 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1211 return -EAFNOSUPPORT;
1212
1213 if (!af_ops->set_link_af)
1214 return -EOPNOTSUPP;
1215
1216 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001217 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001218 if (err < 0)
1219 return err;
1220 }
1221 }
1222 }
1223
Thomas Graf1840bb12008-02-23 19:54:36 -08001224 return 0;
1225}
1226
Chris Wrightc02db8c2010-05-16 01:05:45 -07001227static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1228{
1229 int rem, err = -EINVAL;
1230 struct nlattr *vf;
1231 const struct net_device_ops *ops = dev->netdev_ops;
1232
1233 nla_for_each_nested(vf, attr, rem) {
1234 switch (nla_type(vf)) {
1235 case IFLA_VF_MAC: {
1236 struct ifla_vf_mac *ivm;
1237 ivm = nla_data(vf);
1238 err = -EOPNOTSUPP;
1239 if (ops->ndo_set_vf_mac)
1240 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1241 ivm->mac);
1242 break;
1243 }
1244 case IFLA_VF_VLAN: {
1245 struct ifla_vf_vlan *ivv;
1246 ivv = nla_data(vf);
1247 err = -EOPNOTSUPP;
1248 if (ops->ndo_set_vf_vlan)
1249 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1250 ivv->vlan,
1251 ivv->qos);
1252 break;
1253 }
1254 case IFLA_VF_TX_RATE: {
1255 struct ifla_vf_tx_rate *ivt;
1256 ivt = nla_data(vf);
1257 err = -EOPNOTSUPP;
1258 if (ops->ndo_set_vf_tx_rate)
1259 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1260 ivt->rate);
1261 break;
1262 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001263 case IFLA_VF_SPOOFCHK: {
1264 struct ifla_vf_spoofchk *ivs;
1265 ivs = nla_data(vf);
1266 err = -EOPNOTSUPP;
1267 if (ops->ndo_set_vf_spoofchk)
1268 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1269 ivs->setting);
1270 break;
1271 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001272 default:
1273 err = -EINVAL;
1274 break;
1275 }
1276 if (err)
1277 break;
1278 }
1279 return err;
1280}
1281
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001282static int do_set_master(struct net_device *dev, int ifindex)
1283{
1284 struct net_device *master_dev;
1285 const struct net_device_ops *ops;
1286 int err;
1287
1288 if (dev->master) {
1289 if (dev->master->ifindex == ifindex)
1290 return 0;
1291 ops = dev->master->netdev_ops;
1292 if (ops->ndo_del_slave) {
1293 err = ops->ndo_del_slave(dev->master, dev);
1294 if (err)
1295 return err;
1296 } else {
1297 return -EOPNOTSUPP;
1298 }
1299 }
1300
1301 if (ifindex) {
1302 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1303 if (!master_dev)
1304 return -EINVAL;
1305 ops = master_dev->netdev_ops;
1306 if (ops->ndo_add_slave) {
1307 err = ops->ndo_add_slave(master_dev, dev);
1308 if (err)
1309 return err;
1310 } else {
1311 return -EOPNOTSUPP;
1312 }
1313 }
1314 return 0;
1315}
1316
Patrick McHardy0157f602007-06-13 12:03:36 -07001317static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -07001318 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -07001319{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001320 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001321 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -07001322 int err;
1323
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001324 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001325 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001326 if (IS_ERR(net)) {
1327 err = PTR_ERR(net);
1328 goto errout;
1329 }
1330 err = dev_change_net_namespace(dev, net, ifname);
1331 put_net(net);
1332 if (err)
1333 goto errout;
1334 modified = 1;
1335 }
1336
Patrick McHardy0157f602007-06-13 12:03:36 -07001337 if (tb[IFLA_MAP]) {
1338 struct rtnl_link_ifmap *u_map;
1339 struct ifmap k_map;
1340
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001341 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001342 err = -EOPNOTSUPP;
1343 goto errout;
1344 }
1345
1346 if (!netif_device_present(dev)) {
1347 err = -ENODEV;
1348 goto errout;
1349 }
1350
1351 u_map = nla_data(tb[IFLA_MAP]);
1352 k_map.mem_start = (unsigned long) u_map->mem_start;
1353 k_map.mem_end = (unsigned long) u_map->mem_end;
1354 k_map.base_addr = (unsigned short) u_map->base_addr;
1355 k_map.irq = (unsigned char) u_map->irq;
1356 k_map.dma = (unsigned char) u_map->dma;
1357 k_map.port = (unsigned char) u_map->port;
1358
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001359 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001360 if (err < 0)
1361 goto errout;
1362
1363 modified = 1;
1364 }
1365
1366 if (tb[IFLA_ADDRESS]) {
1367 struct sockaddr *sa;
1368 int len;
1369
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001370 if (!ops->ndo_set_mac_address) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001371 err = -EOPNOTSUPP;
1372 goto errout;
1373 }
1374
1375 if (!netif_device_present(dev)) {
1376 err = -ENODEV;
1377 goto errout;
1378 }
1379
1380 len = sizeof(sa_family_t) + dev->addr_len;
1381 sa = kmalloc(len, GFP_KERNEL);
1382 if (!sa) {
1383 err = -ENOMEM;
1384 goto errout;
1385 }
1386 sa->sa_family = dev->type;
1387 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1388 dev->addr_len);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001389 err = ops->ndo_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001390 kfree(sa);
1391 if (err)
1392 goto errout;
1393 send_addr_notify = 1;
1394 modified = 1;
Theodore Ts'o7044edd2012-07-04 21:23:25 -04001395 add_device_randomness(dev->dev_addr, dev->addr_len);
Patrick McHardy0157f602007-06-13 12:03:36 -07001396 }
1397
1398 if (tb[IFLA_MTU]) {
1399 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1400 if (err < 0)
1401 goto errout;
1402 modified = 1;
1403 }
1404
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001405 if (tb[IFLA_GROUP]) {
1406 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1407 modified = 1;
1408 }
1409
Patrick McHardy0157f602007-06-13 12:03:36 -07001410 /*
1411 * Interface selected by interface index but interface
1412 * name provided implies that a name change has been
1413 * requested.
1414 */
1415 if (ifm->ifi_index > 0 && ifname[0]) {
1416 err = dev_change_name(dev, ifname);
1417 if (err < 0)
1418 goto errout;
1419 modified = 1;
1420 }
1421
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001422 if (tb[IFLA_IFALIAS]) {
1423 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1424 nla_len(tb[IFLA_IFALIAS]));
1425 if (err < 0)
1426 goto errout;
1427 modified = 1;
1428 }
1429
Patrick McHardy0157f602007-06-13 12:03:36 -07001430 if (tb[IFLA_BROADCAST]) {
1431 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1432 send_addr_notify = 1;
1433 }
1434
1435 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001436 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001437 if (err < 0)
1438 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001439 }
1440
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001441 if (tb[IFLA_MASTER]) {
1442 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1443 if (err)
1444 goto errout;
1445 modified = 1;
1446 }
1447
David S. Miller93b2d4a2008-02-17 18:35:07 -08001448 if (tb[IFLA_TXQLEN])
1449 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
Patrick McHardy0157f602007-06-13 12:03:36 -07001450
Patrick McHardy0157f602007-06-13 12:03:36 -07001451 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001452 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001453
1454 if (tb[IFLA_LINKMODE]) {
David S. Miller93b2d4a2008-02-17 18:35:07 -08001455 write_lock_bh(&dev_base_lock);
1456 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1457 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001458 }
1459
Chris Wrightc02db8c2010-05-16 01:05:45 -07001460 if (tb[IFLA_VFINFO_LIST]) {
1461 struct nlattr *attr;
1462 int rem;
1463 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001464 if (nla_type(attr) != IFLA_VF_INFO) {
1465 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001466 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001467 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001468 err = do_setvfinfo(dev, attr);
1469 if (err < 0)
1470 goto errout;
1471 modified = 1;
1472 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001473 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001474 err = 0;
1475
Scott Feldman57b61082010-05-17 22:49:55 -07001476 if (tb[IFLA_VF_PORTS]) {
1477 struct nlattr *port[IFLA_PORT_MAX+1];
1478 struct nlattr *attr;
1479 int vf;
1480 int rem;
1481
1482 err = -EOPNOTSUPP;
1483 if (!ops->ndo_set_vf_port)
1484 goto errout;
1485
1486 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1487 if (nla_type(attr) != IFLA_VF_PORT)
1488 continue;
1489 err = nla_parse_nested(port, IFLA_PORT_MAX,
1490 attr, ifla_port_policy);
1491 if (err < 0)
1492 goto errout;
1493 if (!port[IFLA_PORT_VF]) {
1494 err = -EOPNOTSUPP;
1495 goto errout;
1496 }
1497 vf = nla_get_u32(port[IFLA_PORT_VF]);
1498 err = ops->ndo_set_vf_port(dev, vf, port);
1499 if (err < 0)
1500 goto errout;
1501 modified = 1;
1502 }
1503 }
1504 err = 0;
1505
1506 if (tb[IFLA_PORT_SELF]) {
1507 struct nlattr *port[IFLA_PORT_MAX+1];
1508
1509 err = nla_parse_nested(port, IFLA_PORT_MAX,
1510 tb[IFLA_PORT_SELF], ifla_port_policy);
1511 if (err < 0)
1512 goto errout;
1513
1514 err = -EOPNOTSUPP;
1515 if (ops->ndo_set_vf_port)
1516 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1517 if (err < 0)
1518 goto errout;
1519 modified = 1;
1520 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001521
1522 if (tb[IFLA_AF_SPEC]) {
1523 struct nlattr *af;
1524 int rem;
1525
1526 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1527 const struct rtnl_af_ops *af_ops;
1528
1529 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001530 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001531
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001532 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001533 if (err < 0)
1534 goto errout;
1535
1536 modified = 1;
1537 }
1538 }
Scott Feldman57b61082010-05-17 22:49:55 -07001539 err = 0;
1540
Patrick McHardy0157f602007-06-13 12:03:36 -07001541errout:
1542 if (err < 0 && modified && net_ratelimit())
1543 printk(KERN_WARNING "A link change request failed with "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001544 "some changes committed already. Interface %s may "
Patrick McHardy0157f602007-06-13 12:03:36 -07001545 "have been left with an inconsistent configuration, "
1546 "please check.\n", dev->name);
1547
1548 if (send_addr_notify)
1549 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Stefan Gulaf18da142012-01-26 11:01:06 +00001550
Patrick McHardy0157f602007-06-13 12:03:36 -07001551 return err;
1552}
1553
Thomas Grafda5e0492006-08-10 21:17:37 -07001554static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001556 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001557 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001559 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001560 struct nlattr *tb[IFLA_MAX+1];
1561 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Thomas Grafda5e0492006-08-10 21:17:37 -07001563 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1564 if (err < 0)
1565 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Thomas Graf5176f912006-08-26 20:13:18 -07001567 if (tb[IFLA_IFNAME])
1568 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001569 else
1570 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001573 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001574 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001575 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001576 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001577 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001578 else
1579 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Thomas Grafda5e0492006-08-10 21:17:37 -07001581 if (dev == NULL) {
1582 err = -ENODEV;
1583 goto errout;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Eric Dumazete0d087a2009-11-07 01:26:17 -08001586 err = validate_linkmsg(dev, tb);
1587 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001588 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001589
Patrick McHardy38f7b872007-06-13 12:03:51 -07001590 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001591errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return err;
1593}
1594
Patrick McHardy38f7b872007-06-13 12:03:51 -07001595static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1596{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001597 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001598 const struct rtnl_link_ops *ops;
1599 struct net_device *dev;
1600 struct ifinfomsg *ifm;
1601 char ifname[IFNAMSIZ];
1602 struct nlattr *tb[IFLA_MAX+1];
1603 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001604 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001605
1606 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1607 if (err < 0)
1608 return err;
1609
1610 if (tb[IFLA_IFNAME])
1611 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1612
1613 ifm = nlmsg_data(nlh);
1614 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001615 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001616 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001617 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001618 else
1619 return -EINVAL;
1620
1621 if (!dev)
1622 return -ENODEV;
1623
1624 ops = dev->rtnl_link_ops;
1625 if (!ops)
1626 return -EOPNOTSUPP;
1627
Eric Dumazet226bd342011-05-08 23:17:57 +00001628 ops->dellink(dev, &list_kill);
1629 unregister_netdevice_many(&list_kill);
1630 list_del(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001631 return 0;
1632}
1633
Patrick McHardy3729d502010-02-26 06:34:54 +00001634int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1635{
1636 unsigned int old_flags;
1637 int err;
1638
1639 old_flags = dev->flags;
1640 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1641 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1642 if (err < 0)
1643 return err;
1644 }
1645
1646 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1647 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1648
1649 __dev_notify_flags(dev, old_flags);
1650 return 0;
1651}
1652EXPORT_SYMBOL(rtnl_configure_link);
1653
Eric W. Biederman81adee42009-11-08 00:53:51 -08001654struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1655 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001656{
1657 int err;
1658 struct net_device *dev;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001659 unsigned int num_queues = 1;
1660 unsigned int real_num_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001661
Eric Dumazet2e59af32009-09-02 18:03:00 -07001662 if (ops->get_tx_queues) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001663 err = ops->get_tx_queues(src_net, tb, &num_queues,
Eric Dumazete0d087a2009-11-07 01:26:17 -08001664 &real_num_queues);
Eric Dumazet2e59af32009-09-02 18:03:00 -07001665 if (err)
1666 goto err;
1667 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001668 err = -ENOMEM;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001669 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001670 if (!dev)
1671 goto err;
1672
Eric W. Biederman81adee42009-11-08 00:53:51 -08001673 dev_net_set(dev, net);
1674 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001675 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001676
Pavel Emelianove7199282007-08-08 22:16:38 -07001677 if (tb[IFLA_MTU])
1678 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1679 if (tb[IFLA_ADDRESS])
1680 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1681 nla_len(tb[IFLA_ADDRESS]));
1682 if (tb[IFLA_BROADCAST])
1683 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1684 nla_len(tb[IFLA_BROADCAST]));
1685 if (tb[IFLA_TXQLEN])
1686 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1687 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001688 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001689 if (tb[IFLA_LINKMODE])
1690 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001691 if (tb[IFLA_GROUP])
1692 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001693
1694 return dev;
1695
Pavel Emelianove7199282007-08-08 22:16:38 -07001696err:
1697 return ERR_PTR(err);
1698}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001699EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001700
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001701static int rtnl_group_changelink(struct net *net, int group,
1702 struct ifinfomsg *ifm,
1703 struct nlattr **tb)
1704{
1705 struct net_device *dev;
1706 int err;
1707
1708 for_each_netdev(net, dev) {
1709 if (dev->group == group) {
1710 err = do_setlink(dev, ifm, tb, NULL, 0);
1711 if (err < 0)
1712 return err;
1713 }
1714 }
1715
1716 return 0;
1717}
1718
Patrick McHardy38f7b872007-06-13 12:03:51 -07001719static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1720{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001721 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001722 const struct rtnl_link_ops *ops;
1723 struct net_device *dev;
1724 struct ifinfomsg *ifm;
1725 char kind[MODULE_NAME_LEN];
1726 char ifname[IFNAMSIZ];
1727 struct nlattr *tb[IFLA_MAX+1];
1728 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1729 int err;
1730
Johannes Berg95a5afc2008-10-16 15:24:51 -07001731#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001732replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001733#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001734 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1735 if (err < 0)
1736 return err;
1737
1738 if (tb[IFLA_IFNAME])
1739 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1740 else
1741 ifname[0] = '\0';
1742
1743 ifm = nlmsg_data(nlh);
1744 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001745 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001746 else {
1747 if (ifname[0])
1748 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001749 else
1750 dev = NULL;
1751 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001752
Eric Dumazete0d087a2009-11-07 01:26:17 -08001753 err = validate_linkmsg(dev, tb);
1754 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001755 return err;
1756
Patrick McHardy38f7b872007-06-13 12:03:51 -07001757 if (tb[IFLA_LINKINFO]) {
1758 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1759 tb[IFLA_LINKINFO], ifla_info_policy);
1760 if (err < 0)
1761 return err;
1762 } else
1763 memset(linkinfo, 0, sizeof(linkinfo));
1764
1765 if (linkinfo[IFLA_INFO_KIND]) {
1766 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1767 ops = rtnl_link_ops_get(kind);
1768 } else {
1769 kind[0] = '\0';
1770 ops = NULL;
1771 }
1772
1773 if (1) {
1774 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001775 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001776
1777 if (ops) {
1778 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1779 err = nla_parse_nested(attr, ops->maxtype,
1780 linkinfo[IFLA_INFO_DATA],
1781 ops->policy);
1782 if (err < 0)
1783 return err;
1784 data = attr;
1785 }
1786 if (ops->validate) {
1787 err = ops->validate(tb, data);
1788 if (err < 0)
1789 return err;
1790 }
1791 }
1792
1793 if (dev) {
1794 int modified = 0;
1795
1796 if (nlh->nlmsg_flags & NLM_F_EXCL)
1797 return -EEXIST;
1798 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1799 return -EOPNOTSUPP;
1800
1801 if (linkinfo[IFLA_INFO_DATA]) {
1802 if (!ops || ops != dev->rtnl_link_ops ||
1803 !ops->changelink)
1804 return -EOPNOTSUPP;
1805
1806 err = ops->changelink(dev, tb, data);
1807 if (err < 0)
1808 return err;
1809 modified = 1;
1810 }
1811
1812 return do_setlink(dev, ifm, tb, ifname, modified);
1813 }
1814
Patrick McHardyffa934f2011-01-20 03:00:42 +00001815 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1816 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1817 return rtnl_group_changelink(net,
1818 nla_get_u32(tb[IFLA_GROUP]),
1819 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001820 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00001821 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001822
Patrick McHardy3729d502010-02-26 06:34:54 +00001823 if (ifm->ifi_index)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001824 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001825 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001826 return -EOPNOTSUPP;
1827
1828 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001829#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001830 if (kind[0]) {
1831 __rtnl_unlock();
1832 request_module("rtnl-link-%s", kind);
1833 rtnl_lock();
1834 ops = rtnl_link_ops_get(kind);
1835 if (ops)
1836 goto replay;
1837 }
1838#endif
1839 return -EOPNOTSUPP;
1840 }
1841
1842 if (!ifname[0])
1843 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001844
Eric W. Biederman81adee42009-11-08 00:53:51 -08001845 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00001846 if (IS_ERR(dest_net))
1847 return PTR_ERR(dest_net);
1848
Eric W. Biederman81adee42009-11-08 00:53:51 -08001849 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001850
Pavel Emelianove7199282007-08-08 22:16:38 -07001851 if (IS_ERR(dev))
1852 err = PTR_ERR(dev);
1853 else if (ops->newlink)
Eric W. Biederman81adee42009-11-08 00:53:51 -08001854 err = ops->newlink(net, dev, tb, data);
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001855 else
1856 err = register_netdevice(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001857
1858 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001859 free_netdev(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001860 if (err < 0)
Patrick McHardy3729d502010-02-26 06:34:54 +00001861 goto out;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001862
Patrick McHardy3729d502010-02-26 06:34:54 +00001863 err = rtnl_configure_link(dev, ifm);
1864 if (err < 0)
1865 unregister_netdevice(dev);
1866out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08001867 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001868 return err;
1869 }
1870}
1871
Thomas Grafb60c5112006-08-04 23:05:34 -07001872static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001873{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001874 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07001875 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00001876 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07001877 struct nlattr *tb[IFLA_MAX+1];
1878 struct net_device *dev = NULL;
1879 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001880 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05001881 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001882
Thomas Grafb60c5112006-08-04 23:05:34 -07001883 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1884 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001885 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001886
Eric Dumazeta3d12892009-10-21 10:59:31 +00001887 if (tb[IFLA_IFNAME])
1888 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1889
Greg Rose115c9b82012-02-21 16:54:48 -05001890 if (tb[IFLA_EXT_MASK])
1891 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1892
Thomas Grafb60c5112006-08-04 23:05:34 -07001893 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001894 if (ifm->ifi_index > 0)
1895 dev = __dev_get_by_index(net, ifm->ifi_index);
1896 else if (tb[IFLA_IFNAME])
1897 dev = __dev_get_by_name(net, ifname);
1898 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001899 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001900
Eric Dumazeta3d12892009-10-21 10:59:31 +00001901 if (dev == NULL)
1902 return -ENODEV;
1903
Greg Rose115c9b82012-02-21 16:54:48 -05001904 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001905 if (nskb == NULL)
1906 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001907
Patrick McHardy575c3e22007-05-22 17:00:49 -07001908 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
Greg Rose115c9b82012-02-21 16:54:48 -05001909 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08001910 if (err < 0) {
1911 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1912 WARN_ON(err == -EMSGSIZE);
1913 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001914 } else
1915 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001916
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001917 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001918}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001919
Greg Rose115c9b82012-02-21 16:54:48 -05001920static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00001921{
Greg Rose115c9b82012-02-21 16:54:48 -05001922 struct net *net = sock_net(skb->sk);
1923 struct net_device *dev;
1924 struct nlattr *tb[IFLA_MAX+1];
1925 u32 ext_filter_mask = 0;
1926 u16 min_ifinfo_dump_size = 0;
1927
Michael Rieschb38a2412013-04-08 05:45:26 +00001928 if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001929 ifla_policy) >= 0) {
1930 if (tb[IFLA_EXT_MASK])
1931 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1932 }
Greg Rose115c9b82012-02-21 16:54:48 -05001933
1934 if (!ext_filter_mask)
1935 return NLMSG_GOODSIZE;
1936 /*
1937 * traverse the list of net devices and compute the minimum
1938 * buffer size based upon the filter mask.
1939 */
1940 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1941 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1942 if_nlmsg_size(dev,
1943 ext_filter_mask));
1944 }
1945
Greg Rosec7ac8672011-06-10 01:27:09 +00001946 return min_ifinfo_dump_size;
1947}
1948
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001949static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 int idx;
1952 int s_idx = cb->family;
1953
1954 if (s_idx == 0)
1955 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02001956 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int type = cb->nlh->nlmsg_type-RTM_BASE;
1958 if (idx < s_idx || idx == PF_PACKET)
1959 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001960 if (rtnl_msg_handlers[idx] == NULL ||
1961 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 continue;
1963 if (idx > s_idx)
1964 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001965 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
1967 }
1968 cb->family = idx;
1969
1970 return skb->len;
1971}
1972
1973void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1974{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001975 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001977 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001978 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Greg Rose115c9b82012-02-21 16:54:48 -05001980 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001981 if (skb == NULL)
1982 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Greg Rose115c9b82012-02-21 16:54:48 -05001984 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001985 if (err < 0) {
1986 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1987 WARN_ON(err == -EMSGSIZE);
1988 kfree_skb(skb);
1989 goto errout;
1990 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001991 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1992 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001993errout:
1994 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08001995 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998/* Protected by RTNL sempahore. */
1999static struct rtattr **rta_buf;
2000static int rtattr_max;
2001
2002/* Process one rtnetlink message. */
2003
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002004static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002006 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07002007 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int sz_idx, kind;
2009 int min_len;
2010 int family;
2011 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00002012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07002016 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 type -= RTM_BASE;
2019
2020 /* All the messages must have at least 1 byte length */
2021 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2022 return 0;
2023
Eric Dumazete0d087a2009-11-07 01:26:17 -08002024 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 sz_idx = type>>2;
2026 kind = type&3;
2027
Eric Parisfd778462012-01-03 12:25:16 -05002028 if (kind != 2 && !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002029 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
David S. Millerb8f3ab42011-01-18 12:40:38 -08002031 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002032 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07002033 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00002034 rtnl_calcit_func calcit;
2035 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Thomas Grafe2849862007-03-22 11:48:11 -07002037 dumpit = rtnl_get_dumpit(family, type);
2038 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002039 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00002040 calcit = rtnl_get_calcit(family, type);
2041 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05002042 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Eric Dumazet2907c352011-05-25 07:34:04 +00002044 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002045 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002046 {
2047 struct netlink_dump_control c = {
2048 .dump = dumpit,
2049 .min_dump_alloc = min_dump_alloc,
2050 };
2051 err = netlink_dump_start(rtnl, skb, nlh, &c);
2052 }
Eric Dumazet2907c352011-05-25 07:34:04 +00002053 rtnl_lock();
2054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
2057 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2058
2059 min_len = rtm_min[sz_idx];
2060 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 if (nlh->nlmsg_len > min_len) {
2064 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Eric Dumazete0d087a2009-11-07 01:26:17 -08002065 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 while (RTA_OK(attr, attrlen)) {
Vlad Yasevich40abddfd2013-03-13 04:18:58 +00002068 unsigned int flavor = attr->rta_type & NLA_TYPE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (flavor) {
2070 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 rta_buf[flavor-1] = attr;
2073 }
2074 attr = RTA_NEXT(attr, attrlen);
2075 }
2076 }
2077
Thomas Grafe2849862007-03-22 11:48:11 -07002078 doit = rtnl_get_doit(family, type);
2079 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002080 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002082 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002085static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002087 rtnl_lock();
2088 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2089 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2093{
2094 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case NETDEV_UP:
2098 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00002099 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002100 case NETDEV_POST_INIT:
2101 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00002103 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00002105 case NETDEV_UNREGISTER:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002106 case NETDEV_UNREGISTER_BATCH:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00002107 case NETDEV_RELEASE:
2108 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 break;
2110 default:
2111 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2112 break;
2113 }
2114 return NOTIFY_DONE;
2115}
2116
2117static struct notifier_block rtnetlink_dev_notifier = {
2118 .notifier_call = rtnetlink_event,
2119};
2120
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002121
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002122static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002123{
2124 struct sock *sk;
2125 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
Eric Dumazet2907c352011-05-25 07:34:04 +00002126 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002127 if (!sk)
2128 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002129 net->rtnl = sk;
2130 return 0;
2131}
2132
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002133static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002134{
Denis V. Lunev775516b2008-01-18 23:55:19 -08002135 netlink_kernel_release(net->rtnl);
2136 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002137}
2138
2139static struct pernet_operations rtnetlink_net_ops = {
2140 .init = rtnetlink_net_init,
2141 .exit = rtnetlink_net_exit,
2142};
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144void __init rtnetlink_init(void)
2145{
2146 int i;
2147
2148 rtattr_max = 0;
2149 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2150 if (rta_max[i] > rtattr_max)
2151 rtattr_max = rta_max[i];
2152 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2153 if (!rta_buf)
2154 panic("rtnetlink_init: cannot allocate rta_buf\n");
2155
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002156 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2160 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07002161
Greg Rosec7ac8672011-06-10 01:27:09 +00002162 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2163 rtnl_dump_ifinfo, rtnl_calcit);
2164 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2165 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2166 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07002167
Greg Rosec7ac8672011-06-10 01:27:09 +00002168 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2169 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171