blob: 5503dfe6a050136a45182c0a86914e732e392381 [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
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700500static size_t rtnl_link_get_af_size(const struct net_device *dev,
501 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000502{
503 struct rtnl_af_ops *af_ops;
504 size_t size;
505
506 /* IFLA_AF_SPEC */
507 size = nla_total_size(sizeof(struct nlattr));
508
509 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
510 if (af_ops->get_link_af_size) {
511 /* AF_* + nested data */
512 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700513 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000514 }
515 }
516
517 return size;
518}
519
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100520static bool rtnl_have_link_slave_info(const struct net_device *dev)
521{
522 struct net_device *master_dev;
523
524 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100525 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100526 return true;
527 return false;
528}
529
530static int rtnl_link_slave_info_fill(struct sk_buff *skb,
531 const struct net_device *dev)
532{
533 struct net_device *master_dev;
534 const struct rtnl_link_ops *ops;
535 struct nlattr *slave_data;
536 int err;
537
538 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
539 if (!master_dev)
540 return 0;
541 ops = master_dev->rtnl_link_ops;
542 if (!ops)
543 return 0;
544 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
545 return -EMSGSIZE;
546 if (ops->fill_slave_info) {
547 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
548 if (!slave_data)
549 return -EMSGSIZE;
550 err = ops->fill_slave_info(skb, master_dev, dev);
551 if (err < 0)
552 goto err_cancel_slave_data;
553 nla_nest_end(skb, slave_data);
554 }
555 return 0;
556
557err_cancel_slave_data:
558 nla_nest_cancel(skb, slave_data);
559 return err;
560}
561
562static int rtnl_link_info_fill(struct sk_buff *skb,
563 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700564{
565 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100566 struct nlattr *data;
567 int err;
568
569 if (!ops)
570 return 0;
571 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
572 return -EMSGSIZE;
573 if (ops->fill_xstats) {
574 err = ops->fill_xstats(skb, dev);
575 if (err < 0)
576 return err;
577 }
578 if (ops->fill_info) {
579 data = nla_nest_start(skb, IFLA_INFO_DATA);
580 if (data == NULL)
581 return -EMSGSIZE;
582 err = ops->fill_info(skb, dev);
583 if (err < 0)
584 goto err_cancel_data;
585 nla_nest_end(skb, data);
586 }
587 return 0;
588
589err_cancel_data:
590 nla_nest_cancel(skb, data);
591 return err;
592}
593
594static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
595{
596 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700597 int err = -EMSGSIZE;
598
599 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
600 if (linkinfo == NULL)
601 goto out;
602
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100603 err = rtnl_link_info_fill(skb, dev);
604 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700605 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100606
607 err = rtnl_link_slave_info_fill(skb, dev);
608 if (err < 0)
609 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700610
611 nla_nest_end(skb, linkinfo);
612 return 0;
613
Patrick McHardy38f7b872007-06-13 12:03:51 -0700614err_cancel_link:
615 nla_nest_cancel(skb, linkinfo);
616out:
617 return err;
618}
619
Eric Dumazet95c96172012-04-15 05:58:06 +0000620int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800622 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int err = 0;
624
Patrick McHardyac6d4392005-08-14 19:29:52 -0700625 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (echo)
627 atomic_inc(&skb->users);
628 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
629 if (echo)
630 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
631 return err;
632}
633
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800634int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700635{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800636 struct sock *rtnl = net->rtnl;
637
Thomas Graf2942e902006-08-15 00:30:25 -0700638 return nlmsg_unicast(rtnl, skb, pid);
639}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800640EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700641
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800642void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
643 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700644{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800645 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700646 int report = 0;
647
648 if (nlh)
649 report = nlmsg_report(nlh);
650
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800651 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700652}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800653EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700654
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800655void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700656{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657 struct sock *rtnl = net->rtnl;
658
Thomas Graf97676b62006-08-15 00:31:41 -0700659 netlink_set_err(rtnl, 0, group, error);
660}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800661EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
664{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700665 struct nlattr *mx;
666 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Thomas Graf2d7202b2006-08-22 00:01:27 -0700668 mx = nla_nest_start(skb, RTA_METRICS);
669 if (mx == NULL)
670 return -ENOBUFS;
671
672 for (i = 0; i < RTAX_MAX; i++) {
673 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100674 if (i == RTAX_CC_ALGO - 1) {
675 char tmp[TCP_CA_NAME_MAX], *name;
676
677 name = tcp_ca_get_name_by_key(metrics[i], tmp);
678 if (!name)
679 continue;
680 if (nla_put_string(skb, i + 1, name))
681 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200682 } else if (i == RTAX_FEATURES - 1) {
683 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
684
685 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 if (nla_put_u32(skb, i + 1, user_features))
687 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100688 } else {
689 if (nla_put_u32(skb, i + 1, metrics[i]))
690 goto nla_put_failure;
691 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700692 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David S. Millera57d27f2006-08-22 22:20:14 -0700696 if (!valid) {
697 nla_nest_cancel(skb, mx);
698 return 0;
699 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700700
701 return nla_nest_end(skb, mx);
702
703nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700704 nla_nest_cancel(skb, mx);
705 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800707EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Thomas Grafe3703b32006-11-27 09:27:07 -0800709int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700710 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800711{
712 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000713 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800714 .rta_used = dst->__use,
715 .rta_clntref = atomic_read(&(dst->__refcnt)),
716 .rta_error = error,
717 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 };
719
Li Wei82539472012-07-29 16:01:30 +0000720 if (expires) {
721 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800722
Li Wei82539472012-07-29 16:01:30 +0000723 clock = jiffies_to_clock_t(abs(expires));
724 clock = min_t(unsigned long, clock, INT_MAX);
725 ci.rta_expires = (expires > 0) ? clock : -clock;
726 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800727 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
728}
Thomas Grafe3703b32006-11-27 09:27:07 -0800729EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David S. Miller93b2d4a2008-02-17 18:35:07 -0800731static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800732{
733 unsigned char operstate = dev->operstate;
734
Eric Dumazete0d087a2009-11-07 01:26:17 -0800735 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736 case IF_OPER_UP:
737 if ((operstate == IF_OPER_DORMANT ||
738 operstate == IF_OPER_UNKNOWN) &&
739 !netif_dormant(dev))
740 operstate = IF_OPER_UP;
741 break;
742
743 case IF_OPER_DORMANT:
744 if (operstate == IF_OPER_UP ||
745 operstate == IF_OPER_UNKNOWN)
746 operstate = IF_OPER_DORMANT;
747 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700748 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800749
750 if (dev->operstate != operstate) {
751 write_lock_bh(&dev_base_lock);
752 dev->operstate = operstate;
753 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800754 netdev_state_change(dev);
755 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800756}
757
Jiri Bencb1beb682012-07-27 02:58:22 +0000758static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
759{
760 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
762}
763
Patrick McHardy3729d502010-02-26 06:34:54 +0000764static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 const struct ifinfomsg *ifm)
766{
767 unsigned int flags = ifm->ifi_flags;
768
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 if (ifm->ifi_change)
771 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000772 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000773
774 return flags;
775}
776
Thomas Grafb60c5112006-08-04 23:05:34 -0700777static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000778 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Thomas Grafb60c5112006-08-04 23:05:34 -0700780 a->rx_packets = b->rx_packets;
781 a->tx_packets = b->tx_packets;
782 a->rx_bytes = b->rx_bytes;
783 a->tx_bytes = b->tx_bytes;
784 a->rx_errors = b->rx_errors;
785 a->tx_errors = b->tx_errors;
786 a->rx_dropped = b->rx_dropped;
787 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Grafb60c5112006-08-04 23:05:34 -0700789 a->multicast = b->multicast;
790 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Grafb60c5112006-08-04 23:05:34 -0700792 a->rx_length_errors = b->rx_length_errors;
793 a->rx_over_errors = b->rx_over_errors;
794 a->rx_crc_errors = b->rx_crc_errors;
795 a->rx_frame_errors = b->rx_frame_errors;
796 a->rx_fifo_errors = b->rx_fifo_errors;
797 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Thomas Grafb60c5112006-08-04 23:05:34 -0700799 a->tx_aborted_errors = b->tx_aborted_errors;
800 a->tx_carrier_errors = b->tx_carrier_errors;
801 a->tx_fifo_errors = b->tx_fifo_errors;
802 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Thomas Grafb60c5112006-08-04 23:05:34 -0700805 a->rx_compressed = b->rx_compressed;
806 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500807
808 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000809}
810
Chris Wrightc02db8c2010-05-16 01:05:45 -0700811/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500812static inline int rtnl_vfinfo_size(const struct net_device *dev,
813 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000814{
Greg Rose115c9b82012-02-21 16:54:48 -0500815 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
816 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700817 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700818 size_t size = nla_total_size(sizeof(struct nlattr));
819 size += nla_total_size(num_vfs * sizeof(struct nlattr));
820 size += num_vfs *
821 (nla_total_size(sizeof(struct ifla_vf_mac)) +
822 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400823 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200824 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300825 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300826 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
827 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200828 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300829 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200830 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300831 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200832 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300833 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200834 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300835 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200836 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300837 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200838 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000839 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700840 return size;
841 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000842 return 0;
843}
844
David Gibsonc53864f2014-04-24 10:22:36 +1000845static size_t rtnl_port_size(const struct net_device *dev,
846 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700847{
848 size_t port_size = nla_total_size(4) /* PORT_VF */
849 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
850 + nla_total_size(sizeof(struct ifla_port_vsi))
851 /* PORT_VSI_TYPE */
852 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
853 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
854 + nla_total_size(1) /* PROT_VDP_REQUEST */
855 + nla_total_size(2); /* PORT_VDP_RESPONSE */
856 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
857 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
858 + port_size;
859 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
860 + port_size;
861
David Gibsonc53864f2014-04-24 10:22:36 +1000862 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
863 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700864 return 0;
865 if (dev_num_vf(dev->dev.parent))
866 return port_self_size + vf_ports_size +
867 vf_port_size * dev_num_vf(dev->dev.parent);
868 else
869 return port_self_size;
870}
871
Greg Rose115c9b82012-02-21 16:54:48 -0500872static noinline size_t if_nlmsg_size(const struct net_device *dev,
873 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800874{
875 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
876 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700877 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800878 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200879 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800880 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400881 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800882 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
883 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
884 + nla_total_size(4) /* IFLA_TXQLEN */
885 + nla_total_size(4) /* IFLA_WEIGHT */
886 + nla_total_size(4) /* IFLA_MTU */
887 + nla_total_size(4) /* IFLA_LINK */
888 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000889 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000890 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000891 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
892 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Eric Dumazetc70ce022016-03-21 09:55:10 -0700893 + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
894 + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800895 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700896 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700897 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100898 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500899 + nla_total_size(ext_filter_mask
900 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
901 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000902 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000903 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700904 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100905 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700906 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200907 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700908 + nla_total_size(1); /* IFLA_PROTO_DOWN */
909
Thomas Graf339bf982006-11-10 14:10:15 -0800910}
911
Scott Feldman57b61082010-05-17 22:49:55 -0700912static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
913{
914 struct nlattr *vf_ports;
915 struct nlattr *vf_port;
916 int vf;
917 int err;
918
919 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
920 if (!vf_ports)
921 return -EMSGSIZE;
922
923 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
924 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700925 if (!vf_port)
926 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400927 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
928 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700929 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700930 if (err == -EMSGSIZE)
931 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700932 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700933 nla_nest_cancel(skb, vf_port);
934 continue;
935 }
936 nla_nest_end(skb, vf_port);
937 }
938
939 nla_nest_end(skb, vf_ports);
940
941 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700942
943nla_put_failure:
944 nla_nest_cancel(skb, vf_ports);
945 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700946}
947
948static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
949{
950 struct nlattr *port_self;
951 int err;
952
953 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
954 if (!port_self)
955 return -EMSGSIZE;
956
957 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
958 if (err) {
959 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700960 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700961 }
962
963 nla_nest_end(skb, port_self);
964
965 return 0;
966}
967
David Gibsonc53864f2014-04-24 10:22:36 +1000968static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
969 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700970{
971 int err;
972
David Gibsonc53864f2014-04-24 10:22:36 +1000973 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
974 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700975 return 0;
976
977 err = rtnl_port_self_fill(skb, dev);
978 if (err)
979 return err;
980
981 if (dev_num_vf(dev->dev.parent)) {
982 err = rtnl_vf_ports_fill(skb, dev);
983 if (err)
984 return err;
985 }
986
987 return 0;
988}
989
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200990static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
991{
992 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100993 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200994
995 err = dev_get_phys_port_id(dev, &ppid);
996 if (err) {
997 if (err == -EOPNOTSUPP)
998 return 0;
999 return err;
1000 }
1001
1002 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1003 return -EMSGSIZE;
1004
1005 return 0;
1006}
1007
David Aherndb24a902015-03-17 20:23:15 -06001008static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1009{
1010 char name[IFNAMSIZ];
1011 int err;
1012
1013 err = dev_get_phys_port_name(dev, name, sizeof(name));
1014 if (err) {
1015 if (err == -EOPNOTSUPP)
1016 return 0;
1017 return err;
1018 }
1019
1020 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1021 return -EMSGSIZE;
1022
1023 return 0;
1024}
1025
Jiri Pirko82f28412014-11-28 14:34:18 +01001026static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1027{
1028 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001029 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001030 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001031 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001032 .flags = SWITCHDEV_F_NO_RECURSE,
1033 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001034
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001035 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001036 if (err) {
1037 if (err == -EOPNOTSUPP)
1038 return 0;
1039 return err;
1040 }
1041
Scott Feldman42275bd2015-05-13 11:16:50 -07001042 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1043 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001044 return -EMSGSIZE;
1045
1046 return 0;
1047}
1048
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001049static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1050 struct net_device *dev)
1051{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001052 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001053 struct nlattr *attr;
1054
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001055 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1056 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001057 if (!attr)
1058 return -EMSGSIZE;
1059
Roopa Prabhu550bce52016-04-15 20:36:25 -07001060 sp = nla_data(attr);
1061 dev_get_stats(dev, sp);
1062
1063 attr = nla_reserve(skb, IFLA_STATS,
1064 sizeof(struct rtnl_link_stats));
1065 if (!attr)
1066 return -EMSGSIZE;
1067
1068 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001069
1070 return 0;
1071}
1072
1073static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1074 struct net_device *dev,
1075 int vfs_num,
1076 struct nlattr *vfinfo)
1077{
1078 struct ifla_vf_rss_query_en vf_rss_query_en;
1079 struct ifla_vf_link_state vf_linkstate;
1080 struct ifla_vf_spoofchk vf_spoofchk;
1081 struct ifla_vf_tx_rate vf_tx_rate;
1082 struct ifla_vf_stats vf_stats;
1083 struct ifla_vf_trust vf_trust;
1084 struct ifla_vf_vlan vf_vlan;
1085 struct ifla_vf_rate vf_rate;
1086 struct nlattr *vf, *vfstats;
1087 struct ifla_vf_mac vf_mac;
1088 struct ifla_vf_info ivi;
1089
1090 /* Not all SR-IOV capable drivers support the
1091 * spoofcheck and "RSS query enable" query. Preset to
1092 * -1 so the user space tool can detect that the driver
1093 * didn't report anything.
1094 */
1095 ivi.spoofchk = -1;
1096 ivi.rss_query_en = -1;
1097 ivi.trusted = -1;
1098 memset(ivi.mac, 0, sizeof(ivi.mac));
1099 /* The default value for VF link state is "auto"
1100 * IFLA_VF_LINK_STATE_AUTO which equals zero
1101 */
1102 ivi.linkstate = 0;
1103 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1104 return 0;
1105
1106 vf_mac.vf =
1107 vf_vlan.vf =
1108 vf_rate.vf =
1109 vf_tx_rate.vf =
1110 vf_spoofchk.vf =
1111 vf_linkstate.vf =
1112 vf_rss_query_en.vf =
1113 vf_trust.vf = ivi.vf;
1114
1115 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1116 vf_vlan.vlan = ivi.vlan;
1117 vf_vlan.qos = ivi.qos;
1118 vf_tx_rate.rate = ivi.max_tx_rate;
1119 vf_rate.min_tx_rate = ivi.min_tx_rate;
1120 vf_rate.max_tx_rate = ivi.max_tx_rate;
1121 vf_spoofchk.setting = ivi.spoofchk;
1122 vf_linkstate.link_state = ivi.linkstate;
1123 vf_rss_query_en.setting = ivi.rss_query_en;
1124 vf_trust.setting = ivi.trusted;
1125 vf = nla_nest_start(skb, IFLA_VF_INFO);
1126 if (!vf) {
1127 nla_nest_cancel(skb, vfinfo);
1128 return -EMSGSIZE;
1129 }
1130 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1131 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1132 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1133 &vf_rate) ||
1134 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1135 &vf_tx_rate) ||
1136 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1137 &vf_spoofchk) ||
1138 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1139 &vf_linkstate) ||
1140 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1141 sizeof(vf_rss_query_en),
1142 &vf_rss_query_en) ||
1143 nla_put(skb, IFLA_VF_TRUST,
1144 sizeof(vf_trust), &vf_trust))
1145 return -EMSGSIZE;
1146 memset(&vf_stats, 0, sizeof(vf_stats));
1147 if (dev->netdev_ops->ndo_get_vf_stats)
1148 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1149 &vf_stats);
1150 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1151 if (!vfstats) {
1152 nla_nest_cancel(skb, vf);
1153 nla_nest_cancel(skb, vfinfo);
1154 return -EMSGSIZE;
1155 }
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001156 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1157 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1158 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1159 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1160 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1161 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1162 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1163 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1164 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1165 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1166 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1167 vf_stats.multicast, IFLA_VF_STATS_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 return -EMSGSIZE;
1169 nla_nest_end(skb, vfstats);
1170 nla_nest_end(skb, vf);
1171 return 0;
1172}
1173
1174static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1175{
1176 struct rtnl_link_ifmap map = {
1177 .mem_start = dev->mem_start,
1178 .mem_end = dev->mem_end,
1179 .base_addr = dev->base_addr,
1180 .irq = dev->irq,
1181 .dma = dev->dma,
1182 .port = dev->if_port,
1183 };
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001184 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001185 return -EMSGSIZE;
1186
1187 return 0;
1188}
1189
Thomas Grafb60c5112006-08-04 23:05:34 -07001190static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001191 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001192 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001193{
1194 struct ifinfomsg *ifm;
1195 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001196 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001197 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001198 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001199
Eric Dumazet2907c352011-05-25 07:34:04 +00001200 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001201 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1202 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001203 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001204
1205 ifm = nlmsg_data(nlh);
1206 ifm->ifi_family = AF_UNSPEC;
1207 ifm->__ifi_pad = 0;
1208 ifm->ifi_type = dev->type;
1209 ifm->ifi_index = dev->ifindex;
1210 ifm->ifi_flags = dev_get_flags(dev);
1211 ifm->ifi_change = change;
1212
David S. Millera6574342012-04-01 20:12:00 -04001213 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1214 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1215 nla_put_u8(skb, IFLA_OPERSTATE,
1216 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1217 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1218 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1219 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001220 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001221 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001222 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1223 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001224#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001225 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001226#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001227 (dev->ifindex != dev_get_iflink(dev) &&
1228 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001229 (upper_dev &&
1230 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001231 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001232 (dev->qdisc &&
1233 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1234 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001235 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1236 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001237 atomic_read(&dev->carrier_changes)) ||
1238 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001239 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001240
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001241 if (rtnl_fill_link_ifmap(skb, dev))
1242 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001245 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1246 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1247 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001250 if (rtnl_phys_port_id_fill(skb, dev))
1251 goto nla_put_failure;
1252
David Aherndb24a902015-03-17 20:23:15 -06001253 if (rtnl_phys_port_name_fill(skb, dev))
1254 goto nla_put_failure;
1255
Jiri Pirko82f28412014-11-28 14:34:18 +01001256 if (rtnl_phys_switch_id_fill(skb, dev))
1257 goto nla_put_failure;
1258
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001259 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001260 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
David S. Millera6574342012-04-01 20:12:00 -04001262 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1263 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1264 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001265
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001266 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1267 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001268 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001269 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001270 int num_vfs = dev_num_vf(dev->dev.parent);
1271
Chris Wrightc02db8c2010-05-16 01:05:45 -07001272 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1273 if (!vfinfo)
1274 goto nla_put_failure;
1275 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001276 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001277 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001278 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001279
Chris Wrightc02db8c2010-05-16 01:05:45 -07001280 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001281 }
Scott Feldman57b61082010-05-17 22:49:55 -07001282
David Gibsonc53864f2014-04-24 10:22:36 +10001283 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001284 goto nla_put_failure;
1285
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001286 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001287 if (rtnl_link_fill(skb, dev) < 0)
1288 goto nla_put_failure;
1289 }
1290
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001291 if (dev->rtnl_link_ops &&
1292 dev->rtnl_link_ops->get_link_net) {
1293 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1294
1295 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001296 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001297
1298 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1299 goto nla_put_failure;
1300 }
1301 }
1302
Thomas Graff8ff1822010-11-16 04:30:14 +00001303 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1304 goto nla_put_failure;
1305
1306 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1307 if (af_ops->fill_link_af) {
1308 struct nlattr *af;
1309 int err;
1310
1311 if (!(af = nla_nest_start(skb, af_ops->family)))
1312 goto nla_put_failure;
1313
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001314 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001315
1316 /*
1317 * Caller may return ENODATA to indicate that there
1318 * was no data to be dumped. This is not an error, it
1319 * means we should trim the attribute header and
1320 * continue.
1321 */
1322 if (err == -ENODATA)
1323 nla_nest_cancel(skb, af);
1324 else if (err < 0)
1325 goto nla_put_failure;
1326
1327 nla_nest_end(skb, af);
1328 }
1329 }
1330
1331 nla_nest_end(skb, af_spec);
1332
Johannes Berg053c0952015-01-16 22:09:00 +01001333 nlmsg_end(skb, nlh);
1334 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001335
1336nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001337 nlmsg_cancel(skb, nlh);
1338 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Jiri Pirkof7b12602014-02-18 20:53:18 +01001341static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1342 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1343 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1344 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1345 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1346 [IFLA_MTU] = { .type = NLA_U32 },
1347 [IFLA_LINK] = { .type = NLA_U32 },
1348 [IFLA_MASTER] = { .type = NLA_U32 },
1349 [IFLA_CARRIER] = { .type = NLA_U8 },
1350 [IFLA_TXQLEN] = { .type = NLA_U32 },
1351 [IFLA_WEIGHT] = { .type = NLA_U32 },
1352 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1353 [IFLA_LINKMODE] = { .type = NLA_U8 },
1354 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1355 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1356 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1357 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1358 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1359 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1360 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1361 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1362 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1363 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1364 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1365 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001366 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001367 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001368 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001369 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001370 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001371};
1372
1373static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1374 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1375 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1376 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1377 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1378};
1379
Jiri Pirkof7b12602014-02-18 20:53:18 +01001380static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001381 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1382 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1383 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1384 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1385 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1386 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001387 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001388 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001389 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001390 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1391 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001392};
1393
Jiri Pirkof7b12602014-02-18 20:53:18 +01001394static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1395 [IFLA_PORT_VF] = { .type = NLA_U32 },
1396 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1397 .len = PORT_PROFILE_MAX },
1398 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1399 .len = sizeof(struct ifla_port_vsi)},
1400 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1401 .len = PORT_UUID_MAX },
1402 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1403 .len = PORT_UUID_MAX },
1404 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1405 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1406};
1407
David Aherndc599f72016-02-02 08:17:07 -08001408static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1409{
1410 const struct rtnl_link_ops *ops = NULL;
1411 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1412
1413 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1414 return NULL;
1415
1416 if (linfo[IFLA_INFO_KIND]) {
1417 char kind[MODULE_NAME_LEN];
1418
1419 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1420 ops = rtnl_link_ops_get(kind);
1421 }
1422
1423 return ops;
1424}
1425
1426static bool link_master_filtered(struct net_device *dev, int master_idx)
1427{
1428 struct net_device *master;
1429
1430 if (!master_idx)
1431 return false;
1432
1433 master = netdev_master_upper_dev_get(dev);
1434 if (!master || master->ifindex != master_idx)
1435 return true;
1436
1437 return false;
1438}
1439
1440static bool link_kind_filtered(const struct net_device *dev,
1441 const struct rtnl_link_ops *kind_ops)
1442{
1443 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1444 return true;
1445
1446 return false;
1447}
1448
1449static bool link_dump_filtered(struct net_device *dev,
1450 int master_idx,
1451 const struct rtnl_link_ops *kind_ops)
1452{
1453 if (link_master_filtered(dev, master_idx) ||
1454 link_kind_filtered(dev, kind_ops))
1455 return true;
1456
1457 return false;
1458}
1459
Thomas Grafb60c5112006-08-04 23:05:34 -07001460static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001462 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001463 int h, s_h;
1464 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001466 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001467 struct nlattr *tb[IFLA_MAX+1];
1468 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001469 const struct rtnl_link_ops *kind_ops = NULL;
1470 unsigned int flags = NLM_F_MULTI;
1471 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001472 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001473 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001475 s_h = cb->args[0];
1476 s_idx = cb->args[1];
1477
Thomas Graf4e985ad2011-06-21 03:11:20 +00001478 cb->seq = net->dev_base_seq;
1479
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001480 /* A hack to preserve kernel<->userspace interface.
1481 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1482 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1483 * what iproute2 < v3.9.0 used.
1484 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1485 * attribute, its netlink message is shorter than struct ifinfomsg.
1486 */
1487 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1488 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1489
1490 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001491
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001492 if (tb[IFLA_EXT_MASK])
1493 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001494
1495 if (tb[IFLA_MASTER])
1496 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1497
1498 if (tb[IFLA_LINKINFO])
1499 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1500
1501 if (master_idx || kind_ops)
1502 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001503 }
Greg Rose115c9b82012-02-21 16:54:48 -05001504
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001505 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1506 idx = 0;
1507 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001508 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001509 if (link_dump_filtered(dev, master_idx, kind_ops))
1510 continue;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001511 if (idx < s_idx)
1512 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001513 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1514 NETLINK_CB(cb->skb).portid,
1515 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001516 flags,
David Gibson973462b2014-04-24 10:22:35 +10001517 ext_filter_mask);
1518 /* If we ran out of room on the first message,
1519 * we're in trouble
1520 */
1521 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1522
David S. Miller7b46a642015-01-18 23:36:08 -05001523 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001524 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001525
1526 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001527cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001528 idx++;
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001531out:
1532 cb->args[1] = idx;
1533 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 return skb->len;
1536}
1537
Jiri Pirkof7b12602014-02-18 20:53:18 +01001538int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1539{
1540 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1541}
1542EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001543
Eric W. Biederman81adee42009-11-08 00:53:51 -08001544struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1545{
1546 struct net *net;
1547 /* Examine the link attributes and figure out which
1548 * network namespace we are talking about.
1549 */
1550 if (tb[IFLA_NET_NS_PID])
1551 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001552 else if (tb[IFLA_NET_NS_FD])
1553 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001554 else
1555 net = get_net(src_net);
1556 return net;
1557}
1558EXPORT_SYMBOL(rtnl_link_get_net);
1559
Thomas Graf1840bb12008-02-23 19:54:36 -08001560static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1561{
1562 if (dev) {
1563 if (tb[IFLA_ADDRESS] &&
1564 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1565 return -EINVAL;
1566
1567 if (tb[IFLA_BROADCAST] &&
1568 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1569 return -EINVAL;
1570 }
1571
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001572 if (tb[IFLA_AF_SPEC]) {
1573 struct nlattr *af;
1574 int rem, err;
1575
1576 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1577 const struct rtnl_af_ops *af_ops;
1578
1579 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1580 return -EAFNOSUPPORT;
1581
1582 if (!af_ops->set_link_af)
1583 return -EOPNOTSUPP;
1584
1585 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001586 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001587 if (err < 0)
1588 return err;
1589 }
1590 }
1591 }
1592
Thomas Graf1840bb12008-02-23 19:54:36 -08001593 return 0;
1594}
1595
Eli Cohencc8e27c2016-03-11 22:58:34 +02001596static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1597 int guid_type)
1598{
1599 const struct net_device_ops *ops = dev->netdev_ops;
1600
1601 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1602}
1603
1604static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1605{
1606 if (dev->type != ARPHRD_INFINIBAND)
1607 return -EOPNOTSUPP;
1608
1609 return handle_infiniband_guid(dev, ivt, guid_type);
1610}
1611
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001612static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001613{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001614 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001615 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001616
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001617 if (tb[IFLA_VF_MAC]) {
1618 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001619
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001620 err = -EOPNOTSUPP;
1621 if (ops->ndo_set_vf_mac)
1622 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1623 ivm->mac);
1624 if (err < 0)
1625 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001626 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001627
1628 if (tb[IFLA_VF_VLAN]) {
1629 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1630
1631 err = -EOPNOTSUPP;
1632 if (ops->ndo_set_vf_vlan)
1633 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1634 ivv->qos);
1635 if (err < 0)
1636 return err;
1637 }
1638
1639 if (tb[IFLA_VF_TX_RATE]) {
1640 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1641 struct ifla_vf_info ivf;
1642
1643 err = -EOPNOTSUPP;
1644 if (ops->ndo_get_vf_config)
1645 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1646 if (err < 0)
1647 return err;
1648
1649 err = -EOPNOTSUPP;
1650 if (ops->ndo_set_vf_rate)
1651 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1652 ivf.min_tx_rate,
1653 ivt->rate);
1654 if (err < 0)
1655 return err;
1656 }
1657
1658 if (tb[IFLA_VF_RATE]) {
1659 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1660
1661 err = -EOPNOTSUPP;
1662 if (ops->ndo_set_vf_rate)
1663 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1664 ivt->min_tx_rate,
1665 ivt->max_tx_rate);
1666 if (err < 0)
1667 return err;
1668 }
1669
1670 if (tb[IFLA_VF_SPOOFCHK]) {
1671 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1672
1673 err = -EOPNOTSUPP;
1674 if (ops->ndo_set_vf_spoofchk)
1675 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1676 ivs->setting);
1677 if (err < 0)
1678 return err;
1679 }
1680
1681 if (tb[IFLA_VF_LINK_STATE]) {
1682 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1683
1684 err = -EOPNOTSUPP;
1685 if (ops->ndo_set_vf_link_state)
1686 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1687 ivl->link_state);
1688 if (err < 0)
1689 return err;
1690 }
1691
1692 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1693 struct ifla_vf_rss_query_en *ivrssq_en;
1694
1695 err = -EOPNOTSUPP;
1696 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1697 if (ops->ndo_set_vf_rss_query_en)
1698 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1699 ivrssq_en->setting);
1700 if (err < 0)
1701 return err;
1702 }
1703
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001704 if (tb[IFLA_VF_TRUST]) {
1705 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1706
1707 err = -EOPNOTSUPP;
1708 if (ops->ndo_set_vf_trust)
1709 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1710 if (err < 0)
1711 return err;
1712 }
1713
Eli Cohencc8e27c2016-03-11 22:58:34 +02001714 if (tb[IFLA_VF_IB_NODE_GUID]) {
1715 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1716
1717 if (!ops->ndo_set_vf_guid)
1718 return -EOPNOTSUPP;
1719
1720 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1721 }
1722
1723 if (tb[IFLA_VF_IB_PORT_GUID]) {
1724 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1725
1726 if (!ops->ndo_set_vf_guid)
1727 return -EOPNOTSUPP;
1728
1729 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1730 }
1731
Chris Wrightc02db8c2010-05-16 01:05:45 -07001732 return err;
1733}
1734
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001735static int do_set_master(struct net_device *dev, int ifindex)
1736{
Jiri Pirko898e5062013-01-03 22:48:52 +00001737 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001738 const struct net_device_ops *ops;
1739 int err;
1740
Jiri Pirko898e5062013-01-03 22:48:52 +00001741 if (upper_dev) {
1742 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001743 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001744 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001745 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001746 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001747 if (err)
1748 return err;
1749 } else {
1750 return -EOPNOTSUPP;
1751 }
1752 }
1753
1754 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001755 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1756 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001757 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001758 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001759 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001760 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001761 if (err)
1762 return err;
1763 } else {
1764 return -EOPNOTSUPP;
1765 }
1766 }
1767 return 0;
1768}
1769
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001770#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001771/* notify flag means notify + modified. */
1772#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001773static int do_setlink(const struct sk_buff *skb,
1774 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001775 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001776{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001777 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001778 int err;
1779
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001780 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001781 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001782 if (IS_ERR(net)) {
1783 err = PTR_ERR(net);
1784 goto errout;
1785 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001786 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001787 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001788 err = -EPERM;
1789 goto errout;
1790 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001791 err = dev_change_net_namespace(dev, net, ifname);
1792 put_net(net);
1793 if (err)
1794 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001795 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001796 }
1797
Patrick McHardy0157f602007-06-13 12:03:36 -07001798 if (tb[IFLA_MAP]) {
1799 struct rtnl_link_ifmap *u_map;
1800 struct ifmap k_map;
1801
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001802 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001803 err = -EOPNOTSUPP;
1804 goto errout;
1805 }
1806
1807 if (!netif_device_present(dev)) {
1808 err = -ENODEV;
1809 goto errout;
1810 }
1811
1812 u_map = nla_data(tb[IFLA_MAP]);
1813 k_map.mem_start = (unsigned long) u_map->mem_start;
1814 k_map.mem_end = (unsigned long) u_map->mem_end;
1815 k_map.base_addr = (unsigned short) u_map->base_addr;
1816 k_map.irq = (unsigned char) u_map->irq;
1817 k_map.dma = (unsigned char) u_map->dma;
1818 k_map.port = (unsigned char) u_map->port;
1819
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001820 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001821 if (err < 0)
1822 goto errout;
1823
Nicolas Dichtelba998902014-09-01 16:07:29 +02001824 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001825 }
1826
1827 if (tb[IFLA_ADDRESS]) {
1828 struct sockaddr *sa;
1829 int len;
1830
Patrick McHardy0157f602007-06-13 12:03:36 -07001831 len = sizeof(sa_family_t) + dev->addr_len;
1832 sa = kmalloc(len, GFP_KERNEL);
1833 if (!sa) {
1834 err = -ENOMEM;
1835 goto errout;
1836 }
1837 sa->sa_family = dev->type;
1838 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1839 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001840 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001841 kfree(sa);
1842 if (err)
1843 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001844 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001845 }
1846
1847 if (tb[IFLA_MTU]) {
1848 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1849 if (err < 0)
1850 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001851 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001852 }
1853
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001854 if (tb[IFLA_GROUP]) {
1855 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001856 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001857 }
1858
Patrick McHardy0157f602007-06-13 12:03:36 -07001859 /*
1860 * Interface selected by interface index but interface
1861 * name provided implies that a name change has been
1862 * requested.
1863 */
1864 if (ifm->ifi_index > 0 && ifname[0]) {
1865 err = dev_change_name(dev, ifname);
1866 if (err < 0)
1867 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001868 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001869 }
1870
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001871 if (tb[IFLA_IFALIAS]) {
1872 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1873 nla_len(tb[IFLA_IFALIAS]));
1874 if (err < 0)
1875 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001876 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001877 }
1878
Patrick McHardy0157f602007-06-13 12:03:36 -07001879 if (tb[IFLA_BROADCAST]) {
1880 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001881 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001882 }
1883
1884 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001885 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001886 if (err < 0)
1887 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001888 }
1889
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001890 if (tb[IFLA_MASTER]) {
1891 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1892 if (err)
1893 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001894 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001895 }
1896
Jiri Pirko9a572472012-12-27 23:49:39 +00001897 if (tb[IFLA_CARRIER]) {
1898 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1899 if (err)
1900 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001901 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001902 }
1903
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001904 if (tb[IFLA_TXQLEN]) {
1905 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1906
1907 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001908 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001909
1910 dev->tx_queue_len = value;
1911 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001912
Patrick McHardy0157f602007-06-13 12:03:36 -07001913 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001914 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001915
1916 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001917 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1918
David S. Miller93b2d4a2008-02-17 18:35:07 -08001919 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001920 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001921 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001922 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001923 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001924 }
1925
Chris Wrightc02db8c2010-05-16 01:05:45 -07001926 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001927 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001928 struct nlattr *attr;
1929 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001930
Chris Wrightc02db8c2010-05-16 01:05:45 -07001931 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001932 if (nla_type(attr) != IFLA_VF_INFO ||
1933 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001934 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001935 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001936 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001937 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1938 ifla_vf_policy);
1939 if (err < 0)
1940 goto errout;
1941 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001942 if (err < 0)
1943 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001944 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001945 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001946 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001947 err = 0;
1948
Scott Feldman57b61082010-05-17 22:49:55 -07001949 if (tb[IFLA_VF_PORTS]) {
1950 struct nlattr *port[IFLA_PORT_MAX+1];
1951 struct nlattr *attr;
1952 int vf;
1953 int rem;
1954
1955 err = -EOPNOTSUPP;
1956 if (!ops->ndo_set_vf_port)
1957 goto errout;
1958
1959 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001960 if (nla_type(attr) != IFLA_VF_PORT ||
1961 nla_len(attr) < NLA_HDRLEN) {
1962 err = -EINVAL;
1963 goto errout;
1964 }
1965 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1966 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001967 if (err < 0)
1968 goto errout;
1969 if (!port[IFLA_PORT_VF]) {
1970 err = -EOPNOTSUPP;
1971 goto errout;
1972 }
1973 vf = nla_get_u32(port[IFLA_PORT_VF]);
1974 err = ops->ndo_set_vf_port(dev, vf, port);
1975 if (err < 0)
1976 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001977 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001978 }
1979 }
1980 err = 0;
1981
1982 if (tb[IFLA_PORT_SELF]) {
1983 struct nlattr *port[IFLA_PORT_MAX+1];
1984
1985 err = nla_parse_nested(port, IFLA_PORT_MAX,
1986 tb[IFLA_PORT_SELF], ifla_port_policy);
1987 if (err < 0)
1988 goto errout;
1989
1990 err = -EOPNOTSUPP;
1991 if (ops->ndo_set_vf_port)
1992 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1993 if (err < 0)
1994 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001995 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001996 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001997
1998 if (tb[IFLA_AF_SPEC]) {
1999 struct nlattr *af;
2000 int rem;
2001
2002 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2003 const struct rtnl_af_ops *af_ops;
2004
2005 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002006 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002007
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002008 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002009 if (err < 0)
2010 goto errout;
2011
Nicolas Dichtelba998902014-09-01 16:07:29 +02002012 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002013 }
2014 }
Scott Feldman57b61082010-05-17 22:49:55 -07002015 err = 0;
2016
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002017 if (tb[IFLA_PROTO_DOWN]) {
2018 err = dev_change_proto_down(dev,
2019 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2020 if (err)
2021 goto errout;
2022 status |= DO_SETLINK_NOTIFY;
2023 }
2024
Patrick McHardy0157f602007-06-13 12:03:36 -07002025errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002026 if (status & DO_SETLINK_MODIFIED) {
2027 if (status & DO_SETLINK_NOTIFY)
2028 netdev_state_change(dev);
2029
2030 if (err < 0)
2031 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",
2032 dev->name);
2033 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002034
Patrick McHardy0157f602007-06-13 12:03:36 -07002035 return err;
2036}
2037
Thomas Graf661d2962013-03-21 07:45:29 +00002038static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002040 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002041 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002043 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002044 struct nlattr *tb[IFLA_MAX+1];
2045 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Thomas Grafda5e0492006-08-10 21:17:37 -07002047 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2048 if (err < 0)
2049 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Thomas Graf5176f912006-08-26 20:13:18 -07002051 if (tb[IFLA_IFNAME])
2052 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002053 else
2054 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002057 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002058 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002059 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002060 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002061 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002062 else
2063 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Thomas Grafda5e0492006-08-10 21:17:37 -07002065 if (dev == NULL) {
2066 err = -ENODEV;
2067 goto errout;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Eric Dumazete0d087a2009-11-07 01:26:17 -08002070 err = validate_linkmsg(dev, tb);
2071 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002072 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002073
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002074 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002075errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return err;
2077}
2078
WANG Cong66400d52015-03-24 11:53:31 -07002079static int rtnl_group_dellink(const struct net *net, int group)
2080{
2081 struct net_device *dev, *aux;
2082 LIST_HEAD(list_kill);
2083 bool found = false;
2084
2085 if (!group)
2086 return -EPERM;
2087
2088 for_each_netdev(net, dev) {
2089 if (dev->group == group) {
2090 const struct rtnl_link_ops *ops;
2091
2092 found = true;
2093 ops = dev->rtnl_link_ops;
2094 if (!ops || !ops->dellink)
2095 return -EOPNOTSUPP;
2096 }
2097 }
2098
2099 if (!found)
2100 return -ENODEV;
2101
2102 for_each_netdev_safe(net, dev, aux) {
2103 if (dev->group == group) {
2104 const struct rtnl_link_ops *ops;
2105
2106 ops = dev->rtnl_link_ops;
2107 ops->dellink(dev, &list_kill);
2108 }
2109 }
2110 unregister_netdevice_many(&list_kill);
2111
2112 return 0;
2113}
2114
Thomas Graf614732e2015-07-21 10:44:06 +02002115int rtnl_delete_link(struct net_device *dev)
2116{
2117 const struct rtnl_link_ops *ops;
2118 LIST_HEAD(list_kill);
2119
2120 ops = dev->rtnl_link_ops;
2121 if (!ops || !ops->dellink)
2122 return -EOPNOTSUPP;
2123
2124 ops->dellink(dev, &list_kill);
2125 unregister_netdevice_many(&list_kill);
2126
2127 return 0;
2128}
2129EXPORT_SYMBOL_GPL(rtnl_delete_link);
2130
Thomas Graf661d2962013-03-21 07:45:29 +00002131static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002132{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002133 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002134 struct net_device *dev;
2135 struct ifinfomsg *ifm;
2136 char ifname[IFNAMSIZ];
2137 struct nlattr *tb[IFLA_MAX+1];
2138 int err;
2139
2140 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2141 if (err < 0)
2142 return err;
2143
2144 if (tb[IFLA_IFNAME])
2145 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2146
2147 ifm = nlmsg_data(nlh);
2148 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002149 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002150 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002151 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002152 else if (tb[IFLA_GROUP])
2153 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002154 else
2155 return -EINVAL;
2156
2157 if (!dev)
2158 return -ENODEV;
2159
Thomas Graf614732e2015-07-21 10:44:06 +02002160 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002161}
2162
Patrick McHardy3729d502010-02-26 06:34:54 +00002163int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2164{
2165 unsigned int old_flags;
2166 int err;
2167
2168 old_flags = dev->flags;
2169 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2170 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2171 if (err < 0)
2172 return err;
2173 }
2174
2175 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002176
Nicolas Dichtela528c212013-09-25 12:02:44 +02002177 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002178 return 0;
2179}
2180EXPORT_SYMBOL(rtnl_configure_link);
2181
Rami Rosenc0713562012-11-30 01:08:47 +00002182struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002183 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002184 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002185{
2186 int err;
2187 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002188 unsigned int num_tx_queues = 1;
2189 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002190
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002191 if (tb[IFLA_NUM_TX_QUEUES])
2192 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2193 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002194 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002195
2196 if (tb[IFLA_NUM_RX_QUEUES])
2197 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2198 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002199 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002200
Pavel Emelianove7199282007-08-08 22:16:38 -07002201 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002202 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002203 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002204 if (!dev)
2205 goto err;
2206
Eric W. Biederman81adee42009-11-08 00:53:51 -08002207 dev_net_set(dev, net);
2208 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002209 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002210
Pavel Emelianove7199282007-08-08 22:16:38 -07002211 if (tb[IFLA_MTU])
2212 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002213 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002214 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2215 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002216 dev->addr_assign_type = NET_ADDR_SET;
2217 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002218 if (tb[IFLA_BROADCAST])
2219 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2220 nla_len(tb[IFLA_BROADCAST]));
2221 if (tb[IFLA_TXQLEN])
2222 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2223 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002224 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002225 if (tb[IFLA_LINKMODE])
2226 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002227 if (tb[IFLA_GROUP])
2228 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002229
2230 return dev;
2231
Pavel Emelianove7199282007-08-08 22:16:38 -07002232err:
2233 return ERR_PTR(err);
2234}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002235EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002236
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002237static int rtnl_group_changelink(const struct sk_buff *skb,
2238 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002239 struct ifinfomsg *ifm,
2240 struct nlattr **tb)
2241{
WANG Congd0795352015-03-23 16:31:09 -07002242 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002243 int err;
2244
WANG Congd0795352015-03-23 16:31:09 -07002245 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002246 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002247 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002248 if (err < 0)
2249 return err;
2250 }
2251 }
2252
2253 return 0;
2254}
2255
Thomas Graf661d2962013-03-21 07:45:29 +00002256static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002257{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002258 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002259 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002260 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002261 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002262 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002263 struct ifinfomsg *ifm;
2264 char kind[MODULE_NAME_LEN];
2265 char ifname[IFNAMSIZ];
2266 struct nlattr *tb[IFLA_MAX+1];
2267 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002268 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002269 int err;
2270
Johannes Berg95a5afc2008-10-16 15:24:51 -07002271#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002272replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002273#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002274 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2275 if (err < 0)
2276 return err;
2277
2278 if (tb[IFLA_IFNAME])
2279 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2280 else
2281 ifname[0] = '\0';
2282
2283 ifm = nlmsg_data(nlh);
2284 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002285 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002286 else {
2287 if (ifname[0])
2288 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002289 else
2290 dev = NULL;
2291 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002292
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002293 if (dev) {
2294 master_dev = netdev_master_upper_dev_get(dev);
2295 if (master_dev)
2296 m_ops = master_dev->rtnl_link_ops;
2297 }
2298
Eric Dumazete0d087a2009-11-07 01:26:17 -08002299 err = validate_linkmsg(dev, tb);
2300 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002301 return err;
2302
Patrick McHardy38f7b872007-06-13 12:03:51 -07002303 if (tb[IFLA_LINKINFO]) {
2304 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2305 tb[IFLA_LINKINFO], ifla_info_policy);
2306 if (err < 0)
2307 return err;
2308 } else
2309 memset(linkinfo, 0, sizeof(linkinfo));
2310
2311 if (linkinfo[IFLA_INFO_KIND]) {
2312 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2313 ops = rtnl_link_ops_get(kind);
2314 } else {
2315 kind[0] = '\0';
2316 ops = NULL;
2317 }
2318
2319 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002320 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2321 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002322 struct nlattr **data = NULL;
2323 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002324 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002325
2326 if (ops) {
2327 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2328 err = nla_parse_nested(attr, ops->maxtype,
2329 linkinfo[IFLA_INFO_DATA],
2330 ops->policy);
2331 if (err < 0)
2332 return err;
2333 data = attr;
2334 }
2335 if (ops->validate) {
2336 err = ops->validate(tb, data);
2337 if (err < 0)
2338 return err;
2339 }
2340 }
2341
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002342 if (m_ops) {
2343 if (m_ops->slave_maxtype &&
2344 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2345 err = nla_parse_nested(slave_attr,
2346 m_ops->slave_maxtype,
2347 linkinfo[IFLA_INFO_SLAVE_DATA],
2348 m_ops->slave_policy);
2349 if (err < 0)
2350 return err;
2351 slave_data = slave_attr;
2352 }
2353 if (m_ops->slave_validate) {
2354 err = m_ops->slave_validate(tb, slave_data);
2355 if (err < 0)
2356 return err;
2357 }
2358 }
2359
Patrick McHardy38f7b872007-06-13 12:03:51 -07002360 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002361 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002362
2363 if (nlh->nlmsg_flags & NLM_F_EXCL)
2364 return -EEXIST;
2365 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2366 return -EOPNOTSUPP;
2367
2368 if (linkinfo[IFLA_INFO_DATA]) {
2369 if (!ops || ops != dev->rtnl_link_ops ||
2370 !ops->changelink)
2371 return -EOPNOTSUPP;
2372
2373 err = ops->changelink(dev, tb, data);
2374 if (err < 0)
2375 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002376 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002377 }
2378
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002379 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2380 if (!m_ops || !m_ops->slave_changelink)
2381 return -EOPNOTSUPP;
2382
2383 err = m_ops->slave_changelink(master_dev, dev,
2384 tb, slave_data);
2385 if (err < 0)
2386 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002387 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002388 }
2389
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002390 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002391 }
2392
Patrick McHardyffa934f2011-01-20 03:00:42 +00002393 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2394 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002395 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002396 nla_get_u32(tb[IFLA_GROUP]),
2397 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002398 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002399 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002400
Patrick McHardy0e068772007-07-11 19:42:31 -07002401 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002402 return -EOPNOTSUPP;
2403
2404 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002405#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002406 if (kind[0]) {
2407 __rtnl_unlock();
2408 request_module("rtnl-link-%s", kind);
2409 rtnl_lock();
2410 ops = rtnl_link_ops_get(kind);
2411 if (ops)
2412 goto replay;
2413 }
2414#endif
2415 return -EOPNOTSUPP;
2416 }
2417
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002418 if (!ops->setup)
2419 return -EOPNOTSUPP;
2420
Tom Gundersen55177502014-07-14 16:37:25 +02002421 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002422 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002423 name_assign_type = NET_NAME_ENUM;
2424 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002425
Eric W. Biederman81adee42009-11-08 00:53:51 -08002426 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002427 if (IS_ERR(dest_net))
2428 return PTR_ERR(dest_net);
2429
Eric W. Biederman505ce412015-02-26 16:19:00 -06002430 err = -EPERM;
2431 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2432 goto out;
2433
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002434 if (tb[IFLA_LINK_NETNSID]) {
2435 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2436
2437 link_net = get_net_ns_by_id(dest_net, id);
2438 if (!link_net) {
2439 err = -EINVAL;
2440 goto out;
2441 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002442 err = -EPERM;
2443 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2444 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002445 }
2446
2447 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2448 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002449 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002450 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002451 goto out;
2452 }
2453
2454 dev->ifindex = ifm->ifi_index;
2455
Cong Wang0e0eee22014-02-11 15:51:30 -08002456 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002457 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002458 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002459 * and unregister it on failure after registration
2460 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002461 */
Cong Wange51fb152014-06-03 16:40:47 -07002462 if (err < 0) {
2463 /* If device is not registered at all, free it now */
2464 if (dev->reg_state == NETREG_UNINITIALIZED)
2465 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002466 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002467 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002468 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002469 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002470 if (err < 0) {
2471 free_netdev(dev);
2472 goto out;
2473 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002474 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002475 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002476 if (err < 0)
2477 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002478 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002479 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002480 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002481 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002482 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002483out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002484 if (link_net)
2485 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002486 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002487 return err;
David S. Miller43638902015-03-10 21:58:32 -04002488out_unregister:
2489 if (ops->newlink) {
2490 LIST_HEAD(list_kill);
2491
2492 ops->dellink(dev, &list_kill);
2493 unregister_netdevice_many(&list_kill);
2494 } else {
2495 unregister_netdevice(dev);
2496 }
2497 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002498 }
2499}
2500
Thomas Graf661d2962013-03-21 07:45:29 +00002501static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002502{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002503 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002504 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002505 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002506 struct nlattr *tb[IFLA_MAX+1];
2507 struct net_device *dev = NULL;
2508 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002509 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002510 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002511
Thomas Grafb60c5112006-08-04 23:05:34 -07002512 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2513 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002514 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002515
Eric Dumazeta3d12892009-10-21 10:59:31 +00002516 if (tb[IFLA_IFNAME])
2517 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2518
Greg Rose115c9b82012-02-21 16:54:48 -05002519 if (tb[IFLA_EXT_MASK])
2520 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2521
Thomas Grafb60c5112006-08-04 23:05:34 -07002522 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002523 if (ifm->ifi_index > 0)
2524 dev = __dev_get_by_index(net, ifm->ifi_index);
2525 else if (tb[IFLA_IFNAME])
2526 dev = __dev_get_by_name(net, ifname);
2527 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002528 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002529
Eric Dumazeta3d12892009-10-21 10:59:31 +00002530 if (dev == NULL)
2531 return -ENODEV;
2532
Greg Rose115c9b82012-02-21 16:54:48 -05002533 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002534 if (nskb == NULL)
2535 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002536
Eric W. Biederman15e47302012-09-07 20:12:54 +00002537 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002538 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002539 if (err < 0) {
2540 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2541 WARN_ON(err == -EMSGSIZE);
2542 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002543 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002544 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002545
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002546 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002547}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002548
Greg Rose115c9b82012-02-21 16:54:48 -05002549static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002550{
Greg Rose115c9b82012-02-21 16:54:48 -05002551 struct net *net = sock_net(skb->sk);
2552 struct net_device *dev;
2553 struct nlattr *tb[IFLA_MAX+1];
2554 u32 ext_filter_mask = 0;
2555 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002556 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002557
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002558 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2559 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2560 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2561
2562 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002563 if (tb[IFLA_EXT_MASK])
2564 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2565 }
Greg Rose115c9b82012-02-21 16:54:48 -05002566
2567 if (!ext_filter_mask)
2568 return NLMSG_GOODSIZE;
2569 /*
2570 * traverse the list of net devices and compute the minimum
2571 * buffer size based upon the filter mask.
2572 */
2573 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2574 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2575 if_nlmsg_size(dev,
2576 ext_filter_mask));
2577 }
2578
Greg Rosec7ac8672011-06-10 01:27:09 +00002579 return min_ifinfo_dump_size;
2580}
2581
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002582static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
2584 int idx;
2585 int s_idx = cb->family;
2586
2587 if (s_idx == 0)
2588 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002589 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int type = cb->nlh->nlmsg_type-RTM_BASE;
2591 if (idx < s_idx || idx == PF_PACKET)
2592 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002593 if (rtnl_msg_handlers[idx] == NULL ||
2594 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002596 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002598 cb->prev_seq = 0;
2599 cb->seq = 0;
2600 }
Thomas Grafe2849862007-03-22 11:48:11 -07002601 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 break;
2603 }
2604 cb->family = idx;
2605
2606 return skb->len;
2607}
2608
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002609struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2610 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002612 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002614 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002615 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002617 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002618 if (skb == NULL)
2619 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Greg Rose115c9b82012-02-21 16:54:48 -05002621 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002622 if (err < 0) {
2623 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2624 WARN_ON(err == -EMSGSIZE);
2625 kfree_skb(skb);
2626 goto errout;
2627 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002628 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002629errout:
2630 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002631 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002632 return NULL;
2633}
2634
2635void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2636{
2637 struct net *net = dev_net(dev);
2638
2639 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2640}
2641
2642void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2643 gfp_t flags)
2644{
2645 struct sk_buff *skb;
2646
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002647 if (dev->reg_state != NETREG_REGISTERED)
2648 return;
2649
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002650 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2651 if (skb)
2652 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002654EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
John Fastabendd83b0602012-04-15 06:44:08 +00002656static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2657 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002658 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002659 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002660 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002661{
2662 struct nlmsghdr *nlh;
2663 struct ndmsg *ndm;
2664
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002665 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002666 if (!nlh)
2667 return -EMSGSIZE;
2668
2669 ndm = nlmsg_data(nlh);
2670 ndm->ndm_family = AF_BRIDGE;
2671 ndm->ndm_pad1 = 0;
2672 ndm->ndm_pad2 = 0;
2673 ndm->ndm_flags = flags;
2674 ndm->ndm_type = 0;
2675 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002676 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002677
2678 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2679 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002680 if (vid)
2681 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2682 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002683
Johannes Berg053c0952015-01-16 22:09:00 +01002684 nlmsg_end(skb, nlh);
2685 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002686
2687nla_put_failure:
2688 nlmsg_cancel(skb, nlh);
2689 return -EMSGSIZE;
2690}
2691
John Fastabend3ff661c2012-04-15 06:44:14 +00002692static inline size_t rtnl_fdb_nlmsg_size(void)
2693{
2694 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2695}
2696
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002697static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2698 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002699{
2700 struct net *net = dev_net(dev);
2701 struct sk_buff *skb;
2702 int err = -ENOBUFS;
2703
2704 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2705 if (!skb)
2706 goto errout;
2707
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002708 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002709 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002710 if (err < 0) {
2711 kfree_skb(skb);
2712 goto errout;
2713 }
2714
2715 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2716 return;
2717errout:
2718 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2719}
2720
Vlad Yasevich090096b2013-03-06 15:39:42 +00002721/**
2722 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2723 */
2724int ndo_dflt_fdb_add(struct ndmsg *ndm,
2725 struct nlattr *tb[],
2726 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002727 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002728 u16 flags)
2729{
2730 int err = -EINVAL;
2731
2732 /* If aging addresses are supported device will need to
2733 * implement its own handler for this.
2734 */
2735 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2736 pr_info("%s: FDB only supports static addresses\n", dev->name);
2737 return err;
2738 }
2739
Or Gerlitz65891fe2014-12-14 18:19:05 +02002740 if (vid) {
2741 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2742 return err;
2743 }
2744
Vlad Yasevich090096b2013-03-06 15:39:42 +00002745 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2746 err = dev_uc_add_excl(dev, addr);
2747 else if (is_multicast_ether_addr(addr))
2748 err = dev_mc_add_excl(dev, addr);
2749
2750 /* Only return duplicate errors if NLM_F_EXCL is set */
2751 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2752 err = 0;
2753
2754 return err;
2755}
2756EXPORT_SYMBOL(ndo_dflt_fdb_add);
2757
Jiri Pirkof6f64242014-11-28 14:34:15 +01002758static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2759{
2760 u16 vid = 0;
2761
2762 if (vlan_attr) {
2763 if (nla_len(vlan_attr) != sizeof(u16)) {
2764 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2765 return -EINVAL;
2766 }
2767
2768 vid = nla_get_u16(vlan_attr);
2769
2770 if (!vid || vid >= VLAN_VID_MASK) {
2771 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2772 vid);
2773 return -EINVAL;
2774 }
2775 }
2776 *p_vid = vid;
2777 return 0;
2778}
2779
Thomas Graf661d2962013-03-21 07:45:29 +00002780static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002781{
2782 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002783 struct ndmsg *ndm;
2784 struct nlattr *tb[NDA_MAX+1];
2785 struct net_device *dev;
2786 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002787 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002788 int err;
2789
2790 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2791 if (err < 0)
2792 return err;
2793
2794 ndm = nlmsg_data(nlh);
2795 if (ndm->ndm_ifindex == 0) {
2796 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2797 return -EINVAL;
2798 }
2799
2800 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2801 if (dev == NULL) {
2802 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2803 return -ENODEV;
2804 }
2805
2806 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2807 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2808 return -EINVAL;
2809 }
2810
2811 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002812
Jiri Pirkof6f64242014-11-28 14:34:15 +01002813 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2814 if (err)
2815 return err;
2816
John Fastabend77162022012-04-15 06:43:56 +00002817 err = -EOPNOTSUPP;
2818
2819 /* Support fdb on master device the net/bridge default case */
2820 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2821 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002822 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2823 const struct net_device_ops *ops = br_dev->netdev_ops;
2824
Jiri Pirkof6f64242014-11-28 14:34:15 +01002825 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2826 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002827 if (err)
2828 goto out;
2829 else
2830 ndm->ndm_flags &= ~NTF_MASTER;
2831 }
2832
2833 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002834 if ((ndm->ndm_flags & NTF_SELF)) {
2835 if (dev->netdev_ops->ndo_fdb_add)
2836 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002837 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002838 nlh->nlmsg_flags);
2839 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002840 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002841 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002842
John Fastabend3ff661c2012-04-15 06:44:14 +00002843 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002844 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2845 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002846 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002847 }
John Fastabend77162022012-04-15 06:43:56 +00002848 }
2849out:
2850 return err;
2851}
2852
Vlad Yasevich090096b2013-03-06 15:39:42 +00002853/**
2854 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2855 */
2856int ndo_dflt_fdb_del(struct ndmsg *ndm,
2857 struct nlattr *tb[],
2858 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002859 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002860{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002861 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002862
2863 /* If aging addresses are supported device will need to
2864 * implement its own handler for this.
2865 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002866 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002867 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002868 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002869 }
2870
2871 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2872 err = dev_uc_del(dev, addr);
2873 else if (is_multicast_ether_addr(addr))
2874 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002875
2876 return err;
2877}
2878EXPORT_SYMBOL(ndo_dflt_fdb_del);
2879
Thomas Graf661d2962013-03-21 07:45:29 +00002880static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002881{
2882 struct net *net = sock_net(skb->sk);
2883 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002884 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002885 struct net_device *dev;
2886 int err = -EINVAL;
2887 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002888 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002889
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002890 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002891 return -EPERM;
2892
2893 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2894 if (err < 0)
2895 return err;
John Fastabend77162022012-04-15 06:43:56 +00002896
2897 ndm = nlmsg_data(nlh);
2898 if (ndm->ndm_ifindex == 0) {
2899 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2900 return -EINVAL;
2901 }
2902
2903 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2904 if (dev == NULL) {
2905 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2906 return -ENODEV;
2907 }
2908
Vlad Yasevich1690be62013-02-13 12:00:18 +00002909 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2910 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002911 return -EINVAL;
2912 }
2913
Vlad Yasevich1690be62013-02-13 12:00:18 +00002914 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002915
Jiri Pirkof6f64242014-11-28 14:34:15 +01002916 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2917 if (err)
2918 return err;
2919
John Fastabend77162022012-04-15 06:43:56 +00002920 err = -EOPNOTSUPP;
2921
2922 /* Support fdb on master device the net/bridge default case */
2923 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2924 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002925 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2926 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002927
Jiri Pirko898e5062013-01-03 22:48:52 +00002928 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002929 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002930
2931 if (err)
2932 goto out;
2933 else
2934 ndm->ndm_flags &= ~NTF_MASTER;
2935 }
2936
2937 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002938 if (ndm->ndm_flags & NTF_SELF) {
2939 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002940 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2941 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002942 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002943 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002944
John Fastabend3ff661c2012-04-15 06:44:14 +00002945 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002946 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2947 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002948 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002949 }
John Fastabend77162022012-04-15 06:43:56 +00002950 }
2951out:
2952 return err;
2953}
2954
John Fastabendd83b0602012-04-15 06:44:08 +00002955static int nlmsg_populate_fdb(struct sk_buff *skb,
2956 struct netlink_callback *cb,
2957 struct net_device *dev,
2958 int *idx,
2959 struct netdev_hw_addr_list *list)
2960{
2961 struct netdev_hw_addr *ha;
2962 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002963 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002964
Eric W. Biederman15e47302012-09-07 20:12:54 +00002965 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002966 seq = cb->nlh->nlmsg_seq;
2967
2968 list_for_each_entry(ha, &list->list, list) {
2969 if (*idx < cb->args[0])
2970 goto skip;
2971
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002972 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002973 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002974 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002975 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00002976 if (err < 0)
2977 return err;
2978skip:
2979 *idx += 1;
2980 }
2981 return 0;
2982}
2983
2984/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002985 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002986 * @nlh: netlink message header
2987 * @dev: netdevice
2988 *
2989 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002990 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002991 */
2992int ndo_dflt_fdb_dump(struct sk_buff *skb,
2993 struct netlink_callback *cb,
2994 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002995 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002996 int idx)
2997{
2998 int err;
2999
3000 netif_addr_lock_bh(dev);
3001 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3002 if (err)
3003 goto out;
3004 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3005out:
3006 netif_addr_unlock_bh(dev);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003007 cb->args[1] = err;
John Fastabendd83b0602012-04-15 06:44:08 +00003008 return idx;
3009}
3010EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3011
John Fastabend77162022012-04-15 06:43:56 +00003012static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3013{
John Fastabend77162022012-04-15 06:43:56 +00003014 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003015 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003016 struct net_device *br_dev = NULL;
3017 const struct net_device_ops *ops = NULL;
3018 const struct net_device_ops *cops = NULL;
3019 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3020 struct net *net = sock_net(skb->sk);
3021 int brport_idx = 0;
3022 int br_idx = 0;
3023 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003024
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003025 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3026 ifla_policy) == 0) {
3027 if (tb[IFLA_MASTER])
3028 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3029 }
John Fastabend77162022012-04-15 06:43:56 +00003030
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003031 brport_idx = ifm->ifi_index;
3032
3033 if (br_idx) {
3034 br_dev = __dev_get_by_index(net, br_idx);
3035 if (!br_dev)
3036 return -ENODEV;
3037
3038 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003039 }
3040
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003041 cb->args[1] = 0;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003042 for_each_netdev(net, dev) {
3043 if (brport_idx && (dev->ifindex != brport_idx))
3044 continue;
3045
3046 if (!br_idx) { /* user did not specify a specific bridge */
3047 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3048 br_dev = netdev_master_upper_dev_get(dev);
3049 cops = br_dev->netdev_ops;
3050 }
3051
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003052 } else {
3053 if (dev != br_dev &&
3054 !(dev->priv_flags & IFF_BRIDGE_PORT))
3055 continue;
3056
3057 if (br_dev != netdev_master_upper_dev_get(dev) &&
3058 !(dev->priv_flags & IFF_EBRIDGE))
3059 continue;
3060
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003061 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00003062 }
3063
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003064 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3065 if (cops && cops->ndo_fdb_dump)
3066 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3067 idx);
3068 }
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003069 if (cb->args[1] == -EMSGSIZE)
3070 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003071
John Fastabend77162022012-04-15 06:43:56 +00003072 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003073 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003074 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003075 else
3076 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003077 if (cb->args[1] == -EMSGSIZE)
3078 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003079
3080 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00003081 }
John Fastabend77162022012-04-15 06:43:56 +00003082
3083 cb->args[0] = idx;
3084 return skb->len;
3085}
3086
Scott Feldman2c3c0312014-11-28 14:34:25 +01003087static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3088 unsigned int attrnum, unsigned int flag)
3089{
3090 if (mask & flag)
3091 return nla_put_u8(skb, attrnum, !!(flags & flag));
3092 return 0;
3093}
3094
John Fastabend815cccb2012-10-24 08:13:09 +00003095int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003096 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003097 u32 flags, u32 mask, int nlflags,
3098 u32 filter_mask,
3099 int (*vlan_fill)(struct sk_buff *skb,
3100 struct net_device *dev,
3101 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003102{
3103 struct nlmsghdr *nlh;
3104 struct ifinfomsg *ifm;
3105 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003106 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003107 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003108 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003109 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003110
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003111 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003112 if (nlh == NULL)
3113 return -EMSGSIZE;
3114
3115 ifm = nlmsg_data(nlh);
3116 ifm->ifi_family = AF_BRIDGE;
3117 ifm->__ifi_pad = 0;
3118 ifm->ifi_type = dev->type;
3119 ifm->ifi_index = dev->ifindex;
3120 ifm->ifi_flags = dev_get_flags(dev);
3121 ifm->ifi_change = 0;
3122
3123
3124 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3125 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3126 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003127 (br_dev &&
3128 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003129 (dev->addr_len &&
3130 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003131 (dev->ifindex != dev_get_iflink(dev) &&
3132 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003133 goto nla_put_failure;
3134
3135 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3136 if (!br_afspec)
3137 goto nla_put_failure;
3138
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003139 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003140 nla_nest_cancel(skb, br_afspec);
3141 goto nla_put_failure;
3142 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003143
3144 if (mode != BRIDGE_MODE_UNDEF) {
3145 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3146 nla_nest_cancel(skb, br_afspec);
3147 goto nla_put_failure;
3148 }
3149 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003150 if (vlan_fill) {
3151 err = vlan_fill(skb, dev, filter_mask);
3152 if (err) {
3153 nla_nest_cancel(skb, br_afspec);
3154 goto nla_put_failure;
3155 }
3156 }
John Fastabend815cccb2012-10-24 08:13:09 +00003157 nla_nest_end(skb, br_afspec);
3158
Scott Feldman2c3c0312014-11-28 14:34:25 +01003159 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3160 if (!protinfo)
3161 goto nla_put_failure;
3162
3163 if (brport_nla_put_flag(skb, flags, mask,
3164 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3165 brport_nla_put_flag(skb, flags, mask,
3166 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3167 brport_nla_put_flag(skb, flags, mask,
3168 IFLA_BRPORT_FAST_LEAVE,
3169 BR_MULTICAST_FAST_LEAVE) ||
3170 brport_nla_put_flag(skb, flags, mask,
3171 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3172 brport_nla_put_flag(skb, flags, mask,
3173 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3174 brport_nla_put_flag(skb, flags, mask,
3175 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3176 brport_nla_put_flag(skb, flags, mask,
3177 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3178 brport_nla_put_flag(skb, flags, mask,
3179 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3180 nla_nest_cancel(skb, protinfo);
3181 goto nla_put_failure;
3182 }
3183
3184 nla_nest_end(skb, protinfo);
3185
Johannes Berg053c0952015-01-16 22:09:00 +01003186 nlmsg_end(skb, nlh);
3187 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003188nla_put_failure:
3189 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003190 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003191}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003192EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003193
John Fastabende5a55a82012-10-24 08:12:57 +00003194static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3195{
3196 struct net *net = sock_net(skb->sk);
3197 struct net_device *dev;
3198 int idx = 0;
3199 u32 portid = NETLINK_CB(cb->skb).portid;
3200 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003201 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003202 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003203
Thomas Grafaa68c202014-11-26 13:42:20 +01003204 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3205 struct nlattr *extfilt;
3206
3207 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3208 IFLA_EXT_MASK);
3209 if (extfilt) {
3210 if (nla_len(extfilt) < sizeof(filter_mask))
3211 return -EINVAL;
3212
3213 filter_mask = nla_get_u32(extfilt);
3214 }
3215 }
John Fastabende5a55a82012-10-24 08:12:57 +00003216
3217 rcu_read_lock();
3218 for_each_netdev_rcu(net, dev) {
3219 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003220 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003221
Jiri Pirko898e5062013-01-03 22:48:52 +00003222 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003223 if (idx >= cb->args[0]) {
3224 err = br_dev->netdev_ops->ndo_bridge_getlink(
3225 skb, portid, seq, dev,
3226 filter_mask, NLM_F_MULTI);
3227 if (err < 0 && err != -EOPNOTSUPP)
3228 break;
3229 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003230 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003231 }
3232
3233 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003234 if (idx >= cb->args[0]) {
3235 err = ops->ndo_bridge_getlink(skb, portid,
3236 seq, dev,
3237 filter_mask,
3238 NLM_F_MULTI);
3239 if (err < 0 && err != -EOPNOTSUPP)
3240 break;
3241 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003242 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003243 }
3244 }
3245 rcu_read_unlock();
3246 cb->args[0] = idx;
3247
3248 return skb->len;
3249}
3250
John Fastabend2469ffd2012-10-24 08:13:03 +00003251static inline size_t bridge_nlmsg_size(void)
3252{
3253 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3254 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3255 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3256 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3257 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3258 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3259 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3260 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3261 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3262 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3263 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3264}
3265
Roopa Prabhu02dba432015-01-14 20:02:25 -08003266static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003267{
3268 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003269 struct sk_buff *skb;
3270 int err = -EOPNOTSUPP;
3271
Roopa Prabhu02dba432015-01-14 20:02:25 -08003272 if (!dev->netdev_ops->ndo_bridge_getlink)
3273 return 0;
3274
John Fastabend2469ffd2012-10-24 08:13:03 +00003275 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3276 if (!skb) {
3277 err = -ENOMEM;
3278 goto errout;
3279 }
3280
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003281 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003282 if (err < 0)
3283 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003284
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003285 if (!skb->len)
3286 goto errout;
3287
John Fastabend2469ffd2012-10-24 08:13:03 +00003288 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3289 return 0;
3290errout:
3291 WARN_ON(err == -EMSGSIZE);
3292 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003293 if (err)
3294 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003295 return err;
3296}
3297
Thomas Graf661d2962013-03-21 07:45:29 +00003298static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003299{
3300 struct net *net = sock_net(skb->sk);
3301 struct ifinfomsg *ifm;
3302 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003303 struct nlattr *br_spec, *attr = NULL;
3304 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003305 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003306 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003307
3308 if (nlmsg_len(nlh) < sizeof(*ifm))
3309 return -EINVAL;
3310
3311 ifm = nlmsg_data(nlh);
3312 if (ifm->ifi_family != AF_BRIDGE)
3313 return -EPFNOSUPPORT;
3314
3315 dev = __dev_get_by_index(net, ifm->ifi_index);
3316 if (!dev) {
3317 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3318 return -ENODEV;
3319 }
3320
John Fastabend2469ffd2012-10-24 08:13:03 +00003321 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3322 if (br_spec) {
3323 nla_for_each_nested(attr, br_spec, rem) {
3324 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003325 if (nla_len(attr) < sizeof(flags))
3326 return -EINVAL;
3327
John Fastabendc38e01b2012-11-02 16:32:36 +00003328 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003329 flags = nla_get_u16(attr);
3330 break;
3331 }
3332 }
3333 }
3334
3335 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003336 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3337
3338 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003339 err = -EOPNOTSUPP;
3340 goto out;
3341 }
3342
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003343 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003344 if (err)
3345 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003346
3347 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003348 }
3349
John Fastabend2469ffd2012-10-24 08:13:03 +00003350 if ((flags & BRIDGE_FLAGS_SELF)) {
3351 if (!dev->netdev_ops->ndo_bridge_setlink)
3352 err = -EOPNOTSUPP;
3353 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003354 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3355 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003356 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003357 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003358
3359 /* Generate event to notify upper layer of bridge
3360 * change
3361 */
3362 err = rtnl_bridge_notify(dev);
3363 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003364 }
3365
John Fastabendc38e01b2012-11-02 16:32:36 +00003366 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003367 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003368out:
3369 return err;
3370}
3371
Thomas Graf661d2962013-03-21 07:45:29 +00003372static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003373{
3374 struct net *net = sock_net(skb->sk);
3375 struct ifinfomsg *ifm;
3376 struct net_device *dev;
3377 struct nlattr *br_spec, *attr = NULL;
3378 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003379 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003380 bool have_flags = false;
3381
3382 if (nlmsg_len(nlh) < sizeof(*ifm))
3383 return -EINVAL;
3384
3385 ifm = nlmsg_data(nlh);
3386 if (ifm->ifi_family != AF_BRIDGE)
3387 return -EPFNOSUPPORT;
3388
3389 dev = __dev_get_by_index(net, ifm->ifi_index);
3390 if (!dev) {
3391 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3392 return -ENODEV;
3393 }
3394
3395 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3396 if (br_spec) {
3397 nla_for_each_nested(attr, br_spec, rem) {
3398 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003399 if (nla_len(attr) < sizeof(flags))
3400 return -EINVAL;
3401
Vlad Yasevich407af322013-02-13 12:00:12 +00003402 have_flags = true;
3403 flags = nla_get_u16(attr);
3404 break;
3405 }
3406 }
3407 }
3408
Vlad Yasevich407af322013-02-13 12:00:12 +00003409 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3410 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3411
3412 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3413 err = -EOPNOTSUPP;
3414 goto out;
3415 }
3416
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003417 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003418 if (err)
3419 goto out;
3420
3421 flags &= ~BRIDGE_FLAGS_MASTER;
3422 }
3423
3424 if ((flags & BRIDGE_FLAGS_SELF)) {
3425 if (!dev->netdev_ops->ndo_bridge_dellink)
3426 err = -EOPNOTSUPP;
3427 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003428 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3429 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003430
Roopa Prabhu02dba432015-01-14 20:02:25 -08003431 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003432 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003433
3434 /* Generate event to notify upper layer of bridge
3435 * change
3436 */
3437 err = rtnl_bridge_notify(dev);
3438 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003439 }
3440
3441 if (have_flags)
3442 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003443out:
3444 return err;
3445}
3446
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003447static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3448 int type, u32 pid, u32 seq, u32 change,
3449 unsigned int flags, unsigned int filter_mask)
3450{
3451 struct if_stats_msg *ifsm;
3452 struct nlmsghdr *nlh;
3453 struct nlattr *attr;
3454
3455 ASSERT_RTNL();
3456
3457 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3458 if (!nlh)
3459 return -EMSGSIZE;
3460
3461 ifsm = nlmsg_data(nlh);
3462 ifsm->ifindex = dev->ifindex;
3463 ifsm->filter_mask = filter_mask;
3464
3465 if (filter_mask & IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_64)) {
3466 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003467
Nicolas Dichtel58414d32016-04-21 18:58:25 +02003468 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3469 sizeof(struct rtnl_link_stats64),
3470 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003471 if (!attr)
3472 goto nla_put_failure;
3473
3474 sp = nla_data(attr);
3475 dev_get_stats(dev, sp);
3476 }
3477
3478 nlmsg_end(skb, nlh);
3479
3480 return 0;
3481
3482nla_put_failure:
3483 nlmsg_cancel(skb, nlh);
3484
3485 return -EMSGSIZE;
3486}
3487
3488static const struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
3489 [IFLA_STATS_LINK_64] = { .len = sizeof(struct rtnl_link_stats64) },
3490};
3491
3492static size_t if_nlmsg_stats_size(const struct net_device *dev,
3493 u32 filter_mask)
3494{
3495 size_t size = 0;
3496
3497 if (filter_mask & IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_64))
3498 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3499
3500 return size;
3501}
3502
3503static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
3504{
3505 struct net *net = sock_net(skb->sk);
3506 struct if_stats_msg *ifsm;
3507 struct net_device *dev = NULL;
3508 struct sk_buff *nskb;
3509 u32 filter_mask;
3510 int err;
3511
3512 ifsm = nlmsg_data(nlh);
3513 if (ifsm->ifindex > 0)
3514 dev = __dev_get_by_index(net, ifsm->ifindex);
3515 else
3516 return -EINVAL;
3517
3518 if (!dev)
3519 return -ENODEV;
3520
3521 filter_mask = ifsm->filter_mask;
3522 if (!filter_mask)
3523 return -EINVAL;
3524
3525 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
3526 if (!nskb)
3527 return -ENOBUFS;
3528
3529 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
3530 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
3531 0, filter_mask);
3532 if (err < 0) {
3533 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3534 WARN_ON(err == -EMSGSIZE);
3535 kfree_skb(nskb);
3536 } else {
3537 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3538 }
3539
3540 return err;
3541}
3542
3543static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
3544{
3545 struct net *net = sock_net(skb->sk);
3546 struct if_stats_msg *ifsm;
3547 int h, s_h;
3548 int idx = 0, s_idx;
3549 struct net_device *dev;
3550 struct hlist_head *head;
3551 unsigned int flags = NLM_F_MULTI;
3552 u32 filter_mask = 0;
3553 int err;
3554
3555 s_h = cb->args[0];
3556 s_idx = cb->args[1];
3557
3558 cb->seq = net->dev_base_seq;
3559
3560 ifsm = nlmsg_data(cb->nlh);
3561 filter_mask = ifsm->filter_mask;
3562 if (!filter_mask)
3563 return -EINVAL;
3564
3565 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3566 idx = 0;
3567 head = &net->dev_index_head[h];
3568 hlist_for_each_entry(dev, head, index_hlist) {
3569 if (idx < s_idx)
3570 goto cont;
3571 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
3572 NETLINK_CB(cb->skb).portid,
3573 cb->nlh->nlmsg_seq, 0,
3574 flags, filter_mask);
3575 /* If we ran out of room on the first message,
3576 * we're in trouble
3577 */
3578 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
3579
3580 if (err < 0)
3581 goto out;
3582
3583 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3584cont:
3585 idx++;
3586 }
3587 }
3588out:
3589 cb->args[1] = idx;
3590 cb->args[0] = h;
3591
3592 return skb->len;
3593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595/* Process one rtnetlink message. */
3596
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003597static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003599 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003600 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06003601 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 int family;
3603 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003604 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003608 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 type -= RTM_BASE;
3611
3612 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003613 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return 0;
3615
Hong zhi guo573ce262013-03-27 06:47:04 +00003616 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 kind = type&3;
3618
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003619 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003620 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
David S. Millerb8f3ab42011-01-18 12:40:38 -08003622 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003623 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003624 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003625 rtnl_calcit_func calcit;
3626 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Thomas Grafe2849862007-03-22 11:48:11 -07003628 dumpit = rtnl_get_dumpit(family, type);
3629 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003630 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003631 calcit = rtnl_get_calcit(family, type);
3632 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003633 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Eric Dumazet2907c352011-05-25 07:34:04 +00003635 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003636 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003637 {
3638 struct netlink_dump_control c = {
3639 .dump = dumpit,
3640 .min_dump_alloc = min_dump_alloc,
3641 };
3642 err = netlink_dump_start(rtnl, skb, nlh, &c);
3643 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003644 rtnl_lock();
3645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647
Thomas Grafe2849862007-03-22 11:48:11 -07003648 doit = rtnl_get_doit(family, type);
3649 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003650 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Thomas Graf661d2962013-03-21 07:45:29 +00003652 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653}
3654
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003655static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003657 rtnl_lock();
3658 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3659 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3663{
Jiri Pirko351638e2013-05-28 01:30:21 +00003664 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 case NETDEV_UP:
3668 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003669 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003670 case NETDEV_POST_INIT:
3671 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003673 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003675 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003676 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003677 case NETDEV_RELEASE:
3678 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003679 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 break;
3681 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003682 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
3684 }
3685 return NOTIFY_DONE;
3686}
3687
3688static struct notifier_block rtnetlink_dev_notifier = {
3689 .notifier_call = rtnetlink_event,
3690};
3691
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003692
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003693static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003694{
3695 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003696 struct netlink_kernel_cfg cfg = {
3697 .groups = RTNLGRP_MAX,
3698 .input = rtnetlink_rcv,
3699 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003700 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003701 };
3702
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003703 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003704 if (!sk)
3705 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003706 net->rtnl = sk;
3707 return 0;
3708}
3709
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003710static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003711{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003712 netlink_kernel_release(net->rtnl);
3713 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003714}
3715
3716static struct pernet_operations rtnetlink_net_ops = {
3717 .init = rtnetlink_net_init,
3718 .exit = rtnetlink_net_exit,
3719};
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721void __init rtnetlink_init(void)
3722{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003723 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003727
Greg Rosec7ac8672011-06-10 01:27:09 +00003728 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3729 rtnl_dump_ifinfo, rtnl_calcit);
3730 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3731 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3732 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003733
Greg Rosec7ac8672011-06-10 01:27:09 +00003734 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3735 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003736
3737 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3738 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3739 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003740
3741 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003742 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003743 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07003744
3745 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
3746 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}