blob: f16444bc6cbb1ca25e57df0d18e7d267ceedf1f9 [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>
41#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/inet.h>
44#include <linux/netdevice.h>
45#include <net/ip.h>
46#include <net/protocol.h>
47#include <net/arp.h>
48#include <net/route.h>
49#include <net/udp.h>
50#include <net/sock.h>
51#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070052#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070053#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000054#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Eric Dumazete0d087a2009-11-07 01:26:17 -080056struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070057 rtnl_doit_func doit;
58 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000059 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070060};
61
Stephen Hemminger6756ae42006-03-20 22:23:58 -080062static DEFINE_MUTEX(rtnl_mutex);
Greg Rosec7ac8672011-06-10 01:27:09 +000063static u16 min_ifinfo_dump_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65void rtnl_lock(void)
66{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080067 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Eric Dumazete0d087a2009-11-07 01:26:17 -080069EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Stephen Hemminger6756ae42006-03-20 22:23:58 -080071void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076void rtnl_unlock(void)
77{
Herbert Xu58ec3b42008-10-07 15:50:03 -070078 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 netdev_run_todo();
80}
Eric Dumazete0d087a2009-11-07 01:26:17 -080081EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Stephen Hemminger6756ae42006-03-20 22:23:58 -080083int rtnl_trylock(void)
84{
85 return mutex_trylock(&rtnl_mutex);
86}
Eric Dumazete0d087a2009-11-07 01:26:17 -080087EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080088
Patrick McHardyc9c10142008-04-23 22:10:48 -070089int rtnl_is_locked(void)
90{
91 return mutex_is_locked(&rtnl_mutex);
92}
Eric Dumazete0d087a2009-11-07 01:26:17 -080093EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070094
Paul E. McKenneya898def2010-02-22 17:04:49 -080095#ifdef CONFIG_PROVE_LOCKING
96int lockdep_rtnl_is_held(void)
97{
98 return lockdep_is_held(&rtnl_mutex);
99}
100EXPORT_SYMBOL(lockdep_rtnl_is_held);
101#endif /* #ifdef CONFIG_PROVE_LOCKING */
102
Patrick McHardy25239ce2010-04-26 16:02:05 +0200103static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700104
105static inline int rtm_msgindex(int msgtype)
106{
107 int msgindex = msgtype - RTM_BASE;
108
109 /*
110 * msgindex < 0 implies someone tried to register a netlink
111 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
112 * the message type has not been added to linux/rtnetlink.h
113 */
114 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
115
116 return msgindex;
117}
118
119static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
120{
121 struct rtnl_link *tab;
122
Patrick McHardy25239ce2010-04-26 16:02:05 +0200123 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000124 tab = rtnl_msg_handlers[protocol];
125 else
126 tab = NULL;
127
Thomas Graf51057f22007-03-22 21:41:06 -0700128 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700129 tab = rtnl_msg_handlers[PF_UNSPEC];
130
Thomas Graf51057f22007-03-22 21:41:06 -0700131 return tab ? tab[msgindex].doit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700132}
133
134static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
135{
136 struct rtnl_link *tab;
137
Patrick McHardy25239ce2010-04-26 16:02:05 +0200138 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000139 tab = rtnl_msg_handlers[protocol];
140 else
141 tab = NULL;
142
Thomas Graf51057f22007-03-22 21:41:06 -0700143 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700144 tab = rtnl_msg_handlers[PF_UNSPEC];
145
Thomas Graf51057f22007-03-22 21:41:06 -0700146 return tab ? tab[msgindex].dumpit : NULL;
Thomas Grafe2849862007-03-22 11:48:11 -0700147}
148
Greg Rosec7ac8672011-06-10 01:27:09 +0000149static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
150{
151 struct rtnl_link *tab;
152
153 if (protocol <= RTNL_FAMILY_MAX)
154 tab = rtnl_msg_handlers[protocol];
155 else
156 tab = NULL;
157
158 if (tab == NULL || tab[msgindex].calcit == NULL)
159 tab = rtnl_msg_handlers[PF_UNSPEC];
160
161 return tab ? tab[msgindex].calcit : NULL;
162}
163
Thomas Grafe2849862007-03-22 11:48:11 -0700164/**
165 * __rtnl_register - Register a rtnetlink message type
166 * @protocol: Protocol family or PF_UNSPEC
167 * @msgtype: rtnetlink message type
168 * @doit: Function pointer called for each request message
169 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000170 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700171 *
172 * Registers the specified function pointers (at least one of them has
173 * to be non-NULL) to be called whenever a request message for the
174 * specified protocol family and message type is received.
175 *
176 * The special protocol family PF_UNSPEC may be used to define fallback
177 * function pointers for the case when no entry for the specific protocol
178 * family exists.
179 *
180 * Returns 0 on success or a negative error code.
181 */
182int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000183 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
184 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700185{
186 struct rtnl_link *tab;
187 int msgindex;
188
Patrick McHardy25239ce2010-04-26 16:02:05 +0200189 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700190 msgindex = rtm_msgindex(msgtype);
191
192 tab = rtnl_msg_handlers[protocol];
193 if (tab == NULL) {
194 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
195 if (tab == NULL)
196 return -ENOBUFS;
197
198 rtnl_msg_handlers[protocol] = tab;
199 }
200
201 if (doit)
202 tab[msgindex].doit = doit;
203
204 if (dumpit)
205 tab[msgindex].dumpit = dumpit;
206
Greg Rosec7ac8672011-06-10 01:27:09 +0000207 if (calcit)
208 tab[msgindex].calcit = calcit;
209
Thomas Grafe2849862007-03-22 11:48:11 -0700210 return 0;
211}
Thomas Grafe2849862007-03-22 11:48:11 -0700212EXPORT_SYMBOL_GPL(__rtnl_register);
213
214/**
215 * rtnl_register - Register a rtnetlink message type
216 *
217 * Identical to __rtnl_register() but panics on failure. This is useful
218 * as failure of this function is very unlikely, it can only happen due
219 * to lack of memory when allocating the chain to store all message
220 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300221 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700222 */
223void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000224 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
225 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700226{
Greg Rosec7ac8672011-06-10 01:27:09 +0000227 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700228 panic("Unable to register rtnetlink message handler, "
229 "protocol = %d, message type = %d\n",
230 protocol, msgtype);
231}
Thomas Grafe2849862007-03-22 11:48:11 -0700232EXPORT_SYMBOL_GPL(rtnl_register);
233
234/**
235 * rtnl_unregister - Unregister a rtnetlink message type
236 * @protocol: Protocol family or PF_UNSPEC
237 * @msgtype: rtnetlink message type
238 *
239 * Returns 0 on success or a negative error code.
240 */
241int rtnl_unregister(int protocol, int msgtype)
242{
243 int msgindex;
244
Patrick McHardy25239ce2010-04-26 16:02:05 +0200245 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700246 msgindex = rtm_msgindex(msgtype);
247
248 if (rtnl_msg_handlers[protocol] == NULL)
249 return -ENOENT;
250
251 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
252 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
253
254 return 0;
255}
Thomas Grafe2849862007-03-22 11:48:11 -0700256EXPORT_SYMBOL_GPL(rtnl_unregister);
257
258/**
259 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
260 * @protocol : Protocol family or PF_UNSPEC
261 *
262 * Identical to calling rtnl_unregster() for all registered message types
263 * of a certain protocol family.
264 */
265void rtnl_unregister_all(int protocol)
266{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200267 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700268
269 kfree(rtnl_msg_handlers[protocol]);
270 rtnl_msg_handlers[protocol] = NULL;
271}
Thomas Grafe2849862007-03-22 11:48:11 -0700272EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Patrick McHardy38f7b872007-06-13 12:03:51 -0700274static LIST_HEAD(link_ops);
275
Eric Dumazetc63044f2011-12-13 11:38:00 +0000276static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
277{
278 const struct rtnl_link_ops *ops;
279
280 list_for_each_entry(ops, &link_ops, list) {
281 if (!strcmp(ops->kind, kind))
282 return ops;
283 }
284 return NULL;
285}
286
Patrick McHardy38f7b872007-06-13 12:03:51 -0700287/**
288 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
289 * @ops: struct rtnl_link_ops * to register
290 *
291 * The caller must hold the rtnl_mutex. This function should be used
292 * by drivers that create devices during module initialization. It
293 * must be called before registering the devices.
294 *
295 * Returns 0 on success or a negative error code.
296 */
297int __rtnl_link_register(struct rtnl_link_ops *ops)
298{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000299 if (rtnl_link_ops_get(ops->kind))
300 return -EEXIST;
301
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700302 if (!ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000303 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700304
Patrick McHardy38f7b872007-06-13 12:03:51 -0700305 list_add_tail(&ops->list, &link_ops);
306 return 0;
307}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700308EXPORT_SYMBOL_GPL(__rtnl_link_register);
309
310/**
311 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
312 * @ops: struct rtnl_link_ops * to register
313 *
314 * Returns 0 on success or a negative error code.
315 */
316int rtnl_link_register(struct rtnl_link_ops *ops)
317{
318 int err;
319
320 rtnl_lock();
321 err = __rtnl_link_register(ops);
322 rtnl_unlock();
323 return err;
324}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700325EXPORT_SYMBOL_GPL(rtnl_link_register);
326
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700327static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
328{
329 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000330 LIST_HEAD(list_kill);
331
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700332 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000333 if (dev->rtnl_link_ops == ops)
334 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000336 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700337}
338
Patrick McHardy38f7b872007-06-13 12:03:51 -0700339/**
340 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
341 * @ops: struct rtnl_link_ops * to unregister
342 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700343 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700344 */
345void __rtnl_link_unregister(struct rtnl_link_ops *ops)
346{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700347 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700348
Eric W. Biederman881d9662007-09-17 11:56:21 -0700349 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700350 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700351 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352 list_del(&ops->list);
353}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700354EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
355
356/**
357 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
358 * @ops: struct rtnl_link_ops * to unregister
359 */
360void rtnl_link_unregister(struct rtnl_link_ops *ops)
361{
362 rtnl_lock();
363 __rtnl_link_unregister(ops);
364 rtnl_unlock();
365}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700366EXPORT_SYMBOL_GPL(rtnl_link_unregister);
367
Patrick McHardy38f7b872007-06-13 12:03:51 -0700368static size_t rtnl_link_get_size(const struct net_device *dev)
369{
370 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
371 size_t size;
372
373 if (!ops)
374 return 0;
375
Thomas Graf369cf772010-11-11 15:47:59 +0000376 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
377 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700378
379 if (ops->get_size)
380 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000381 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700382 ops->get_size(dev);
383
384 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000385 /* IFLA_INFO_XSTATS */
386 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700387
388 return size;
389}
390
Thomas Graff8ff1822010-11-16 04:30:14 +0000391static LIST_HEAD(rtnl_af_ops);
392
393static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
394{
395 const struct rtnl_af_ops *ops;
396
397 list_for_each_entry(ops, &rtnl_af_ops, list) {
398 if (ops->family == family)
399 return ops;
400 }
401
402 return NULL;
403}
404
405/**
406 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
407 * @ops: struct rtnl_af_ops * to register
408 *
409 * The caller must hold the rtnl_mutex.
410 *
411 * Returns 0 on success or a negative error code.
412 */
413int __rtnl_af_register(struct rtnl_af_ops *ops)
414{
415 list_add_tail(&ops->list, &rtnl_af_ops);
416 return 0;
417}
418EXPORT_SYMBOL_GPL(__rtnl_af_register);
419
420/**
421 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
422 * @ops: struct rtnl_af_ops * to register
423 *
424 * Returns 0 on success or a negative error code.
425 */
426int rtnl_af_register(struct rtnl_af_ops *ops)
427{
428 int err;
429
430 rtnl_lock();
431 err = __rtnl_af_register(ops);
432 rtnl_unlock();
433 return err;
434}
435EXPORT_SYMBOL_GPL(rtnl_af_register);
436
437/**
438 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
439 * @ops: struct rtnl_af_ops * to unregister
440 *
441 * The caller must hold the rtnl_mutex.
442 */
443void __rtnl_af_unregister(struct rtnl_af_ops *ops)
444{
445 list_del(&ops->list);
446}
447EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
448
449/**
450 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
451 * @ops: struct rtnl_af_ops * to unregister
452 */
453void rtnl_af_unregister(struct rtnl_af_ops *ops)
454{
455 rtnl_lock();
456 __rtnl_af_unregister(ops);
457 rtnl_unlock();
458}
459EXPORT_SYMBOL_GPL(rtnl_af_unregister);
460
461static size_t rtnl_link_get_af_size(const struct net_device *dev)
462{
463 struct rtnl_af_ops *af_ops;
464 size_t size;
465
466 /* IFLA_AF_SPEC */
467 size = nla_total_size(sizeof(struct nlattr));
468
469 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
470 if (af_ops->get_link_af_size) {
471 /* AF_* + nested data */
472 size += nla_total_size(sizeof(struct nlattr)) +
473 af_ops->get_link_af_size(dev);
474 }
475 }
476
477 return size;
478}
479
Patrick McHardy38f7b872007-06-13 12:03:51 -0700480static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
481{
482 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
483 struct nlattr *linkinfo, *data;
484 int err = -EMSGSIZE;
485
486 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
487 if (linkinfo == NULL)
488 goto out;
489
490 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
491 goto err_cancel_link;
492 if (ops->fill_xstats) {
493 err = ops->fill_xstats(skb, dev);
494 if (err < 0)
495 goto err_cancel_link;
496 }
497 if (ops->fill_info) {
498 data = nla_nest_start(skb, IFLA_INFO_DATA);
499 if (data == NULL)
500 goto err_cancel_link;
501 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
Patrick McHardy3729d502010-02-26 06:34:54 +0000676static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
677 const struct ifinfomsg *ifm)
678{
679 unsigned int flags = ifm->ifi_flags;
680
681 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
682 if (ifm->ifi_change)
683 flags = (flags & ifm->ifi_change) |
684 (dev->flags & ~ifm->ifi_change);
685
686 return flags;
687}
688
Thomas Grafb60c5112006-08-04 23:05:34 -0700689static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000690 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Thomas Grafb60c5112006-08-04 23:05:34 -0700692 a->rx_packets = b->rx_packets;
693 a->tx_packets = b->tx_packets;
694 a->rx_bytes = b->rx_bytes;
695 a->tx_bytes = b->tx_bytes;
696 a->rx_errors = b->rx_errors;
697 a->tx_errors = b->tx_errors;
698 a->rx_dropped = b->rx_dropped;
699 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Thomas Grafb60c5112006-08-04 23:05:34 -0700701 a->multicast = b->multicast;
702 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Grafb60c5112006-08-04 23:05:34 -0700704 a->rx_length_errors = b->rx_length_errors;
705 a->rx_over_errors = b->rx_over_errors;
706 a->rx_crc_errors = b->rx_crc_errors;
707 a->rx_frame_errors = b->rx_frame_errors;
708 a->rx_fifo_errors = b->rx_fifo_errors;
709 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Thomas Grafb60c5112006-08-04 23:05:34 -0700711 a->tx_aborted_errors = b->tx_aborted_errors;
712 a->tx_carrier_errors = b->tx_carrier_errors;
713 a->tx_fifo_errors = b->tx_fifo_errors;
714 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
715 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Thomas Grafb60c5112006-08-04 23:05:34 -0700717 a->rx_compressed = b->rx_compressed;
718 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000719}
720
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000721static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000722{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000723 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000724}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Chris Wrightc02db8c2010-05-16 01:05:45 -0700726/* All VF info */
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000727static inline int rtnl_vfinfo_size(const struct net_device *dev)
728{
Chris Wrightc02db8c2010-05-16 01:05:45 -0700729 if (dev->dev.parent && dev_is_pci(dev->dev.parent)) {
730
731 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700732 size_t size = nla_total_size(sizeof(struct nlattr));
733 size += nla_total_size(num_vfs * sizeof(struct nlattr));
734 size += num_vfs *
735 (nla_total_size(sizeof(struct ifla_vf_mac)) +
736 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Greg Rose5f8444a2011-10-08 03:05:24 +0000737 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
738 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700739 return size;
740 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000741 return 0;
742}
743
Scott Feldman57b61082010-05-17 22:49:55 -0700744static size_t rtnl_port_size(const struct net_device *dev)
745{
746 size_t port_size = nla_total_size(4) /* PORT_VF */
747 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
748 + nla_total_size(sizeof(struct ifla_port_vsi))
749 /* PORT_VSI_TYPE */
750 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
751 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
752 + nla_total_size(1) /* PROT_VDP_REQUEST */
753 + nla_total_size(2); /* PORT_VDP_RESPONSE */
754 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
755 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
756 + port_size;
757 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
758 + port_size;
759
760 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
761 return 0;
762 if (dev_num_vf(dev->dev.parent))
763 return port_self_size + vf_ports_size +
764 vf_port_size * dev_num_vf(dev->dev.parent);
765 else
766 return port_self_size;
767}
768
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000769static noinline size_t if_nlmsg_size(const struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -0800770{
771 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
772 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700773 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800774 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
775 + nla_total_size(sizeof(struct rtnl_link_ifmap))
776 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700777 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800778 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
779 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
780 + nla_total_size(4) /* IFLA_TXQLEN */
781 + nla_total_size(4) /* IFLA_WEIGHT */
782 + nla_total_size(4) /* IFLA_MTU */
783 + nla_total_size(4) /* IFLA_LINK */
784 + nla_total_size(4) /* IFLA_MASTER */
785 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700786 + nla_total_size(1) /* IFLA_LINKMODE */
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000787 + nla_total_size(4) /* IFLA_NUM_VF */
Chris Wrightc02db8c2010-05-16 01:05:45 -0700788 + rtnl_vfinfo_size(dev) /* IFLA_VFINFO_LIST */
Scott Feldman57b61082010-05-17 22:49:55 -0700789 + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000790 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
791 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
Thomas Graf339bf982006-11-10 14:10:15 -0800792}
793
Scott Feldman57b61082010-05-17 22:49:55 -0700794static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
795{
796 struct nlattr *vf_ports;
797 struct nlattr *vf_port;
798 int vf;
799 int err;
800
801 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
802 if (!vf_ports)
803 return -EMSGSIZE;
804
805 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
806 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700807 if (!vf_port)
808 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700809 NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
810 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700811 if (err == -EMSGSIZE)
812 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700813 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700814 nla_nest_cancel(skb, vf_port);
815 continue;
816 }
817 nla_nest_end(skb, vf_port);
818 }
819
820 nla_nest_end(skb, vf_ports);
821
822 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700823
824nla_put_failure:
825 nla_nest_cancel(skb, vf_ports);
826 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700827}
828
829static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
830{
831 struct nlattr *port_self;
832 int err;
833
834 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
835 if (!port_self)
836 return -EMSGSIZE;
837
838 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
839 if (err) {
840 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700841 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700842 }
843
844 nla_nest_end(skb, port_self);
845
846 return 0;
847}
848
849static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev)
850{
851 int err;
852
853 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent)
854 return 0;
855
856 err = rtnl_port_self_fill(skb, dev);
857 if (err)
858 return err;
859
860 if (dev_num_vf(dev->dev.parent)) {
861 err = rtnl_vf_ports_fill(skb, dev);
862 if (err)
863 return err;
864 }
865
866 return 0;
867}
868
Thomas Grafb60c5112006-08-04 23:05:34 -0700869static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700870 int type, u32 pid, u32 seq, u32 change,
871 unsigned int flags)
Thomas Grafb60c5112006-08-04 23:05:34 -0700872{
873 struct ifinfomsg *ifm;
874 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -0700875 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000876 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +0000877 struct nlattr *attr, *af_spec;
878 struct rtnl_af_ops *af_ops;
Thomas Grafb60c5112006-08-04 23:05:34 -0700879
Eric Dumazet2907c352011-05-25 07:34:04 +0000880 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -0700881 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
882 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800883 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700884
885 ifm = nlmsg_data(nlh);
886 ifm->ifi_family = AF_UNSPEC;
887 ifm->__ifi_pad = 0;
888 ifm->ifi_type = dev->type;
889 ifm->ifi_index = dev->ifindex;
890 ifm->ifi_flags = dev_get_flags(dev);
891 ifm->ifi_change = change;
892
893 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
894 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
Thomas Grafb60c5112006-08-04 23:05:34 -0700895 NLA_PUT_U8(skb, IFLA_OPERSTATE,
896 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
897 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
898 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
Vlad Dogarucbda10f2011-01-13 23:38:30 +0000899 NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
Thomas Grafb60c5112006-08-04 23:05:34 -0700900
901 if (dev->ifindex != dev->iflink)
902 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
903
904 if (dev->master)
905 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
906
Patrick McHardyaf356af2009-09-04 06:41:18 +0000907 if (dev->qdisc)
908 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
Stefan Rompfb00055a2006-03-20 17:09:11 -0800909
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700910 if (dev->ifalias)
911 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
912
Stefan Rompfb00055a2006-03-20 17:09:11 -0800913 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct rtnl_link_ifmap map = {
915 .mem_start = dev->mem_start,
916 .mem_end = dev->mem_end,
917 .base_addr = dev->base_addr,
918 .irq = dev->irq,
919 .dma = dev->dma,
920 .port = dev->if_port,
921 };
Thomas Grafb60c5112006-08-04 23:05:34 -0700922 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 if (dev->addr_len) {
Thomas Grafb60c5112006-08-04 23:05:34 -0700926 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
927 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700930 attr = nla_reserve(skb, IFLA_STATS,
931 sizeof(struct rtnl_link_stats));
932 if (attr == NULL)
933 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Dumazet28172732010-07-07 14:58:56 -0700935 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -0700936 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jan Engelhardt10708f32010-03-11 09:57:29 +0000938 attr = nla_reserve(skb, IFLA_STATS64,
939 sizeof(struct rtnl_link_stats64));
940 if (attr == NULL)
941 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000942 copy_rtnl_link_stats64(nla_data(attr), stats);
943
Scott Feldman57b61082010-05-17 22:49:55 -0700944 if (dev->dev.parent)
945 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
946
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000947 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent) {
948 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000949
Chris Wrightc02db8c2010-05-16 01:05:45 -0700950 struct nlattr *vfinfo, *vf;
951 int num_vfs = dev_num_vf(dev->dev.parent);
952
Chris Wrightc02db8c2010-05-16 01:05:45 -0700953 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
954 if (!vfinfo)
955 goto nla_put_failure;
956 for (i = 0; i < num_vfs; i++) {
957 struct ifla_vf_info ivi;
958 struct ifla_vf_mac vf_mac;
959 struct ifla_vf_vlan vf_vlan;
960 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000961 struct ifla_vf_spoofchk vf_spoofchk;
962
963 /*
964 * Not all SR-IOV capable drivers support the
965 * spoofcheck query. Preset to -1 so the user
966 * space tool can detect that the driver didn't
967 * report anything.
968 */
969 ivi.spoofchk = -1;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000970 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
971 break;
Greg Rose5f8444a2011-10-08 03:05:24 +0000972 vf_mac.vf =
973 vf_vlan.vf =
974 vf_tx_rate.vf =
975 vf_spoofchk.vf = ivi.vf;
976
Chris Wrightc02db8c2010-05-16 01:05:45 -0700977 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
978 vf_vlan.vlan = ivi.vlan;
979 vf_vlan.qos = ivi.qos;
980 vf_tx_rate.rate = ivi.tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +0000981 vf_spoofchk.setting = ivi.spoofchk;
Chris Wrightc02db8c2010-05-16 01:05:45 -0700982 vf = nla_nest_start(skb, IFLA_VF_INFO);
983 if (!vf) {
984 nla_nest_cancel(skb, vfinfo);
985 goto nla_put_failure;
986 }
987 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
988 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
Greg Rose5f8444a2011-10-08 03:05:24 +0000989 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
990 &vf_tx_rate);
991 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
992 &vf_spoofchk);
Chris Wrightc02db8c2010-05-16 01:05:45 -0700993 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000994 }
Chris Wrightc02db8c2010-05-16 01:05:45 -0700995 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000996 }
Scott Feldman57b61082010-05-17 22:49:55 -0700997
998 if (rtnl_port_fill(skb, dev))
999 goto nla_put_failure;
1000
Patrick McHardy38f7b872007-06-13 12:03:51 -07001001 if (dev->rtnl_link_ops) {
1002 if (rtnl_link_fill(skb, dev) < 0)
1003 goto nla_put_failure;
1004 }
1005
Thomas Graff8ff1822010-11-16 04:30:14 +00001006 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1007 goto nla_put_failure;
1008
1009 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1010 if (af_ops->fill_link_af) {
1011 struct nlattr *af;
1012 int err;
1013
1014 if (!(af = nla_nest_start(skb, af_ops->family)))
1015 goto nla_put_failure;
1016
1017 err = af_ops->fill_link_af(skb, dev);
1018
1019 /*
1020 * Caller may return ENODATA to indicate that there
1021 * was no data to be dumped. This is not an error, it
1022 * means we should trim the attribute header and
1023 * continue.
1024 */
1025 if (err == -ENODATA)
1026 nla_nest_cancel(skb, af);
1027 else if (err < 0)
1028 goto nla_put_failure;
1029
1030 nla_nest_end(skb, af);
1031 }
1032 }
1033
1034 nla_nest_end(skb, af_spec);
1035
Thomas Grafb60c5112006-08-04 23:05:34 -07001036 return nlmsg_end(skb, nlh);
1037
1038nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001039 nlmsg_cancel(skb, nlh);
1040 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Thomas Grafb60c5112006-08-04 23:05:34 -07001043static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001045 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001046 int h, s_h;
1047 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001049 struct hlist_head *head;
1050 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001052 s_h = cb->args[0];
1053 s_idx = cb->args[1];
1054
Eric Dumazete67f88d2011-04-27 22:56:07 +00001055 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001056 cb->seq = net->dev_base_seq;
1057
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001058 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1059 idx = 0;
1060 head = &net->dev_index_head[h];
Eric Dumazete67f88d2011-04-27 22:56:07 +00001061 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001062 if (idx < s_idx)
1063 goto cont;
1064 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1065 NETLINK_CB(cb->skb).pid,
1066 cb->nlh->nlmsg_seq, 0,
1067 NLM_F_MULTI) <= 0)
1068 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001069
1070 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001071cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001072 idx++;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001075out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001076 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001077 cb->args[1] = idx;
1078 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 return skb->len;
1081}
1082
Pavel Emelianove7199282007-08-08 22:16:38 -07001083const struct nla_policy ifla_policy[IFLA_MAX+1] = {
Thomas Graf5176f912006-08-26 20:13:18 -07001084 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
Patrick McHardy38f7b872007-06-13 12:03:51 -07001085 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1086 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
Thomas Graf5176f912006-08-26 20:13:18 -07001087 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
Thomas Grafda5e0492006-08-10 21:17:37 -07001088 [IFLA_MTU] = { .type = NLA_U32 },
Thomas Graf76e87302008-02-19 16:12:08 -08001089 [IFLA_LINK] = { .type = NLA_U32 },
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001090 [IFLA_MASTER] = { .type = NLA_U32 },
Thomas Grafda5e0492006-08-10 21:17:37 -07001091 [IFLA_TXQLEN] = { .type = NLA_U32 },
1092 [IFLA_WEIGHT] = { .type = NLA_U32 },
1093 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1094 [IFLA_LINKMODE] = { .type = NLA_U8 },
Thomas Graf76e87302008-02-19 16:12:08 -08001095 [IFLA_LINKINFO] = { .type = NLA_NESTED },
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001096 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001097 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001098 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
Chris Wrightc02db8c2010-05-16 01:05:45 -07001099 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
Scott Feldman57b61082010-05-17 22:49:55 -07001100 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1101 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
Thomas Graff8ff1822010-11-16 04:30:14 +00001102 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
Thomas Grafda5e0492006-08-10 21:17:37 -07001103};
Eric Dumazete0d087a2009-11-07 01:26:17 -08001104EXPORT_SYMBOL(ifla_policy);
Thomas Grafda5e0492006-08-10 21:17:37 -07001105
Patrick McHardy38f7b872007-06-13 12:03:51 -07001106static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1107 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1108 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1109};
1110
Chris Wrightc02db8c2010-05-16 01:05:45 -07001111static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1112 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1113};
1114
1115static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1116 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1117 .len = sizeof(struct ifla_vf_mac) },
1118 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1119 .len = sizeof(struct ifla_vf_vlan) },
1120 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1121 .len = sizeof(struct ifla_vf_tx_rate) },
1122};
1123
Scott Feldman57b61082010-05-17 22:49:55 -07001124static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1125 [IFLA_PORT_VF] = { .type = NLA_U32 },
1126 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1127 .len = PORT_PROFILE_MAX },
1128 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1129 .len = sizeof(struct ifla_port_vsi)},
1130 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1131 .len = PORT_UUID_MAX },
1132 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1133 .len = PORT_UUID_MAX },
1134 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1135 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1136};
1137
Eric W. Biederman81adee42009-11-08 00:53:51 -08001138struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1139{
1140 struct net *net;
1141 /* Examine the link attributes and figure out which
1142 * network namespace we are talking about.
1143 */
1144 if (tb[IFLA_NET_NS_PID])
1145 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001146 else if (tb[IFLA_NET_NS_FD])
1147 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001148 else
1149 net = get_net(src_net);
1150 return net;
1151}
1152EXPORT_SYMBOL(rtnl_link_get_net);
1153
Thomas Graf1840bb12008-02-23 19:54:36 -08001154static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1155{
1156 if (dev) {
1157 if (tb[IFLA_ADDRESS] &&
1158 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1159 return -EINVAL;
1160
1161 if (tb[IFLA_BROADCAST] &&
1162 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1163 return -EINVAL;
1164 }
1165
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001166 if (tb[IFLA_AF_SPEC]) {
1167 struct nlattr *af;
1168 int rem, err;
1169
1170 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1171 const struct rtnl_af_ops *af_ops;
1172
1173 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1174 return -EAFNOSUPPORT;
1175
1176 if (!af_ops->set_link_af)
1177 return -EOPNOTSUPP;
1178
1179 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001180 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001181 if (err < 0)
1182 return err;
1183 }
1184 }
1185 }
1186
Thomas Graf1840bb12008-02-23 19:54:36 -08001187 return 0;
1188}
1189
Chris Wrightc02db8c2010-05-16 01:05:45 -07001190static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1191{
1192 int rem, err = -EINVAL;
1193 struct nlattr *vf;
1194 const struct net_device_ops *ops = dev->netdev_ops;
1195
1196 nla_for_each_nested(vf, attr, rem) {
1197 switch (nla_type(vf)) {
1198 case IFLA_VF_MAC: {
1199 struct ifla_vf_mac *ivm;
1200 ivm = nla_data(vf);
1201 err = -EOPNOTSUPP;
1202 if (ops->ndo_set_vf_mac)
1203 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1204 ivm->mac);
1205 break;
1206 }
1207 case IFLA_VF_VLAN: {
1208 struct ifla_vf_vlan *ivv;
1209 ivv = nla_data(vf);
1210 err = -EOPNOTSUPP;
1211 if (ops->ndo_set_vf_vlan)
1212 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1213 ivv->vlan,
1214 ivv->qos);
1215 break;
1216 }
1217 case IFLA_VF_TX_RATE: {
1218 struct ifla_vf_tx_rate *ivt;
1219 ivt = nla_data(vf);
1220 err = -EOPNOTSUPP;
1221 if (ops->ndo_set_vf_tx_rate)
1222 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1223 ivt->rate);
1224 break;
1225 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001226 case IFLA_VF_SPOOFCHK: {
1227 struct ifla_vf_spoofchk *ivs;
1228 ivs = nla_data(vf);
1229 err = -EOPNOTSUPP;
1230 if (ops->ndo_set_vf_spoofchk)
1231 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1232 ivs->setting);
1233 break;
1234 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001235 default:
1236 err = -EINVAL;
1237 break;
1238 }
1239 if (err)
1240 break;
1241 }
1242 return err;
1243}
1244
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001245static int do_set_master(struct net_device *dev, int ifindex)
1246{
1247 struct net_device *master_dev;
1248 const struct net_device_ops *ops;
1249 int err;
1250
1251 if (dev->master) {
1252 if (dev->master->ifindex == ifindex)
1253 return 0;
1254 ops = dev->master->netdev_ops;
1255 if (ops->ndo_del_slave) {
1256 err = ops->ndo_del_slave(dev->master, dev);
1257 if (err)
1258 return err;
1259 } else {
1260 return -EOPNOTSUPP;
1261 }
1262 }
1263
1264 if (ifindex) {
1265 master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1266 if (!master_dev)
1267 return -EINVAL;
1268 ops = master_dev->netdev_ops;
1269 if (ops->ndo_add_slave) {
1270 err = ops->ndo_add_slave(master_dev, dev);
1271 if (err)
1272 return err;
1273 } else {
1274 return -EOPNOTSUPP;
1275 }
1276 }
1277 return 0;
1278}
1279
Patrick McHardy0157f602007-06-13 12:03:36 -07001280static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
Patrick McHardy38f7b872007-06-13 12:03:51 -07001281 struct nlattr **tb, char *ifname, int modified)
Patrick McHardy0157f602007-06-13 12:03:36 -07001282{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001283 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001284 int send_addr_notify = 0;
Patrick McHardy0157f602007-06-13 12:03:36 -07001285 int err;
1286
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001287 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001288 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001289 if (IS_ERR(net)) {
1290 err = PTR_ERR(net);
1291 goto errout;
1292 }
1293 err = dev_change_net_namespace(dev, net, ifname);
1294 put_net(net);
1295 if (err)
1296 goto errout;
1297 modified = 1;
1298 }
1299
Patrick McHardy0157f602007-06-13 12:03:36 -07001300 if (tb[IFLA_MAP]) {
1301 struct rtnl_link_ifmap *u_map;
1302 struct ifmap k_map;
1303
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001304 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001305 err = -EOPNOTSUPP;
1306 goto errout;
1307 }
1308
1309 if (!netif_device_present(dev)) {
1310 err = -ENODEV;
1311 goto errout;
1312 }
1313
1314 u_map = nla_data(tb[IFLA_MAP]);
1315 k_map.mem_start = (unsigned long) u_map->mem_start;
1316 k_map.mem_end = (unsigned long) u_map->mem_end;
1317 k_map.base_addr = (unsigned short) u_map->base_addr;
1318 k_map.irq = (unsigned char) u_map->irq;
1319 k_map.dma = (unsigned char) u_map->dma;
1320 k_map.port = (unsigned char) u_map->port;
1321
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001322 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001323 if (err < 0)
1324 goto errout;
1325
1326 modified = 1;
1327 }
1328
1329 if (tb[IFLA_ADDRESS]) {
1330 struct sockaddr *sa;
1331 int len;
1332
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001333 if (!ops->ndo_set_mac_address) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001334 err = -EOPNOTSUPP;
1335 goto errout;
1336 }
1337
1338 if (!netif_device_present(dev)) {
1339 err = -ENODEV;
1340 goto errout;
1341 }
1342
1343 len = sizeof(sa_family_t) + dev->addr_len;
1344 sa = kmalloc(len, GFP_KERNEL);
1345 if (!sa) {
1346 err = -ENOMEM;
1347 goto errout;
1348 }
1349 sa->sa_family = dev->type;
1350 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1351 dev->addr_len);
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001352 err = ops->ndo_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001353 kfree(sa);
1354 if (err)
1355 goto errout;
1356 send_addr_notify = 1;
1357 modified = 1;
1358 }
1359
1360 if (tb[IFLA_MTU]) {
1361 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1362 if (err < 0)
1363 goto errout;
1364 modified = 1;
1365 }
1366
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001367 if (tb[IFLA_GROUP]) {
1368 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1369 modified = 1;
1370 }
1371
Patrick McHardy0157f602007-06-13 12:03:36 -07001372 /*
1373 * Interface selected by interface index but interface
1374 * name provided implies that a name change has been
1375 * requested.
1376 */
1377 if (ifm->ifi_index > 0 && ifname[0]) {
1378 err = dev_change_name(dev, ifname);
1379 if (err < 0)
1380 goto errout;
1381 modified = 1;
1382 }
1383
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001384 if (tb[IFLA_IFALIAS]) {
1385 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1386 nla_len(tb[IFLA_IFALIAS]));
1387 if (err < 0)
1388 goto errout;
1389 modified = 1;
1390 }
1391
Patrick McHardy0157f602007-06-13 12:03:36 -07001392 if (tb[IFLA_BROADCAST]) {
1393 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1394 send_addr_notify = 1;
1395 }
1396
1397 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001398 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001399 if (err < 0)
1400 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001401 }
1402
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001403 if (tb[IFLA_MASTER]) {
1404 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1405 if (err)
1406 goto errout;
1407 modified = 1;
1408 }
1409
David S. Miller93b2d4a2008-02-17 18:35:07 -08001410 if (tb[IFLA_TXQLEN])
1411 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
Patrick McHardy0157f602007-06-13 12:03:36 -07001412
Patrick McHardy0157f602007-06-13 12:03:36 -07001413 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001414 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001415
1416 if (tb[IFLA_LINKMODE]) {
David S. Miller93b2d4a2008-02-17 18:35:07 -08001417 write_lock_bh(&dev_base_lock);
1418 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1419 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001420 }
1421
Chris Wrightc02db8c2010-05-16 01:05:45 -07001422 if (tb[IFLA_VFINFO_LIST]) {
1423 struct nlattr *attr;
1424 int rem;
1425 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001426 if (nla_type(attr) != IFLA_VF_INFO) {
1427 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001428 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001429 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001430 err = do_setvfinfo(dev, attr);
1431 if (err < 0)
1432 goto errout;
1433 modified = 1;
1434 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001435 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001436 err = 0;
1437
Scott Feldman57b61082010-05-17 22:49:55 -07001438 if (tb[IFLA_VF_PORTS]) {
1439 struct nlattr *port[IFLA_PORT_MAX+1];
1440 struct nlattr *attr;
1441 int vf;
1442 int rem;
1443
1444 err = -EOPNOTSUPP;
1445 if (!ops->ndo_set_vf_port)
1446 goto errout;
1447
1448 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1449 if (nla_type(attr) != IFLA_VF_PORT)
1450 continue;
1451 err = nla_parse_nested(port, IFLA_PORT_MAX,
1452 attr, ifla_port_policy);
1453 if (err < 0)
1454 goto errout;
1455 if (!port[IFLA_PORT_VF]) {
1456 err = -EOPNOTSUPP;
1457 goto errout;
1458 }
1459 vf = nla_get_u32(port[IFLA_PORT_VF]);
1460 err = ops->ndo_set_vf_port(dev, vf, port);
1461 if (err < 0)
1462 goto errout;
1463 modified = 1;
1464 }
1465 }
1466 err = 0;
1467
1468 if (tb[IFLA_PORT_SELF]) {
1469 struct nlattr *port[IFLA_PORT_MAX+1];
1470
1471 err = nla_parse_nested(port, IFLA_PORT_MAX,
1472 tb[IFLA_PORT_SELF], ifla_port_policy);
1473 if (err < 0)
1474 goto errout;
1475
1476 err = -EOPNOTSUPP;
1477 if (ops->ndo_set_vf_port)
1478 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1479 if (err < 0)
1480 goto errout;
1481 modified = 1;
1482 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001483
1484 if (tb[IFLA_AF_SPEC]) {
1485 struct nlattr *af;
1486 int rem;
1487
1488 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1489 const struct rtnl_af_ops *af_ops;
1490
1491 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001492 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001493
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001494 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001495 if (err < 0)
1496 goto errout;
1497
1498 modified = 1;
1499 }
1500 }
Scott Feldman57b61082010-05-17 22:49:55 -07001501 err = 0;
1502
Patrick McHardy0157f602007-06-13 12:03:36 -07001503errout:
1504 if (err < 0 && modified && net_ratelimit())
1505 printk(KERN_WARNING "A link change request failed with "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001506 "some changes committed already. Interface %s may "
Patrick McHardy0157f602007-06-13 12:03:36 -07001507 "have been left with an inconsistent configuration, "
1508 "please check.\n", dev->name);
1509
1510 if (send_addr_notify)
1511 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1512 return err;
1513}
1514
Thomas Grafda5e0492006-08-10 21:17:37 -07001515static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001517 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001518 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001520 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001521 struct nlattr *tb[IFLA_MAX+1];
1522 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Thomas Grafda5e0492006-08-10 21:17:37 -07001524 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1525 if (err < 0)
1526 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Thomas Graf5176f912006-08-26 20:13:18 -07001528 if (tb[IFLA_IFNAME])
1529 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001530 else
1531 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001534 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001535 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001536 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001537 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001538 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001539 else
1540 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Thomas Grafda5e0492006-08-10 21:17:37 -07001542 if (dev == NULL) {
1543 err = -ENODEV;
1544 goto errout;
1545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Eric Dumazete0d087a2009-11-07 01:26:17 -08001547 err = validate_linkmsg(dev, tb);
1548 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001549 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001550
Patrick McHardy38f7b872007-06-13 12:03:51 -07001551 err = do_setlink(dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001552errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return err;
1554}
1555
Patrick McHardy38f7b872007-06-13 12:03:51 -07001556static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1557{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001558 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001559 const struct rtnl_link_ops *ops;
1560 struct net_device *dev;
1561 struct ifinfomsg *ifm;
1562 char ifname[IFNAMSIZ];
1563 struct nlattr *tb[IFLA_MAX+1];
1564 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001565 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001566
1567 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1568 if (err < 0)
1569 return err;
1570
1571 if (tb[IFLA_IFNAME])
1572 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1573
1574 ifm = nlmsg_data(nlh);
1575 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001576 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001577 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001578 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001579 else
1580 return -EINVAL;
1581
1582 if (!dev)
1583 return -ENODEV;
1584
1585 ops = dev->rtnl_link_ops;
1586 if (!ops)
1587 return -EOPNOTSUPP;
1588
Eric Dumazet226bd342011-05-08 23:17:57 +00001589 ops->dellink(dev, &list_kill);
1590 unregister_netdevice_many(&list_kill);
1591 list_del(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001592 return 0;
1593}
1594
Patrick McHardy3729d502010-02-26 06:34:54 +00001595int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1596{
1597 unsigned int old_flags;
1598 int err;
1599
1600 old_flags = dev->flags;
1601 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1602 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1603 if (err < 0)
1604 return err;
1605 }
1606
1607 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1608 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1609
1610 __dev_notify_flags(dev, old_flags);
1611 return 0;
1612}
1613EXPORT_SYMBOL(rtnl_configure_link);
1614
Eric W. Biederman81adee42009-11-08 00:53:51 -08001615struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1616 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001617{
1618 int err;
1619 struct net_device *dev;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001620 unsigned int num_queues = 1;
1621 unsigned int real_num_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001622
Eric Dumazet2e59af32009-09-02 18:03:00 -07001623 if (ops->get_tx_queues) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001624 err = ops->get_tx_queues(src_net, tb, &num_queues,
Eric Dumazete0d087a2009-11-07 01:26:17 -08001625 &real_num_queues);
Eric Dumazet2e59af32009-09-02 18:03:00 -07001626 if (err)
1627 goto err;
1628 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001629 err = -ENOMEM;
Eric Dumazet2e59af32009-09-02 18:03:00 -07001630 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001631 if (!dev)
1632 goto err;
1633
Eric W. Biederman81adee42009-11-08 00:53:51 -08001634 dev_net_set(dev, net);
1635 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001636 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001637
Pavel Emelianove7199282007-08-08 22:16:38 -07001638 if (tb[IFLA_MTU])
1639 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1640 if (tb[IFLA_ADDRESS])
1641 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1642 nla_len(tb[IFLA_ADDRESS]));
1643 if (tb[IFLA_BROADCAST])
1644 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1645 nla_len(tb[IFLA_BROADCAST]));
1646 if (tb[IFLA_TXQLEN])
1647 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1648 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001649 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001650 if (tb[IFLA_LINKMODE])
1651 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001652 if (tb[IFLA_GROUP])
1653 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001654
1655 return dev;
1656
Pavel Emelianove7199282007-08-08 22:16:38 -07001657err:
1658 return ERR_PTR(err);
1659}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001660EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001661
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001662static int rtnl_group_changelink(struct net *net, int group,
1663 struct ifinfomsg *ifm,
1664 struct nlattr **tb)
1665{
1666 struct net_device *dev;
1667 int err;
1668
1669 for_each_netdev(net, dev) {
1670 if (dev->group == group) {
1671 err = do_setlink(dev, ifm, tb, NULL, 0);
1672 if (err < 0)
1673 return err;
1674 }
1675 }
1676
1677 return 0;
1678}
1679
Patrick McHardy38f7b872007-06-13 12:03:51 -07001680static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1681{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001682 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001683 const struct rtnl_link_ops *ops;
1684 struct net_device *dev;
1685 struct ifinfomsg *ifm;
1686 char kind[MODULE_NAME_LEN];
1687 char ifname[IFNAMSIZ];
1688 struct nlattr *tb[IFLA_MAX+1];
1689 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1690 int err;
1691
Johannes Berg95a5afc2008-10-16 15:24:51 -07001692#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001693replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001694#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001695 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1696 if (err < 0)
1697 return err;
1698
1699 if (tb[IFLA_IFNAME])
1700 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1701 else
1702 ifname[0] = '\0';
1703
1704 ifm = nlmsg_data(nlh);
1705 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001706 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001707 else {
1708 if (ifname[0])
1709 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001710 else
1711 dev = NULL;
1712 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001713
Eric Dumazete0d087a2009-11-07 01:26:17 -08001714 err = validate_linkmsg(dev, tb);
1715 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001716 return err;
1717
Patrick McHardy38f7b872007-06-13 12:03:51 -07001718 if (tb[IFLA_LINKINFO]) {
1719 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1720 tb[IFLA_LINKINFO], ifla_info_policy);
1721 if (err < 0)
1722 return err;
1723 } else
1724 memset(linkinfo, 0, sizeof(linkinfo));
1725
1726 if (linkinfo[IFLA_INFO_KIND]) {
1727 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1728 ops = rtnl_link_ops_get(kind);
1729 } else {
1730 kind[0] = '\0';
1731 ops = NULL;
1732 }
1733
1734 if (1) {
1735 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001736 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001737
1738 if (ops) {
1739 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1740 err = nla_parse_nested(attr, ops->maxtype,
1741 linkinfo[IFLA_INFO_DATA],
1742 ops->policy);
1743 if (err < 0)
1744 return err;
1745 data = attr;
1746 }
1747 if (ops->validate) {
1748 err = ops->validate(tb, data);
1749 if (err < 0)
1750 return err;
1751 }
1752 }
1753
1754 if (dev) {
1755 int modified = 0;
1756
1757 if (nlh->nlmsg_flags & NLM_F_EXCL)
1758 return -EEXIST;
1759 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1760 return -EOPNOTSUPP;
1761
1762 if (linkinfo[IFLA_INFO_DATA]) {
1763 if (!ops || ops != dev->rtnl_link_ops ||
1764 !ops->changelink)
1765 return -EOPNOTSUPP;
1766
1767 err = ops->changelink(dev, tb, data);
1768 if (err < 0)
1769 return err;
1770 modified = 1;
1771 }
1772
1773 return do_setlink(dev, ifm, tb, ifname, modified);
1774 }
1775
Patrick McHardyffa934f2011-01-20 03:00:42 +00001776 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1777 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1778 return rtnl_group_changelink(net,
1779 nla_get_u32(tb[IFLA_GROUP]),
1780 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001781 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00001782 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001783
Patrick McHardy3729d502010-02-26 06:34:54 +00001784 if (ifm->ifi_index)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001785 return -EOPNOTSUPP;
Patrick McHardy0e068772007-07-11 19:42:31 -07001786 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07001787 return -EOPNOTSUPP;
1788
1789 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07001790#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001791 if (kind[0]) {
1792 __rtnl_unlock();
1793 request_module("rtnl-link-%s", kind);
1794 rtnl_lock();
1795 ops = rtnl_link_ops_get(kind);
1796 if (ops)
1797 goto replay;
1798 }
1799#endif
1800 return -EOPNOTSUPP;
1801 }
1802
1803 if (!ifname[0])
1804 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001805
Eric W. Biederman81adee42009-11-08 00:53:51 -08001806 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00001807 if (IS_ERR(dest_net))
1808 return PTR_ERR(dest_net);
1809
Eric W. Biederman81adee42009-11-08 00:53:51 -08001810 dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001811
Pavel Emelianove7199282007-08-08 22:16:38 -07001812 if (IS_ERR(dev))
1813 err = PTR_ERR(dev);
1814 else if (ops->newlink)
Eric W. Biederman81adee42009-11-08 00:53:51 -08001815 err = ops->newlink(net, dev, tb, data);
Patrick McHardy2d85cba2007-07-11 19:42:13 -07001816 else
1817 err = register_netdevice(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001818
1819 if (err < 0 && !IS_ERR(dev))
Patrick McHardy38f7b872007-06-13 12:03:51 -07001820 free_netdev(dev);
Dan Carpenter80032cf2010-04-21 23:53:27 +00001821 if (err < 0)
Patrick McHardy3729d502010-02-26 06:34:54 +00001822 goto out;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001823
Patrick McHardy3729d502010-02-26 06:34:54 +00001824 err = rtnl_configure_link(dev, ifm);
1825 if (err < 0)
1826 unregister_netdevice(dev);
1827out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08001828 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001829 return err;
1830 }
1831}
1832
Thomas Grafb60c5112006-08-04 23:05:34 -07001833static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001834{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001835 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07001836 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00001837 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07001838 struct nlattr *tb[IFLA_MAX+1];
1839 struct net_device *dev = NULL;
1840 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08001841 int err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001842
Thomas Grafb60c5112006-08-04 23:05:34 -07001843 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1844 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07001845 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07001846
Eric Dumazeta3d12892009-10-21 10:59:31 +00001847 if (tb[IFLA_IFNAME])
1848 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1849
Thomas Grafb60c5112006-08-04 23:05:34 -07001850 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001851 if (ifm->ifi_index > 0)
1852 dev = __dev_get_by_index(net, ifm->ifi_index);
1853 else if (tb[IFLA_IFNAME])
1854 dev = __dev_get_by_name(net, ifname);
1855 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001856 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07001857
Eric Dumazeta3d12892009-10-21 10:59:31 +00001858 if (dev == NULL)
1859 return -ENODEV;
1860
Patrick McHardy38f7b872007-06-13 12:03:51 -07001861 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001862 if (nskb == NULL)
1863 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001864
Patrick McHardy575c3e22007-05-22 17:00:49 -07001865 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1866 nlh->nlmsg_seq, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001867 if (err < 0) {
1868 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1869 WARN_ON(err == -EMSGSIZE);
1870 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00001871 } else
1872 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
Thomas Grafb60c5112006-08-04 23:05:34 -07001873
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001874 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001875}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001876
Greg Rosec7ac8672011-06-10 01:27:09 +00001877static u16 rtnl_calcit(struct sk_buff *skb)
1878{
1879 return min_ifinfo_dump_size;
1880}
1881
Adrian Bunk42bad1d2007-04-26 00:57:41 -07001882static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 int idx;
1885 int s_idx = cb->family;
1886
1887 if (s_idx == 0)
1888 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02001889 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int type = cb->nlh->nlmsg_type-RTM_BASE;
1891 if (idx < s_idx || idx == PF_PACKET)
1892 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07001893 if (rtnl_msg_handlers[idx] == NULL ||
1894 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 continue;
1896 if (idx > s_idx)
1897 memset(&cb->args[0], 0, sizeof(cb->args));
Thomas Grafe2849862007-03-22 11:48:11 -07001898 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 break;
1900 }
1901 cb->family = idx;
1902
1903 return skb->len;
1904}
1905
1906void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1907{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001908 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001910 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001911 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Greg Rosec7ac8672011-06-10 01:27:09 +00001913 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev)), GFP_KERNEL);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001914 if (skb == NULL)
1915 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Greg Rosec7ac8672011-06-10 01:27:09 +00001917 min_ifinfo_dump_size = max_t(u16, if_info_size, min_ifinfo_dump_size);
1918
Patrick McHardy575c3e22007-05-22 17:00:49 -07001919 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08001920 if (err < 0) {
1921 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1922 WARN_ON(err == -EMSGSIZE);
1923 kfree_skb(skb);
1924 goto errout;
1925 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08001926 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1927 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07001928errout:
1929 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08001930 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/* Protected by RTNL sempahore. */
1934static struct rtattr **rta_buf;
1935static int rtattr_max;
1936
1937/* Process one rtnetlink message. */
1938
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001939static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001941 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07001942 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int sz_idx, kind;
1944 int min_len;
1945 int family;
1946 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00001947 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07001951 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 type -= RTM_BASE;
1954
1955 /* All the messages must have at least 1 byte length */
1956 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1957 return 0;
1958
Eric Dumazete0d087a2009-11-07 01:26:17 -08001959 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 sz_idx = type>>2;
1961 kind = type&3;
1962
Eric Parisfd778462012-01-03 12:25:16 -05001963 if (kind != 2 && !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001964 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
David S. Millerb8f3ab42011-01-18 12:40:38 -08001966 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001967 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07001968 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00001969 rtnl_calcit_func calcit;
1970 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Thomas Grafe2849862007-03-22 11:48:11 -07001972 dumpit = rtnl_get_dumpit(family, type);
1973 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07001974 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00001975 calcit = rtnl_get_calcit(family, type);
1976 if (calcit)
1977 min_dump_alloc = calcit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Eric Dumazet2907c352011-05-25 07:34:04 +00001979 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001980 rtnl = net->rtnl;
Greg Rosec7ac8672011-06-10 01:27:09 +00001981 err = netlink_dump_start(rtnl, skb, nlh, dumpit,
1982 NULL, min_dump_alloc);
Eric Dumazet2907c352011-05-25 07:34:04 +00001983 rtnl_lock();
1984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986
1987 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1988
1989 min_len = rtm_min[sz_idx];
1990 if (nlh->nlmsg_len < min_len)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 if (nlh->nlmsg_len > min_len) {
1994 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Eric Dumazete0d087a2009-11-07 01:26:17 -08001995 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 while (RTA_OK(attr, attrlen)) {
1998 unsigned flavor = attr->rta_type;
1999 if (flavor) {
2000 if (flavor > rta_max[sz_idx])
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002001 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rta_buf[flavor-1] = attr;
2003 }
2004 attr = RTA_NEXT(attr, attrlen);
2005 }
2006 }
2007
Thomas Grafe2849862007-03-22 11:48:11 -07002008 doit = rtnl_get_doit(family, type);
2009 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002010 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002012 return doit(skb, nlh, (void *)&rta_buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002015static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002017 rtnl_lock();
2018 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2019 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2023{
2024 struct net_device *dev = ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 case NETDEV_UP:
2028 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00002029 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002030 case NETDEV_POST_INIT:
2031 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00002033 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00002035 case NETDEV_UNREGISTER:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00002036 case NETDEV_UNREGISTER_BATCH:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00002037 case NETDEV_RELEASE:
2038 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 break;
2040 default:
2041 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2042 break;
2043 }
2044 return NOTIFY_DONE;
2045}
2046
2047static struct notifier_block rtnetlink_dev_notifier = {
2048 .notifier_call = rtnetlink_event,
2049};
2050
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002051
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002052static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002053{
2054 struct sock *sk;
2055 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
Eric Dumazet2907c352011-05-25 07:34:04 +00002056 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002057 if (!sk)
2058 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002059 net->rtnl = sk;
2060 return 0;
2061}
2062
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002063static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002064{
Denis V. Lunev775516b2008-01-18 23:55:19 -08002065 netlink_kernel_release(net->rtnl);
2066 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002067}
2068
2069static struct pernet_operations rtnetlink_net_ops = {
2070 .init = rtnetlink_net_init,
2071 .exit = rtnetlink_net_exit,
2072};
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074void __init rtnetlink_init(void)
2075{
2076 int i;
2077
2078 rtattr_max = 0;
2079 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2080 if (rta_max[i] > rtattr_max)
2081 rtattr_max = rta_max[i];
2082 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2083 if (!rta_buf)
2084 panic("rtnetlink_init: cannot allocate rta_buf\n");
2085
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002086 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2090 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07002091
Greg Rosec7ac8672011-06-10 01:27:09 +00002092 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2093 rtnl_dump_ifinfo, rtnl_calcit);
2094 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2095 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2096 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07002097
Greg Rosec7ac8672011-06-10 01:27:09 +00002098 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2099 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101