blob: 24775953fa68e03445ba4eaaf3640808782ea744 [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>
Jiri Pirkof6f64242014-11-28 14:34:15 +010039#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000040#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000041#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/inet.h>
46#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010047#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/ip.h>
49#include <net/protocol.h>
50#include <net/arp.h>
51#include <net/route.h>
52#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010053#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070056#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070057#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000058#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Eric Dumazete0d087a2009-11-07 01:26:17 -080060struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070061 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000063 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070064};
65
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68void rtnl_lock(void)
69{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Eric Dumazete0d087a2009-11-07 01:26:17 -080072EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void rtnl_unlock(void)
80{
Herbert Xu58ec3b42008-10-07 15:50:03 -070081 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 netdev_run_todo();
83}
Eric Dumazete0d087a2009-11-07 01:26:17 -080084EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086int rtnl_trylock(void)
87{
88 return mutex_trylock(&rtnl_mutex);
89}
Eric Dumazete0d087a2009-11-07 01:26:17 -080090EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080091
Patrick McHardyc9c10142008-04-23 22:10:48 -070092int rtnl_is_locked(void)
93{
94 return mutex_is_locked(&rtnl_mutex);
95}
Eric Dumazete0d087a2009-11-07 01:26:17 -080096EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070097
Paul E. McKenneya898def2010-02-22 17:04:49 -080098#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +080099bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800100{
101 return lockdep_is_held(&rtnl_mutex);
102}
103EXPORT_SYMBOL(lockdep_rtnl_is_held);
104#endif /* #ifdef CONFIG_PROVE_LOCKING */
105
Patrick McHardy25239ce2010-04-26 16:02:05 +0200106static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700107
108static inline int rtm_msgindex(int msgtype)
109{
110 int msgindex = msgtype - RTM_BASE;
111
112 /*
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
116 */
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118
119 return msgindex;
120}
121
122static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
123{
124 struct rtnl_link *tab;
125
Patrick McHardy25239ce2010-04-26 16:02:05 +0200126 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
130
Thomas Graf51057f22007-03-22 21:41:06 -0700131 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700132 tab = rtnl_msg_handlers[PF_UNSPEC];
133
Hans Zhangc80bbea2012-10-22 22:21:23 +0000134 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700135}
136
137static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
138{
139 struct rtnl_link *tab;
140
Patrick McHardy25239ce2010-04-26 16:02:05 +0200141 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
145
Thomas Graf51057f22007-03-22 21:41:06 -0700146 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700147 tab = rtnl_msg_handlers[PF_UNSPEC];
148
Hans Zhangc80bbea2012-10-22 22:21:23 +0000149 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700150}
151
Greg Rosec7ac8672011-06-10 01:27:09 +0000152static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
153{
154 struct rtnl_link *tab;
155
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
160
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
163
Hans Zhangc80bbea2012-10-22 22:21:23 +0000164 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000165}
166
Thomas Grafe2849862007-03-22 11:48:11 -0700167/**
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000173 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700174 *
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
178 *
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
182 *
183 * Returns 0 on success or a negative error code.
184 */
185int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700188{
189 struct rtnl_link *tab;
190 int msgindex;
191
Patrick McHardy25239ce2010-04-26 16:02:05 +0200192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700193 msgindex = rtm_msgindex(msgtype);
194
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
200
201 rtnl_msg_handlers[protocol] = tab;
202 }
203
204 if (doit)
205 tab[msgindex].doit = doit;
206
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
209
Greg Rosec7ac8672011-06-10 01:27:09 +0000210 if (calcit)
211 tab[msgindex].calcit = calcit;
212
Thomas Grafe2849862007-03-22 11:48:11 -0700213 return 0;
214}
Thomas Grafe2849862007-03-22 11:48:11 -0700215EXPORT_SYMBOL_GPL(__rtnl_register);
216
217/**
218 * rtnl_register - Register a rtnetlink message type
219 *
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700225 */
226void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700229{
Greg Rosec7ac8672011-06-10 01:27:09 +0000230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
234}
Thomas Grafe2849862007-03-22 11:48:11 -0700235EXPORT_SYMBOL_GPL(rtnl_register);
236
237/**
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
241 *
242 * Returns 0 on success or a negative error code.
243 */
244int rtnl_unregister(int protocol, int msgtype)
245{
246 int msgindex;
247
Patrick McHardy25239ce2010-04-26 16:02:05 +0200248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700249 msgindex = rtm_msgindex(msgtype);
250
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
253
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256
257 return 0;
258}
Thomas Grafe2849862007-03-22 11:48:11 -0700259EXPORT_SYMBOL_GPL(rtnl_unregister);
260
261/**
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
264 *
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
267 */
268void rtnl_unregister_all(int protocol)
269{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700271
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
274}
Thomas Grafe2849862007-03-22 11:48:11 -0700275EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick McHardy38f7b872007-06-13 12:03:51 -0700277static LIST_HEAD(link_ops);
278
Eric Dumazetc63044f2011-12-13 11:38:00 +0000279static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
280{
281 const struct rtnl_link_ops *ops;
282
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
286 }
287 return NULL;
288}
289
Patrick McHardy38f7b872007-06-13 12:03:51 -0700290/**
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
293 *
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
297 *
298 * Returns 0 on success or a negative error code.
299 */
300int __rtnl_link_register(struct rtnl_link_ops *ops)
301{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
304
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
309 */
310 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000311 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700312
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313 list_add_tail(&ops->list, &link_ops);
314 return 0;
315}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700316EXPORT_SYMBOL_GPL(__rtnl_link_register);
317
318/**
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
321 *
322 * Returns 0 on success or a negative error code.
323 */
324int rtnl_link_register(struct rtnl_link_ops *ops)
325{
326 int err;
327
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
332}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700333EXPORT_SYMBOL_GPL(rtnl_link_register);
334
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
336{
337 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 LIST_HEAD(list_kill);
339
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700343 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000344 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700345}
346
Patrick McHardy38f7b872007-06-13 12:03:51 -0700347/**
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
350 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700351 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352 */
353void __rtnl_link_unregister(struct rtnl_link_ops *ops)
354{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700355 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356
Eric W. Biederman881d9662007-09-17 11:56:21 -0700357 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700358 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700359 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700360 list_del(&ops->list);
361}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700362EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
363
Cong Wang200b9162014-05-12 15:11:20 -0700364/* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
366 */
367static void rtnl_lock_unregistering_all(void)
368{
369 struct net *net;
370 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700372
Peter Zijlstraff960a72014-10-29 17:04:56 +0100373 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700374 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
381 }
382 }
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100386
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700388 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100389 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700390}
391
Patrick McHardy38f7b872007-06-13 12:03:51 -0700392/**
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
395 */
396void rtnl_link_unregister(struct rtnl_link_ops *ops)
397{
Cong Wang200b9162014-05-12 15:11:20 -0700398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700403 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405EXPORT_SYMBOL_GPL(rtnl_link_unregister);
406
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100407static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
408{
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
411
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900416 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
421}
422
Patrick McHardy38f7b872007-06-13 12:03:51 -0700423static size_t rtnl_link_get_size(const struct net_device *dev)
424{
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
427
428 if (!ops)
429 return 0;
430
Thomas Graf369cf772010-11-11 15:47:59 +0000431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700433
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000436 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700437 ops->get_size(dev);
438
439 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100443 size += rtnl_link_get_slave_info_data_size(dev);
444
Patrick McHardy38f7b872007-06-13 12:03:51 -0700445 return size;
446}
447
Thomas Graff8ff1822010-11-16 04:30:14 +0000448static LIST_HEAD(rtnl_af_ops);
449
450static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
451{
452 const struct rtnl_af_ops *ops;
453
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
457 }
458
459 return NULL;
460}
461
462/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
465 *
466 * Returns 0 on success or a negative error code.
467 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800468void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000469{
Thomas Graff8ff1822010-11-16 04:30:14 +0000470 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800471 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000473}
474EXPORT_SYMBOL_GPL(rtnl_af_register);
475
476/**
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
479 *
480 * The caller must hold the rtnl_mutex.
481 */
482void __rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 list_del(&ops->list);
485}
486EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
487
488/**
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
491 */
492void rtnl_af_unregister(struct rtnl_af_ops *ops)
493{
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
497}
498EXPORT_SYMBOL_GPL(rtnl_af_unregister);
499
500static size_t rtnl_link_get_af_size(const struct net_device *dev)
501{
502 struct rtnl_af_ops *af_ops;
503 size_t size;
504
505 /* IFLA_AF_SPEC */
506 size = nla_total_size(sizeof(struct nlattr));
507
508 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
509 if (af_ops->get_link_af_size) {
510 /* AF_* + nested data */
511 size += nla_total_size(sizeof(struct nlattr)) +
512 af_ops->get_link_af_size(dev);
513 }
514 }
515
516 return size;
517}
518
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100519static bool rtnl_have_link_slave_info(const struct net_device *dev)
520{
521 struct net_device *master_dev;
522
523 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100524 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100525 return true;
526 return false;
527}
528
529static int rtnl_link_slave_info_fill(struct sk_buff *skb,
530 const struct net_device *dev)
531{
532 struct net_device *master_dev;
533 const struct rtnl_link_ops *ops;
534 struct nlattr *slave_data;
535 int err;
536
537 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
538 if (!master_dev)
539 return 0;
540 ops = master_dev->rtnl_link_ops;
541 if (!ops)
542 return 0;
543 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
544 return -EMSGSIZE;
545 if (ops->fill_slave_info) {
546 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
547 if (!slave_data)
548 return -EMSGSIZE;
549 err = ops->fill_slave_info(skb, master_dev, dev);
550 if (err < 0)
551 goto err_cancel_slave_data;
552 nla_nest_end(skb, slave_data);
553 }
554 return 0;
555
556err_cancel_slave_data:
557 nla_nest_cancel(skb, slave_data);
558 return err;
559}
560
561static int rtnl_link_info_fill(struct sk_buff *skb,
562 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700563{
564 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100565 struct nlattr *data;
566 int err;
567
568 if (!ops)
569 return 0;
570 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
571 return -EMSGSIZE;
572 if (ops->fill_xstats) {
573 err = ops->fill_xstats(skb, dev);
574 if (err < 0)
575 return err;
576 }
577 if (ops->fill_info) {
578 data = nla_nest_start(skb, IFLA_INFO_DATA);
579 if (data == NULL)
580 return -EMSGSIZE;
581 err = ops->fill_info(skb, dev);
582 if (err < 0)
583 goto err_cancel_data;
584 nla_nest_end(skb, data);
585 }
586 return 0;
587
588err_cancel_data:
589 nla_nest_cancel(skb, data);
590 return err;
591}
592
593static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
594{
595 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700596 int err = -EMSGSIZE;
597
598 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
599 if (linkinfo == NULL)
600 goto out;
601
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100602 err = rtnl_link_info_fill(skb, dev);
603 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700604 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605
606 err = rtnl_link_slave_info_fill(skb, dev);
607 if (err < 0)
608 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700609
610 nla_nest_end(skb, linkinfo);
611 return 0;
612
Patrick McHardy38f7b872007-06-13 12:03:51 -0700613err_cancel_link:
614 nla_nest_cancel(skb, linkinfo);
615out:
616 return err;
617}
618
Eric Dumazet95c96172012-04-15 05:58:06 +0000619int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800621 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int err = 0;
623
Patrick McHardyac6d4392005-08-14 19:29:52 -0700624 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (echo)
626 atomic_inc(&skb->users);
627 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
628 if (echo)
629 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
630 return err;
631}
632
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800633int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700634{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800635 struct sock *rtnl = net->rtnl;
636
Thomas Graf2942e902006-08-15 00:30:25 -0700637 return nlmsg_unicast(rtnl, skb, pid);
638}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800639EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700640
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800641void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
642 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700643{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800644 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700645 int report = 0;
646
647 if (nlh)
648 report = nlmsg_report(nlh);
649
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800650 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700651}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800652EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700653
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800654void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700655{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800656 struct sock *rtnl = net->rtnl;
657
Thomas Graf97676b62006-08-15 00:31:41 -0700658 netlink_set_err(rtnl, 0, group, error);
659}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800660EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
663{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700664 struct nlattr *mx;
665 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 mx = nla_nest_start(skb, RTA_METRICS);
668 if (mx == NULL)
669 return -ENOBUFS;
670
671 for (i = 0; i < RTAX_MAX; i++) {
672 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100673 if (i == RTAX_CC_ALGO - 1) {
674 char tmp[TCP_CA_NAME_MAX], *name;
675
676 name = tcp_ca_get_name_by_key(metrics[i], tmp);
677 if (!name)
678 continue;
679 if (nla_put_string(skb, i + 1, name))
680 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200681 } else if (i == RTAX_FEATURES - 1) {
682 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
683
684 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
685 if (nla_put_u32(skb, i + 1, user_features))
686 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100687 } else {
688 if (nla_put_u32(skb, i + 1, metrics[i]))
689 goto nla_put_failure;
690 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700691 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David S. Millera57d27f2006-08-22 22:20:14 -0700695 if (!valid) {
696 nla_nest_cancel(skb, mx);
697 return 0;
698 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700699
700 return nla_nest_end(skb, mx);
701
702nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700703 nla_nest_cancel(skb, mx);
704 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800706EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Thomas Grafe3703b32006-11-27 09:27:07 -0800708int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700709 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800710{
711 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000712 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800713 .rta_used = dst->__use,
714 .rta_clntref = atomic_read(&(dst->__refcnt)),
715 .rta_error = error,
716 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800717 };
718
Li Wei82539472012-07-29 16:01:30 +0000719 if (expires) {
720 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800721
Li Wei82539472012-07-29 16:01:30 +0000722 clock = jiffies_to_clock_t(abs(expires));
723 clock = min_t(unsigned long, clock, INT_MAX);
724 ci.rta_expires = (expires > 0) ? clock : -clock;
725 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800726 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
727}
Thomas Grafe3703b32006-11-27 09:27:07 -0800728EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David S. Miller93b2d4a2008-02-17 18:35:07 -0800730static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800731{
732 unsigned char operstate = dev->operstate;
733
Eric Dumazete0d087a2009-11-07 01:26:17 -0800734 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800735 case IF_OPER_UP:
736 if ((operstate == IF_OPER_DORMANT ||
737 operstate == IF_OPER_UNKNOWN) &&
738 !netif_dormant(dev))
739 operstate = IF_OPER_UP;
740 break;
741
742 case IF_OPER_DORMANT:
743 if (operstate == IF_OPER_UP ||
744 operstate == IF_OPER_UNKNOWN)
745 operstate = IF_OPER_DORMANT;
746 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700747 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800748
749 if (dev->operstate != operstate) {
750 write_lock_bh(&dev_base_lock);
751 dev->operstate = operstate;
752 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800753 netdev_state_change(dev);
754 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800755}
756
Jiri Bencb1beb682012-07-27 02:58:22 +0000757static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
758{
759 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
760 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
761}
762
Patrick McHardy3729d502010-02-26 06:34:54 +0000763static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
764 const struct ifinfomsg *ifm)
765{
766 unsigned int flags = ifm->ifi_flags;
767
768 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
769 if (ifm->ifi_change)
770 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000771 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000772
773 return flags;
774}
775
Thomas Grafb60c5112006-08-04 23:05:34 -0700776static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000777 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Thomas Grafb60c5112006-08-04 23:05:34 -0700779 a->rx_packets = b->rx_packets;
780 a->tx_packets = b->tx_packets;
781 a->rx_bytes = b->rx_bytes;
782 a->tx_bytes = b->tx_bytes;
783 a->rx_errors = b->rx_errors;
784 a->tx_errors = b->tx_errors;
785 a->rx_dropped = b->rx_dropped;
786 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Thomas Grafb60c5112006-08-04 23:05:34 -0700788 a->multicast = b->multicast;
789 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Thomas Grafb60c5112006-08-04 23:05:34 -0700791 a->rx_length_errors = b->rx_length_errors;
792 a->rx_over_errors = b->rx_over_errors;
793 a->rx_crc_errors = b->rx_crc_errors;
794 a->rx_frame_errors = b->rx_frame_errors;
795 a->rx_fifo_errors = b->rx_fifo_errors;
796 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Thomas Grafb60c5112006-08-04 23:05:34 -0700798 a->tx_aborted_errors = b->tx_aborted_errors;
799 a->tx_carrier_errors = b->tx_carrier_errors;
800 a->tx_fifo_errors = b->tx_fifo_errors;
801 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
802 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Thomas Grafb60c5112006-08-04 23:05:34 -0700804 a->rx_compressed = b->rx_compressed;
805 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000806}
807
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000808static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000809{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000810 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chris Wrightc02db8c2010-05-16 01:05:45 -0700813/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500814static inline int rtnl_vfinfo_size(const struct net_device *dev,
815 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000816{
Greg Rose115c9b82012-02-21 16:54:48 -0500817 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
818 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700819 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700820 size_t size = nla_total_size(sizeof(struct nlattr));
821 size += nla_total_size(num_vfs * sizeof(struct nlattr));
822 size += num_vfs *
823 (nla_total_size(sizeof(struct ifla_vf_mac)) +
824 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400825 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200826 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300827 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300828 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
829 /* IFLA_VF_STATS_RX_PACKETS */
830 nla_total_size(sizeof(__u64)) +
831 /* IFLA_VF_STATS_TX_PACKETS */
832 nla_total_size(sizeof(__u64)) +
833 /* IFLA_VF_STATS_RX_BYTES */
834 nla_total_size(sizeof(__u64)) +
835 /* IFLA_VF_STATS_TX_BYTES */
836 nla_total_size(sizeof(__u64)) +
837 /* IFLA_VF_STATS_BROADCAST */
838 nla_total_size(sizeof(__u64)) +
839 /* IFLA_VF_STATS_MULTICAST */
840 nla_total_size(sizeof(__u64)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700841 return size;
842 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000843 return 0;
844}
845
David Gibsonc53864f2014-04-24 10:22:36 +1000846static size_t rtnl_port_size(const struct net_device *dev,
847 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700848{
849 size_t port_size = nla_total_size(4) /* PORT_VF */
850 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
851 + nla_total_size(sizeof(struct ifla_port_vsi))
852 /* PORT_VSI_TYPE */
853 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
854 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
855 + nla_total_size(1) /* PROT_VDP_REQUEST */
856 + nla_total_size(2); /* PORT_VDP_RESPONSE */
857 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
858 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
859 + port_size;
860 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
861 + port_size;
862
David Gibsonc53864f2014-04-24 10:22:36 +1000863 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
864 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700865 return 0;
866 if (dev_num_vf(dev->dev.parent))
867 return port_self_size + vf_ports_size +
868 vf_port_size * dev_num_vf(dev->dev.parent);
869 else
870 return port_self_size;
871}
872
Greg Rose115c9b82012-02-21 16:54:48 -0500873static noinline size_t if_nlmsg_size(const struct net_device *dev,
874 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800875{
876 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
877 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700878 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800879 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
880 + nla_total_size(sizeof(struct rtnl_link_ifmap))
881 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700882 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800883 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
884 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
885 + nla_total_size(4) /* IFLA_TXQLEN */
886 + nla_total_size(4) /* IFLA_WEIGHT */
887 + nla_total_size(4) /* IFLA_MTU */
888 + nla_total_size(4) /* IFLA_LINK */
889 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000890 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000891 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000892 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
893 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800894 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700895 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700896 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100897 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500898 + nla_total_size(ext_filter_mask
899 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
900 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000901 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000902 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200903 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100904 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700905 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
906 + nla_total_size(1); /* IFLA_PROTO_DOWN */
907
Thomas Graf339bf982006-11-10 14:10:15 -0800908}
909
Scott Feldman57b61082010-05-17 22:49:55 -0700910static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
911{
912 struct nlattr *vf_ports;
913 struct nlattr *vf_port;
914 int vf;
915 int err;
916
917 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
918 if (!vf_ports)
919 return -EMSGSIZE;
920
921 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
922 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700923 if (!vf_port)
924 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400925 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
926 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700927 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700928 if (err == -EMSGSIZE)
929 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700930 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700931 nla_nest_cancel(skb, vf_port);
932 continue;
933 }
934 nla_nest_end(skb, vf_port);
935 }
936
937 nla_nest_end(skb, vf_ports);
938
939 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700940
941nla_put_failure:
942 nla_nest_cancel(skb, vf_ports);
943 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700944}
945
946static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
947{
948 struct nlattr *port_self;
949 int err;
950
951 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
952 if (!port_self)
953 return -EMSGSIZE;
954
955 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
956 if (err) {
957 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700958 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700959 }
960
961 nla_nest_end(skb, port_self);
962
963 return 0;
964}
965
David Gibsonc53864f2014-04-24 10:22:36 +1000966static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
967 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700968{
969 int err;
970
David Gibsonc53864f2014-04-24 10:22:36 +1000971 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
972 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700973 return 0;
974
975 err = rtnl_port_self_fill(skb, dev);
976 if (err)
977 return err;
978
979 if (dev_num_vf(dev->dev.parent)) {
980 err = rtnl_vf_ports_fill(skb, dev);
981 if (err)
982 return err;
983 }
984
985 return 0;
986}
987
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200988static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
989{
990 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100991 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200992
993 err = dev_get_phys_port_id(dev, &ppid);
994 if (err) {
995 if (err == -EOPNOTSUPP)
996 return 0;
997 return err;
998 }
999
1000 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1001 return -EMSGSIZE;
1002
1003 return 0;
1004}
1005
David Aherndb24a902015-03-17 20:23:15 -06001006static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1007{
1008 char name[IFNAMSIZ];
1009 int err;
1010
1011 err = dev_get_phys_port_name(dev, name, sizeof(name));
1012 if (err) {
1013 if (err == -EOPNOTSUPP)
1014 return 0;
1015 return err;
1016 }
1017
1018 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1019 return -EMSGSIZE;
1020
1021 return 0;
1022}
1023
Jiri Pirko82f28412014-11-28 14:34:18 +01001024static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1025{
1026 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001027 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001028 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001029 .flags = SWITCHDEV_F_NO_RECURSE,
1030 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001031
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001032 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001033 if (err) {
1034 if (err == -EOPNOTSUPP)
1035 return 0;
1036 return err;
1037 }
1038
Scott Feldman42275bd2015-05-13 11:16:50 -07001039 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1040 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001041 return -EMSGSIZE;
1042
1043 return 0;
1044}
1045
Thomas Grafb60c5112006-08-04 23:05:34 -07001046static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001047 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001048 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001049{
1050 struct ifinfomsg *ifm;
1051 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001052 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001053 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001054 struct nlattr *attr, *af_spec;
1055 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001056 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001057
Eric Dumazet2907c352011-05-25 07:34:04 +00001058 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001059 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1060 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001061 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001062
1063 ifm = nlmsg_data(nlh);
1064 ifm->ifi_family = AF_UNSPEC;
1065 ifm->__ifi_pad = 0;
1066 ifm->ifi_type = dev->type;
1067 ifm->ifi_index = dev->ifindex;
1068 ifm->ifi_flags = dev_get_flags(dev);
1069 ifm->ifi_change = change;
1070
David S. Millera6574342012-04-01 20:12:00 -04001071 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1072 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1073 nla_put_u8(skb, IFLA_OPERSTATE,
1074 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1075 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1076 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1077 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001078 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001079 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001080#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001081 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001082#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001083 (dev->ifindex != dev_get_iflink(dev) &&
1084 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001085 (upper_dev &&
1086 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001087 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001088 (dev->qdisc &&
1089 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1090 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001091 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1092 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001093 atomic_read(&dev->carrier_changes)) ||
1094 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001095 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001096
Stefan Rompfb00055a2006-03-20 17:09:11 -08001097 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct rtnl_link_ifmap map = {
1099 .mem_start = dev->mem_start,
1100 .mem_end = dev->mem_end,
1101 .base_addr = dev->base_addr,
1102 .irq = dev->irq,
1103 .dma = dev->dma,
1104 .port = dev->if_port,
1105 };
David S. Millera6574342012-04-01 20:12:00 -04001106 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1107 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001111 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1112 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1113 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001116 if (rtnl_phys_port_id_fill(skb, dev))
1117 goto nla_put_failure;
1118
David Aherndb24a902015-03-17 20:23:15 -06001119 if (rtnl_phys_port_name_fill(skb, dev))
1120 goto nla_put_failure;
1121
Jiri Pirko82f28412014-11-28 14:34:18 +01001122 if (rtnl_phys_switch_id_fill(skb, dev))
1123 goto nla_put_failure;
1124
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001125 attr = nla_reserve(skb, IFLA_STATS,
1126 sizeof(struct rtnl_link_stats));
1127 if (attr == NULL)
1128 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Eric Dumazet28172732010-07-07 14:58:56 -07001130 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001131 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jan Engelhardt10708f32010-03-11 09:57:29 +00001133 attr = nla_reserve(skb, IFLA_STATS64,
1134 sizeof(struct rtnl_link_stats64));
1135 if (attr == NULL)
1136 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001137 copy_rtnl_link_stats64(nla_data(attr), stats);
1138
David S. Millera6574342012-04-01 20:12:00 -04001139 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1140 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1141 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001142
Greg Rose115c9b82012-02-21 16:54:48 -05001143 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1144 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001145 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001146
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001147 struct nlattr *vfinfo, *vf, *vfstats;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001148 int num_vfs = dev_num_vf(dev->dev.parent);
1149
Chris Wrightc02db8c2010-05-16 01:05:45 -07001150 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1151 if (!vfinfo)
1152 goto nla_put_failure;
1153 for (i = 0; i < num_vfs; i++) {
1154 struct ifla_vf_info ivi;
1155 struct ifla_vf_mac vf_mac;
1156 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001157 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001158 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001159 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001160 struct ifla_vf_link_state vf_linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001161 struct ifla_vf_rss_query_en vf_rss_query_en;
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001162 struct ifla_vf_stats vf_stats;
Greg Rose5f8444a2011-10-08 03:05:24 +00001163
1164 /*
1165 * Not all SR-IOV capable drivers support the
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001166 * spoofcheck and "RSS query enable" query. Preset to
1167 * -1 so the user space tool can detect that the driver
1168 * didn't report anything.
Greg Rose5f8444a2011-10-08 03:05:24 +00001169 */
1170 ivi.spoofchk = -1;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001171 ivi.rss_query_en = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001172 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001173 /* The default value for VF link state is "auto"
1174 * IFLA_VF_LINK_STATE_AUTO which equals zero
1175 */
1176 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001177 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1178 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001179 vf_mac.vf =
1180 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001181 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001182 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001183 vf_spoofchk.vf =
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001184 vf_linkstate.vf =
1185 vf_rss_query_en.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001186
Chris Wrightc02db8c2010-05-16 01:05:45 -07001187 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1188 vf_vlan.vlan = ivi.vlan;
1189 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001190 vf_tx_rate.rate = ivi.max_tx_rate;
1191 vf_rate.min_tx_rate = ivi.min_tx_rate;
1192 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001193 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001194 vf_linkstate.link_state = ivi.linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001195 vf_rss_query_en.setting = ivi.rss_query_en;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001196 vf = nla_nest_start(skb, IFLA_VF_INFO);
1197 if (!vf) {
1198 nla_nest_cancel(skb, vfinfo);
1199 goto nla_put_failure;
1200 }
David S. Millera6574342012-04-01 20:12:00 -04001201 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1202 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001203 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1204 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001205 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1206 &vf_tx_rate) ||
1207 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001208 &vf_spoofchk) ||
1209 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001210 &vf_linkstate) ||
1211 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1212 sizeof(vf_rss_query_en),
1213 &vf_rss_query_en))
David S. Millera6574342012-04-01 20:12:00 -04001214 goto nla_put_failure;
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001215 memset(&vf_stats, 0, sizeof(vf_stats));
1216 if (dev->netdev_ops->ndo_get_vf_stats)
1217 dev->netdev_ops->ndo_get_vf_stats(dev, i,
1218 &vf_stats);
1219 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1220 if (!vfstats) {
1221 nla_nest_cancel(skb, vf);
1222 nla_nest_cancel(skb, vfinfo);
1223 goto nla_put_failure;
1224 }
1225 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1226 vf_stats.rx_packets) ||
1227 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1228 vf_stats.tx_packets) ||
1229 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1230 vf_stats.rx_bytes) ||
1231 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1232 vf_stats.tx_bytes) ||
1233 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1234 vf_stats.broadcast) ||
1235 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1236 vf_stats.multicast))
1237 goto nla_put_failure;
1238 nla_nest_end(skb, vfstats);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001239 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001240 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001241 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001242 }
Scott Feldman57b61082010-05-17 22:49:55 -07001243
David Gibsonc53864f2014-04-24 10:22:36 +10001244 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001245 goto nla_put_failure;
1246
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001247 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001248 if (rtnl_link_fill(skb, dev) < 0)
1249 goto nla_put_failure;
1250 }
1251
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001252 if (dev->rtnl_link_ops &&
1253 dev->rtnl_link_ops->get_link_net) {
1254 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1255
1256 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001257 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001258
1259 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1260 goto nla_put_failure;
1261 }
1262 }
1263
Thomas Graff8ff1822010-11-16 04:30:14 +00001264 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1265 goto nla_put_failure;
1266
1267 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1268 if (af_ops->fill_link_af) {
1269 struct nlattr *af;
1270 int err;
1271
1272 if (!(af = nla_nest_start(skb, af_ops->family)))
1273 goto nla_put_failure;
1274
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001275 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001276
1277 /*
1278 * Caller may return ENODATA to indicate that there
1279 * was no data to be dumped. This is not an error, it
1280 * means we should trim the attribute header and
1281 * continue.
1282 */
1283 if (err == -ENODATA)
1284 nla_nest_cancel(skb, af);
1285 else if (err < 0)
1286 goto nla_put_failure;
1287
1288 nla_nest_end(skb, af);
1289 }
1290 }
1291
1292 nla_nest_end(skb, af_spec);
1293
Johannes Berg053c0952015-01-16 22:09:00 +01001294 nlmsg_end(skb, nlh);
1295 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001296
1297nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001298 nlmsg_cancel(skb, nlh);
1299 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Jiri Pirkof7b12602014-02-18 20:53:18 +01001302static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1303 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1304 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1305 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1306 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1307 [IFLA_MTU] = { .type = NLA_U32 },
1308 [IFLA_LINK] = { .type = NLA_U32 },
1309 [IFLA_MASTER] = { .type = NLA_U32 },
1310 [IFLA_CARRIER] = { .type = NLA_U8 },
1311 [IFLA_TXQLEN] = { .type = NLA_U32 },
1312 [IFLA_WEIGHT] = { .type = NLA_U32 },
1313 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1314 [IFLA_LINKMODE] = { .type = NLA_U8 },
1315 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1316 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1317 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1318 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1319 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1320 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1321 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1322 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1323 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1324 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1325 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1326 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001327 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001328 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001329 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001330 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001331 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001332};
1333
1334static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1335 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1336 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1337 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1338 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1339};
1340
Jiri Pirkof7b12602014-02-18 20:53:18 +01001341static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001342 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1343 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1344 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1345 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1346 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1347 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001348 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001349 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1350};
1351
1352static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = {
1353 [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 },
1354 [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 },
1355 [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 },
1356 [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 },
1357 [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 },
1358 [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001359};
1360
1361static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1362 [IFLA_PORT_VF] = { .type = NLA_U32 },
1363 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1364 .len = PORT_PROFILE_MAX },
1365 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1366 .len = sizeof(struct ifla_port_vsi)},
1367 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1368 .len = PORT_UUID_MAX },
1369 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1370 .len = PORT_UUID_MAX },
1371 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1372 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1373};
1374
Thomas Grafb60c5112006-08-04 23:05:34 -07001375static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001377 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001378 int h, s_h;
1379 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001381 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001382 struct nlattr *tb[IFLA_MAX+1];
1383 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001384 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001385 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001387 s_h = cb->args[0];
1388 s_idx = cb->args[1];
1389
Thomas Graf4e985ad2011-06-21 03:11:20 +00001390 cb->seq = net->dev_base_seq;
1391
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001392 /* A hack to preserve kernel<->userspace interface.
1393 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1394 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1395 * what iproute2 < v3.9.0 used.
1396 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1397 * attribute, its netlink message is shorter than struct ifinfomsg.
1398 */
1399 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1400 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1401
1402 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001403
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001404 if (tb[IFLA_EXT_MASK])
1405 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1406 }
Greg Rose115c9b82012-02-21 16:54:48 -05001407
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001408 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1409 idx = 0;
1410 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001411 hlist_for_each_entry(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001412 if (idx < s_idx)
1413 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001414 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1415 NETLINK_CB(cb->skb).portid,
1416 cb->nlh->nlmsg_seq, 0,
1417 NLM_F_MULTI,
1418 ext_filter_mask);
1419 /* If we ran out of room on the first message,
1420 * we're in trouble
1421 */
1422 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1423
David S. Miller7b46a642015-01-18 23:36:08 -05001424 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001425 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001426
1427 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001428cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001429 idx++;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001432out:
1433 cb->args[1] = idx;
1434 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 return skb->len;
1437}
1438
Jiri Pirkof7b12602014-02-18 20:53:18 +01001439int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1440{
1441 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1442}
1443EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001444
Eric W. Biederman81adee42009-11-08 00:53:51 -08001445struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1446{
1447 struct net *net;
1448 /* Examine the link attributes and figure out which
1449 * network namespace we are talking about.
1450 */
1451 if (tb[IFLA_NET_NS_PID])
1452 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001453 else if (tb[IFLA_NET_NS_FD])
1454 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001455 else
1456 net = get_net(src_net);
1457 return net;
1458}
1459EXPORT_SYMBOL(rtnl_link_get_net);
1460
Thomas Graf1840bb12008-02-23 19:54:36 -08001461static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1462{
1463 if (dev) {
1464 if (tb[IFLA_ADDRESS] &&
1465 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1466 return -EINVAL;
1467
1468 if (tb[IFLA_BROADCAST] &&
1469 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1470 return -EINVAL;
1471 }
1472
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001473 if (tb[IFLA_AF_SPEC]) {
1474 struct nlattr *af;
1475 int rem, err;
1476
1477 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1478 const struct rtnl_af_ops *af_ops;
1479
1480 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1481 return -EAFNOSUPPORT;
1482
1483 if (!af_ops->set_link_af)
1484 return -EOPNOTSUPP;
1485
1486 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001487 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001488 if (err < 0)
1489 return err;
1490 }
1491 }
1492 }
1493
Thomas Graf1840bb12008-02-23 19:54:36 -08001494 return 0;
1495}
1496
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001497static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001498{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001499 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001500 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001501
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001502 if (tb[IFLA_VF_MAC]) {
1503 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001504
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001505 err = -EOPNOTSUPP;
1506 if (ops->ndo_set_vf_mac)
1507 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1508 ivm->mac);
1509 if (err < 0)
1510 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001511 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001512
1513 if (tb[IFLA_VF_VLAN]) {
1514 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1515
1516 err = -EOPNOTSUPP;
1517 if (ops->ndo_set_vf_vlan)
1518 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1519 ivv->qos);
1520 if (err < 0)
1521 return err;
1522 }
1523
1524 if (tb[IFLA_VF_TX_RATE]) {
1525 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1526 struct ifla_vf_info ivf;
1527
1528 err = -EOPNOTSUPP;
1529 if (ops->ndo_get_vf_config)
1530 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1531 if (err < 0)
1532 return err;
1533
1534 err = -EOPNOTSUPP;
1535 if (ops->ndo_set_vf_rate)
1536 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1537 ivf.min_tx_rate,
1538 ivt->rate);
1539 if (err < 0)
1540 return err;
1541 }
1542
1543 if (tb[IFLA_VF_RATE]) {
1544 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1545
1546 err = -EOPNOTSUPP;
1547 if (ops->ndo_set_vf_rate)
1548 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1549 ivt->min_tx_rate,
1550 ivt->max_tx_rate);
1551 if (err < 0)
1552 return err;
1553 }
1554
1555 if (tb[IFLA_VF_SPOOFCHK]) {
1556 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1557
1558 err = -EOPNOTSUPP;
1559 if (ops->ndo_set_vf_spoofchk)
1560 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1561 ivs->setting);
1562 if (err < 0)
1563 return err;
1564 }
1565
1566 if (tb[IFLA_VF_LINK_STATE]) {
1567 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1568
1569 err = -EOPNOTSUPP;
1570 if (ops->ndo_set_vf_link_state)
1571 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1572 ivl->link_state);
1573 if (err < 0)
1574 return err;
1575 }
1576
1577 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1578 struct ifla_vf_rss_query_en *ivrssq_en;
1579
1580 err = -EOPNOTSUPP;
1581 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1582 if (ops->ndo_set_vf_rss_query_en)
1583 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1584 ivrssq_en->setting);
1585 if (err < 0)
1586 return err;
1587 }
1588
Chris Wrightc02db8c2010-05-16 01:05:45 -07001589 return err;
1590}
1591
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001592static int do_set_master(struct net_device *dev, int ifindex)
1593{
Jiri Pirko898e5062013-01-03 22:48:52 +00001594 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001595 const struct net_device_ops *ops;
1596 int err;
1597
Jiri Pirko898e5062013-01-03 22:48:52 +00001598 if (upper_dev) {
1599 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001600 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001601 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001602 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001603 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001604 if (err)
1605 return err;
1606 } else {
1607 return -EOPNOTSUPP;
1608 }
1609 }
1610
1611 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001612 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1613 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001614 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001615 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001616 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001617 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001618 if (err)
1619 return err;
1620 } else {
1621 return -EOPNOTSUPP;
1622 }
1623 }
1624 return 0;
1625}
1626
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001627#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001628/* notify flag means notify + modified. */
1629#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001630static int do_setlink(const struct sk_buff *skb,
1631 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001632 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001633{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001634 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001635 int err;
1636
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001637 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001638 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001639 if (IS_ERR(net)) {
1640 err = PTR_ERR(net);
1641 goto errout;
1642 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001643 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001644 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001645 err = -EPERM;
1646 goto errout;
1647 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001648 err = dev_change_net_namespace(dev, net, ifname);
1649 put_net(net);
1650 if (err)
1651 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001652 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001653 }
1654
Patrick McHardy0157f602007-06-13 12:03:36 -07001655 if (tb[IFLA_MAP]) {
1656 struct rtnl_link_ifmap *u_map;
1657 struct ifmap k_map;
1658
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001659 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001660 err = -EOPNOTSUPP;
1661 goto errout;
1662 }
1663
1664 if (!netif_device_present(dev)) {
1665 err = -ENODEV;
1666 goto errout;
1667 }
1668
1669 u_map = nla_data(tb[IFLA_MAP]);
1670 k_map.mem_start = (unsigned long) u_map->mem_start;
1671 k_map.mem_end = (unsigned long) u_map->mem_end;
1672 k_map.base_addr = (unsigned short) u_map->base_addr;
1673 k_map.irq = (unsigned char) u_map->irq;
1674 k_map.dma = (unsigned char) u_map->dma;
1675 k_map.port = (unsigned char) u_map->port;
1676
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001677 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001678 if (err < 0)
1679 goto errout;
1680
Nicolas Dichtelba998902014-09-01 16:07:29 +02001681 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001682 }
1683
1684 if (tb[IFLA_ADDRESS]) {
1685 struct sockaddr *sa;
1686 int len;
1687
Patrick McHardy0157f602007-06-13 12:03:36 -07001688 len = sizeof(sa_family_t) + dev->addr_len;
1689 sa = kmalloc(len, GFP_KERNEL);
1690 if (!sa) {
1691 err = -ENOMEM;
1692 goto errout;
1693 }
1694 sa->sa_family = dev->type;
1695 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1696 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001697 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001698 kfree(sa);
1699 if (err)
1700 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001701 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001702 }
1703
1704 if (tb[IFLA_MTU]) {
1705 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1706 if (err < 0)
1707 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001708 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001709 }
1710
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001711 if (tb[IFLA_GROUP]) {
1712 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001713 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001714 }
1715
Patrick McHardy0157f602007-06-13 12:03:36 -07001716 /*
1717 * Interface selected by interface index but interface
1718 * name provided implies that a name change has been
1719 * requested.
1720 */
1721 if (ifm->ifi_index > 0 && ifname[0]) {
1722 err = dev_change_name(dev, ifname);
1723 if (err < 0)
1724 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001725 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001726 }
1727
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001728 if (tb[IFLA_IFALIAS]) {
1729 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1730 nla_len(tb[IFLA_IFALIAS]));
1731 if (err < 0)
1732 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001733 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001734 }
1735
Patrick McHardy0157f602007-06-13 12:03:36 -07001736 if (tb[IFLA_BROADCAST]) {
1737 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001738 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001739 }
1740
1741 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001742 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001743 if (err < 0)
1744 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001745 }
1746
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001747 if (tb[IFLA_MASTER]) {
1748 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1749 if (err)
1750 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001751 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001752 }
1753
Jiri Pirko9a572472012-12-27 23:49:39 +00001754 if (tb[IFLA_CARRIER]) {
1755 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1756 if (err)
1757 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001758 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001759 }
1760
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001761 if (tb[IFLA_TXQLEN]) {
1762 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1763
1764 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001765 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001766
1767 dev->tx_queue_len = value;
1768 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001769
Patrick McHardy0157f602007-06-13 12:03:36 -07001770 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001771 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001772
1773 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001774 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1775
David S. Miller93b2d4a2008-02-17 18:35:07 -08001776 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001777 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001778 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001779 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001780 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001781 }
1782
Chris Wrightc02db8c2010-05-16 01:05:45 -07001783 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001784 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001785 struct nlattr *attr;
1786 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001787
Chris Wrightc02db8c2010-05-16 01:05:45 -07001788 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001789 if (nla_type(attr) != IFLA_VF_INFO ||
1790 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001791 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001792 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001793 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001794 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1795 ifla_vf_policy);
1796 if (err < 0)
1797 goto errout;
1798 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001799 if (err < 0)
1800 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001801 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001802 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001803 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001804 err = 0;
1805
Scott Feldman57b61082010-05-17 22:49:55 -07001806 if (tb[IFLA_VF_PORTS]) {
1807 struct nlattr *port[IFLA_PORT_MAX+1];
1808 struct nlattr *attr;
1809 int vf;
1810 int rem;
1811
1812 err = -EOPNOTSUPP;
1813 if (!ops->ndo_set_vf_port)
1814 goto errout;
1815
1816 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001817 if (nla_type(attr) != IFLA_VF_PORT ||
1818 nla_len(attr) < NLA_HDRLEN) {
1819 err = -EINVAL;
1820 goto errout;
1821 }
1822 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1823 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001824 if (err < 0)
1825 goto errout;
1826 if (!port[IFLA_PORT_VF]) {
1827 err = -EOPNOTSUPP;
1828 goto errout;
1829 }
1830 vf = nla_get_u32(port[IFLA_PORT_VF]);
1831 err = ops->ndo_set_vf_port(dev, vf, port);
1832 if (err < 0)
1833 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001834 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001835 }
1836 }
1837 err = 0;
1838
1839 if (tb[IFLA_PORT_SELF]) {
1840 struct nlattr *port[IFLA_PORT_MAX+1];
1841
1842 err = nla_parse_nested(port, IFLA_PORT_MAX,
1843 tb[IFLA_PORT_SELF], ifla_port_policy);
1844 if (err < 0)
1845 goto errout;
1846
1847 err = -EOPNOTSUPP;
1848 if (ops->ndo_set_vf_port)
1849 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1850 if (err < 0)
1851 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001852 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001853 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001854
1855 if (tb[IFLA_AF_SPEC]) {
1856 struct nlattr *af;
1857 int rem;
1858
1859 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1860 const struct rtnl_af_ops *af_ops;
1861
1862 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001863 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001864
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001865 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001866 if (err < 0)
1867 goto errout;
1868
Nicolas Dichtelba998902014-09-01 16:07:29 +02001869 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001870 }
1871 }
Scott Feldman57b61082010-05-17 22:49:55 -07001872 err = 0;
1873
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001874 if (tb[IFLA_PROTO_DOWN]) {
1875 err = dev_change_proto_down(dev,
1876 nla_get_u8(tb[IFLA_PROTO_DOWN]));
1877 if (err)
1878 goto errout;
1879 status |= DO_SETLINK_NOTIFY;
1880 }
1881
Patrick McHardy0157f602007-06-13 12:03:36 -07001882errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001883 if (status & DO_SETLINK_MODIFIED) {
1884 if (status & DO_SETLINK_NOTIFY)
1885 netdev_state_change(dev);
1886
1887 if (err < 0)
1888 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",
1889 dev->name);
1890 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001891
Patrick McHardy0157f602007-06-13 12:03:36 -07001892 return err;
1893}
1894
Thomas Graf661d2962013-03-21 07:45:29 +00001895static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001897 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001898 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001900 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001901 struct nlattr *tb[IFLA_MAX+1];
1902 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Thomas Grafda5e0492006-08-10 21:17:37 -07001904 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1905 if (err < 0)
1906 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Thomas Graf5176f912006-08-26 20:13:18 -07001908 if (tb[IFLA_IFNAME])
1909 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001910 else
1911 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001914 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001915 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001916 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001917 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001918 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001919 else
1920 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Thomas Grafda5e0492006-08-10 21:17:37 -07001922 if (dev == NULL) {
1923 err = -ENODEV;
1924 goto errout;
1925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Eric Dumazete0d087a2009-11-07 01:26:17 -08001927 err = validate_linkmsg(dev, tb);
1928 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001929 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001930
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001931 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001932errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return err;
1934}
1935
WANG Cong66400d52015-03-24 11:53:31 -07001936static int rtnl_group_dellink(const struct net *net, int group)
1937{
1938 struct net_device *dev, *aux;
1939 LIST_HEAD(list_kill);
1940 bool found = false;
1941
1942 if (!group)
1943 return -EPERM;
1944
1945 for_each_netdev(net, dev) {
1946 if (dev->group == group) {
1947 const struct rtnl_link_ops *ops;
1948
1949 found = true;
1950 ops = dev->rtnl_link_ops;
1951 if (!ops || !ops->dellink)
1952 return -EOPNOTSUPP;
1953 }
1954 }
1955
1956 if (!found)
1957 return -ENODEV;
1958
1959 for_each_netdev_safe(net, dev, aux) {
1960 if (dev->group == group) {
1961 const struct rtnl_link_ops *ops;
1962
1963 ops = dev->rtnl_link_ops;
1964 ops->dellink(dev, &list_kill);
1965 }
1966 }
1967 unregister_netdevice_many(&list_kill);
1968
1969 return 0;
1970}
1971
Thomas Graf614732e2015-07-21 10:44:06 +02001972int rtnl_delete_link(struct net_device *dev)
1973{
1974 const struct rtnl_link_ops *ops;
1975 LIST_HEAD(list_kill);
1976
1977 ops = dev->rtnl_link_ops;
1978 if (!ops || !ops->dellink)
1979 return -EOPNOTSUPP;
1980
1981 ops->dellink(dev, &list_kill);
1982 unregister_netdevice_many(&list_kill);
1983
1984 return 0;
1985}
1986EXPORT_SYMBOL_GPL(rtnl_delete_link);
1987
Thomas Graf661d2962013-03-21 07:45:29 +00001988static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001989{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001990 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001991 struct net_device *dev;
1992 struct ifinfomsg *ifm;
1993 char ifname[IFNAMSIZ];
1994 struct nlattr *tb[IFLA_MAX+1];
1995 int err;
1996
1997 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1998 if (err < 0)
1999 return err;
2000
2001 if (tb[IFLA_IFNAME])
2002 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2003
2004 ifm = nlmsg_data(nlh);
2005 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002006 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002007 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002008 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002009 else if (tb[IFLA_GROUP])
2010 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002011 else
2012 return -EINVAL;
2013
2014 if (!dev)
2015 return -ENODEV;
2016
Thomas Graf614732e2015-07-21 10:44:06 +02002017 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002018}
2019
Patrick McHardy3729d502010-02-26 06:34:54 +00002020int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2021{
2022 unsigned int old_flags;
2023 int err;
2024
2025 old_flags = dev->flags;
2026 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2027 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2028 if (err < 0)
2029 return err;
2030 }
2031
2032 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002033
Nicolas Dichtela528c212013-09-25 12:02:44 +02002034 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002035 return 0;
2036}
2037EXPORT_SYMBOL(rtnl_configure_link);
2038
Rami Rosenc0713562012-11-30 01:08:47 +00002039struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002040 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002041 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002042{
2043 int err;
2044 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002045 unsigned int num_tx_queues = 1;
2046 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002047
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002048 if (tb[IFLA_NUM_TX_QUEUES])
2049 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2050 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002051 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002052
2053 if (tb[IFLA_NUM_RX_QUEUES])
2054 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2055 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002056 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002057
Pavel Emelianove7199282007-08-08 22:16:38 -07002058 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002059 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002060 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002061 if (!dev)
2062 goto err;
2063
Eric W. Biederman81adee42009-11-08 00:53:51 -08002064 dev_net_set(dev, net);
2065 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002066 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002067
Pavel Emelianove7199282007-08-08 22:16:38 -07002068 if (tb[IFLA_MTU])
2069 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002070 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002071 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2072 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002073 dev->addr_assign_type = NET_ADDR_SET;
2074 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002075 if (tb[IFLA_BROADCAST])
2076 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2077 nla_len(tb[IFLA_BROADCAST]));
2078 if (tb[IFLA_TXQLEN])
2079 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2080 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002081 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002082 if (tb[IFLA_LINKMODE])
2083 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002084 if (tb[IFLA_GROUP])
2085 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002086
2087 return dev;
2088
Pavel Emelianove7199282007-08-08 22:16:38 -07002089err:
2090 return ERR_PTR(err);
2091}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002092EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002093
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002094static int rtnl_group_changelink(const struct sk_buff *skb,
2095 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002096 struct ifinfomsg *ifm,
2097 struct nlattr **tb)
2098{
WANG Congd0795352015-03-23 16:31:09 -07002099 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002100 int err;
2101
WANG Congd0795352015-03-23 16:31:09 -07002102 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002103 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002104 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002105 if (err < 0)
2106 return err;
2107 }
2108 }
2109
2110 return 0;
2111}
2112
Thomas Graf661d2962013-03-21 07:45:29 +00002113static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002114{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002115 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002116 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002117 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002118 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002119 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002120 struct ifinfomsg *ifm;
2121 char kind[MODULE_NAME_LEN];
2122 char ifname[IFNAMSIZ];
2123 struct nlattr *tb[IFLA_MAX+1];
2124 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002125 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002126 int err;
2127
Johannes Berg95a5afc2008-10-16 15:24:51 -07002128#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002129replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002130#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002131 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2132 if (err < 0)
2133 return err;
2134
2135 if (tb[IFLA_IFNAME])
2136 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2137 else
2138 ifname[0] = '\0';
2139
2140 ifm = nlmsg_data(nlh);
2141 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002142 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002143 else {
2144 if (ifname[0])
2145 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002146 else
2147 dev = NULL;
2148 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002149
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002150 if (dev) {
2151 master_dev = netdev_master_upper_dev_get(dev);
2152 if (master_dev)
2153 m_ops = master_dev->rtnl_link_ops;
2154 }
2155
Eric Dumazete0d087a2009-11-07 01:26:17 -08002156 err = validate_linkmsg(dev, tb);
2157 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002158 return err;
2159
Patrick McHardy38f7b872007-06-13 12:03:51 -07002160 if (tb[IFLA_LINKINFO]) {
2161 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2162 tb[IFLA_LINKINFO], ifla_info_policy);
2163 if (err < 0)
2164 return err;
2165 } else
2166 memset(linkinfo, 0, sizeof(linkinfo));
2167
2168 if (linkinfo[IFLA_INFO_KIND]) {
2169 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2170 ops = rtnl_link_ops_get(kind);
2171 } else {
2172 kind[0] = '\0';
2173 ops = NULL;
2174 }
2175
2176 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002177 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2178 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002179 struct nlattr **data = NULL;
2180 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002181 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002182
2183 if (ops) {
2184 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2185 err = nla_parse_nested(attr, ops->maxtype,
2186 linkinfo[IFLA_INFO_DATA],
2187 ops->policy);
2188 if (err < 0)
2189 return err;
2190 data = attr;
2191 }
2192 if (ops->validate) {
2193 err = ops->validate(tb, data);
2194 if (err < 0)
2195 return err;
2196 }
2197 }
2198
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002199 if (m_ops) {
2200 if (m_ops->slave_maxtype &&
2201 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2202 err = nla_parse_nested(slave_attr,
2203 m_ops->slave_maxtype,
2204 linkinfo[IFLA_INFO_SLAVE_DATA],
2205 m_ops->slave_policy);
2206 if (err < 0)
2207 return err;
2208 slave_data = slave_attr;
2209 }
2210 if (m_ops->slave_validate) {
2211 err = m_ops->slave_validate(tb, slave_data);
2212 if (err < 0)
2213 return err;
2214 }
2215 }
2216
Patrick McHardy38f7b872007-06-13 12:03:51 -07002217 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002218 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002219
2220 if (nlh->nlmsg_flags & NLM_F_EXCL)
2221 return -EEXIST;
2222 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2223 return -EOPNOTSUPP;
2224
2225 if (linkinfo[IFLA_INFO_DATA]) {
2226 if (!ops || ops != dev->rtnl_link_ops ||
2227 !ops->changelink)
2228 return -EOPNOTSUPP;
2229
2230 err = ops->changelink(dev, tb, data);
2231 if (err < 0)
2232 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002233 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002234 }
2235
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002236 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2237 if (!m_ops || !m_ops->slave_changelink)
2238 return -EOPNOTSUPP;
2239
2240 err = m_ops->slave_changelink(master_dev, dev,
2241 tb, slave_data);
2242 if (err < 0)
2243 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002244 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002245 }
2246
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002247 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002248 }
2249
Patrick McHardyffa934f2011-01-20 03:00:42 +00002250 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2251 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002252 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002253 nla_get_u32(tb[IFLA_GROUP]),
2254 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002255 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002256 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002257
Patrick McHardy0e068772007-07-11 19:42:31 -07002258 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002259 return -EOPNOTSUPP;
2260
2261 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002262#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002263 if (kind[0]) {
2264 __rtnl_unlock();
2265 request_module("rtnl-link-%s", kind);
2266 rtnl_lock();
2267 ops = rtnl_link_ops_get(kind);
2268 if (ops)
2269 goto replay;
2270 }
2271#endif
2272 return -EOPNOTSUPP;
2273 }
2274
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002275 if (!ops->setup)
2276 return -EOPNOTSUPP;
2277
Tom Gundersen55177502014-07-14 16:37:25 +02002278 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002279 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002280 name_assign_type = NET_NAME_ENUM;
2281 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002282
Eric W. Biederman81adee42009-11-08 00:53:51 -08002283 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002284 if (IS_ERR(dest_net))
2285 return PTR_ERR(dest_net);
2286
Eric W. Biederman505ce412015-02-26 16:19:00 -06002287 err = -EPERM;
2288 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2289 goto out;
2290
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002291 if (tb[IFLA_LINK_NETNSID]) {
2292 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2293
2294 link_net = get_net_ns_by_id(dest_net, id);
2295 if (!link_net) {
2296 err = -EINVAL;
2297 goto out;
2298 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002299 err = -EPERM;
2300 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2301 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002302 }
2303
2304 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2305 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002306 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002307 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002308 goto out;
2309 }
2310
2311 dev->ifindex = ifm->ifi_index;
2312
Cong Wang0e0eee22014-02-11 15:51:30 -08002313 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002314 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002315 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002316 * and unregister it on failure after registration
2317 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002318 */
Cong Wange51fb152014-06-03 16:40:47 -07002319 if (err < 0) {
2320 /* If device is not registered at all, free it now */
2321 if (dev->reg_state == NETREG_UNINITIALIZED)
2322 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002323 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002324 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002325 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002326 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002327 if (err < 0) {
2328 free_netdev(dev);
2329 goto out;
2330 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002331 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002332 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002333 if (err < 0)
2334 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002335 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002336 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002337 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002338 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002339 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002340out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002341 if (link_net)
2342 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002343 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002344 return err;
David S. Miller43638902015-03-10 21:58:32 -04002345out_unregister:
2346 if (ops->newlink) {
2347 LIST_HEAD(list_kill);
2348
2349 ops->dellink(dev, &list_kill);
2350 unregister_netdevice_many(&list_kill);
2351 } else {
2352 unregister_netdevice(dev);
2353 }
2354 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002355 }
2356}
2357
Thomas Graf661d2962013-03-21 07:45:29 +00002358static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002359{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002360 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002361 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002362 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002363 struct nlattr *tb[IFLA_MAX+1];
2364 struct net_device *dev = NULL;
2365 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002366 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002367 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002368
Thomas Grafb60c5112006-08-04 23:05:34 -07002369 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2370 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002371 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002372
Eric Dumazeta3d12892009-10-21 10:59:31 +00002373 if (tb[IFLA_IFNAME])
2374 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2375
Greg Rose115c9b82012-02-21 16:54:48 -05002376 if (tb[IFLA_EXT_MASK])
2377 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2378
Thomas Grafb60c5112006-08-04 23:05:34 -07002379 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002380 if (ifm->ifi_index > 0)
2381 dev = __dev_get_by_index(net, ifm->ifi_index);
2382 else if (tb[IFLA_IFNAME])
2383 dev = __dev_get_by_name(net, ifname);
2384 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002385 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002386
Eric Dumazeta3d12892009-10-21 10:59:31 +00002387 if (dev == NULL)
2388 return -ENODEV;
2389
Greg Rose115c9b82012-02-21 16:54:48 -05002390 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002391 if (nskb == NULL)
2392 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002393
Eric W. Biederman15e47302012-09-07 20:12:54 +00002394 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002395 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002396 if (err < 0) {
2397 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2398 WARN_ON(err == -EMSGSIZE);
2399 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002400 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002401 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002402
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002403 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002404}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002405
Greg Rose115c9b82012-02-21 16:54:48 -05002406static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002407{
Greg Rose115c9b82012-02-21 16:54:48 -05002408 struct net *net = sock_net(skb->sk);
2409 struct net_device *dev;
2410 struct nlattr *tb[IFLA_MAX+1];
2411 u32 ext_filter_mask = 0;
2412 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002413 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002414
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002415 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2416 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2417 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2418
2419 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002420 if (tb[IFLA_EXT_MASK])
2421 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2422 }
Greg Rose115c9b82012-02-21 16:54:48 -05002423
2424 if (!ext_filter_mask)
2425 return NLMSG_GOODSIZE;
2426 /*
2427 * traverse the list of net devices and compute the minimum
2428 * buffer size based upon the filter mask.
2429 */
2430 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2431 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2432 if_nlmsg_size(dev,
2433 ext_filter_mask));
2434 }
2435
Greg Rosec7ac8672011-06-10 01:27:09 +00002436 return min_ifinfo_dump_size;
2437}
2438
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002439static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 int idx;
2442 int s_idx = cb->family;
2443
2444 if (s_idx == 0)
2445 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002446 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 int type = cb->nlh->nlmsg_type-RTM_BASE;
2448 if (idx < s_idx || idx == PF_PACKET)
2449 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002450 if (rtnl_msg_handlers[idx] == NULL ||
2451 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002453 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002455 cb->prev_seq = 0;
2456 cb->seq = 0;
2457 }
Thomas Grafe2849862007-03-22 11:48:11 -07002458 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 break;
2460 }
2461 cb->family = idx;
2462
2463 return skb->len;
2464}
2465
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002466struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2467 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002469 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002471 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002472 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002474 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002475 if (skb == NULL)
2476 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Greg Rose115c9b82012-02-21 16:54:48 -05002478 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002479 if (err < 0) {
2480 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2481 WARN_ON(err == -EMSGSIZE);
2482 kfree_skb(skb);
2483 goto errout;
2484 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002485 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002486errout:
2487 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002488 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002489 return NULL;
2490}
2491
2492void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2493{
2494 struct net *net = dev_net(dev);
2495
2496 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2497}
2498
2499void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2500 gfp_t flags)
2501{
2502 struct sk_buff *skb;
2503
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002504 if (dev->reg_state != NETREG_REGISTERED)
2505 return;
2506
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002507 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2508 if (skb)
2509 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002511EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
John Fastabendd83b0602012-04-15 06:44:08 +00002513static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2514 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002515 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002516 int type, unsigned int flags,
2517 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002518{
2519 struct nlmsghdr *nlh;
2520 struct ndmsg *ndm;
2521
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002522 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002523 if (!nlh)
2524 return -EMSGSIZE;
2525
2526 ndm = nlmsg_data(nlh);
2527 ndm->ndm_family = AF_BRIDGE;
2528 ndm->ndm_pad1 = 0;
2529 ndm->ndm_pad2 = 0;
2530 ndm->ndm_flags = flags;
2531 ndm->ndm_type = 0;
2532 ndm->ndm_ifindex = dev->ifindex;
2533 ndm->ndm_state = NUD_PERMANENT;
2534
2535 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2536 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002537 if (vid)
2538 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2539 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002540
Johannes Berg053c0952015-01-16 22:09:00 +01002541 nlmsg_end(skb, nlh);
2542 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002543
2544nla_put_failure:
2545 nlmsg_cancel(skb, nlh);
2546 return -EMSGSIZE;
2547}
2548
John Fastabend3ff661c2012-04-15 06:44:14 +00002549static inline size_t rtnl_fdb_nlmsg_size(void)
2550{
2551 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2552}
2553
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002554static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
John Fastabend3ff661c2012-04-15 06:44:14 +00002555{
2556 struct net *net = dev_net(dev);
2557 struct sk_buff *skb;
2558 int err = -ENOBUFS;
2559
2560 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2561 if (!skb)
2562 goto errout;
2563
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002564 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2565 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002566 if (err < 0) {
2567 kfree_skb(skb);
2568 goto errout;
2569 }
2570
2571 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2572 return;
2573errout:
2574 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2575}
2576
Vlad Yasevich090096b2013-03-06 15:39:42 +00002577/**
2578 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2579 */
2580int ndo_dflt_fdb_add(struct ndmsg *ndm,
2581 struct nlattr *tb[],
2582 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002583 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002584 u16 flags)
2585{
2586 int err = -EINVAL;
2587
2588 /* If aging addresses are supported device will need to
2589 * implement its own handler for this.
2590 */
2591 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2592 pr_info("%s: FDB only supports static addresses\n", dev->name);
2593 return err;
2594 }
2595
Or Gerlitz65891fe2014-12-14 18:19:05 +02002596 if (vid) {
2597 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2598 return err;
2599 }
2600
Vlad Yasevich090096b2013-03-06 15:39:42 +00002601 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2602 err = dev_uc_add_excl(dev, addr);
2603 else if (is_multicast_ether_addr(addr))
2604 err = dev_mc_add_excl(dev, addr);
2605
2606 /* Only return duplicate errors if NLM_F_EXCL is set */
2607 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2608 err = 0;
2609
2610 return err;
2611}
2612EXPORT_SYMBOL(ndo_dflt_fdb_add);
2613
Jiri Pirkof6f64242014-11-28 14:34:15 +01002614static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2615{
2616 u16 vid = 0;
2617
2618 if (vlan_attr) {
2619 if (nla_len(vlan_attr) != sizeof(u16)) {
2620 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2621 return -EINVAL;
2622 }
2623
2624 vid = nla_get_u16(vlan_attr);
2625
2626 if (!vid || vid >= VLAN_VID_MASK) {
2627 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2628 vid);
2629 return -EINVAL;
2630 }
2631 }
2632 *p_vid = vid;
2633 return 0;
2634}
2635
Thomas Graf661d2962013-03-21 07:45:29 +00002636static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002637{
2638 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002639 struct ndmsg *ndm;
2640 struct nlattr *tb[NDA_MAX+1];
2641 struct net_device *dev;
2642 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002643 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002644 int err;
2645
2646 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2647 if (err < 0)
2648 return err;
2649
2650 ndm = nlmsg_data(nlh);
2651 if (ndm->ndm_ifindex == 0) {
2652 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2653 return -EINVAL;
2654 }
2655
2656 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2657 if (dev == NULL) {
2658 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2659 return -ENODEV;
2660 }
2661
2662 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2663 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2664 return -EINVAL;
2665 }
2666
2667 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002668
Jiri Pirkof6f64242014-11-28 14:34:15 +01002669 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2670 if (err)
2671 return err;
2672
John Fastabend77162022012-04-15 06:43:56 +00002673 err = -EOPNOTSUPP;
2674
2675 /* Support fdb on master device the net/bridge default case */
2676 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2677 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002678 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2679 const struct net_device_ops *ops = br_dev->netdev_ops;
2680
Jiri Pirkof6f64242014-11-28 14:34:15 +01002681 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2682 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002683 if (err)
2684 goto out;
2685 else
2686 ndm->ndm_flags &= ~NTF_MASTER;
2687 }
2688
2689 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002690 if ((ndm->ndm_flags & NTF_SELF)) {
2691 if (dev->netdev_ops->ndo_fdb_add)
2692 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002693 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002694 nlh->nlmsg_flags);
2695 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002696 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002697 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002698
John Fastabend3ff661c2012-04-15 06:44:14 +00002699 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002700 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002701 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002702 }
John Fastabend77162022012-04-15 06:43:56 +00002703 }
2704out:
2705 return err;
2706}
2707
Vlad Yasevich090096b2013-03-06 15:39:42 +00002708/**
2709 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2710 */
2711int ndo_dflt_fdb_del(struct ndmsg *ndm,
2712 struct nlattr *tb[],
2713 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002714 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002715{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002716 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002717
2718 /* If aging addresses are supported device will need to
2719 * implement its own handler for this.
2720 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002721 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002722 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002723 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002724 }
2725
2726 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2727 err = dev_uc_del(dev, addr);
2728 else if (is_multicast_ether_addr(addr))
2729 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002730
2731 return err;
2732}
2733EXPORT_SYMBOL(ndo_dflt_fdb_del);
2734
Thomas Graf661d2962013-03-21 07:45:29 +00002735static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002736{
2737 struct net *net = sock_net(skb->sk);
2738 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002739 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002740 struct net_device *dev;
2741 int err = -EINVAL;
2742 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002743 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002744
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002745 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002746 return -EPERM;
2747
2748 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2749 if (err < 0)
2750 return err;
John Fastabend77162022012-04-15 06:43:56 +00002751
2752 ndm = nlmsg_data(nlh);
2753 if (ndm->ndm_ifindex == 0) {
2754 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2755 return -EINVAL;
2756 }
2757
2758 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2759 if (dev == NULL) {
2760 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2761 return -ENODEV;
2762 }
2763
Vlad Yasevich1690be62013-02-13 12:00:18 +00002764 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2765 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002766 return -EINVAL;
2767 }
2768
Vlad Yasevich1690be62013-02-13 12:00:18 +00002769 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002770
Jiri Pirkof6f64242014-11-28 14:34:15 +01002771 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2772 if (err)
2773 return err;
2774
John Fastabend77162022012-04-15 06:43:56 +00002775 err = -EOPNOTSUPP;
2776
2777 /* Support fdb on master device the net/bridge default case */
2778 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2779 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002780 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2781 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002782
Jiri Pirko898e5062013-01-03 22:48:52 +00002783 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002784 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002785
2786 if (err)
2787 goto out;
2788 else
2789 ndm->ndm_flags &= ~NTF_MASTER;
2790 }
2791
2792 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002793 if (ndm->ndm_flags & NTF_SELF) {
2794 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002795 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2796 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002797 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002798 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002799
John Fastabend3ff661c2012-04-15 06:44:14 +00002800 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002801 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002802 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002803 }
John Fastabend77162022012-04-15 06:43:56 +00002804 }
2805out:
2806 return err;
2807}
2808
John Fastabendd83b0602012-04-15 06:44:08 +00002809static int nlmsg_populate_fdb(struct sk_buff *skb,
2810 struct netlink_callback *cb,
2811 struct net_device *dev,
2812 int *idx,
2813 struct netdev_hw_addr_list *list)
2814{
2815 struct netdev_hw_addr *ha;
2816 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002817 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002818
Eric W. Biederman15e47302012-09-07 20:12:54 +00002819 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002820 seq = cb->nlh->nlmsg_seq;
2821
2822 list_for_each_entry(ha, &list->list, list) {
2823 if (*idx < cb->args[0])
2824 goto skip;
2825
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002826 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002827 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002828 RTM_NEWNEIGH, NTF_SELF,
2829 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002830 if (err < 0)
2831 return err;
2832skip:
2833 *idx += 1;
2834 }
2835 return 0;
2836}
2837
2838/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002839 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002840 * @nlh: netlink message header
2841 * @dev: netdevice
2842 *
2843 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002844 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002845 */
2846int ndo_dflt_fdb_dump(struct sk_buff *skb,
2847 struct netlink_callback *cb,
2848 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002849 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002850 int idx)
2851{
2852 int err;
2853
2854 netif_addr_lock_bh(dev);
2855 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2856 if (err)
2857 goto out;
2858 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2859out:
2860 netif_addr_unlock_bh(dev);
2861 return idx;
2862}
2863EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2864
John Fastabend77162022012-04-15 06:43:56 +00002865static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2866{
John Fastabend77162022012-04-15 06:43:56 +00002867 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002868 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002869 struct net_device *br_dev = NULL;
2870 const struct net_device_ops *ops = NULL;
2871 const struct net_device_ops *cops = NULL;
2872 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2873 struct net *net = sock_net(skb->sk);
2874 int brport_idx = 0;
2875 int br_idx = 0;
2876 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002877
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002878 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2879 ifla_policy) == 0) {
2880 if (tb[IFLA_MASTER])
2881 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2882 }
John Fastabend77162022012-04-15 06:43:56 +00002883
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002884 brport_idx = ifm->ifi_index;
2885
2886 if (br_idx) {
2887 br_dev = __dev_get_by_index(net, br_idx);
2888 if (!br_dev)
2889 return -ENODEV;
2890
2891 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002892 }
2893
2894 for_each_netdev(net, dev) {
2895 if (brport_idx && (dev->ifindex != brport_idx))
2896 continue;
2897
2898 if (!br_idx) { /* user did not specify a specific bridge */
2899 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2900 br_dev = netdev_master_upper_dev_get(dev);
2901 cops = br_dev->netdev_ops;
2902 }
2903
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002904 } else {
2905 if (dev != br_dev &&
2906 !(dev->priv_flags & IFF_BRIDGE_PORT))
2907 continue;
2908
2909 if (br_dev != netdev_master_upper_dev_get(dev) &&
2910 !(dev->priv_flags & IFF_EBRIDGE))
2911 continue;
2912
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002913 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002914 }
2915
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002916 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2917 if (cops && cops->ndo_fdb_dump)
2918 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2919 idx);
2920 }
2921
John Fastabend77162022012-04-15 06:43:56 +00002922 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002923 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002924 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002925 else
2926 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002927
2928 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002929 }
John Fastabend77162022012-04-15 06:43:56 +00002930
2931 cb->args[0] = idx;
2932 return skb->len;
2933}
2934
Scott Feldman2c3c0312014-11-28 14:34:25 +01002935static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2936 unsigned int attrnum, unsigned int flag)
2937{
2938 if (mask & flag)
2939 return nla_put_u8(skb, attrnum, !!(flags & flag));
2940 return 0;
2941}
2942
John Fastabend815cccb2012-10-24 08:13:09 +00002943int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002944 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002945 u32 flags, u32 mask, int nlflags,
2946 u32 filter_mask,
2947 int (*vlan_fill)(struct sk_buff *skb,
2948 struct net_device *dev,
2949 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00002950{
2951 struct nlmsghdr *nlh;
2952 struct ifinfomsg *ifm;
2953 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002954 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002955 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002956 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002957 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00002958
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002959 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00002960 if (nlh == NULL)
2961 return -EMSGSIZE;
2962
2963 ifm = nlmsg_data(nlh);
2964 ifm->ifi_family = AF_BRIDGE;
2965 ifm->__ifi_pad = 0;
2966 ifm->ifi_type = dev->type;
2967 ifm->ifi_index = dev->ifindex;
2968 ifm->ifi_flags = dev_get_flags(dev);
2969 ifm->ifi_change = 0;
2970
2971
2972 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2973 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2974 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002975 (br_dev &&
2976 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002977 (dev->addr_len &&
2978 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02002979 (dev->ifindex != dev_get_iflink(dev) &&
2980 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00002981 goto nla_put_failure;
2982
2983 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2984 if (!br_afspec)
2985 goto nla_put_failure;
2986
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002987 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002988 nla_nest_cancel(skb, br_afspec);
2989 goto nla_put_failure;
2990 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002991
2992 if (mode != BRIDGE_MODE_UNDEF) {
2993 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2994 nla_nest_cancel(skb, br_afspec);
2995 goto nla_put_failure;
2996 }
2997 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002998 if (vlan_fill) {
2999 err = vlan_fill(skb, dev, filter_mask);
3000 if (err) {
3001 nla_nest_cancel(skb, br_afspec);
3002 goto nla_put_failure;
3003 }
3004 }
John Fastabend815cccb2012-10-24 08:13:09 +00003005 nla_nest_end(skb, br_afspec);
3006
Scott Feldman2c3c0312014-11-28 14:34:25 +01003007 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3008 if (!protinfo)
3009 goto nla_put_failure;
3010
3011 if (brport_nla_put_flag(skb, flags, mask,
3012 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3013 brport_nla_put_flag(skb, flags, mask,
3014 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3015 brport_nla_put_flag(skb, flags, mask,
3016 IFLA_BRPORT_FAST_LEAVE,
3017 BR_MULTICAST_FAST_LEAVE) ||
3018 brport_nla_put_flag(skb, flags, mask,
3019 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3020 brport_nla_put_flag(skb, flags, mask,
3021 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3022 brport_nla_put_flag(skb, flags, mask,
3023 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3024 brport_nla_put_flag(skb, flags, mask,
3025 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3026 brport_nla_put_flag(skb, flags, mask,
3027 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3028 nla_nest_cancel(skb, protinfo);
3029 goto nla_put_failure;
3030 }
3031
3032 nla_nest_end(skb, protinfo);
3033
Johannes Berg053c0952015-01-16 22:09:00 +01003034 nlmsg_end(skb, nlh);
3035 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003036nla_put_failure:
3037 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003038 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003039}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003040EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003041
John Fastabende5a55a82012-10-24 08:12:57 +00003042static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3043{
3044 struct net *net = sock_net(skb->sk);
3045 struct net_device *dev;
3046 int idx = 0;
3047 u32 portid = NETLINK_CB(cb->skb).portid;
3048 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003049 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003050 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003051
Thomas Grafaa68c202014-11-26 13:42:20 +01003052 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3053 struct nlattr *extfilt;
3054
3055 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3056 IFLA_EXT_MASK);
3057 if (extfilt) {
3058 if (nla_len(extfilt) < sizeof(filter_mask))
3059 return -EINVAL;
3060
3061 filter_mask = nla_get_u32(extfilt);
3062 }
3063 }
John Fastabende5a55a82012-10-24 08:12:57 +00003064
3065 rcu_read_lock();
3066 for_each_netdev_rcu(net, dev) {
3067 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003068 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003069
Jiri Pirko898e5062013-01-03 22:48:52 +00003070 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003071 if (idx >= cb->args[0]) {
3072 err = br_dev->netdev_ops->ndo_bridge_getlink(
3073 skb, portid, seq, dev,
3074 filter_mask, NLM_F_MULTI);
3075 if (err < 0 && err != -EOPNOTSUPP)
3076 break;
3077 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003078 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003079 }
3080
3081 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003082 if (idx >= cb->args[0]) {
3083 err = ops->ndo_bridge_getlink(skb, portid,
3084 seq, dev,
3085 filter_mask,
3086 NLM_F_MULTI);
3087 if (err < 0 && err != -EOPNOTSUPP)
3088 break;
3089 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003090 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003091 }
3092 }
3093 rcu_read_unlock();
3094 cb->args[0] = idx;
3095
3096 return skb->len;
3097}
3098
John Fastabend2469ffd2012-10-24 08:13:03 +00003099static inline size_t bridge_nlmsg_size(void)
3100{
3101 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3102 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3103 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3104 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3105 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3106 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3107 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3108 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3109 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3110 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3111 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3112}
3113
Roopa Prabhu02dba432015-01-14 20:02:25 -08003114static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003115{
3116 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003117 struct sk_buff *skb;
3118 int err = -EOPNOTSUPP;
3119
Roopa Prabhu02dba432015-01-14 20:02:25 -08003120 if (!dev->netdev_ops->ndo_bridge_getlink)
3121 return 0;
3122
John Fastabend2469ffd2012-10-24 08:13:03 +00003123 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3124 if (!skb) {
3125 err = -ENOMEM;
3126 goto errout;
3127 }
3128
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003129 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003130 if (err < 0)
3131 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003132
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003133 if (!skb->len)
3134 goto errout;
3135
John Fastabend2469ffd2012-10-24 08:13:03 +00003136 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3137 return 0;
3138errout:
3139 WARN_ON(err == -EMSGSIZE);
3140 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003141 if (err)
3142 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003143 return err;
3144}
3145
Thomas Graf661d2962013-03-21 07:45:29 +00003146static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003147{
3148 struct net *net = sock_net(skb->sk);
3149 struct ifinfomsg *ifm;
3150 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003151 struct nlattr *br_spec, *attr = NULL;
3152 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003153 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003154 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003155
3156 if (nlmsg_len(nlh) < sizeof(*ifm))
3157 return -EINVAL;
3158
3159 ifm = nlmsg_data(nlh);
3160 if (ifm->ifi_family != AF_BRIDGE)
3161 return -EPFNOSUPPORT;
3162
3163 dev = __dev_get_by_index(net, ifm->ifi_index);
3164 if (!dev) {
3165 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3166 return -ENODEV;
3167 }
3168
John Fastabend2469ffd2012-10-24 08:13:03 +00003169 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3170 if (br_spec) {
3171 nla_for_each_nested(attr, br_spec, rem) {
3172 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003173 if (nla_len(attr) < sizeof(flags))
3174 return -EINVAL;
3175
John Fastabendc38e01b2012-11-02 16:32:36 +00003176 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003177 flags = nla_get_u16(attr);
3178 break;
3179 }
3180 }
3181 }
3182
3183 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003184 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3185
3186 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003187 err = -EOPNOTSUPP;
3188 goto out;
3189 }
3190
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003191 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003192 if (err)
3193 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003194
3195 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003196 }
3197
John Fastabend2469ffd2012-10-24 08:13:03 +00003198 if ((flags & BRIDGE_FLAGS_SELF)) {
3199 if (!dev->netdev_ops->ndo_bridge_setlink)
3200 err = -EOPNOTSUPP;
3201 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003202 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3203 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003204 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003205 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003206
3207 /* Generate event to notify upper layer of bridge
3208 * change
3209 */
3210 err = rtnl_bridge_notify(dev);
3211 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003212 }
3213
John Fastabendc38e01b2012-11-02 16:32:36 +00003214 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003215 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003216out:
3217 return err;
3218}
3219
Thomas Graf661d2962013-03-21 07:45:29 +00003220static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003221{
3222 struct net *net = sock_net(skb->sk);
3223 struct ifinfomsg *ifm;
3224 struct net_device *dev;
3225 struct nlattr *br_spec, *attr = NULL;
3226 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003227 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003228 bool have_flags = false;
3229
3230 if (nlmsg_len(nlh) < sizeof(*ifm))
3231 return -EINVAL;
3232
3233 ifm = nlmsg_data(nlh);
3234 if (ifm->ifi_family != AF_BRIDGE)
3235 return -EPFNOSUPPORT;
3236
3237 dev = __dev_get_by_index(net, ifm->ifi_index);
3238 if (!dev) {
3239 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3240 return -ENODEV;
3241 }
3242
3243 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3244 if (br_spec) {
3245 nla_for_each_nested(attr, br_spec, rem) {
3246 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003247 if (nla_len(attr) < sizeof(flags))
3248 return -EINVAL;
3249
Vlad Yasevich407af322013-02-13 12:00:12 +00003250 have_flags = true;
3251 flags = nla_get_u16(attr);
3252 break;
3253 }
3254 }
3255 }
3256
Vlad Yasevich407af322013-02-13 12:00:12 +00003257 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3258 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3259
3260 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3261 err = -EOPNOTSUPP;
3262 goto out;
3263 }
3264
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003265 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003266 if (err)
3267 goto out;
3268
3269 flags &= ~BRIDGE_FLAGS_MASTER;
3270 }
3271
3272 if ((flags & BRIDGE_FLAGS_SELF)) {
3273 if (!dev->netdev_ops->ndo_bridge_dellink)
3274 err = -EOPNOTSUPP;
3275 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003276 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3277 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003278
Roopa Prabhu02dba432015-01-14 20:02:25 -08003279 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003280 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003281
3282 /* Generate event to notify upper layer of bridge
3283 * change
3284 */
3285 err = rtnl_bridge_notify(dev);
3286 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003287 }
3288
3289 if (have_flags)
3290 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003291out:
3292 return err;
3293}
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295/* Process one rtnetlink message. */
3296
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003297static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003299 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003300 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int family;
3303 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003304 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003308 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 type -= RTM_BASE;
3311
3312 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003313 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return 0;
3315
Hong zhi guo573ce262013-03-27 06:47:04 +00003316 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 sz_idx = type>>2;
3318 kind = type&3;
3319
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003320 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003321 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
David S. Millerb8f3ab42011-01-18 12:40:38 -08003323 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003324 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003325 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003326 rtnl_calcit_func calcit;
3327 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Thomas Grafe2849862007-03-22 11:48:11 -07003329 dumpit = rtnl_get_dumpit(family, type);
3330 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003331 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003332 calcit = rtnl_get_calcit(family, type);
3333 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003334 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Eric Dumazet2907c352011-05-25 07:34:04 +00003336 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003337 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003338 {
3339 struct netlink_dump_control c = {
3340 .dump = dumpit,
3341 .min_dump_alloc = min_dump_alloc,
3342 };
3343 err = netlink_dump_start(rtnl, skb, nlh, &c);
3344 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003345 rtnl_lock();
3346 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
3348
Thomas Grafe2849862007-03-22 11:48:11 -07003349 doit = rtnl_get_doit(family, type);
3350 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003351 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Thomas Graf661d2962013-03-21 07:45:29 +00003353 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003356static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003358 rtnl_lock();
3359 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3360 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3364{
Jiri Pirko351638e2013-05-28 01:30:21 +00003365 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 case NETDEV_UP:
3369 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003370 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003371 case NETDEV_POST_INIT:
3372 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003374 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003376 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003377 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003378 case NETDEV_RELEASE:
3379 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003380 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 break;
3382 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003383 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 break;
3385 }
3386 return NOTIFY_DONE;
3387}
3388
3389static struct notifier_block rtnetlink_dev_notifier = {
3390 .notifier_call = rtnetlink_event,
3391};
3392
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003393
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003394static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003395{
3396 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003397 struct netlink_kernel_cfg cfg = {
3398 .groups = RTNLGRP_MAX,
3399 .input = rtnetlink_rcv,
3400 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003401 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003402 };
3403
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003404 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003405 if (!sk)
3406 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003407 net->rtnl = sk;
3408 return 0;
3409}
3410
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003411static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003412{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003413 netlink_kernel_release(net->rtnl);
3414 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003415}
3416
3417static struct pernet_operations rtnetlink_net_ops = {
3418 .init = rtnetlink_net_init,
3419 .exit = rtnetlink_net_exit,
3420};
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422void __init rtnetlink_init(void)
3423{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003424 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003428
Greg Rosec7ac8672011-06-10 01:27:09 +00003429 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3430 rtnl_dump_ifinfo, rtnl_calcit);
3431 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3432 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3433 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003434
Greg Rosec7ac8672011-06-10 01:27:09 +00003435 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3436 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003437
3438 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3439 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3440 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003441
3442 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003443 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003444 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446