blob: b9b7dfaf202b9be668bf29153593aa2c7dba86ee [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>
John Fastabend77162022012-04-15 06:43:56 +000038#include <linux/if_bridge.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000039#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000040#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/inet.h>
45#include <linux/netdevice.h>
46#include <net/ip.h>
47#include <net/protocol.h>
48#include <net/arp.h>
49#include <net/route.h>
50#include <net/udp.h>
51#include <net/sock.h>
52#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070053#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070054#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000055#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Dumazete0d087a2009-11-07 01:26:17 -080057struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070058 rtnl_doit_func doit;
59 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000060 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070061};
62
Stephen Hemminger6756ae42006-03-20 22:23:58 -080063static DEFINE_MUTEX(rtnl_mutex);
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
Hans Zhangc80bbea2012-10-22 22:21:23 +0000131 return tab[msgindex].doit;
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
Hans Zhangc80bbea2012-10-22 22:21:23 +0000146 return tab[msgindex].dumpit;
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
Hans Zhangc80bbea2012-10-22 22:21:23 +0000161 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000162}
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
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200302 /* The check for setup is here because if ops
303 * does not have that filled up, it is not possible
304 * to use the ops for creating device. So do not
305 * fill up dellink as well. That disables rtnl_dellink.
306 */
307 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000308 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700309
Patrick McHardy38f7b872007-06-13 12:03:51 -0700310 list_add_tail(&ops->list, &link_ops);
311 return 0;
312}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313EXPORT_SYMBOL_GPL(__rtnl_link_register);
314
315/**
316 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
317 * @ops: struct rtnl_link_ops * to register
318 *
319 * Returns 0 on success or a negative error code.
320 */
321int rtnl_link_register(struct rtnl_link_ops *ops)
322{
323 int err;
324
325 rtnl_lock();
326 err = __rtnl_link_register(ops);
327 rtnl_unlock();
328 return err;
329}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700330EXPORT_SYMBOL_GPL(rtnl_link_register);
331
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700332static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
333{
334 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000335 LIST_HEAD(list_kill);
336
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700337 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 if (dev->rtnl_link_ops == ops)
339 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000341 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700342}
343
Patrick McHardy38f7b872007-06-13 12:03:51 -0700344/**
345 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
346 * @ops: struct rtnl_link_ops * to unregister
347 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700348 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700349 */
350void __rtnl_link_unregister(struct rtnl_link_ops *ops)
351{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700352 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700353
Eric W. Biederman881d9662007-09-17 11:56:21 -0700354 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700355 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700357 list_del(&ops->list);
358}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700359EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
360
Cong Wang200b9162014-05-12 15:11:20 -0700361/* Return with the rtnl_lock held when there are no network
362 * devices unregistering in any network namespace.
363 */
364static void rtnl_lock_unregistering_all(void)
365{
366 struct net *net;
367 bool unregistering;
368 DEFINE_WAIT(wait);
369
370 for (;;) {
371 prepare_to_wait(&netdev_unregistering_wq, &wait,
372 TASK_UNINTERRUPTIBLE);
373 unregistering = false;
374 rtnl_lock();
375 for_each_net(net) {
376 if (net->dev_unreg_count > 0) {
377 unregistering = true;
378 break;
379 }
380 }
381 if (!unregistering)
382 break;
383 __rtnl_unlock();
384 schedule();
385 }
386 finish_wait(&netdev_unregistering_wq, &wait);
387}
388
Patrick McHardy38f7b872007-06-13 12:03:51 -0700389/**
390 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
391 * @ops: struct rtnl_link_ops * to unregister
392 */
393void rtnl_link_unregister(struct rtnl_link_ops *ops)
394{
Cong Wang200b9162014-05-12 15:11:20 -0700395 /* Close the race with cleanup_net() */
396 mutex_lock(&net_mutex);
397 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700398 __rtnl_link_unregister(ops);
399 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700400 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700401}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700402EXPORT_SYMBOL_GPL(rtnl_link_unregister);
403
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100404static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
405{
406 struct net_device *master_dev;
407 const struct rtnl_link_ops *ops;
408
409 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
410 if (!master_dev)
411 return 0;
412 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900413 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100414 return 0;
415 /* IFLA_INFO_SLAVE_DATA + nested data */
416 return nla_total_size(sizeof(struct nlattr)) +
417 ops->get_slave_size(master_dev, dev);
418}
419
Patrick McHardy38f7b872007-06-13 12:03:51 -0700420static size_t rtnl_link_get_size(const struct net_device *dev)
421{
422 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
423 size_t size;
424
425 if (!ops)
426 return 0;
427
Thomas Graf369cf772010-11-11 15:47:59 +0000428 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
429 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700430
431 if (ops->get_size)
432 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000433 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700434 ops->get_size(dev);
435
436 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000437 /* IFLA_INFO_XSTATS */
438 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700439
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100440 size += rtnl_link_get_slave_info_data_size(dev);
441
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442 return size;
443}
444
Thomas Graff8ff1822010-11-16 04:30:14 +0000445static LIST_HEAD(rtnl_af_ops);
446
447static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
448{
449 const struct rtnl_af_ops *ops;
450
451 list_for_each_entry(ops, &rtnl_af_ops, list) {
452 if (ops->family == family)
453 return ops;
454 }
455
456 return NULL;
457}
458
459/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000460 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
461 * @ops: struct rtnl_af_ops * to register
462 *
463 * Returns 0 on success or a negative error code.
464 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800465void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000466{
Thomas Graff8ff1822010-11-16 04:30:14 +0000467 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800468 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000469 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000470}
471EXPORT_SYMBOL_GPL(rtnl_af_register);
472
473/**
474 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
475 * @ops: struct rtnl_af_ops * to unregister
476 *
477 * The caller must hold the rtnl_mutex.
478 */
479void __rtnl_af_unregister(struct rtnl_af_ops *ops)
480{
481 list_del(&ops->list);
482}
483EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
484
485/**
486 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
487 * @ops: struct rtnl_af_ops * to unregister
488 */
489void rtnl_af_unregister(struct rtnl_af_ops *ops)
490{
491 rtnl_lock();
492 __rtnl_af_unregister(ops);
493 rtnl_unlock();
494}
495EXPORT_SYMBOL_GPL(rtnl_af_unregister);
496
497static size_t rtnl_link_get_af_size(const struct net_device *dev)
498{
499 struct rtnl_af_ops *af_ops;
500 size_t size;
501
502 /* IFLA_AF_SPEC */
503 size = nla_total_size(sizeof(struct nlattr));
504
505 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
506 if (af_ops->get_link_af_size) {
507 /* AF_* + nested data */
508 size += nla_total_size(sizeof(struct nlattr)) +
509 af_ops->get_link_af_size(dev);
510 }
511 }
512
513 return size;
514}
515
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100516static bool rtnl_have_link_slave_info(const struct net_device *dev)
517{
518 struct net_device *master_dev;
519
520 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100521 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100522 return true;
523 return false;
524}
525
526static int rtnl_link_slave_info_fill(struct sk_buff *skb,
527 const struct net_device *dev)
528{
529 struct net_device *master_dev;
530 const struct rtnl_link_ops *ops;
531 struct nlattr *slave_data;
532 int err;
533
534 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
535 if (!master_dev)
536 return 0;
537 ops = master_dev->rtnl_link_ops;
538 if (!ops)
539 return 0;
540 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
541 return -EMSGSIZE;
542 if (ops->fill_slave_info) {
543 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
544 if (!slave_data)
545 return -EMSGSIZE;
546 err = ops->fill_slave_info(skb, master_dev, dev);
547 if (err < 0)
548 goto err_cancel_slave_data;
549 nla_nest_end(skb, slave_data);
550 }
551 return 0;
552
553err_cancel_slave_data:
554 nla_nest_cancel(skb, slave_data);
555 return err;
556}
557
558static int rtnl_link_info_fill(struct sk_buff *skb,
559 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700560{
561 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100562 struct nlattr *data;
563 int err;
564
565 if (!ops)
566 return 0;
567 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
568 return -EMSGSIZE;
569 if (ops->fill_xstats) {
570 err = ops->fill_xstats(skb, dev);
571 if (err < 0)
572 return err;
573 }
574 if (ops->fill_info) {
575 data = nla_nest_start(skb, IFLA_INFO_DATA);
576 if (data == NULL)
577 return -EMSGSIZE;
578 err = ops->fill_info(skb, dev);
579 if (err < 0)
580 goto err_cancel_data;
581 nla_nest_end(skb, data);
582 }
583 return 0;
584
585err_cancel_data:
586 nla_nest_cancel(skb, data);
587 return err;
588}
589
590static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
591{
592 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700593 int err = -EMSGSIZE;
594
595 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
596 if (linkinfo == NULL)
597 goto out;
598
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100599 err = rtnl_link_info_fill(skb, dev);
600 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700601 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100602
603 err = rtnl_link_slave_info_fill(skb, dev);
604 if (err < 0)
605 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700606
607 nla_nest_end(skb, linkinfo);
608 return 0;
609
Patrick McHardy38f7b872007-06-13 12:03:51 -0700610err_cancel_link:
611 nla_nest_cancel(skb, linkinfo);
612out:
613 return err;
614}
615
Eric Dumazet95c96172012-04-15 05:58:06 +0000616int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800618 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int err = 0;
620
Patrick McHardyac6d4392005-08-14 19:29:52 -0700621 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (echo)
623 atomic_inc(&skb->users);
624 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
625 if (echo)
626 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
627 return err;
628}
629
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800630int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700631{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800632 struct sock *rtnl = net->rtnl;
633
Thomas Graf2942e902006-08-15 00:30:25 -0700634 return nlmsg_unicast(rtnl, skb, pid);
635}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800636EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700637
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800638void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
639 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700640{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800641 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700642 int report = 0;
643
644 if (nlh)
645 report = nlmsg_report(nlh);
646
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800647 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700648}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800649EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700650
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800651void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700652{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800653 struct sock *rtnl = net->rtnl;
654
Thomas Graf97676b62006-08-15 00:31:41 -0700655 netlink_set_err(rtnl, 0, group, error);
656}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800657EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
660{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700661 struct nlattr *mx;
662 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Thomas Graf2d7202b2006-08-22 00:01:27 -0700664 mx = nla_nest_start(skb, RTA_METRICS);
665 if (mx == NULL)
666 return -ENOBUFS;
667
668 for (i = 0; i < RTAX_MAX; i++) {
669 if (metrics[i]) {
670 valid++;
David S. Millera6574342012-04-01 20:12:00 -0400671 if (nla_put_u32(skb, i+1, metrics[i]))
672 goto nla_put_failure;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
David S. Millera57d27f2006-08-22 22:20:14 -0700676 if (!valid) {
677 nla_nest_cancel(skb, mx);
678 return 0;
679 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700680
681 return nla_nest_end(skb, mx);
682
683nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700684 nla_nest_cancel(skb, mx);
685 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800687EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Thomas Grafe3703b32006-11-27 09:27:07 -0800689int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700690 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800691{
692 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000693 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800694 .rta_used = dst->__use,
695 .rta_clntref = atomic_read(&(dst->__refcnt)),
696 .rta_error = error,
697 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800698 };
699
Li Wei82539472012-07-29 16:01:30 +0000700 if (expires) {
701 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800702
Li Wei82539472012-07-29 16:01:30 +0000703 clock = jiffies_to_clock_t(abs(expires));
704 clock = min_t(unsigned long, clock, INT_MAX);
705 ci.rta_expires = (expires > 0) ? clock : -clock;
706 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800707 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
708}
Thomas Grafe3703b32006-11-27 09:27:07 -0800709EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
David S. Miller93b2d4a2008-02-17 18:35:07 -0800711static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800712{
713 unsigned char operstate = dev->operstate;
714
Eric Dumazete0d087a2009-11-07 01:26:17 -0800715 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800716 case IF_OPER_UP:
717 if ((operstate == IF_OPER_DORMANT ||
718 operstate == IF_OPER_UNKNOWN) &&
719 !netif_dormant(dev))
720 operstate = IF_OPER_UP;
721 break;
722
723 case IF_OPER_DORMANT:
724 if (operstate == IF_OPER_UP ||
725 operstate == IF_OPER_UNKNOWN)
726 operstate = IF_OPER_DORMANT;
727 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700728 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800729
730 if (dev->operstate != operstate) {
731 write_lock_bh(&dev_base_lock);
732 dev->operstate = operstate;
733 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800734 netdev_state_change(dev);
735 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736}
737
Jiri Bencb1beb682012-07-27 02:58:22 +0000738static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
739{
740 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
741 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
742}
743
Patrick McHardy3729d502010-02-26 06:34:54 +0000744static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
745 const struct ifinfomsg *ifm)
746{
747 unsigned int flags = ifm->ifi_flags;
748
749 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
750 if (ifm->ifi_change)
751 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000752 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000753
754 return flags;
755}
756
Thomas Grafb60c5112006-08-04 23:05:34 -0700757static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000758 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Thomas Grafb60c5112006-08-04 23:05:34 -0700760 a->rx_packets = b->rx_packets;
761 a->tx_packets = b->tx_packets;
762 a->rx_bytes = b->rx_bytes;
763 a->tx_bytes = b->tx_bytes;
764 a->rx_errors = b->rx_errors;
765 a->tx_errors = b->tx_errors;
766 a->rx_dropped = b->rx_dropped;
767 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Thomas Grafb60c5112006-08-04 23:05:34 -0700769 a->multicast = b->multicast;
770 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Thomas Grafb60c5112006-08-04 23:05:34 -0700772 a->rx_length_errors = b->rx_length_errors;
773 a->rx_over_errors = b->rx_over_errors;
774 a->rx_crc_errors = b->rx_crc_errors;
775 a->rx_frame_errors = b->rx_frame_errors;
776 a->rx_fifo_errors = b->rx_fifo_errors;
777 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Thomas Grafb60c5112006-08-04 23:05:34 -0700779 a->tx_aborted_errors = b->tx_aborted_errors;
780 a->tx_carrier_errors = b->tx_carrier_errors;
781 a->tx_fifo_errors = b->tx_fifo_errors;
782 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
783 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Thomas Grafb60c5112006-08-04 23:05:34 -0700785 a->rx_compressed = b->rx_compressed;
786 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000787}
788
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000789static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000790{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000791 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000792}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Chris Wrightc02db8c2010-05-16 01:05:45 -0700794/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500795static inline int rtnl_vfinfo_size(const struct net_device *dev,
796 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000797{
Greg Rose115c9b82012-02-21 16:54:48 -0500798 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
799 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700800 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700801 size_t size = nla_total_size(sizeof(struct nlattr));
802 size += nla_total_size(num_vfs * sizeof(struct nlattr));
803 size += num_vfs *
804 (nla_total_size(sizeof(struct ifla_vf_mac)) +
805 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400806 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200807 nla_total_size(sizeof(struct ifla_vf_rate)) +
808 nla_total_size(sizeof(struct ifla_vf_link_state)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700809 return size;
810 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000811 return 0;
812}
813
David Gibsonc53864f2014-04-24 10:22:36 +1000814static size_t rtnl_port_size(const struct net_device *dev,
815 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700816{
817 size_t port_size = nla_total_size(4) /* PORT_VF */
818 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
819 + nla_total_size(sizeof(struct ifla_port_vsi))
820 /* PORT_VSI_TYPE */
821 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
822 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
823 + nla_total_size(1) /* PROT_VDP_REQUEST */
824 + nla_total_size(2); /* PORT_VDP_RESPONSE */
825 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
826 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
827 + port_size;
828 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
829 + port_size;
830
David Gibsonc53864f2014-04-24 10:22:36 +1000831 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
832 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700833 return 0;
834 if (dev_num_vf(dev->dev.parent))
835 return port_self_size + vf_ports_size +
836 vf_port_size * dev_num_vf(dev->dev.parent);
837 else
838 return port_self_size;
839}
840
Greg Rose115c9b82012-02-21 16:54:48 -0500841static noinline size_t if_nlmsg_size(const struct net_device *dev,
842 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800843{
844 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
845 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700846 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800847 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
848 + nla_total_size(sizeof(struct rtnl_link_ifmap))
849 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700850 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800851 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
852 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
853 + nla_total_size(4) /* IFLA_TXQLEN */
854 + nla_total_size(4) /* IFLA_WEIGHT */
855 + nla_total_size(4) /* IFLA_MTU */
856 + nla_total_size(4) /* IFLA_LINK */
857 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000858 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000859 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000860 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
861 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800862 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700863 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700864 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Greg Rose115c9b82012-02-21 16:54:48 -0500865 + nla_total_size(ext_filter_mask
866 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
867 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000868 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000869 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200870 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
871 + nla_total_size(MAX_PHYS_PORT_ID_LEN); /* IFLA_PHYS_PORT_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800872}
873
Scott Feldman57b61082010-05-17 22:49:55 -0700874static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
875{
876 struct nlattr *vf_ports;
877 struct nlattr *vf_port;
878 int vf;
879 int err;
880
881 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
882 if (!vf_ports)
883 return -EMSGSIZE;
884
885 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
886 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700887 if (!vf_port)
888 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400889 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
890 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700891 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700892 if (err == -EMSGSIZE)
893 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700894 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700895 nla_nest_cancel(skb, vf_port);
896 continue;
897 }
898 nla_nest_end(skb, vf_port);
899 }
900
901 nla_nest_end(skb, vf_ports);
902
903 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700904
905nla_put_failure:
906 nla_nest_cancel(skb, vf_ports);
907 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700908}
909
910static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
911{
912 struct nlattr *port_self;
913 int err;
914
915 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
916 if (!port_self)
917 return -EMSGSIZE;
918
919 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
920 if (err) {
921 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700922 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700923 }
924
925 nla_nest_end(skb, port_self);
926
927 return 0;
928}
929
David Gibsonc53864f2014-04-24 10:22:36 +1000930static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
931 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700932{
933 int err;
934
David Gibsonc53864f2014-04-24 10:22:36 +1000935 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
936 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700937 return 0;
938
939 err = rtnl_port_self_fill(skb, dev);
940 if (err)
941 return err;
942
943 if (dev_num_vf(dev->dev.parent)) {
944 err = rtnl_vf_ports_fill(skb, dev);
945 if (err)
946 return err;
947 }
948
949 return 0;
950}
951
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200952static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
953{
954 int err;
955 struct netdev_phys_port_id ppid;
956
957 err = dev_get_phys_port_id(dev, &ppid);
958 if (err) {
959 if (err == -EOPNOTSUPP)
960 return 0;
961 return err;
962 }
963
964 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
965 return -EMSGSIZE;
966
967 return 0;
968}
969
Thomas Grafb60c5112006-08-04 23:05:34 -0700970static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700971 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -0500972 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -0700973{
974 struct ifinfomsg *ifm;
975 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -0700976 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000977 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +0000978 struct nlattr *attr, *af_spec;
979 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +0000980 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -0700981
Eric Dumazet2907c352011-05-25 07:34:04 +0000982 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -0700983 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
984 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800985 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -0700986
987 ifm = nlmsg_data(nlh);
988 ifm->ifi_family = AF_UNSPEC;
989 ifm->__ifi_pad = 0;
990 ifm->ifi_type = dev->type;
991 ifm->ifi_index = dev->ifindex;
992 ifm->ifi_flags = dev_get_flags(dev);
993 ifm->ifi_change = change;
994
David S. Millera6574342012-04-01 20:12:00 -0400995 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
996 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
997 nla_put_u8(skb, IFLA_OPERSTATE,
998 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
999 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1000 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1001 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001002 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001003 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001004#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001005 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001006#endif
David S. Millera6574342012-04-01 20:12:00 -04001007 (dev->ifindex != dev->iflink &&
1008 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001009 (upper_dev &&
1010 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001011 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001012 (dev->qdisc &&
1013 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1014 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001015 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1016 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1017 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001018 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001019
Stefan Rompfb00055a2006-03-20 17:09:11 -08001020 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct rtnl_link_ifmap map = {
1022 .mem_start = dev->mem_start,
1023 .mem_end = dev->mem_end,
1024 .base_addr = dev->base_addr,
1025 .irq = dev->irq,
1026 .dma = dev->dma,
1027 .port = dev->if_port,
1028 };
David S. Millera6574342012-04-01 20:12:00 -04001029 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1030 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
1033 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001034 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1035 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1036 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001039 if (rtnl_phys_port_id_fill(skb, dev))
1040 goto nla_put_failure;
1041
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001042 attr = nla_reserve(skb, IFLA_STATS,
1043 sizeof(struct rtnl_link_stats));
1044 if (attr == NULL)
1045 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Eric Dumazet28172732010-07-07 14:58:56 -07001047 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001048 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Jan Engelhardt10708f32010-03-11 09:57:29 +00001050 attr = nla_reserve(skb, IFLA_STATS64,
1051 sizeof(struct rtnl_link_stats64));
1052 if (attr == NULL)
1053 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001054 copy_rtnl_link_stats64(nla_data(attr), stats);
1055
David S. Millera6574342012-04-01 20:12:00 -04001056 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1057 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1058 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001059
Greg Rose115c9b82012-02-21 16:54:48 -05001060 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1061 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001062 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001063
Chris Wrightc02db8c2010-05-16 01:05:45 -07001064 struct nlattr *vfinfo, *vf;
1065 int num_vfs = dev_num_vf(dev->dev.parent);
1066
Chris Wrightc02db8c2010-05-16 01:05:45 -07001067 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1068 if (!vfinfo)
1069 goto nla_put_failure;
1070 for (i = 0; i < num_vfs; i++) {
1071 struct ifla_vf_info ivi;
1072 struct ifla_vf_mac vf_mac;
1073 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001074 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001075 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001076 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001077 struct ifla_vf_link_state vf_linkstate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001078
1079 /*
1080 * Not all SR-IOV capable drivers support the
1081 * spoofcheck query. Preset to -1 so the user
1082 * space tool can detect that the driver didn't
1083 * report anything.
1084 */
1085 ivi.spoofchk = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001086 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001087 /* The default value for VF link state is "auto"
1088 * IFLA_VF_LINK_STATE_AUTO which equals zero
1089 */
1090 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001091 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1092 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001093 vf_mac.vf =
1094 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001095 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001096 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001097 vf_spoofchk.vf =
1098 vf_linkstate.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001099
Chris Wrightc02db8c2010-05-16 01:05:45 -07001100 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1101 vf_vlan.vlan = ivi.vlan;
1102 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001103 vf_tx_rate.rate = ivi.max_tx_rate;
1104 vf_rate.min_tx_rate = ivi.min_tx_rate;
1105 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001106 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001107 vf_linkstate.link_state = ivi.linkstate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001108 vf = nla_nest_start(skb, IFLA_VF_INFO);
1109 if (!vf) {
1110 nla_nest_cancel(skb, vfinfo);
1111 goto nla_put_failure;
1112 }
David S. Millera6574342012-04-01 20:12:00 -04001113 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1114 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001115 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1116 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001117 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1118 &vf_tx_rate) ||
1119 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001120 &vf_spoofchk) ||
1121 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1122 &vf_linkstate))
David S. Millera6574342012-04-01 20:12:00 -04001123 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001124 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001125 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001126 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001127 }
Scott Feldman57b61082010-05-17 22:49:55 -07001128
David Gibsonc53864f2014-04-24 10:22:36 +10001129 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001130 goto nla_put_failure;
1131
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001132 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001133 if (rtnl_link_fill(skb, dev) < 0)
1134 goto nla_put_failure;
1135 }
1136
Thomas Graff8ff1822010-11-16 04:30:14 +00001137 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1138 goto nla_put_failure;
1139
1140 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1141 if (af_ops->fill_link_af) {
1142 struct nlattr *af;
1143 int err;
1144
1145 if (!(af = nla_nest_start(skb, af_ops->family)))
1146 goto nla_put_failure;
1147
1148 err = af_ops->fill_link_af(skb, dev);
1149
1150 /*
1151 * Caller may return ENODATA to indicate that there
1152 * was no data to be dumped. This is not an error, it
1153 * means we should trim the attribute header and
1154 * continue.
1155 */
1156 if (err == -ENODATA)
1157 nla_nest_cancel(skb, af);
1158 else if (err < 0)
1159 goto nla_put_failure;
1160
1161 nla_nest_end(skb, af);
1162 }
1163 }
1164
1165 nla_nest_end(skb, af_spec);
1166
Thomas Grafb60c5112006-08-04 23:05:34 -07001167 return nlmsg_end(skb, nlh);
1168
1169nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001170 nlmsg_cancel(skb, nlh);
1171 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
Jiri Pirkof7b12602014-02-18 20:53:18 +01001174static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1175 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1176 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1177 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1178 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1179 [IFLA_MTU] = { .type = NLA_U32 },
1180 [IFLA_LINK] = { .type = NLA_U32 },
1181 [IFLA_MASTER] = { .type = NLA_U32 },
1182 [IFLA_CARRIER] = { .type = NLA_U8 },
1183 [IFLA_TXQLEN] = { .type = NLA_U32 },
1184 [IFLA_WEIGHT] = { .type = NLA_U32 },
1185 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1186 [IFLA_LINKMODE] = { .type = NLA_U8 },
1187 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1188 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1189 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1190 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1191 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1192 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1193 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1194 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1195 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1196 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1197 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1198 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
1199 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001200 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirkof7b12602014-02-18 20:53:18 +01001201};
1202
1203static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1204 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1205 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1206 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1207 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1208};
1209
1210static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1211 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1212};
1213
1214static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1215 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1216 .len = sizeof(struct ifla_vf_mac) },
1217 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1218 .len = sizeof(struct ifla_vf_vlan) },
1219 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1220 .len = sizeof(struct ifla_vf_tx_rate) },
1221 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1222 .len = sizeof(struct ifla_vf_spoofchk) },
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001223 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1224 .len = sizeof(struct ifla_vf_rate) },
Doug Ledfordc5b46162014-06-11 10:38:03 -04001225 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1226 .len = sizeof(struct ifla_vf_link_state) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001227};
1228
1229static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1230 [IFLA_PORT_VF] = { .type = NLA_U32 },
1231 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1232 .len = PORT_PROFILE_MAX },
1233 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1234 .len = sizeof(struct ifla_port_vsi)},
1235 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1236 .len = PORT_UUID_MAX },
1237 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1238 .len = PORT_UUID_MAX },
1239 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1240 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1241};
1242
Thomas Grafb60c5112006-08-04 23:05:34 -07001243static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001245 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001246 int h, s_h;
1247 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001249 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001250 struct nlattr *tb[IFLA_MAX+1];
1251 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001252 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001253 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001255 s_h = cb->args[0];
1256 s_idx = cb->args[1];
1257
Eric Dumazete67f88d2011-04-27 22:56:07 +00001258 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001259 cb->seq = net->dev_base_seq;
1260
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001261 /* A hack to preserve kernel<->userspace interface.
1262 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1263 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1264 * what iproute2 < v3.9.0 used.
1265 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1266 * attribute, its netlink message is shorter than struct ifinfomsg.
1267 */
1268 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1269 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1270
1271 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001272
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001273 if (tb[IFLA_EXT_MASK])
1274 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1275 }
Greg Rose115c9b82012-02-21 16:54:48 -05001276
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001277 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1278 idx = 0;
1279 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001280 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001281 if (idx < s_idx)
1282 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001283 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1284 NETLINK_CB(cb->skb).portid,
1285 cb->nlh->nlmsg_seq, 0,
1286 NLM_F_MULTI,
1287 ext_filter_mask);
1288 /* If we ran out of room on the first message,
1289 * we're in trouble
1290 */
1291 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1292
1293 if (err <= 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001294 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001295
1296 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001297cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001298 idx++;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001301out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001302 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001303 cb->args[1] = idx;
1304 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 return skb->len;
1307}
1308
Jiri Pirkof7b12602014-02-18 20:53:18 +01001309int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1310{
1311 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1312}
1313EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001314
Eric W. Biederman81adee42009-11-08 00:53:51 -08001315struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1316{
1317 struct net *net;
1318 /* Examine the link attributes and figure out which
1319 * network namespace we are talking about.
1320 */
1321 if (tb[IFLA_NET_NS_PID])
1322 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001323 else if (tb[IFLA_NET_NS_FD])
1324 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001325 else
1326 net = get_net(src_net);
1327 return net;
1328}
1329EXPORT_SYMBOL(rtnl_link_get_net);
1330
Thomas Graf1840bb12008-02-23 19:54:36 -08001331static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1332{
1333 if (dev) {
1334 if (tb[IFLA_ADDRESS] &&
1335 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1336 return -EINVAL;
1337
1338 if (tb[IFLA_BROADCAST] &&
1339 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1340 return -EINVAL;
1341 }
1342
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001343 if (tb[IFLA_AF_SPEC]) {
1344 struct nlattr *af;
1345 int rem, err;
1346
1347 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1348 const struct rtnl_af_ops *af_ops;
1349
1350 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1351 return -EAFNOSUPPORT;
1352
1353 if (!af_ops->set_link_af)
1354 return -EOPNOTSUPP;
1355
1356 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001357 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001358 if (err < 0)
1359 return err;
1360 }
1361 }
1362 }
1363
Thomas Graf1840bb12008-02-23 19:54:36 -08001364 return 0;
1365}
1366
Chris Wrightc02db8c2010-05-16 01:05:45 -07001367static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1368{
1369 int rem, err = -EINVAL;
1370 struct nlattr *vf;
1371 const struct net_device_ops *ops = dev->netdev_ops;
1372
1373 nla_for_each_nested(vf, attr, rem) {
1374 switch (nla_type(vf)) {
1375 case IFLA_VF_MAC: {
1376 struct ifla_vf_mac *ivm;
1377 ivm = nla_data(vf);
1378 err = -EOPNOTSUPP;
1379 if (ops->ndo_set_vf_mac)
1380 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1381 ivm->mac);
1382 break;
1383 }
1384 case IFLA_VF_VLAN: {
1385 struct ifla_vf_vlan *ivv;
1386 ivv = nla_data(vf);
1387 err = -EOPNOTSUPP;
1388 if (ops->ndo_set_vf_vlan)
1389 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1390 ivv->vlan,
1391 ivv->qos);
1392 break;
1393 }
1394 case IFLA_VF_TX_RATE: {
1395 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001396 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001397 ivt = nla_data(vf);
1398 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001399 if (ops->ndo_get_vf_config)
1400 err = ops->ndo_get_vf_config(dev, ivt->vf,
1401 &ivf);
1402 if (err)
1403 break;
1404 err = -EOPNOTSUPP;
1405 if (ops->ndo_set_vf_rate)
1406 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1407 ivf.min_tx_rate,
1408 ivt->rate);
1409 break;
1410 }
1411 case IFLA_VF_RATE: {
1412 struct ifla_vf_rate *ivt;
1413 ivt = nla_data(vf);
1414 err = -EOPNOTSUPP;
1415 if (ops->ndo_set_vf_rate)
1416 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1417 ivt->min_tx_rate,
1418 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001419 break;
1420 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001421 case IFLA_VF_SPOOFCHK: {
1422 struct ifla_vf_spoofchk *ivs;
1423 ivs = nla_data(vf);
1424 err = -EOPNOTSUPP;
1425 if (ops->ndo_set_vf_spoofchk)
1426 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1427 ivs->setting);
1428 break;
1429 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001430 case IFLA_VF_LINK_STATE: {
1431 struct ifla_vf_link_state *ivl;
1432 ivl = nla_data(vf);
1433 err = -EOPNOTSUPP;
1434 if (ops->ndo_set_vf_link_state)
1435 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1436 ivl->link_state);
1437 break;
1438 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001439 default:
1440 err = -EINVAL;
1441 break;
1442 }
1443 if (err)
1444 break;
1445 }
1446 return err;
1447}
1448
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001449static int do_set_master(struct net_device *dev, int ifindex)
1450{
Jiri Pirko898e5062013-01-03 22:48:52 +00001451 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001452 const struct net_device_ops *ops;
1453 int err;
1454
Jiri Pirko898e5062013-01-03 22:48:52 +00001455 if (upper_dev) {
1456 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001457 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001458 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001459 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001460 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001461 if (err)
1462 return err;
1463 } else {
1464 return -EOPNOTSUPP;
1465 }
1466 }
1467
1468 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001469 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1470 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001471 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001472 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001473 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001474 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001475 if (err)
1476 return err;
1477 } else {
1478 return -EOPNOTSUPP;
1479 }
1480 }
1481 return 0;
1482}
1483
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001484#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001485/* notify flag means notify + modified. */
1486#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001487static int do_setlink(const struct sk_buff *skb,
1488 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001489 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001490{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001491 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001492 int err;
1493
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001494 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001495 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001496 if (IS_ERR(net)) {
1497 err = PTR_ERR(net);
1498 goto errout;
1499 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001500 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001501 err = -EPERM;
1502 goto errout;
1503 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001504 err = dev_change_net_namespace(dev, net, ifname);
1505 put_net(net);
1506 if (err)
1507 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001508 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001509 }
1510
Patrick McHardy0157f602007-06-13 12:03:36 -07001511 if (tb[IFLA_MAP]) {
1512 struct rtnl_link_ifmap *u_map;
1513 struct ifmap k_map;
1514
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001515 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001516 err = -EOPNOTSUPP;
1517 goto errout;
1518 }
1519
1520 if (!netif_device_present(dev)) {
1521 err = -ENODEV;
1522 goto errout;
1523 }
1524
1525 u_map = nla_data(tb[IFLA_MAP]);
1526 k_map.mem_start = (unsigned long) u_map->mem_start;
1527 k_map.mem_end = (unsigned long) u_map->mem_end;
1528 k_map.base_addr = (unsigned short) u_map->base_addr;
1529 k_map.irq = (unsigned char) u_map->irq;
1530 k_map.dma = (unsigned char) u_map->dma;
1531 k_map.port = (unsigned char) u_map->port;
1532
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001533 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001534 if (err < 0)
1535 goto errout;
1536
Nicolas Dichtelba998902014-09-01 16:07:29 +02001537 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001538 }
1539
1540 if (tb[IFLA_ADDRESS]) {
1541 struct sockaddr *sa;
1542 int len;
1543
Patrick McHardy0157f602007-06-13 12:03:36 -07001544 len = sizeof(sa_family_t) + dev->addr_len;
1545 sa = kmalloc(len, GFP_KERNEL);
1546 if (!sa) {
1547 err = -ENOMEM;
1548 goto errout;
1549 }
1550 sa->sa_family = dev->type;
1551 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1552 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001553 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001554 kfree(sa);
1555 if (err)
1556 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001557 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001558 }
1559
1560 if (tb[IFLA_MTU]) {
1561 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1562 if (err < 0)
1563 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001564 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001565 }
1566
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001567 if (tb[IFLA_GROUP]) {
1568 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001569 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001570 }
1571
Patrick McHardy0157f602007-06-13 12:03:36 -07001572 /*
1573 * Interface selected by interface index but interface
1574 * name provided implies that a name change has been
1575 * requested.
1576 */
1577 if (ifm->ifi_index > 0 && ifname[0]) {
1578 err = dev_change_name(dev, ifname);
1579 if (err < 0)
1580 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001581 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001582 }
1583
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001584 if (tb[IFLA_IFALIAS]) {
1585 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1586 nla_len(tb[IFLA_IFALIAS]));
1587 if (err < 0)
1588 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001589 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001590 }
1591
Patrick McHardy0157f602007-06-13 12:03:36 -07001592 if (tb[IFLA_BROADCAST]) {
1593 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001594 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001595 }
1596
1597 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001598 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001599 if (err < 0)
1600 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001601 }
1602
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001603 if (tb[IFLA_MASTER]) {
1604 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1605 if (err)
1606 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001607 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001608 }
1609
Jiri Pirko9a572472012-12-27 23:49:39 +00001610 if (tb[IFLA_CARRIER]) {
1611 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1612 if (err)
1613 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001614 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001615 }
1616
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001617 if (tb[IFLA_TXQLEN]) {
1618 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1619
1620 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001621 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001622
1623 dev->tx_queue_len = value;
1624 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001625
Patrick McHardy0157f602007-06-13 12:03:36 -07001626 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001627 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001628
1629 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001630 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1631
David S. Miller93b2d4a2008-02-17 18:35:07 -08001632 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001633 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001634 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001635 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001636 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001637 }
1638
Chris Wrightc02db8c2010-05-16 01:05:45 -07001639 if (tb[IFLA_VFINFO_LIST]) {
1640 struct nlattr *attr;
1641 int rem;
1642 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001643 if (nla_type(attr) != IFLA_VF_INFO) {
1644 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001645 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001646 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001647 err = do_setvfinfo(dev, attr);
1648 if (err < 0)
1649 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001650 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001651 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001652 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001653 err = 0;
1654
Scott Feldman57b61082010-05-17 22:49:55 -07001655 if (tb[IFLA_VF_PORTS]) {
1656 struct nlattr *port[IFLA_PORT_MAX+1];
1657 struct nlattr *attr;
1658 int vf;
1659 int rem;
1660
1661 err = -EOPNOTSUPP;
1662 if (!ops->ndo_set_vf_port)
1663 goto errout;
1664
1665 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1666 if (nla_type(attr) != IFLA_VF_PORT)
1667 continue;
1668 err = nla_parse_nested(port, IFLA_PORT_MAX,
1669 attr, ifla_port_policy);
1670 if (err < 0)
1671 goto errout;
1672 if (!port[IFLA_PORT_VF]) {
1673 err = -EOPNOTSUPP;
1674 goto errout;
1675 }
1676 vf = nla_get_u32(port[IFLA_PORT_VF]);
1677 err = ops->ndo_set_vf_port(dev, vf, port);
1678 if (err < 0)
1679 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001680 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001681 }
1682 }
1683 err = 0;
1684
1685 if (tb[IFLA_PORT_SELF]) {
1686 struct nlattr *port[IFLA_PORT_MAX+1];
1687
1688 err = nla_parse_nested(port, IFLA_PORT_MAX,
1689 tb[IFLA_PORT_SELF], ifla_port_policy);
1690 if (err < 0)
1691 goto errout;
1692
1693 err = -EOPNOTSUPP;
1694 if (ops->ndo_set_vf_port)
1695 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1696 if (err < 0)
1697 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001698 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001699 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001700
1701 if (tb[IFLA_AF_SPEC]) {
1702 struct nlattr *af;
1703 int rem;
1704
1705 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1706 const struct rtnl_af_ops *af_ops;
1707
1708 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001709 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001710
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001711 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001712 if (err < 0)
1713 goto errout;
1714
Nicolas Dichtelba998902014-09-01 16:07:29 +02001715 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001716 }
1717 }
Scott Feldman57b61082010-05-17 22:49:55 -07001718 err = 0;
1719
Patrick McHardy0157f602007-06-13 12:03:36 -07001720errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001721 if (status & DO_SETLINK_MODIFIED) {
1722 if (status & DO_SETLINK_NOTIFY)
1723 netdev_state_change(dev);
1724
1725 if (err < 0)
1726 net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
1727 dev->name);
1728 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001729
Patrick McHardy0157f602007-06-13 12:03:36 -07001730 return err;
1731}
1732
Thomas Graf661d2962013-03-21 07:45:29 +00001733static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001735 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001736 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001738 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001739 struct nlattr *tb[IFLA_MAX+1];
1740 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Thomas Grafda5e0492006-08-10 21:17:37 -07001742 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1743 if (err < 0)
1744 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Thomas Graf5176f912006-08-26 20:13:18 -07001746 if (tb[IFLA_IFNAME])
1747 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001748 else
1749 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001752 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001753 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001754 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001755 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001756 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001757 else
1758 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Thomas Grafda5e0492006-08-10 21:17:37 -07001760 if (dev == NULL) {
1761 err = -ENODEV;
1762 goto errout;
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Eric Dumazete0d087a2009-11-07 01:26:17 -08001765 err = validate_linkmsg(dev, tb);
1766 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001767 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001768
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001769 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001770errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return err;
1772}
1773
Thomas Graf661d2962013-03-21 07:45:29 +00001774static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001775{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001776 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001777 const struct rtnl_link_ops *ops;
1778 struct net_device *dev;
1779 struct ifinfomsg *ifm;
1780 char ifname[IFNAMSIZ];
1781 struct nlattr *tb[IFLA_MAX+1];
1782 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001783 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001784
1785 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1786 if (err < 0)
1787 return err;
1788
1789 if (tb[IFLA_IFNAME])
1790 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1791
1792 ifm = nlmsg_data(nlh);
1793 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001794 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001795 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001796 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001797 else
1798 return -EINVAL;
1799
1800 if (!dev)
1801 return -ENODEV;
1802
1803 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001804 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001805 return -EOPNOTSUPP;
1806
Eric Dumazet226bd342011-05-08 23:17:57 +00001807 ops->dellink(dev, &list_kill);
1808 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001809 return 0;
1810}
1811
Patrick McHardy3729d502010-02-26 06:34:54 +00001812int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1813{
1814 unsigned int old_flags;
1815 int err;
1816
1817 old_flags = dev->flags;
1818 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1819 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1820 if (err < 0)
1821 return err;
1822 }
1823
1824 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001825
Nicolas Dichtela528c212013-09-25 12:02:44 +02001826 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001827 return 0;
1828}
1829EXPORT_SYMBOL(rtnl_configure_link);
1830
Rami Rosenc0713562012-11-30 01:08:47 +00001831struct net_device *rtnl_create_link(struct net *net,
Tom Gundersen55177502014-07-14 16:37:25 +02001832 char *ifname, unsigned char name_assign_type,
1833 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001834{
1835 int err;
1836 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001837 unsigned int num_tx_queues = 1;
1838 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001839
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001840 if (tb[IFLA_NUM_TX_QUEUES])
1841 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1842 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001843 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001844
1845 if (tb[IFLA_NUM_RX_QUEUES])
1846 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1847 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001848 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001849
Pavel Emelianove7199282007-08-08 22:16:38 -07001850 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001851 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001852 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001853 if (!dev)
1854 goto err;
1855
Eric W. Biederman81adee42009-11-08 00:53:51 -08001856 dev_net_set(dev, net);
1857 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001858 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001859
Pavel Emelianove7199282007-08-08 22:16:38 -07001860 if (tb[IFLA_MTU])
1861 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001862 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001863 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1864 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001865 dev->addr_assign_type = NET_ADDR_SET;
1866 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001867 if (tb[IFLA_BROADCAST])
1868 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1869 nla_len(tb[IFLA_BROADCAST]));
1870 if (tb[IFLA_TXQLEN])
1871 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1872 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001873 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001874 if (tb[IFLA_LINKMODE])
1875 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001876 if (tb[IFLA_GROUP])
1877 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001878
1879 return dev;
1880
Pavel Emelianove7199282007-08-08 22:16:38 -07001881err:
1882 return ERR_PTR(err);
1883}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001884EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001885
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001886static int rtnl_group_changelink(const struct sk_buff *skb,
1887 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001888 struct ifinfomsg *ifm,
1889 struct nlattr **tb)
1890{
1891 struct net_device *dev;
1892 int err;
1893
1894 for_each_netdev(net, dev) {
1895 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001896 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001897 if (err < 0)
1898 return err;
1899 }
1900 }
1901
1902 return 0;
1903}
1904
Thomas Graf661d2962013-03-21 07:45:29 +00001905static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001906{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001907 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001908 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001909 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001910 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001911 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001912 struct ifinfomsg *ifm;
1913 char kind[MODULE_NAME_LEN];
1914 char ifname[IFNAMSIZ];
1915 struct nlattr *tb[IFLA_MAX+1];
1916 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02001917 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001918 int err;
1919
Johannes Berg95a5afc2008-10-16 15:24:51 -07001920#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001921replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001922#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001923 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1924 if (err < 0)
1925 return err;
1926
1927 if (tb[IFLA_IFNAME])
1928 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1929 else
1930 ifname[0] = '\0';
1931
1932 ifm = nlmsg_data(nlh);
1933 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001934 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001935 else {
1936 if (ifname[0])
1937 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001938 else
1939 dev = NULL;
1940 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001941
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001942 if (dev) {
1943 master_dev = netdev_master_upper_dev_get(dev);
1944 if (master_dev)
1945 m_ops = master_dev->rtnl_link_ops;
1946 }
1947
Eric Dumazete0d087a2009-11-07 01:26:17 -08001948 err = validate_linkmsg(dev, tb);
1949 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001950 return err;
1951
Patrick McHardy38f7b872007-06-13 12:03:51 -07001952 if (tb[IFLA_LINKINFO]) {
1953 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1954 tb[IFLA_LINKINFO], ifla_info_policy);
1955 if (err < 0)
1956 return err;
1957 } else
1958 memset(linkinfo, 0, sizeof(linkinfo));
1959
1960 if (linkinfo[IFLA_INFO_KIND]) {
1961 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1962 ops = rtnl_link_ops_get(kind);
1963 } else {
1964 kind[0] = '\0';
1965 ops = NULL;
1966 }
1967
1968 if (1) {
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001969 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1970 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1971 struct nlattr **data = NULL;
1972 struct nlattr **slave_data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001973 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001974
1975 if (ops) {
1976 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1977 err = nla_parse_nested(attr, ops->maxtype,
1978 linkinfo[IFLA_INFO_DATA],
1979 ops->policy);
1980 if (err < 0)
1981 return err;
1982 data = attr;
1983 }
1984 if (ops->validate) {
1985 err = ops->validate(tb, data);
1986 if (err < 0)
1987 return err;
1988 }
1989 }
1990
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001991 if (m_ops) {
1992 if (m_ops->slave_maxtype &&
1993 linkinfo[IFLA_INFO_SLAVE_DATA]) {
1994 err = nla_parse_nested(slave_attr,
1995 m_ops->slave_maxtype,
1996 linkinfo[IFLA_INFO_SLAVE_DATA],
1997 m_ops->slave_policy);
1998 if (err < 0)
1999 return err;
2000 slave_data = slave_attr;
2001 }
2002 if (m_ops->slave_validate) {
2003 err = m_ops->slave_validate(tb, slave_data);
2004 if (err < 0)
2005 return err;
2006 }
2007 }
2008
Patrick McHardy38f7b872007-06-13 12:03:51 -07002009 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002010 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002011
2012 if (nlh->nlmsg_flags & NLM_F_EXCL)
2013 return -EEXIST;
2014 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2015 return -EOPNOTSUPP;
2016
2017 if (linkinfo[IFLA_INFO_DATA]) {
2018 if (!ops || ops != dev->rtnl_link_ops ||
2019 !ops->changelink)
2020 return -EOPNOTSUPP;
2021
2022 err = ops->changelink(dev, tb, data);
2023 if (err < 0)
2024 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002025 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002026 }
2027
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002028 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2029 if (!m_ops || !m_ops->slave_changelink)
2030 return -EOPNOTSUPP;
2031
2032 err = m_ops->slave_changelink(master_dev, dev,
2033 tb, slave_data);
2034 if (err < 0)
2035 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002036 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002037 }
2038
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002039 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002040 }
2041
Patrick McHardyffa934f2011-01-20 03:00:42 +00002042 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2043 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002044 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002045 nla_get_u32(tb[IFLA_GROUP]),
2046 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002047 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002048 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002049
Patrick McHardy0e068772007-07-11 19:42:31 -07002050 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002051 return -EOPNOTSUPP;
2052
2053 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002054#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002055 if (kind[0]) {
2056 __rtnl_unlock();
2057 request_module("rtnl-link-%s", kind);
2058 rtnl_lock();
2059 ops = rtnl_link_ops_get(kind);
2060 if (ops)
2061 goto replay;
2062 }
2063#endif
2064 return -EOPNOTSUPP;
2065 }
2066
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002067 if (!ops->setup)
2068 return -EOPNOTSUPP;
2069
Tom Gundersen55177502014-07-14 16:37:25 +02002070 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002071 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002072 name_assign_type = NET_NAME_ENUM;
2073 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002074
Eric W. Biederman81adee42009-11-08 00:53:51 -08002075 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002076 if (IS_ERR(dest_net))
2077 return PTR_ERR(dest_net);
2078
Tom Gundersen55177502014-07-14 16:37:25 +02002079 dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002080 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002081 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002082 goto out;
2083 }
2084
2085 dev->ifindex = ifm->ifi_index;
2086
Cong Wang0e0eee22014-02-11 15:51:30 -08002087 if (ops->newlink) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002088 err = ops->newlink(net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002089 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002090 * and unregister it on failure after registration
2091 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002092 */
Cong Wange51fb152014-06-03 16:40:47 -07002093 if (err < 0) {
2094 /* If device is not registered at all, free it now */
2095 if (dev->reg_state == NETREG_UNINITIALIZED)
2096 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002097 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002098 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002099 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002100 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002101 if (err < 0) {
2102 free_netdev(dev);
2103 goto out;
2104 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002105 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002106 err = rtnl_configure_link(dev, ifm);
2107 if (err < 0)
2108 unregister_netdevice(dev);
2109out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08002110 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002111 return err;
2112 }
2113}
2114
Thomas Graf661d2962013-03-21 07:45:29 +00002115static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002116{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002117 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002118 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002119 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002120 struct nlattr *tb[IFLA_MAX+1];
2121 struct net_device *dev = NULL;
2122 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002123 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002124 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002125
Thomas Grafb60c5112006-08-04 23:05:34 -07002126 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2127 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002128 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002129
Eric Dumazeta3d12892009-10-21 10:59:31 +00002130 if (tb[IFLA_IFNAME])
2131 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2132
Greg Rose115c9b82012-02-21 16:54:48 -05002133 if (tb[IFLA_EXT_MASK])
2134 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2135
Thomas Grafb60c5112006-08-04 23:05:34 -07002136 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002137 if (ifm->ifi_index > 0)
2138 dev = __dev_get_by_index(net, ifm->ifi_index);
2139 else if (tb[IFLA_IFNAME])
2140 dev = __dev_get_by_name(net, ifname);
2141 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002142 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002143
Eric Dumazeta3d12892009-10-21 10:59:31 +00002144 if (dev == NULL)
2145 return -ENODEV;
2146
Greg Rose115c9b82012-02-21 16:54:48 -05002147 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002148 if (nskb == NULL)
2149 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002150
Eric W. Biederman15e47302012-09-07 20:12:54 +00002151 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002152 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002153 if (err < 0) {
2154 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2155 WARN_ON(err == -EMSGSIZE);
2156 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002157 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002158 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002159
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002160 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002161}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002162
Greg Rose115c9b82012-02-21 16:54:48 -05002163static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002164{
Greg Rose115c9b82012-02-21 16:54:48 -05002165 struct net *net = sock_net(skb->sk);
2166 struct net_device *dev;
2167 struct nlattr *tb[IFLA_MAX+1];
2168 u32 ext_filter_mask = 0;
2169 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002170 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002171
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002172 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2173 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2174 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2175
2176 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002177 if (tb[IFLA_EXT_MASK])
2178 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2179 }
Greg Rose115c9b82012-02-21 16:54:48 -05002180
2181 if (!ext_filter_mask)
2182 return NLMSG_GOODSIZE;
2183 /*
2184 * traverse the list of net devices and compute the minimum
2185 * buffer size based upon the filter mask.
2186 */
2187 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2188 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2189 if_nlmsg_size(dev,
2190 ext_filter_mask));
2191 }
2192
Greg Rosec7ac8672011-06-10 01:27:09 +00002193 return min_ifinfo_dump_size;
2194}
2195
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002196static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int idx;
2199 int s_idx = cb->family;
2200
2201 if (s_idx == 0)
2202 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002203 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int type = cb->nlh->nlmsg_type-RTM_BASE;
2205 if (idx < s_idx || idx == PF_PACKET)
2206 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002207 if (rtnl_msg_handlers[idx] == NULL ||
2208 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002210 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002212 cb->prev_seq = 0;
2213 cb->seq = 0;
2214 }
Thomas Grafe2849862007-03-22 11:48:11 -07002215 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 break;
2217 }
2218 cb->family = idx;
2219
2220 return skb->len;
2221}
2222
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002223void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2224 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002226 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002228 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002229 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002231 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002232 if (skb == NULL)
2233 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Greg Rose115c9b82012-02-21 16:54:48 -05002235 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002236 if (err < 0) {
2237 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2238 WARN_ON(err == -EMSGSIZE);
2239 kfree_skb(skb);
2240 goto errout;
2241 }
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002242 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002243 return;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002244errout:
2245 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002246 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002248EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
John Fastabendd83b0602012-04-15 06:44:08 +00002250static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2251 struct net_device *dev,
2252 u8 *addr, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002253 int type, unsigned int flags,
2254 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002255{
2256 struct nlmsghdr *nlh;
2257 struct ndmsg *ndm;
2258
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002259 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002260 if (!nlh)
2261 return -EMSGSIZE;
2262
2263 ndm = nlmsg_data(nlh);
2264 ndm->ndm_family = AF_BRIDGE;
2265 ndm->ndm_pad1 = 0;
2266 ndm->ndm_pad2 = 0;
2267 ndm->ndm_flags = flags;
2268 ndm->ndm_type = 0;
2269 ndm->ndm_ifindex = dev->ifindex;
2270 ndm->ndm_state = NUD_PERMANENT;
2271
2272 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2273 goto nla_put_failure;
2274
2275 return nlmsg_end(skb, nlh);
2276
2277nla_put_failure:
2278 nlmsg_cancel(skb, nlh);
2279 return -EMSGSIZE;
2280}
2281
John Fastabend3ff661c2012-04-15 06:44:14 +00002282static inline size_t rtnl_fdb_nlmsg_size(void)
2283{
2284 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2285}
2286
2287static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2288{
2289 struct net *net = dev_net(dev);
2290 struct sk_buff *skb;
2291 int err = -ENOBUFS;
2292
2293 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2294 if (!skb)
2295 goto errout;
2296
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002297 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002298 if (err < 0) {
2299 kfree_skb(skb);
2300 goto errout;
2301 }
2302
2303 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2304 return;
2305errout:
2306 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2307}
2308
Vlad Yasevich090096b2013-03-06 15:39:42 +00002309/**
2310 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2311 */
2312int ndo_dflt_fdb_add(struct ndmsg *ndm,
2313 struct nlattr *tb[],
2314 struct net_device *dev,
2315 const unsigned char *addr,
2316 u16 flags)
2317{
2318 int err = -EINVAL;
2319
2320 /* If aging addresses are supported device will need to
2321 * implement its own handler for this.
2322 */
2323 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2324 pr_info("%s: FDB only supports static addresses\n", dev->name);
2325 return err;
2326 }
2327
2328 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2329 err = dev_uc_add_excl(dev, addr);
2330 else if (is_multicast_ether_addr(addr))
2331 err = dev_mc_add_excl(dev, addr);
2332
2333 /* Only return duplicate errors if NLM_F_EXCL is set */
2334 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2335 err = 0;
2336
2337 return err;
2338}
2339EXPORT_SYMBOL(ndo_dflt_fdb_add);
2340
Thomas Graf661d2962013-03-21 07:45:29 +00002341static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002342{
2343 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002344 struct ndmsg *ndm;
2345 struct nlattr *tb[NDA_MAX+1];
2346 struct net_device *dev;
2347 u8 *addr;
2348 int err;
2349
2350 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2351 if (err < 0)
2352 return err;
2353
2354 ndm = nlmsg_data(nlh);
2355 if (ndm->ndm_ifindex == 0) {
2356 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2357 return -EINVAL;
2358 }
2359
2360 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2361 if (dev == NULL) {
2362 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2363 return -ENODEV;
2364 }
2365
2366 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2367 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2368 return -EINVAL;
2369 }
2370
2371 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002372
2373 err = -EOPNOTSUPP;
2374
2375 /* Support fdb on master device the net/bridge default case */
2376 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2377 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002378 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2379 const struct net_device_ops *ops = br_dev->netdev_ops;
2380
2381 err = ops->ndo_fdb_add(ndm, tb, dev, addr, nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002382 if (err)
2383 goto out;
2384 else
2385 ndm->ndm_flags &= ~NTF_MASTER;
2386 }
2387
2388 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002389 if ((ndm->ndm_flags & NTF_SELF)) {
2390 if (dev->netdev_ops->ndo_fdb_add)
2391 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2392 nlh->nlmsg_flags);
2393 else
2394 err = ndo_dflt_fdb_add(ndm, tb, dev, addr,
2395 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002396
John Fastabend3ff661c2012-04-15 06:44:14 +00002397 if (!err) {
2398 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002399 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002400 }
John Fastabend77162022012-04-15 06:43:56 +00002401 }
2402out:
2403 return err;
2404}
2405
Vlad Yasevich090096b2013-03-06 15:39:42 +00002406/**
2407 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2408 */
2409int ndo_dflt_fdb_del(struct ndmsg *ndm,
2410 struct nlattr *tb[],
2411 struct net_device *dev,
2412 const unsigned char *addr)
2413{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002414 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002415
2416 /* If aging addresses are supported device will need to
2417 * implement its own handler for this.
2418 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002419 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002420 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002421 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002422 }
2423
2424 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2425 err = dev_uc_del(dev, addr);
2426 else if (is_multicast_ether_addr(addr))
2427 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002428
2429 return err;
2430}
2431EXPORT_SYMBOL(ndo_dflt_fdb_del);
2432
Thomas Graf661d2962013-03-21 07:45:29 +00002433static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002434{
2435 struct net *net = sock_net(skb->sk);
2436 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002437 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002438 struct net_device *dev;
2439 int err = -EINVAL;
2440 __u8 *addr;
2441
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002442 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002443 return -EPERM;
2444
2445 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2446 if (err < 0)
2447 return err;
John Fastabend77162022012-04-15 06:43:56 +00002448
2449 ndm = nlmsg_data(nlh);
2450 if (ndm->ndm_ifindex == 0) {
2451 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2452 return -EINVAL;
2453 }
2454
2455 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2456 if (dev == NULL) {
2457 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2458 return -ENODEV;
2459 }
2460
Vlad Yasevich1690be62013-02-13 12:00:18 +00002461 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2462 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002463 return -EINVAL;
2464 }
2465
Vlad Yasevich1690be62013-02-13 12:00:18 +00002466 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002467
John Fastabend77162022012-04-15 06:43:56 +00002468 err = -EOPNOTSUPP;
2469
2470 /* Support fdb on master device the net/bridge default case */
2471 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2472 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002473 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2474 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002475
Jiri Pirko898e5062013-01-03 22:48:52 +00002476 if (ops->ndo_fdb_del)
Vlad Yasevich1690be62013-02-13 12:00:18 +00002477 err = ops->ndo_fdb_del(ndm, tb, dev, addr);
John Fastabend77162022012-04-15 06:43:56 +00002478
2479 if (err)
2480 goto out;
2481 else
2482 ndm->ndm_flags &= ~NTF_MASTER;
2483 }
2484
2485 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002486 if (ndm->ndm_flags & NTF_SELF) {
2487 if (dev->netdev_ops->ndo_fdb_del)
2488 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr);
2489 else
2490 err = ndo_dflt_fdb_del(ndm, tb, dev, addr);
John Fastabend77162022012-04-15 06:43:56 +00002491
John Fastabend3ff661c2012-04-15 06:44:14 +00002492 if (!err) {
2493 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002494 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002495 }
John Fastabend77162022012-04-15 06:43:56 +00002496 }
2497out:
2498 return err;
2499}
2500
John Fastabendd83b0602012-04-15 06:44:08 +00002501static int nlmsg_populate_fdb(struct sk_buff *skb,
2502 struct netlink_callback *cb,
2503 struct net_device *dev,
2504 int *idx,
2505 struct netdev_hw_addr_list *list)
2506{
2507 struct netdev_hw_addr *ha;
2508 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002509 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002510
Eric W. Biederman15e47302012-09-07 20:12:54 +00002511 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002512 seq = cb->nlh->nlmsg_seq;
2513
2514 list_for_each_entry(ha, &list->list, list) {
2515 if (*idx < cb->args[0])
2516 goto skip;
2517
2518 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
John Fastabenda7a558f2012-11-01 16:23:10 +00002519 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002520 RTM_NEWNEIGH, NTF_SELF,
2521 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002522 if (err < 0)
2523 return err;
2524skip:
2525 *idx += 1;
2526 }
2527 return 0;
2528}
2529
2530/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002531 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002532 * @nlh: netlink message header
2533 * @dev: netdevice
2534 *
2535 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002536 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002537 */
2538int ndo_dflt_fdb_dump(struct sk_buff *skb,
2539 struct netlink_callback *cb,
2540 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002541 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002542 int idx)
2543{
2544 int err;
2545
2546 netif_addr_lock_bh(dev);
2547 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2548 if (err)
2549 goto out;
2550 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2551out:
2552 netif_addr_unlock_bh(dev);
2553 return idx;
2554}
2555EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2556
John Fastabend77162022012-04-15 06:43:56 +00002557static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2558{
John Fastabend77162022012-04-15 06:43:56 +00002559 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002560 struct nlattr *tb[IFLA_MAX+1];
2561 struct net_device *bdev = NULL;
2562 struct net_device *br_dev = NULL;
2563 const struct net_device_ops *ops = NULL;
2564 const struct net_device_ops *cops = NULL;
2565 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2566 struct net *net = sock_net(skb->sk);
2567 int brport_idx = 0;
2568 int br_idx = 0;
2569 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002570
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002571 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2572 ifla_policy) == 0) {
2573 if (tb[IFLA_MASTER])
2574 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2575 }
John Fastabend77162022012-04-15 06:43:56 +00002576
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002577 brport_idx = ifm->ifi_index;
2578
2579 if (br_idx) {
2580 br_dev = __dev_get_by_index(net, br_idx);
2581 if (!br_dev)
2582 return -ENODEV;
2583
2584 ops = br_dev->netdev_ops;
2585 bdev = br_dev;
2586 }
2587
2588 for_each_netdev(net, dev) {
2589 if (brport_idx && (dev->ifindex != brport_idx))
2590 continue;
2591
2592 if (!br_idx) { /* user did not specify a specific bridge */
2593 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2594 br_dev = netdev_master_upper_dev_get(dev);
2595 cops = br_dev->netdev_ops;
2596 }
2597
2598 bdev = dev;
2599 } else {
2600 if (dev != br_dev &&
2601 !(dev->priv_flags & IFF_BRIDGE_PORT))
2602 continue;
2603
2604 if (br_dev != netdev_master_upper_dev_get(dev) &&
2605 !(dev->priv_flags & IFF_EBRIDGE))
2606 continue;
2607
2608 bdev = br_dev;
2609 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002610 }
2611
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002612 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2613 if (cops && cops->ndo_fdb_dump)
2614 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2615 idx);
2616 }
2617
2618 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
John Fastabend77162022012-04-15 06:43:56 +00002619 if (dev->netdev_ops->ndo_fdb_dump)
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002620 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002621 idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002622
2623 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002624 }
John Fastabend77162022012-04-15 06:43:56 +00002625
2626 cb->args[0] = idx;
2627 return skb->len;
2628}
2629
John Fastabend815cccb2012-10-24 08:13:09 +00002630int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2631 struct net_device *dev, u16 mode)
2632{
2633 struct nlmsghdr *nlh;
2634 struct ifinfomsg *ifm;
2635 struct nlattr *br_afspec;
2636 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002637 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002638
2639 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2640 if (nlh == NULL)
2641 return -EMSGSIZE;
2642
2643 ifm = nlmsg_data(nlh);
2644 ifm->ifi_family = AF_BRIDGE;
2645 ifm->__ifi_pad = 0;
2646 ifm->ifi_type = dev->type;
2647 ifm->ifi_index = dev->ifindex;
2648 ifm->ifi_flags = dev_get_flags(dev);
2649 ifm->ifi_change = 0;
2650
2651
2652 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2653 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2654 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002655 (br_dev &&
2656 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002657 (dev->addr_len &&
2658 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2659 (dev->ifindex != dev->iflink &&
2660 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2661 goto nla_put_failure;
2662
2663 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2664 if (!br_afspec)
2665 goto nla_put_failure;
2666
2667 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF) ||
2668 nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2669 nla_nest_cancel(skb, br_afspec);
2670 goto nla_put_failure;
2671 }
2672 nla_nest_end(skb, br_afspec);
2673
2674 return nlmsg_end(skb, nlh);
2675nla_put_failure:
2676 nlmsg_cancel(skb, nlh);
2677 return -EMSGSIZE;
2678}
2679EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2680
John Fastabende5a55a82012-10-24 08:12:57 +00002681static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2682{
2683 struct net *net = sock_net(skb->sk);
2684 struct net_device *dev;
2685 int idx = 0;
2686 u32 portid = NETLINK_CB(cb->skb).portid;
2687 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002688 u32 filter_mask = 0;
2689
Thomas Grafaa68c202014-11-26 13:42:20 +01002690 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2691 struct nlattr *extfilt;
2692
2693 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2694 IFLA_EXT_MASK);
2695 if (extfilt) {
2696 if (nla_len(extfilt) < sizeof(filter_mask))
2697 return -EINVAL;
2698
2699 filter_mask = nla_get_u32(extfilt);
2700 }
2701 }
John Fastabende5a55a82012-10-24 08:12:57 +00002702
2703 rcu_read_lock();
2704 for_each_netdev_rcu(net, dev) {
2705 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002706 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002707
Jiri Pirko898e5062013-01-03 22:48:52 +00002708 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002709 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002710 br_dev->netdev_ops->ndo_bridge_getlink(
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002711 skb, portid, seq, dev, filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002712 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002713 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002714 }
2715
2716 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002717 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002718 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2719 filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002720 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002721 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002722 }
2723 }
2724 rcu_read_unlock();
2725 cb->args[0] = idx;
2726
2727 return skb->len;
2728}
2729
John Fastabend2469ffd2012-10-24 08:13:03 +00002730static inline size_t bridge_nlmsg_size(void)
2731{
2732 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2733 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2734 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2735 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2736 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2737 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2738 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2739 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2740 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2741 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2742 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2743}
2744
2745static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2746{
2747 struct net *net = dev_net(dev);
Jiri Pirko898e5062013-01-03 22:48:52 +00002748 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00002749 struct sk_buff *skb;
2750 int err = -EOPNOTSUPP;
2751
2752 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2753 if (!skb) {
2754 err = -ENOMEM;
2755 goto errout;
2756 }
2757
John Fastabendc38e01b2012-11-02 16:32:36 +00002758 if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002759 br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002760 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002761 if (err < 0)
2762 goto errout;
2763 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002764
John Fastabendc38e01b2012-11-02 16:32:36 +00002765 if ((flags & BRIDGE_FLAGS_SELF) &&
2766 dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002767 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002768 if (err < 0)
2769 goto errout;
2770 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002771
2772 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2773 return 0;
2774errout:
2775 WARN_ON(err == -EMSGSIZE);
2776 kfree_skb(skb);
2777 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2778 return err;
2779}
2780
Thomas Graf661d2962013-03-21 07:45:29 +00002781static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00002782{
2783 struct net *net = sock_net(skb->sk);
2784 struct ifinfomsg *ifm;
2785 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00002786 struct nlattr *br_spec, *attr = NULL;
2787 int rem, err = -EOPNOTSUPP;
John Fastabendc38e01b2012-11-02 16:32:36 +00002788 u16 oflags, flags = 0;
2789 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00002790
2791 if (nlmsg_len(nlh) < sizeof(*ifm))
2792 return -EINVAL;
2793
2794 ifm = nlmsg_data(nlh);
2795 if (ifm->ifi_family != AF_BRIDGE)
2796 return -EPFNOSUPPORT;
2797
2798 dev = __dev_get_by_index(net, ifm->ifi_index);
2799 if (!dev) {
2800 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2801 return -ENODEV;
2802 }
2803
John Fastabend2469ffd2012-10-24 08:13:03 +00002804 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2805 if (br_spec) {
2806 nla_for_each_nested(attr, br_spec, rem) {
2807 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002808 if (nla_len(attr) < sizeof(flags))
2809 return -EINVAL;
2810
John Fastabendc38e01b2012-11-02 16:32:36 +00002811 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00002812 flags = nla_get_u16(attr);
2813 break;
2814 }
2815 }
2816 }
2817
John Fastabendc38e01b2012-11-02 16:32:36 +00002818 oflags = flags;
2819
John Fastabend2469ffd2012-10-24 08:13:03 +00002820 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002821 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2822
2823 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002824 err = -EOPNOTSUPP;
2825 goto out;
2826 }
2827
Jiri Pirko898e5062013-01-03 22:48:52 +00002828 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002829 if (err)
2830 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00002831
2832 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00002833 }
2834
John Fastabend2469ffd2012-10-24 08:13:03 +00002835 if ((flags & BRIDGE_FLAGS_SELF)) {
2836 if (!dev->netdev_ops->ndo_bridge_setlink)
2837 err = -EOPNOTSUPP;
2838 else
2839 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002840
John Fastabend2469ffd2012-10-24 08:13:03 +00002841 if (!err)
2842 flags &= ~BRIDGE_FLAGS_SELF;
2843 }
2844
John Fastabendc38e01b2012-11-02 16:32:36 +00002845 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00002846 memcpy(nla_data(attr), &flags, sizeof(flags));
2847 /* Generate event to notify upper layer of bridge change */
2848 if (!err)
John Fastabendc38e01b2012-11-02 16:32:36 +00002849 err = rtnl_bridge_notify(dev, oflags);
John Fastabende5a55a82012-10-24 08:12:57 +00002850out:
2851 return err;
2852}
2853
Thomas Graf661d2962013-03-21 07:45:29 +00002854static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00002855{
2856 struct net *net = sock_net(skb->sk);
2857 struct ifinfomsg *ifm;
2858 struct net_device *dev;
2859 struct nlattr *br_spec, *attr = NULL;
2860 int rem, err = -EOPNOTSUPP;
2861 u16 oflags, flags = 0;
2862 bool have_flags = false;
2863
2864 if (nlmsg_len(nlh) < sizeof(*ifm))
2865 return -EINVAL;
2866
2867 ifm = nlmsg_data(nlh);
2868 if (ifm->ifi_family != AF_BRIDGE)
2869 return -EPFNOSUPPORT;
2870
2871 dev = __dev_get_by_index(net, ifm->ifi_index);
2872 if (!dev) {
2873 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2874 return -ENODEV;
2875 }
2876
2877 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2878 if (br_spec) {
2879 nla_for_each_nested(attr, br_spec, rem) {
2880 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002881 if (nla_len(attr) < sizeof(flags))
2882 return -EINVAL;
2883
Vlad Yasevich407af322013-02-13 12:00:12 +00002884 have_flags = true;
2885 flags = nla_get_u16(attr);
2886 break;
2887 }
2888 }
2889 }
2890
2891 oflags = flags;
2892
2893 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
2894 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2895
2896 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
2897 err = -EOPNOTSUPP;
2898 goto out;
2899 }
2900
2901 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2902 if (err)
2903 goto out;
2904
2905 flags &= ~BRIDGE_FLAGS_MASTER;
2906 }
2907
2908 if ((flags & BRIDGE_FLAGS_SELF)) {
2909 if (!dev->netdev_ops->ndo_bridge_dellink)
2910 err = -EOPNOTSUPP;
2911 else
2912 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
2913
2914 if (!err)
2915 flags &= ~BRIDGE_FLAGS_SELF;
2916 }
2917
2918 if (have_flags)
2919 memcpy(nla_data(attr), &flags, sizeof(flags));
2920 /* Generate event to notify upper layer of bridge change */
2921 if (!err)
2922 err = rtnl_bridge_notify(dev, oflags);
2923out:
2924 return err;
2925}
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927/* Process one rtnetlink message. */
2928
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002929static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002931 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07002932 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 int family;
2935 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00002936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07002940 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 type -= RTM_BASE;
2943
2944 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00002945 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return 0;
2947
Hong zhi guo573ce262013-03-27 06:47:04 +00002948 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 sz_idx = type>>2;
2950 kind = type&3;
2951
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002952 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002953 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
David S. Millerb8f3ab42011-01-18 12:40:38 -08002955 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002956 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07002957 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00002958 rtnl_calcit_func calcit;
2959 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Thomas Grafe2849862007-03-22 11:48:11 -07002961 dumpit = rtnl_get_dumpit(family, type);
2962 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002963 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00002964 calcit = rtnl_get_calcit(family, type);
2965 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05002966 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Eric Dumazet2907c352011-05-25 07:34:04 +00002968 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08002969 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002970 {
2971 struct netlink_dump_control c = {
2972 .dump = dumpit,
2973 .min_dump_alloc = min_dump_alloc,
2974 };
2975 err = netlink_dump_start(rtnl, skb, nlh, &c);
2976 }
Eric Dumazet2907c352011-05-25 07:34:04 +00002977 rtnl_lock();
2978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
Thomas Grafe2849862007-03-22 11:48:11 -07002981 doit = rtnl_get_doit(family, type);
2982 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07002983 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Thomas Graf661d2962013-03-21 07:45:29 +00002985 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002988static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002990 rtnl_lock();
2991 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2992 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2996{
Jiri Pirko351638e2013-05-28 01:30:21 +00002997 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 case NETDEV_UP:
3001 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003002 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003003 case NETDEV_POST_INIT:
3004 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003006 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003008 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003009 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003010 case NETDEV_RELEASE:
3011 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 break;
3013 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003014 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 break;
3016 }
3017 return NOTIFY_DONE;
3018}
3019
3020static struct notifier_block rtnetlink_dev_notifier = {
3021 .notifier_call = rtnetlink_event,
3022};
3023
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003024
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003025static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003026{
3027 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003028 struct netlink_kernel_cfg cfg = {
3029 .groups = RTNLGRP_MAX,
3030 .input = rtnetlink_rcv,
3031 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003032 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003033 };
3034
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003035 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003036 if (!sk)
3037 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003038 net->rtnl = sk;
3039 return 0;
3040}
3041
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003042static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003043{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003044 netlink_kernel_release(net->rtnl);
3045 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003046}
3047
3048static struct pernet_operations rtnetlink_net_ops = {
3049 .init = rtnetlink_net_init,
3050 .exit = rtnetlink_net_exit,
3051};
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053void __init rtnetlink_init(void)
3054{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003055 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003059
Greg Rosec7ac8672011-06-10 01:27:09 +00003060 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3061 rtnl_dump_ifinfo, rtnl_calcit);
3062 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3063 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3064 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003065
Greg Rosec7ac8672011-06-10 01:27:09 +00003066 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3067 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003068
3069 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3070 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3071 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003072
3073 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003074 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003075 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077