blob: d3694a13c85aafde74bd2c0616e6668fe3dc7c41 [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 */
828 nla_total_size(sizeof(__u64)) +
829 /* IFLA_VF_STATS_TX_PACKETS */
830 nla_total_size(sizeof(__u64)) +
831 /* IFLA_VF_STATS_RX_BYTES */
832 nla_total_size(sizeof(__u64)) +
833 /* IFLA_VF_STATS_TX_BYTES */
834 nla_total_size(sizeof(__u64)) +
835 /* IFLA_VF_STATS_BROADCAST */
836 nla_total_size(sizeof(__u64)) +
837 /* IFLA_VF_STATS_MULTICAST */
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000838 nla_total_size(sizeof(__u64)) +
839 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 */
879 + nla_total_size(sizeof(struct rtnl_link_ifmap))
880 + 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;
David S. Miller35c58452016-04-19 19:49:29 -04001054 int err;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001055
David S. Miller35c58452016-04-19 19:49:29 -04001056 err = nla_align_64bit(skb, IFLA_PAD);
1057 if (err)
1058 return err;
David S. Miller18402842016-04-19 14:30:10 -04001059
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001060 attr = nla_reserve(skb, IFLA_STATS64,
1061 sizeof(struct rtnl_link_stats64));
1062 if (!attr)
1063 return -EMSGSIZE;
1064
Roopa Prabhu550bce52016-04-15 20:36:25 -07001065 sp = nla_data(attr);
1066 dev_get_stats(dev, sp);
1067
1068 attr = nla_reserve(skb, IFLA_STATS,
1069 sizeof(struct rtnl_link_stats));
1070 if (!attr)
1071 return -EMSGSIZE;
1072
1073 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001074
1075 return 0;
1076}
1077
1078static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1079 struct net_device *dev,
1080 int vfs_num,
1081 struct nlattr *vfinfo)
1082{
1083 struct ifla_vf_rss_query_en vf_rss_query_en;
1084 struct ifla_vf_link_state vf_linkstate;
1085 struct ifla_vf_spoofchk vf_spoofchk;
1086 struct ifla_vf_tx_rate vf_tx_rate;
1087 struct ifla_vf_stats vf_stats;
1088 struct ifla_vf_trust vf_trust;
1089 struct ifla_vf_vlan vf_vlan;
1090 struct ifla_vf_rate vf_rate;
1091 struct nlattr *vf, *vfstats;
1092 struct ifla_vf_mac vf_mac;
1093 struct ifla_vf_info ivi;
1094
1095 /* Not all SR-IOV capable drivers support the
1096 * spoofcheck and "RSS query enable" query. Preset to
1097 * -1 so the user space tool can detect that the driver
1098 * didn't report anything.
1099 */
1100 ivi.spoofchk = -1;
1101 ivi.rss_query_en = -1;
1102 ivi.trusted = -1;
1103 memset(ivi.mac, 0, sizeof(ivi.mac));
1104 /* The default value for VF link state is "auto"
1105 * IFLA_VF_LINK_STATE_AUTO which equals zero
1106 */
1107 ivi.linkstate = 0;
1108 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1109 return 0;
1110
1111 vf_mac.vf =
1112 vf_vlan.vf =
1113 vf_rate.vf =
1114 vf_tx_rate.vf =
1115 vf_spoofchk.vf =
1116 vf_linkstate.vf =
1117 vf_rss_query_en.vf =
1118 vf_trust.vf = ivi.vf;
1119
1120 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1121 vf_vlan.vlan = ivi.vlan;
1122 vf_vlan.qos = ivi.qos;
1123 vf_tx_rate.rate = ivi.max_tx_rate;
1124 vf_rate.min_tx_rate = ivi.min_tx_rate;
1125 vf_rate.max_tx_rate = ivi.max_tx_rate;
1126 vf_spoofchk.setting = ivi.spoofchk;
1127 vf_linkstate.link_state = ivi.linkstate;
1128 vf_rss_query_en.setting = ivi.rss_query_en;
1129 vf_trust.setting = ivi.trusted;
1130 vf = nla_nest_start(skb, IFLA_VF_INFO);
1131 if (!vf) {
1132 nla_nest_cancel(skb, vfinfo);
1133 return -EMSGSIZE;
1134 }
1135 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1136 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1137 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1138 &vf_rate) ||
1139 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1140 &vf_tx_rate) ||
1141 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1142 &vf_spoofchk) ||
1143 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1144 &vf_linkstate) ||
1145 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1146 sizeof(vf_rss_query_en),
1147 &vf_rss_query_en) ||
1148 nla_put(skb, IFLA_VF_TRUST,
1149 sizeof(vf_trust), &vf_trust))
1150 return -EMSGSIZE;
1151 memset(&vf_stats, 0, sizeof(vf_stats));
1152 if (dev->netdev_ops->ndo_get_vf_stats)
1153 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1154 &vf_stats);
1155 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1156 if (!vfstats) {
1157 nla_nest_cancel(skb, vf);
1158 nla_nest_cancel(skb, vfinfo);
1159 return -EMSGSIZE;
1160 }
1161 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1162 vf_stats.rx_packets) ||
1163 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1164 vf_stats.tx_packets) ||
1165 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1166 vf_stats.rx_bytes) ||
1167 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1168 vf_stats.tx_bytes) ||
1169 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1170 vf_stats.broadcast) ||
1171 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1172 vf_stats.multicast))
1173 return -EMSGSIZE;
1174 nla_nest_end(skb, vfstats);
1175 nla_nest_end(skb, vf);
1176 return 0;
1177}
1178
1179static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1180{
1181 struct rtnl_link_ifmap map = {
1182 .mem_start = dev->mem_start,
1183 .mem_end = dev->mem_end,
1184 .base_addr = dev->base_addr,
1185 .irq = dev->irq,
1186 .dma = dev->dma,
1187 .port = dev->if_port,
1188 };
1189 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1190 return -EMSGSIZE;
1191
1192 return 0;
1193}
1194
Thomas Grafb60c5112006-08-04 23:05:34 -07001195static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001196 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001197 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001198{
1199 struct ifinfomsg *ifm;
1200 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001201 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001202 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001203 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001204
Eric Dumazet2907c352011-05-25 07:34:04 +00001205 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001206 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1207 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001208 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001209
1210 ifm = nlmsg_data(nlh);
1211 ifm->ifi_family = AF_UNSPEC;
1212 ifm->__ifi_pad = 0;
1213 ifm->ifi_type = dev->type;
1214 ifm->ifi_index = dev->ifindex;
1215 ifm->ifi_flags = dev_get_flags(dev);
1216 ifm->ifi_change = change;
1217
David S. Millera6574342012-04-01 20:12:00 -04001218 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1219 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1220 nla_put_u8(skb, IFLA_OPERSTATE,
1221 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1222 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1223 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1224 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001225 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001226 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001227 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1228 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001229#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001230 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001231#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001232 (dev->ifindex != dev_get_iflink(dev) &&
1233 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001234 (upper_dev &&
1235 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001236 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001237 (dev->qdisc &&
1238 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1239 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001240 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1241 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001242 atomic_read(&dev->carrier_changes)) ||
1243 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001244 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001245
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001246 if (rtnl_fill_link_ifmap(skb, dev))
1247 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001250 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1251 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1252 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001255 if (rtnl_phys_port_id_fill(skb, dev))
1256 goto nla_put_failure;
1257
David Aherndb24a902015-03-17 20:23:15 -06001258 if (rtnl_phys_port_name_fill(skb, dev))
1259 goto nla_put_failure;
1260
Jiri Pirko82f28412014-11-28 14:34:18 +01001261 if (rtnl_phys_switch_id_fill(skb, dev))
1262 goto nla_put_failure;
1263
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001264 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001265 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
David S. Millera6574342012-04-01 20:12:00 -04001267 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1268 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1269 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001270
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001271 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1272 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001273 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001274 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001275 int num_vfs = dev_num_vf(dev->dev.parent);
1276
Chris Wrightc02db8c2010-05-16 01:05:45 -07001277 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1278 if (!vfinfo)
1279 goto nla_put_failure;
1280 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001281 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001282 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001283 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001284
Chris Wrightc02db8c2010-05-16 01:05:45 -07001285 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001286 }
Scott Feldman57b61082010-05-17 22:49:55 -07001287
David Gibsonc53864f2014-04-24 10:22:36 +10001288 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001289 goto nla_put_failure;
1290
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001291 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001292 if (rtnl_link_fill(skb, dev) < 0)
1293 goto nla_put_failure;
1294 }
1295
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001296 if (dev->rtnl_link_ops &&
1297 dev->rtnl_link_ops->get_link_net) {
1298 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1299
1300 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001301 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001302
1303 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1304 goto nla_put_failure;
1305 }
1306 }
1307
Thomas Graff8ff1822010-11-16 04:30:14 +00001308 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1309 goto nla_put_failure;
1310
1311 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1312 if (af_ops->fill_link_af) {
1313 struct nlattr *af;
1314 int err;
1315
1316 if (!(af = nla_nest_start(skb, af_ops->family)))
1317 goto nla_put_failure;
1318
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001319 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001320
1321 /*
1322 * Caller may return ENODATA to indicate that there
1323 * was no data to be dumped. This is not an error, it
1324 * means we should trim the attribute header and
1325 * continue.
1326 */
1327 if (err == -ENODATA)
1328 nla_nest_cancel(skb, af);
1329 else if (err < 0)
1330 goto nla_put_failure;
1331
1332 nla_nest_end(skb, af);
1333 }
1334 }
1335
1336 nla_nest_end(skb, af_spec);
1337
Johannes Berg053c0952015-01-16 22:09:00 +01001338 nlmsg_end(skb, nlh);
1339 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001340
1341nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001342 nlmsg_cancel(skb, nlh);
1343 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Jiri Pirkof7b12602014-02-18 20:53:18 +01001346static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1347 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1348 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1349 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1350 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1351 [IFLA_MTU] = { .type = NLA_U32 },
1352 [IFLA_LINK] = { .type = NLA_U32 },
1353 [IFLA_MASTER] = { .type = NLA_U32 },
1354 [IFLA_CARRIER] = { .type = NLA_U8 },
1355 [IFLA_TXQLEN] = { .type = NLA_U32 },
1356 [IFLA_WEIGHT] = { .type = NLA_U32 },
1357 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1358 [IFLA_LINKMODE] = { .type = NLA_U8 },
1359 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1360 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1361 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1362 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1363 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1364 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1365 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1366 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1367 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1368 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1369 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1370 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001371 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001372 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001373 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001374 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001375 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001376};
1377
1378static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1379 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1380 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1381 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1382 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1383};
1384
Jiri Pirkof7b12602014-02-18 20:53:18 +01001385static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001386 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1387 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1388 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1389 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1390 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1391 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001392 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001393 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001394 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001395 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1396 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001397};
1398
Jiri Pirkof7b12602014-02-18 20:53:18 +01001399static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1400 [IFLA_PORT_VF] = { .type = NLA_U32 },
1401 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1402 .len = PORT_PROFILE_MAX },
1403 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1404 .len = sizeof(struct ifla_port_vsi)},
1405 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1406 .len = PORT_UUID_MAX },
1407 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1408 .len = PORT_UUID_MAX },
1409 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1410 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1411};
1412
David Aherndc599f72016-02-02 08:17:07 -08001413static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1414{
1415 const struct rtnl_link_ops *ops = NULL;
1416 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1417
1418 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1419 return NULL;
1420
1421 if (linfo[IFLA_INFO_KIND]) {
1422 char kind[MODULE_NAME_LEN];
1423
1424 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1425 ops = rtnl_link_ops_get(kind);
1426 }
1427
1428 return ops;
1429}
1430
1431static bool link_master_filtered(struct net_device *dev, int master_idx)
1432{
1433 struct net_device *master;
1434
1435 if (!master_idx)
1436 return false;
1437
1438 master = netdev_master_upper_dev_get(dev);
1439 if (!master || master->ifindex != master_idx)
1440 return true;
1441
1442 return false;
1443}
1444
1445static bool link_kind_filtered(const struct net_device *dev,
1446 const struct rtnl_link_ops *kind_ops)
1447{
1448 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1449 return true;
1450
1451 return false;
1452}
1453
1454static bool link_dump_filtered(struct net_device *dev,
1455 int master_idx,
1456 const struct rtnl_link_ops *kind_ops)
1457{
1458 if (link_master_filtered(dev, master_idx) ||
1459 link_kind_filtered(dev, kind_ops))
1460 return true;
1461
1462 return false;
1463}
1464
Thomas Grafb60c5112006-08-04 23:05:34 -07001465static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001467 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001468 int h, s_h;
1469 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001471 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001472 struct nlattr *tb[IFLA_MAX+1];
1473 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001474 const struct rtnl_link_ops *kind_ops = NULL;
1475 unsigned int flags = NLM_F_MULTI;
1476 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001477 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001478 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001480 s_h = cb->args[0];
1481 s_idx = cb->args[1];
1482
Thomas Graf4e985ad2011-06-21 03:11:20 +00001483 cb->seq = net->dev_base_seq;
1484
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001485 /* A hack to preserve kernel<->userspace interface.
1486 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1487 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1488 * what iproute2 < v3.9.0 used.
1489 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1490 * attribute, its netlink message is shorter than struct ifinfomsg.
1491 */
1492 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1493 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1494
1495 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001496
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001497 if (tb[IFLA_EXT_MASK])
1498 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001499
1500 if (tb[IFLA_MASTER])
1501 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1502
1503 if (tb[IFLA_LINKINFO])
1504 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1505
1506 if (master_idx || kind_ops)
1507 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001508 }
Greg Rose115c9b82012-02-21 16:54:48 -05001509
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001510 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1511 idx = 0;
1512 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001513 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001514 if (link_dump_filtered(dev, master_idx, kind_ops))
1515 continue;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001516 if (idx < s_idx)
1517 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001518 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1519 NETLINK_CB(cb->skb).portid,
1520 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001521 flags,
David Gibson973462b2014-04-24 10:22:35 +10001522 ext_filter_mask);
1523 /* If we ran out of room on the first message,
1524 * we're in trouble
1525 */
1526 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1527
David S. Miller7b46a642015-01-18 23:36:08 -05001528 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001529 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001530
1531 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001532cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001533 idx++;
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001536out:
1537 cb->args[1] = idx;
1538 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 return skb->len;
1541}
1542
Jiri Pirkof7b12602014-02-18 20:53:18 +01001543int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1544{
1545 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1546}
1547EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001548
Eric W. Biederman81adee42009-11-08 00:53:51 -08001549struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1550{
1551 struct net *net;
1552 /* Examine the link attributes and figure out which
1553 * network namespace we are talking about.
1554 */
1555 if (tb[IFLA_NET_NS_PID])
1556 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001557 else if (tb[IFLA_NET_NS_FD])
1558 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001559 else
1560 net = get_net(src_net);
1561 return net;
1562}
1563EXPORT_SYMBOL(rtnl_link_get_net);
1564
Thomas Graf1840bb12008-02-23 19:54:36 -08001565static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1566{
1567 if (dev) {
1568 if (tb[IFLA_ADDRESS] &&
1569 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1570 return -EINVAL;
1571
1572 if (tb[IFLA_BROADCAST] &&
1573 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1574 return -EINVAL;
1575 }
1576
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001577 if (tb[IFLA_AF_SPEC]) {
1578 struct nlattr *af;
1579 int rem, err;
1580
1581 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1582 const struct rtnl_af_ops *af_ops;
1583
1584 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1585 return -EAFNOSUPPORT;
1586
1587 if (!af_ops->set_link_af)
1588 return -EOPNOTSUPP;
1589
1590 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001591 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001592 if (err < 0)
1593 return err;
1594 }
1595 }
1596 }
1597
Thomas Graf1840bb12008-02-23 19:54:36 -08001598 return 0;
1599}
1600
Eli Cohencc8e27c2016-03-11 22:58:34 +02001601static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1602 int guid_type)
1603{
1604 const struct net_device_ops *ops = dev->netdev_ops;
1605
1606 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1607}
1608
1609static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1610{
1611 if (dev->type != ARPHRD_INFINIBAND)
1612 return -EOPNOTSUPP;
1613
1614 return handle_infiniband_guid(dev, ivt, guid_type);
1615}
1616
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001617static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001618{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001619 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001620 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001621
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001622 if (tb[IFLA_VF_MAC]) {
1623 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001624
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001625 err = -EOPNOTSUPP;
1626 if (ops->ndo_set_vf_mac)
1627 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1628 ivm->mac);
1629 if (err < 0)
1630 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001631 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001632
1633 if (tb[IFLA_VF_VLAN]) {
1634 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1635
1636 err = -EOPNOTSUPP;
1637 if (ops->ndo_set_vf_vlan)
1638 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1639 ivv->qos);
1640 if (err < 0)
1641 return err;
1642 }
1643
1644 if (tb[IFLA_VF_TX_RATE]) {
1645 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1646 struct ifla_vf_info ivf;
1647
1648 err = -EOPNOTSUPP;
1649 if (ops->ndo_get_vf_config)
1650 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1651 if (err < 0)
1652 return err;
1653
1654 err = -EOPNOTSUPP;
1655 if (ops->ndo_set_vf_rate)
1656 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1657 ivf.min_tx_rate,
1658 ivt->rate);
1659 if (err < 0)
1660 return err;
1661 }
1662
1663 if (tb[IFLA_VF_RATE]) {
1664 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1665
1666 err = -EOPNOTSUPP;
1667 if (ops->ndo_set_vf_rate)
1668 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1669 ivt->min_tx_rate,
1670 ivt->max_tx_rate);
1671 if (err < 0)
1672 return err;
1673 }
1674
1675 if (tb[IFLA_VF_SPOOFCHK]) {
1676 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1677
1678 err = -EOPNOTSUPP;
1679 if (ops->ndo_set_vf_spoofchk)
1680 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1681 ivs->setting);
1682 if (err < 0)
1683 return err;
1684 }
1685
1686 if (tb[IFLA_VF_LINK_STATE]) {
1687 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1688
1689 err = -EOPNOTSUPP;
1690 if (ops->ndo_set_vf_link_state)
1691 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1692 ivl->link_state);
1693 if (err < 0)
1694 return err;
1695 }
1696
1697 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1698 struct ifla_vf_rss_query_en *ivrssq_en;
1699
1700 err = -EOPNOTSUPP;
1701 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1702 if (ops->ndo_set_vf_rss_query_en)
1703 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1704 ivrssq_en->setting);
1705 if (err < 0)
1706 return err;
1707 }
1708
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001709 if (tb[IFLA_VF_TRUST]) {
1710 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1711
1712 err = -EOPNOTSUPP;
1713 if (ops->ndo_set_vf_trust)
1714 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1715 if (err < 0)
1716 return err;
1717 }
1718
Eli Cohencc8e27c2016-03-11 22:58:34 +02001719 if (tb[IFLA_VF_IB_NODE_GUID]) {
1720 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1721
1722 if (!ops->ndo_set_vf_guid)
1723 return -EOPNOTSUPP;
1724
1725 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1726 }
1727
1728 if (tb[IFLA_VF_IB_PORT_GUID]) {
1729 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1730
1731 if (!ops->ndo_set_vf_guid)
1732 return -EOPNOTSUPP;
1733
1734 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1735 }
1736
Chris Wrightc02db8c2010-05-16 01:05:45 -07001737 return err;
1738}
1739
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001740static int do_set_master(struct net_device *dev, int ifindex)
1741{
Jiri Pirko898e5062013-01-03 22:48:52 +00001742 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001743 const struct net_device_ops *ops;
1744 int err;
1745
Jiri Pirko898e5062013-01-03 22:48:52 +00001746 if (upper_dev) {
1747 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001748 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001749 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001750 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001751 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001752 if (err)
1753 return err;
1754 } else {
1755 return -EOPNOTSUPP;
1756 }
1757 }
1758
1759 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001760 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1761 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001762 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001763 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001764 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001765 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001766 if (err)
1767 return err;
1768 } else {
1769 return -EOPNOTSUPP;
1770 }
1771 }
1772 return 0;
1773}
1774
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001775#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001776/* notify flag means notify + modified. */
1777#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001778static int do_setlink(const struct sk_buff *skb,
1779 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001780 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001781{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001782 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001783 int err;
1784
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001785 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001786 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001787 if (IS_ERR(net)) {
1788 err = PTR_ERR(net);
1789 goto errout;
1790 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001791 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001792 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001793 err = -EPERM;
1794 goto errout;
1795 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001796 err = dev_change_net_namespace(dev, net, ifname);
1797 put_net(net);
1798 if (err)
1799 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001800 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001801 }
1802
Patrick McHardy0157f602007-06-13 12:03:36 -07001803 if (tb[IFLA_MAP]) {
1804 struct rtnl_link_ifmap *u_map;
1805 struct ifmap k_map;
1806
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001807 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001808 err = -EOPNOTSUPP;
1809 goto errout;
1810 }
1811
1812 if (!netif_device_present(dev)) {
1813 err = -ENODEV;
1814 goto errout;
1815 }
1816
1817 u_map = nla_data(tb[IFLA_MAP]);
1818 k_map.mem_start = (unsigned long) u_map->mem_start;
1819 k_map.mem_end = (unsigned long) u_map->mem_end;
1820 k_map.base_addr = (unsigned short) u_map->base_addr;
1821 k_map.irq = (unsigned char) u_map->irq;
1822 k_map.dma = (unsigned char) u_map->dma;
1823 k_map.port = (unsigned char) u_map->port;
1824
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001825 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001826 if (err < 0)
1827 goto errout;
1828
Nicolas Dichtelba998902014-09-01 16:07:29 +02001829 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001830 }
1831
1832 if (tb[IFLA_ADDRESS]) {
1833 struct sockaddr *sa;
1834 int len;
1835
Patrick McHardy0157f602007-06-13 12:03:36 -07001836 len = sizeof(sa_family_t) + dev->addr_len;
1837 sa = kmalloc(len, GFP_KERNEL);
1838 if (!sa) {
1839 err = -ENOMEM;
1840 goto errout;
1841 }
1842 sa->sa_family = dev->type;
1843 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1844 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001845 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001846 kfree(sa);
1847 if (err)
1848 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001849 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001850 }
1851
1852 if (tb[IFLA_MTU]) {
1853 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1854 if (err < 0)
1855 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001856 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001857 }
1858
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001859 if (tb[IFLA_GROUP]) {
1860 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001861 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001862 }
1863
Patrick McHardy0157f602007-06-13 12:03:36 -07001864 /*
1865 * Interface selected by interface index but interface
1866 * name provided implies that a name change has been
1867 * requested.
1868 */
1869 if (ifm->ifi_index > 0 && ifname[0]) {
1870 err = dev_change_name(dev, ifname);
1871 if (err < 0)
1872 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001873 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001874 }
1875
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001876 if (tb[IFLA_IFALIAS]) {
1877 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1878 nla_len(tb[IFLA_IFALIAS]));
1879 if (err < 0)
1880 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001881 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001882 }
1883
Patrick McHardy0157f602007-06-13 12:03:36 -07001884 if (tb[IFLA_BROADCAST]) {
1885 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001886 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001887 }
1888
1889 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001890 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001891 if (err < 0)
1892 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001893 }
1894
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001895 if (tb[IFLA_MASTER]) {
1896 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1897 if (err)
1898 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001899 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001900 }
1901
Jiri Pirko9a572472012-12-27 23:49:39 +00001902 if (tb[IFLA_CARRIER]) {
1903 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1904 if (err)
1905 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001906 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001907 }
1908
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001909 if (tb[IFLA_TXQLEN]) {
1910 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1911
1912 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001913 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001914
1915 dev->tx_queue_len = value;
1916 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001917
Patrick McHardy0157f602007-06-13 12:03:36 -07001918 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001919 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001920
1921 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001922 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1923
David S. Miller93b2d4a2008-02-17 18:35:07 -08001924 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001925 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001926 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001927 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001928 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001929 }
1930
Chris Wrightc02db8c2010-05-16 01:05:45 -07001931 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001932 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001933 struct nlattr *attr;
1934 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001935
Chris Wrightc02db8c2010-05-16 01:05:45 -07001936 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001937 if (nla_type(attr) != IFLA_VF_INFO ||
1938 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001939 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001940 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001941 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001942 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1943 ifla_vf_policy);
1944 if (err < 0)
1945 goto errout;
1946 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001947 if (err < 0)
1948 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001949 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001950 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001951 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001952 err = 0;
1953
Scott Feldman57b61082010-05-17 22:49:55 -07001954 if (tb[IFLA_VF_PORTS]) {
1955 struct nlattr *port[IFLA_PORT_MAX+1];
1956 struct nlattr *attr;
1957 int vf;
1958 int rem;
1959
1960 err = -EOPNOTSUPP;
1961 if (!ops->ndo_set_vf_port)
1962 goto errout;
1963
1964 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001965 if (nla_type(attr) != IFLA_VF_PORT ||
1966 nla_len(attr) < NLA_HDRLEN) {
1967 err = -EINVAL;
1968 goto errout;
1969 }
1970 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1971 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001972 if (err < 0)
1973 goto errout;
1974 if (!port[IFLA_PORT_VF]) {
1975 err = -EOPNOTSUPP;
1976 goto errout;
1977 }
1978 vf = nla_get_u32(port[IFLA_PORT_VF]);
1979 err = ops->ndo_set_vf_port(dev, vf, port);
1980 if (err < 0)
1981 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001982 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001983 }
1984 }
1985 err = 0;
1986
1987 if (tb[IFLA_PORT_SELF]) {
1988 struct nlattr *port[IFLA_PORT_MAX+1];
1989
1990 err = nla_parse_nested(port, IFLA_PORT_MAX,
1991 tb[IFLA_PORT_SELF], ifla_port_policy);
1992 if (err < 0)
1993 goto errout;
1994
1995 err = -EOPNOTSUPP;
1996 if (ops->ndo_set_vf_port)
1997 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1998 if (err < 0)
1999 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002000 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002001 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002002
2003 if (tb[IFLA_AF_SPEC]) {
2004 struct nlattr *af;
2005 int rem;
2006
2007 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2008 const struct rtnl_af_ops *af_ops;
2009
2010 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002011 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002012
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002013 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002014 if (err < 0)
2015 goto errout;
2016
Nicolas Dichtelba998902014-09-01 16:07:29 +02002017 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002018 }
2019 }
Scott Feldman57b61082010-05-17 22:49:55 -07002020 err = 0;
2021
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002022 if (tb[IFLA_PROTO_DOWN]) {
2023 err = dev_change_proto_down(dev,
2024 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2025 if (err)
2026 goto errout;
2027 status |= DO_SETLINK_NOTIFY;
2028 }
2029
Patrick McHardy0157f602007-06-13 12:03:36 -07002030errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002031 if (status & DO_SETLINK_MODIFIED) {
2032 if (status & DO_SETLINK_NOTIFY)
2033 netdev_state_change(dev);
2034
2035 if (err < 0)
2036 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",
2037 dev->name);
2038 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002039
Patrick McHardy0157f602007-06-13 12:03:36 -07002040 return err;
2041}
2042
Thomas Graf661d2962013-03-21 07:45:29 +00002043static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002045 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002046 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002048 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002049 struct nlattr *tb[IFLA_MAX+1];
2050 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Thomas Grafda5e0492006-08-10 21:17:37 -07002052 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2053 if (err < 0)
2054 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Thomas Graf5176f912006-08-26 20:13:18 -07002056 if (tb[IFLA_IFNAME])
2057 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002058 else
2059 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002062 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002063 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002064 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002065 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002066 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002067 else
2068 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Thomas Grafda5e0492006-08-10 21:17:37 -07002070 if (dev == NULL) {
2071 err = -ENODEV;
2072 goto errout;
2073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Eric Dumazete0d087a2009-11-07 01:26:17 -08002075 err = validate_linkmsg(dev, tb);
2076 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002077 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002078
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002079 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002080errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return err;
2082}
2083
WANG Cong66400d52015-03-24 11:53:31 -07002084static int rtnl_group_dellink(const struct net *net, int group)
2085{
2086 struct net_device *dev, *aux;
2087 LIST_HEAD(list_kill);
2088 bool found = false;
2089
2090 if (!group)
2091 return -EPERM;
2092
2093 for_each_netdev(net, dev) {
2094 if (dev->group == group) {
2095 const struct rtnl_link_ops *ops;
2096
2097 found = true;
2098 ops = dev->rtnl_link_ops;
2099 if (!ops || !ops->dellink)
2100 return -EOPNOTSUPP;
2101 }
2102 }
2103
2104 if (!found)
2105 return -ENODEV;
2106
2107 for_each_netdev_safe(net, dev, aux) {
2108 if (dev->group == group) {
2109 const struct rtnl_link_ops *ops;
2110
2111 ops = dev->rtnl_link_ops;
2112 ops->dellink(dev, &list_kill);
2113 }
2114 }
2115 unregister_netdevice_many(&list_kill);
2116
2117 return 0;
2118}
2119
Thomas Graf614732e2015-07-21 10:44:06 +02002120int rtnl_delete_link(struct net_device *dev)
2121{
2122 const struct rtnl_link_ops *ops;
2123 LIST_HEAD(list_kill);
2124
2125 ops = dev->rtnl_link_ops;
2126 if (!ops || !ops->dellink)
2127 return -EOPNOTSUPP;
2128
2129 ops->dellink(dev, &list_kill);
2130 unregister_netdevice_many(&list_kill);
2131
2132 return 0;
2133}
2134EXPORT_SYMBOL_GPL(rtnl_delete_link);
2135
Thomas Graf661d2962013-03-21 07:45:29 +00002136static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002137{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002138 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002139 struct net_device *dev;
2140 struct ifinfomsg *ifm;
2141 char ifname[IFNAMSIZ];
2142 struct nlattr *tb[IFLA_MAX+1];
2143 int err;
2144
2145 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2146 if (err < 0)
2147 return err;
2148
2149 if (tb[IFLA_IFNAME])
2150 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2151
2152 ifm = nlmsg_data(nlh);
2153 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002154 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002155 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002156 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002157 else if (tb[IFLA_GROUP])
2158 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002159 else
2160 return -EINVAL;
2161
2162 if (!dev)
2163 return -ENODEV;
2164
Thomas Graf614732e2015-07-21 10:44:06 +02002165 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002166}
2167
Patrick McHardy3729d502010-02-26 06:34:54 +00002168int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2169{
2170 unsigned int old_flags;
2171 int err;
2172
2173 old_flags = dev->flags;
2174 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2175 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2176 if (err < 0)
2177 return err;
2178 }
2179
2180 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002181
Nicolas Dichtela528c212013-09-25 12:02:44 +02002182 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002183 return 0;
2184}
2185EXPORT_SYMBOL(rtnl_configure_link);
2186
Rami Rosenc0713562012-11-30 01:08:47 +00002187struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002188 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002189 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002190{
2191 int err;
2192 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002193 unsigned int num_tx_queues = 1;
2194 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002195
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002196 if (tb[IFLA_NUM_TX_QUEUES])
2197 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2198 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002199 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002200
2201 if (tb[IFLA_NUM_RX_QUEUES])
2202 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2203 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002204 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002205
Pavel Emelianove7199282007-08-08 22:16:38 -07002206 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002207 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002208 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002209 if (!dev)
2210 goto err;
2211
Eric W. Biederman81adee42009-11-08 00:53:51 -08002212 dev_net_set(dev, net);
2213 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002214 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002215
Pavel Emelianove7199282007-08-08 22:16:38 -07002216 if (tb[IFLA_MTU])
2217 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002218 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002219 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2220 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002221 dev->addr_assign_type = NET_ADDR_SET;
2222 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002223 if (tb[IFLA_BROADCAST])
2224 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2225 nla_len(tb[IFLA_BROADCAST]));
2226 if (tb[IFLA_TXQLEN])
2227 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2228 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002229 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002230 if (tb[IFLA_LINKMODE])
2231 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002232 if (tb[IFLA_GROUP])
2233 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002234
2235 return dev;
2236
Pavel Emelianove7199282007-08-08 22:16:38 -07002237err:
2238 return ERR_PTR(err);
2239}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002240EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002241
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002242static int rtnl_group_changelink(const struct sk_buff *skb,
2243 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002244 struct ifinfomsg *ifm,
2245 struct nlattr **tb)
2246{
WANG Congd0795352015-03-23 16:31:09 -07002247 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002248 int err;
2249
WANG Congd0795352015-03-23 16:31:09 -07002250 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002251 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002252 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002253 if (err < 0)
2254 return err;
2255 }
2256 }
2257
2258 return 0;
2259}
2260
Thomas Graf661d2962013-03-21 07:45:29 +00002261static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002262{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002263 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002264 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002265 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002266 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002267 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002268 struct ifinfomsg *ifm;
2269 char kind[MODULE_NAME_LEN];
2270 char ifname[IFNAMSIZ];
2271 struct nlattr *tb[IFLA_MAX+1];
2272 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002273 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002274 int err;
2275
Johannes Berg95a5afc2008-10-16 15:24:51 -07002276#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002277replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002278#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002279 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2280 if (err < 0)
2281 return err;
2282
2283 if (tb[IFLA_IFNAME])
2284 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2285 else
2286 ifname[0] = '\0';
2287
2288 ifm = nlmsg_data(nlh);
2289 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002290 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002291 else {
2292 if (ifname[0])
2293 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002294 else
2295 dev = NULL;
2296 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002297
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002298 if (dev) {
2299 master_dev = netdev_master_upper_dev_get(dev);
2300 if (master_dev)
2301 m_ops = master_dev->rtnl_link_ops;
2302 }
2303
Eric Dumazete0d087a2009-11-07 01:26:17 -08002304 err = validate_linkmsg(dev, tb);
2305 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002306 return err;
2307
Patrick McHardy38f7b872007-06-13 12:03:51 -07002308 if (tb[IFLA_LINKINFO]) {
2309 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2310 tb[IFLA_LINKINFO], ifla_info_policy);
2311 if (err < 0)
2312 return err;
2313 } else
2314 memset(linkinfo, 0, sizeof(linkinfo));
2315
2316 if (linkinfo[IFLA_INFO_KIND]) {
2317 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2318 ops = rtnl_link_ops_get(kind);
2319 } else {
2320 kind[0] = '\0';
2321 ops = NULL;
2322 }
2323
2324 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002325 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2326 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002327 struct nlattr **data = NULL;
2328 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002329 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002330
2331 if (ops) {
2332 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2333 err = nla_parse_nested(attr, ops->maxtype,
2334 linkinfo[IFLA_INFO_DATA],
2335 ops->policy);
2336 if (err < 0)
2337 return err;
2338 data = attr;
2339 }
2340 if (ops->validate) {
2341 err = ops->validate(tb, data);
2342 if (err < 0)
2343 return err;
2344 }
2345 }
2346
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002347 if (m_ops) {
2348 if (m_ops->slave_maxtype &&
2349 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2350 err = nla_parse_nested(slave_attr,
2351 m_ops->slave_maxtype,
2352 linkinfo[IFLA_INFO_SLAVE_DATA],
2353 m_ops->slave_policy);
2354 if (err < 0)
2355 return err;
2356 slave_data = slave_attr;
2357 }
2358 if (m_ops->slave_validate) {
2359 err = m_ops->slave_validate(tb, slave_data);
2360 if (err < 0)
2361 return err;
2362 }
2363 }
2364
Patrick McHardy38f7b872007-06-13 12:03:51 -07002365 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002366 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002367
2368 if (nlh->nlmsg_flags & NLM_F_EXCL)
2369 return -EEXIST;
2370 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2371 return -EOPNOTSUPP;
2372
2373 if (linkinfo[IFLA_INFO_DATA]) {
2374 if (!ops || ops != dev->rtnl_link_ops ||
2375 !ops->changelink)
2376 return -EOPNOTSUPP;
2377
2378 err = ops->changelink(dev, tb, data);
2379 if (err < 0)
2380 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002381 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002382 }
2383
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002384 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2385 if (!m_ops || !m_ops->slave_changelink)
2386 return -EOPNOTSUPP;
2387
2388 err = m_ops->slave_changelink(master_dev, dev,
2389 tb, slave_data);
2390 if (err < 0)
2391 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002392 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002393 }
2394
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002395 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002396 }
2397
Patrick McHardyffa934f2011-01-20 03:00:42 +00002398 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2399 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002400 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002401 nla_get_u32(tb[IFLA_GROUP]),
2402 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002403 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002404 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002405
Patrick McHardy0e068772007-07-11 19:42:31 -07002406 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002407 return -EOPNOTSUPP;
2408
2409 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002410#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002411 if (kind[0]) {
2412 __rtnl_unlock();
2413 request_module("rtnl-link-%s", kind);
2414 rtnl_lock();
2415 ops = rtnl_link_ops_get(kind);
2416 if (ops)
2417 goto replay;
2418 }
2419#endif
2420 return -EOPNOTSUPP;
2421 }
2422
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002423 if (!ops->setup)
2424 return -EOPNOTSUPP;
2425
Tom Gundersen55177502014-07-14 16:37:25 +02002426 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002427 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002428 name_assign_type = NET_NAME_ENUM;
2429 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002430
Eric W. Biederman81adee42009-11-08 00:53:51 -08002431 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002432 if (IS_ERR(dest_net))
2433 return PTR_ERR(dest_net);
2434
Eric W. Biederman505ce412015-02-26 16:19:00 -06002435 err = -EPERM;
2436 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2437 goto out;
2438
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002439 if (tb[IFLA_LINK_NETNSID]) {
2440 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2441
2442 link_net = get_net_ns_by_id(dest_net, id);
2443 if (!link_net) {
2444 err = -EINVAL;
2445 goto out;
2446 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002447 err = -EPERM;
2448 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2449 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002450 }
2451
2452 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2453 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002454 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002455 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002456 goto out;
2457 }
2458
2459 dev->ifindex = ifm->ifi_index;
2460
Cong Wang0e0eee22014-02-11 15:51:30 -08002461 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002462 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002463 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002464 * and unregister it on failure after registration
2465 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002466 */
Cong Wange51fb152014-06-03 16:40:47 -07002467 if (err < 0) {
2468 /* If device is not registered at all, free it now */
2469 if (dev->reg_state == NETREG_UNINITIALIZED)
2470 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002471 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002472 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002473 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002474 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002475 if (err < 0) {
2476 free_netdev(dev);
2477 goto out;
2478 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002479 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002480 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002481 if (err < 0)
2482 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002483 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002484 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002485 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002486 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002487 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002488out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002489 if (link_net)
2490 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002491 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002492 return err;
David S. Miller43638902015-03-10 21:58:32 -04002493out_unregister:
2494 if (ops->newlink) {
2495 LIST_HEAD(list_kill);
2496
2497 ops->dellink(dev, &list_kill);
2498 unregister_netdevice_many(&list_kill);
2499 } else {
2500 unregister_netdevice(dev);
2501 }
2502 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002503 }
2504}
2505
Thomas Graf661d2962013-03-21 07:45:29 +00002506static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002507{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002508 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002509 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002510 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002511 struct nlattr *tb[IFLA_MAX+1];
2512 struct net_device *dev = NULL;
2513 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002514 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002515 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002516
Thomas Grafb60c5112006-08-04 23:05:34 -07002517 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2518 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002519 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002520
Eric Dumazeta3d12892009-10-21 10:59:31 +00002521 if (tb[IFLA_IFNAME])
2522 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2523
Greg Rose115c9b82012-02-21 16:54:48 -05002524 if (tb[IFLA_EXT_MASK])
2525 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2526
Thomas Grafb60c5112006-08-04 23:05:34 -07002527 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002528 if (ifm->ifi_index > 0)
2529 dev = __dev_get_by_index(net, ifm->ifi_index);
2530 else if (tb[IFLA_IFNAME])
2531 dev = __dev_get_by_name(net, ifname);
2532 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002533 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002534
Eric Dumazeta3d12892009-10-21 10:59:31 +00002535 if (dev == NULL)
2536 return -ENODEV;
2537
Greg Rose115c9b82012-02-21 16:54:48 -05002538 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002539 if (nskb == NULL)
2540 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002541
Eric W. Biederman15e47302012-09-07 20:12:54 +00002542 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002543 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002544 if (err < 0) {
2545 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2546 WARN_ON(err == -EMSGSIZE);
2547 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002548 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002549 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002550
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002551 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002552}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002553
Greg Rose115c9b82012-02-21 16:54:48 -05002554static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002555{
Greg Rose115c9b82012-02-21 16:54:48 -05002556 struct net *net = sock_net(skb->sk);
2557 struct net_device *dev;
2558 struct nlattr *tb[IFLA_MAX+1];
2559 u32 ext_filter_mask = 0;
2560 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002561 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002562
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002563 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2564 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2565 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2566
2567 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002568 if (tb[IFLA_EXT_MASK])
2569 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2570 }
Greg Rose115c9b82012-02-21 16:54:48 -05002571
2572 if (!ext_filter_mask)
2573 return NLMSG_GOODSIZE;
2574 /*
2575 * traverse the list of net devices and compute the minimum
2576 * buffer size based upon the filter mask.
2577 */
2578 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2579 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2580 if_nlmsg_size(dev,
2581 ext_filter_mask));
2582 }
2583
Greg Rosec7ac8672011-06-10 01:27:09 +00002584 return min_ifinfo_dump_size;
2585}
2586
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002587static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 int idx;
2590 int s_idx = cb->family;
2591
2592 if (s_idx == 0)
2593 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002594 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 int type = cb->nlh->nlmsg_type-RTM_BASE;
2596 if (idx < s_idx || idx == PF_PACKET)
2597 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002598 if (rtnl_msg_handlers[idx] == NULL ||
2599 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002601 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002603 cb->prev_seq = 0;
2604 cb->seq = 0;
2605 }
Thomas Grafe2849862007-03-22 11:48:11 -07002606 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 break;
2608 }
2609 cb->family = idx;
2610
2611 return skb->len;
2612}
2613
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002614struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2615 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002617 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002619 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002620 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002622 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002623 if (skb == NULL)
2624 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Greg Rose115c9b82012-02-21 16:54:48 -05002626 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002627 if (err < 0) {
2628 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2629 WARN_ON(err == -EMSGSIZE);
2630 kfree_skb(skb);
2631 goto errout;
2632 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002633 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002634errout:
2635 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002636 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002637 return NULL;
2638}
2639
2640void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2641{
2642 struct net *net = dev_net(dev);
2643
2644 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2645}
2646
2647void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2648 gfp_t flags)
2649{
2650 struct sk_buff *skb;
2651
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002652 if (dev->reg_state != NETREG_REGISTERED)
2653 return;
2654
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002655 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2656 if (skb)
2657 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002659EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
John Fastabendd83b0602012-04-15 06:44:08 +00002661static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2662 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002663 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002664 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002665 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002666{
2667 struct nlmsghdr *nlh;
2668 struct ndmsg *ndm;
2669
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002670 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002671 if (!nlh)
2672 return -EMSGSIZE;
2673
2674 ndm = nlmsg_data(nlh);
2675 ndm->ndm_family = AF_BRIDGE;
2676 ndm->ndm_pad1 = 0;
2677 ndm->ndm_pad2 = 0;
2678 ndm->ndm_flags = flags;
2679 ndm->ndm_type = 0;
2680 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002681 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002682
2683 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2684 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002685 if (vid)
2686 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2687 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002688
Johannes Berg053c0952015-01-16 22:09:00 +01002689 nlmsg_end(skb, nlh);
2690 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002691
2692nla_put_failure:
2693 nlmsg_cancel(skb, nlh);
2694 return -EMSGSIZE;
2695}
2696
John Fastabend3ff661c2012-04-15 06:44:14 +00002697static inline size_t rtnl_fdb_nlmsg_size(void)
2698{
2699 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2700}
2701
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002702static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2703 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002704{
2705 struct net *net = dev_net(dev);
2706 struct sk_buff *skb;
2707 int err = -ENOBUFS;
2708
2709 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2710 if (!skb)
2711 goto errout;
2712
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002713 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002714 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002715 if (err < 0) {
2716 kfree_skb(skb);
2717 goto errout;
2718 }
2719
2720 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2721 return;
2722errout:
2723 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2724}
2725
Vlad Yasevich090096b2013-03-06 15:39:42 +00002726/**
2727 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2728 */
2729int ndo_dflt_fdb_add(struct ndmsg *ndm,
2730 struct nlattr *tb[],
2731 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002732 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002733 u16 flags)
2734{
2735 int err = -EINVAL;
2736
2737 /* If aging addresses are supported device will need to
2738 * implement its own handler for this.
2739 */
2740 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2741 pr_info("%s: FDB only supports static addresses\n", dev->name);
2742 return err;
2743 }
2744
Or Gerlitz65891fe2014-12-14 18:19:05 +02002745 if (vid) {
2746 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2747 return err;
2748 }
2749
Vlad Yasevich090096b2013-03-06 15:39:42 +00002750 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2751 err = dev_uc_add_excl(dev, addr);
2752 else if (is_multicast_ether_addr(addr))
2753 err = dev_mc_add_excl(dev, addr);
2754
2755 /* Only return duplicate errors if NLM_F_EXCL is set */
2756 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2757 err = 0;
2758
2759 return err;
2760}
2761EXPORT_SYMBOL(ndo_dflt_fdb_add);
2762
Jiri Pirkof6f64242014-11-28 14:34:15 +01002763static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2764{
2765 u16 vid = 0;
2766
2767 if (vlan_attr) {
2768 if (nla_len(vlan_attr) != sizeof(u16)) {
2769 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2770 return -EINVAL;
2771 }
2772
2773 vid = nla_get_u16(vlan_attr);
2774
2775 if (!vid || vid >= VLAN_VID_MASK) {
2776 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2777 vid);
2778 return -EINVAL;
2779 }
2780 }
2781 *p_vid = vid;
2782 return 0;
2783}
2784
Thomas Graf661d2962013-03-21 07:45:29 +00002785static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002786{
2787 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002788 struct ndmsg *ndm;
2789 struct nlattr *tb[NDA_MAX+1];
2790 struct net_device *dev;
2791 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002792 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002793 int err;
2794
2795 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2796 if (err < 0)
2797 return err;
2798
2799 ndm = nlmsg_data(nlh);
2800 if (ndm->ndm_ifindex == 0) {
2801 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2802 return -EINVAL;
2803 }
2804
2805 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2806 if (dev == NULL) {
2807 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2808 return -ENODEV;
2809 }
2810
2811 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2812 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2813 return -EINVAL;
2814 }
2815
2816 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002817
Jiri Pirkof6f64242014-11-28 14:34:15 +01002818 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2819 if (err)
2820 return err;
2821
John Fastabend77162022012-04-15 06:43:56 +00002822 err = -EOPNOTSUPP;
2823
2824 /* Support fdb on master device the net/bridge default case */
2825 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2826 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002827 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2828 const struct net_device_ops *ops = br_dev->netdev_ops;
2829
Jiri Pirkof6f64242014-11-28 14:34:15 +01002830 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2831 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002832 if (err)
2833 goto out;
2834 else
2835 ndm->ndm_flags &= ~NTF_MASTER;
2836 }
2837
2838 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002839 if ((ndm->ndm_flags & NTF_SELF)) {
2840 if (dev->netdev_ops->ndo_fdb_add)
2841 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002842 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002843 nlh->nlmsg_flags);
2844 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002845 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002846 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002847
John Fastabend3ff661c2012-04-15 06:44:14 +00002848 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002849 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2850 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002851 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002852 }
John Fastabend77162022012-04-15 06:43:56 +00002853 }
2854out:
2855 return err;
2856}
2857
Vlad Yasevich090096b2013-03-06 15:39:42 +00002858/**
2859 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2860 */
2861int ndo_dflt_fdb_del(struct ndmsg *ndm,
2862 struct nlattr *tb[],
2863 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002864 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002865{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002866 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002867
2868 /* If aging addresses are supported device will need to
2869 * implement its own handler for this.
2870 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002871 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002872 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002873 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002874 }
2875
2876 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2877 err = dev_uc_del(dev, addr);
2878 else if (is_multicast_ether_addr(addr))
2879 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002880
2881 return err;
2882}
2883EXPORT_SYMBOL(ndo_dflt_fdb_del);
2884
Thomas Graf661d2962013-03-21 07:45:29 +00002885static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002886{
2887 struct net *net = sock_net(skb->sk);
2888 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002889 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002890 struct net_device *dev;
2891 int err = -EINVAL;
2892 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002893 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002894
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002895 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002896 return -EPERM;
2897
2898 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2899 if (err < 0)
2900 return err;
John Fastabend77162022012-04-15 06:43:56 +00002901
2902 ndm = nlmsg_data(nlh);
2903 if (ndm->ndm_ifindex == 0) {
2904 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2905 return -EINVAL;
2906 }
2907
2908 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2909 if (dev == NULL) {
2910 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2911 return -ENODEV;
2912 }
2913
Vlad Yasevich1690be62013-02-13 12:00:18 +00002914 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2915 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002916 return -EINVAL;
2917 }
2918
Vlad Yasevich1690be62013-02-13 12:00:18 +00002919 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002920
Jiri Pirkof6f64242014-11-28 14:34:15 +01002921 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2922 if (err)
2923 return err;
2924
John Fastabend77162022012-04-15 06:43:56 +00002925 err = -EOPNOTSUPP;
2926
2927 /* Support fdb on master device the net/bridge default case */
2928 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2929 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002930 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2931 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002932
Jiri Pirko898e5062013-01-03 22:48:52 +00002933 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002934 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002935
2936 if (err)
2937 goto out;
2938 else
2939 ndm->ndm_flags &= ~NTF_MASTER;
2940 }
2941
2942 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002943 if (ndm->ndm_flags & NTF_SELF) {
2944 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002945 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2946 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002947 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002948 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002949
John Fastabend3ff661c2012-04-15 06:44:14 +00002950 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002951 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2952 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002953 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002954 }
John Fastabend77162022012-04-15 06:43:56 +00002955 }
2956out:
2957 return err;
2958}
2959
John Fastabendd83b0602012-04-15 06:44:08 +00002960static int nlmsg_populate_fdb(struct sk_buff *skb,
2961 struct netlink_callback *cb,
2962 struct net_device *dev,
2963 int *idx,
2964 struct netdev_hw_addr_list *list)
2965{
2966 struct netdev_hw_addr *ha;
2967 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002968 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002969
Eric W. Biederman15e47302012-09-07 20:12:54 +00002970 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002971 seq = cb->nlh->nlmsg_seq;
2972
2973 list_for_each_entry(ha, &list->list, list) {
2974 if (*idx < cb->args[0])
2975 goto skip;
2976
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002977 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002978 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002979 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002980 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00002981 if (err < 0)
2982 return err;
2983skip:
2984 *idx += 1;
2985 }
2986 return 0;
2987}
2988
2989/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002990 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002991 * @nlh: netlink message header
2992 * @dev: netdevice
2993 *
2994 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002995 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002996 */
2997int ndo_dflt_fdb_dump(struct sk_buff *skb,
2998 struct netlink_callback *cb,
2999 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003000 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00003001 int idx)
3002{
3003 int err;
3004
3005 netif_addr_lock_bh(dev);
3006 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3007 if (err)
3008 goto out;
3009 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3010out:
3011 netif_addr_unlock_bh(dev);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003012 cb->args[1] = err;
John Fastabendd83b0602012-04-15 06:44:08 +00003013 return idx;
3014}
3015EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3016
John Fastabend77162022012-04-15 06:43:56 +00003017static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3018{
John Fastabend77162022012-04-15 06:43:56 +00003019 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003020 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003021 struct net_device *br_dev = NULL;
3022 const struct net_device_ops *ops = NULL;
3023 const struct net_device_ops *cops = NULL;
3024 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3025 struct net *net = sock_net(skb->sk);
3026 int brport_idx = 0;
3027 int br_idx = 0;
3028 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003029
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003030 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3031 ifla_policy) == 0) {
3032 if (tb[IFLA_MASTER])
3033 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3034 }
John Fastabend77162022012-04-15 06:43:56 +00003035
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003036 brport_idx = ifm->ifi_index;
3037
3038 if (br_idx) {
3039 br_dev = __dev_get_by_index(net, br_idx);
3040 if (!br_dev)
3041 return -ENODEV;
3042
3043 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003044 }
3045
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003046 cb->args[1] = 0;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003047 for_each_netdev(net, dev) {
3048 if (brport_idx && (dev->ifindex != brport_idx))
3049 continue;
3050
3051 if (!br_idx) { /* user did not specify a specific bridge */
3052 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3053 br_dev = netdev_master_upper_dev_get(dev);
3054 cops = br_dev->netdev_ops;
3055 }
3056
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003057 } else {
3058 if (dev != br_dev &&
3059 !(dev->priv_flags & IFF_BRIDGE_PORT))
3060 continue;
3061
3062 if (br_dev != netdev_master_upper_dev_get(dev) &&
3063 !(dev->priv_flags & IFF_EBRIDGE))
3064 continue;
3065
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003066 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00003067 }
3068
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003069 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3070 if (cops && cops->ndo_fdb_dump)
3071 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3072 idx);
3073 }
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003074 if (cb->args[1] == -EMSGSIZE)
3075 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003076
John Fastabend77162022012-04-15 06:43:56 +00003077 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003078 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003079 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003080 else
3081 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003082 if (cb->args[1] == -EMSGSIZE)
3083 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003084
3085 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00003086 }
John Fastabend77162022012-04-15 06:43:56 +00003087
3088 cb->args[0] = idx;
3089 return skb->len;
3090}
3091
Scott Feldman2c3c0312014-11-28 14:34:25 +01003092static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3093 unsigned int attrnum, unsigned int flag)
3094{
3095 if (mask & flag)
3096 return nla_put_u8(skb, attrnum, !!(flags & flag));
3097 return 0;
3098}
3099
John Fastabend815cccb2012-10-24 08:13:09 +00003100int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003101 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003102 u32 flags, u32 mask, int nlflags,
3103 u32 filter_mask,
3104 int (*vlan_fill)(struct sk_buff *skb,
3105 struct net_device *dev,
3106 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003107{
3108 struct nlmsghdr *nlh;
3109 struct ifinfomsg *ifm;
3110 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003111 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003112 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003113 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003114 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003115
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003116 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003117 if (nlh == NULL)
3118 return -EMSGSIZE;
3119
3120 ifm = nlmsg_data(nlh);
3121 ifm->ifi_family = AF_BRIDGE;
3122 ifm->__ifi_pad = 0;
3123 ifm->ifi_type = dev->type;
3124 ifm->ifi_index = dev->ifindex;
3125 ifm->ifi_flags = dev_get_flags(dev);
3126 ifm->ifi_change = 0;
3127
3128
3129 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3130 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3131 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003132 (br_dev &&
3133 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003134 (dev->addr_len &&
3135 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003136 (dev->ifindex != dev_get_iflink(dev) &&
3137 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003138 goto nla_put_failure;
3139
3140 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3141 if (!br_afspec)
3142 goto nla_put_failure;
3143
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003144 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003145 nla_nest_cancel(skb, br_afspec);
3146 goto nla_put_failure;
3147 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003148
3149 if (mode != BRIDGE_MODE_UNDEF) {
3150 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3151 nla_nest_cancel(skb, br_afspec);
3152 goto nla_put_failure;
3153 }
3154 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003155 if (vlan_fill) {
3156 err = vlan_fill(skb, dev, filter_mask);
3157 if (err) {
3158 nla_nest_cancel(skb, br_afspec);
3159 goto nla_put_failure;
3160 }
3161 }
John Fastabend815cccb2012-10-24 08:13:09 +00003162 nla_nest_end(skb, br_afspec);
3163
Scott Feldman2c3c0312014-11-28 14:34:25 +01003164 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3165 if (!protinfo)
3166 goto nla_put_failure;
3167
3168 if (brport_nla_put_flag(skb, flags, mask,
3169 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3170 brport_nla_put_flag(skb, flags, mask,
3171 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3172 brport_nla_put_flag(skb, flags, mask,
3173 IFLA_BRPORT_FAST_LEAVE,
3174 BR_MULTICAST_FAST_LEAVE) ||
3175 brport_nla_put_flag(skb, flags, mask,
3176 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3177 brport_nla_put_flag(skb, flags, mask,
3178 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3179 brport_nla_put_flag(skb, flags, mask,
3180 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3181 brport_nla_put_flag(skb, flags, mask,
3182 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3183 brport_nla_put_flag(skb, flags, mask,
3184 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3185 nla_nest_cancel(skb, protinfo);
3186 goto nla_put_failure;
3187 }
3188
3189 nla_nest_end(skb, protinfo);
3190
Johannes Berg053c0952015-01-16 22:09:00 +01003191 nlmsg_end(skb, nlh);
3192 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003193nla_put_failure:
3194 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003195 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003196}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003197EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003198
John Fastabende5a55a82012-10-24 08:12:57 +00003199static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3200{
3201 struct net *net = sock_net(skb->sk);
3202 struct net_device *dev;
3203 int idx = 0;
3204 u32 portid = NETLINK_CB(cb->skb).portid;
3205 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003206 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003207 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003208
Thomas Grafaa68c202014-11-26 13:42:20 +01003209 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3210 struct nlattr *extfilt;
3211
3212 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3213 IFLA_EXT_MASK);
3214 if (extfilt) {
3215 if (nla_len(extfilt) < sizeof(filter_mask))
3216 return -EINVAL;
3217
3218 filter_mask = nla_get_u32(extfilt);
3219 }
3220 }
John Fastabende5a55a82012-10-24 08:12:57 +00003221
3222 rcu_read_lock();
3223 for_each_netdev_rcu(net, dev) {
3224 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003225 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003226
Jiri Pirko898e5062013-01-03 22:48:52 +00003227 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003228 if (idx >= cb->args[0]) {
3229 err = br_dev->netdev_ops->ndo_bridge_getlink(
3230 skb, portid, seq, dev,
3231 filter_mask, NLM_F_MULTI);
3232 if (err < 0 && err != -EOPNOTSUPP)
3233 break;
3234 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003235 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003236 }
3237
3238 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003239 if (idx >= cb->args[0]) {
3240 err = ops->ndo_bridge_getlink(skb, portid,
3241 seq, dev,
3242 filter_mask,
3243 NLM_F_MULTI);
3244 if (err < 0 && err != -EOPNOTSUPP)
3245 break;
3246 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003247 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003248 }
3249 }
3250 rcu_read_unlock();
3251 cb->args[0] = idx;
3252
3253 return skb->len;
3254}
3255
John Fastabend2469ffd2012-10-24 08:13:03 +00003256static inline size_t bridge_nlmsg_size(void)
3257{
3258 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3259 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3260 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3261 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3262 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3263 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3264 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3265 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3266 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3267 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3268 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3269}
3270
Roopa Prabhu02dba432015-01-14 20:02:25 -08003271static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003272{
3273 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003274 struct sk_buff *skb;
3275 int err = -EOPNOTSUPP;
3276
Roopa Prabhu02dba432015-01-14 20:02:25 -08003277 if (!dev->netdev_ops->ndo_bridge_getlink)
3278 return 0;
3279
John Fastabend2469ffd2012-10-24 08:13:03 +00003280 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3281 if (!skb) {
3282 err = -ENOMEM;
3283 goto errout;
3284 }
3285
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003286 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003287 if (err < 0)
3288 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003289
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003290 if (!skb->len)
3291 goto errout;
3292
John Fastabend2469ffd2012-10-24 08:13:03 +00003293 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3294 return 0;
3295errout:
3296 WARN_ON(err == -EMSGSIZE);
3297 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003298 if (err)
3299 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003300 return err;
3301}
3302
Thomas Graf661d2962013-03-21 07:45:29 +00003303static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003304{
3305 struct net *net = sock_net(skb->sk);
3306 struct ifinfomsg *ifm;
3307 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003308 struct nlattr *br_spec, *attr = NULL;
3309 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003310 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003311 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003312
3313 if (nlmsg_len(nlh) < sizeof(*ifm))
3314 return -EINVAL;
3315
3316 ifm = nlmsg_data(nlh);
3317 if (ifm->ifi_family != AF_BRIDGE)
3318 return -EPFNOSUPPORT;
3319
3320 dev = __dev_get_by_index(net, ifm->ifi_index);
3321 if (!dev) {
3322 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3323 return -ENODEV;
3324 }
3325
John Fastabend2469ffd2012-10-24 08:13:03 +00003326 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3327 if (br_spec) {
3328 nla_for_each_nested(attr, br_spec, rem) {
3329 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003330 if (nla_len(attr) < sizeof(flags))
3331 return -EINVAL;
3332
John Fastabendc38e01b2012-11-02 16:32:36 +00003333 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003334 flags = nla_get_u16(attr);
3335 break;
3336 }
3337 }
3338 }
3339
3340 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003341 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3342
3343 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003344 err = -EOPNOTSUPP;
3345 goto out;
3346 }
3347
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003348 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003349 if (err)
3350 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003351
3352 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003353 }
3354
John Fastabend2469ffd2012-10-24 08:13:03 +00003355 if ((flags & BRIDGE_FLAGS_SELF)) {
3356 if (!dev->netdev_ops->ndo_bridge_setlink)
3357 err = -EOPNOTSUPP;
3358 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003359 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3360 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003361 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003362 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003363
3364 /* Generate event to notify upper layer of bridge
3365 * change
3366 */
3367 err = rtnl_bridge_notify(dev);
3368 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003369 }
3370
John Fastabendc38e01b2012-11-02 16:32:36 +00003371 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003372 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003373out:
3374 return err;
3375}
3376
Thomas Graf661d2962013-03-21 07:45:29 +00003377static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003378{
3379 struct net *net = sock_net(skb->sk);
3380 struct ifinfomsg *ifm;
3381 struct net_device *dev;
3382 struct nlattr *br_spec, *attr = NULL;
3383 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003384 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003385 bool have_flags = false;
3386
3387 if (nlmsg_len(nlh) < sizeof(*ifm))
3388 return -EINVAL;
3389
3390 ifm = nlmsg_data(nlh);
3391 if (ifm->ifi_family != AF_BRIDGE)
3392 return -EPFNOSUPPORT;
3393
3394 dev = __dev_get_by_index(net, ifm->ifi_index);
3395 if (!dev) {
3396 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3397 return -ENODEV;
3398 }
3399
3400 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3401 if (br_spec) {
3402 nla_for_each_nested(attr, br_spec, rem) {
3403 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003404 if (nla_len(attr) < sizeof(flags))
3405 return -EINVAL;
3406
Vlad Yasevich407af322013-02-13 12:00:12 +00003407 have_flags = true;
3408 flags = nla_get_u16(attr);
3409 break;
3410 }
3411 }
3412 }
3413
Vlad Yasevich407af322013-02-13 12:00:12 +00003414 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3415 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3416
3417 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3418 err = -EOPNOTSUPP;
3419 goto out;
3420 }
3421
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003422 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003423 if (err)
3424 goto out;
3425
3426 flags &= ~BRIDGE_FLAGS_MASTER;
3427 }
3428
3429 if ((flags & BRIDGE_FLAGS_SELF)) {
3430 if (!dev->netdev_ops->ndo_bridge_dellink)
3431 err = -EOPNOTSUPP;
3432 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003433 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3434 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003435
Roopa Prabhu02dba432015-01-14 20:02:25 -08003436 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003437 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003438
3439 /* Generate event to notify upper layer of bridge
3440 * change
3441 */
3442 err = rtnl_bridge_notify(dev);
3443 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003444 }
3445
3446 if (have_flags)
3447 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003448out:
3449 return err;
3450}
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452/* Process one rtnetlink message. */
3453
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003454static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003456 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003457 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06003458 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 int family;
3460 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003461 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003465 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 type -= RTM_BASE;
3468
3469 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003470 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return 0;
3472
Hong zhi guo573ce262013-03-27 06:47:04 +00003473 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 kind = type&3;
3475
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003476 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003477 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
David S. Millerb8f3ab42011-01-18 12:40:38 -08003479 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003480 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003481 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003482 rtnl_calcit_func calcit;
3483 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Thomas Grafe2849862007-03-22 11:48:11 -07003485 dumpit = rtnl_get_dumpit(family, type);
3486 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003487 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003488 calcit = rtnl_get_calcit(family, type);
3489 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003490 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Eric Dumazet2907c352011-05-25 07:34:04 +00003492 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003493 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003494 {
3495 struct netlink_dump_control c = {
3496 .dump = dumpit,
3497 .min_dump_alloc = min_dump_alloc,
3498 };
3499 err = netlink_dump_start(rtnl, skb, nlh, &c);
3500 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003501 rtnl_lock();
3502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504
Thomas Grafe2849862007-03-22 11:48:11 -07003505 doit = rtnl_get_doit(family, type);
3506 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003507 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Thomas Graf661d2962013-03-21 07:45:29 +00003509 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003512static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003514 rtnl_lock();
3515 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3516 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3520{
Jiri Pirko351638e2013-05-28 01:30:21 +00003521 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 case NETDEV_UP:
3525 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003526 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003527 case NETDEV_POST_INIT:
3528 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003530 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003532 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003533 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003534 case NETDEV_RELEASE:
3535 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003536 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 break;
3538 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003539 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 break;
3541 }
3542 return NOTIFY_DONE;
3543}
3544
3545static struct notifier_block rtnetlink_dev_notifier = {
3546 .notifier_call = rtnetlink_event,
3547};
3548
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003549
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003550static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003551{
3552 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003553 struct netlink_kernel_cfg cfg = {
3554 .groups = RTNLGRP_MAX,
3555 .input = rtnetlink_rcv,
3556 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003557 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003558 };
3559
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003560 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003561 if (!sk)
3562 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003563 net->rtnl = sk;
3564 return 0;
3565}
3566
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003567static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003568{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003569 netlink_kernel_release(net->rtnl);
3570 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003571}
3572
3573static struct pernet_operations rtnetlink_net_ops = {
3574 .init = rtnetlink_net_init,
3575 .exit = rtnetlink_net_exit,
3576};
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578void __init rtnetlink_init(void)
3579{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003580 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003584
Greg Rosec7ac8672011-06-10 01:27:09 +00003585 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3586 rtnl_dump_ifinfo, rtnl_calcit);
3587 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3588 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3589 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003590
Greg Rosec7ac8672011-06-10 01:27:09 +00003591 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3592 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003593
3594 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3595 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3596 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003597
3598 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003599 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003600 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}