blob: f2066772d0f397c545737ff0a353f7f4f81ad5fc [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
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000811static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000812{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000813 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000814}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Chris Wrightc02db8c2010-05-16 01:05:45 -0700816/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500817static inline int rtnl_vfinfo_size(const struct net_device *dev,
818 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000819{
Greg Rose115c9b82012-02-21 16:54:48 -0500820 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
821 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700822 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700823 size_t size = nla_total_size(sizeof(struct nlattr));
824 size += nla_total_size(num_vfs * sizeof(struct nlattr));
825 size += num_vfs *
826 (nla_total_size(sizeof(struct ifla_vf_mac)) +
827 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400828 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200829 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300830 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300831 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
832 /* IFLA_VF_STATS_RX_PACKETS */
833 nla_total_size(sizeof(__u64)) +
834 /* IFLA_VF_STATS_TX_PACKETS */
835 nla_total_size(sizeof(__u64)) +
836 /* IFLA_VF_STATS_RX_BYTES */
837 nla_total_size(sizeof(__u64)) +
838 /* IFLA_VF_STATS_TX_BYTES */
839 nla_total_size(sizeof(__u64)) +
840 /* IFLA_VF_STATS_BROADCAST */
841 nla_total_size(sizeof(__u64)) +
842 /* IFLA_VF_STATS_MULTICAST */
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000843 nla_total_size(sizeof(__u64)) +
844 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700845 return size;
846 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000847 return 0;
848}
849
David Gibsonc53864f2014-04-24 10:22:36 +1000850static size_t rtnl_port_size(const struct net_device *dev,
851 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700852{
853 size_t port_size = nla_total_size(4) /* PORT_VF */
854 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
855 + nla_total_size(sizeof(struct ifla_port_vsi))
856 /* PORT_VSI_TYPE */
857 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
858 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
859 + nla_total_size(1) /* PROT_VDP_REQUEST */
860 + nla_total_size(2); /* PORT_VDP_RESPONSE */
861 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
862 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
863 + port_size;
864 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
865 + port_size;
866
David Gibsonc53864f2014-04-24 10:22:36 +1000867 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
868 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700869 return 0;
870 if (dev_num_vf(dev->dev.parent))
871 return port_self_size + vf_ports_size +
872 vf_port_size * dev_num_vf(dev->dev.parent);
873 else
874 return port_self_size;
875}
876
Greg Rose115c9b82012-02-21 16:54:48 -0500877static noinline size_t if_nlmsg_size(const struct net_device *dev,
878 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800879{
880 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
881 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700882 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800883 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
884 + nla_total_size(sizeof(struct rtnl_link_ifmap))
885 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700886 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800887 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
888 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
889 + nla_total_size(4) /* IFLA_TXQLEN */
890 + nla_total_size(4) /* IFLA_WEIGHT */
891 + nla_total_size(4) /* IFLA_MTU */
892 + nla_total_size(4) /* IFLA_LINK */
893 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000894 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000895 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000896 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
897 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Eric Dumazetc70ce022016-03-21 09:55:10 -0700898 + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
899 + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800900 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700901 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700902 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100903 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500904 + nla_total_size(ext_filter_mask
905 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
906 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000907 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000908 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700909 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100910 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700911 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
912 + nla_total_size(1); /* IFLA_PROTO_DOWN */
913
Thomas Graf339bf982006-11-10 14:10:15 -0800914}
915
Scott Feldman57b61082010-05-17 22:49:55 -0700916static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
917{
918 struct nlattr *vf_ports;
919 struct nlattr *vf_port;
920 int vf;
921 int err;
922
923 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
924 if (!vf_ports)
925 return -EMSGSIZE;
926
927 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
928 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700929 if (!vf_port)
930 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400931 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
932 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700933 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700934 if (err == -EMSGSIZE)
935 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700936 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700937 nla_nest_cancel(skb, vf_port);
938 continue;
939 }
940 nla_nest_end(skb, vf_port);
941 }
942
943 nla_nest_end(skb, vf_ports);
944
945 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700946
947nla_put_failure:
948 nla_nest_cancel(skb, vf_ports);
949 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700950}
951
952static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
953{
954 struct nlattr *port_self;
955 int err;
956
957 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
958 if (!port_self)
959 return -EMSGSIZE;
960
961 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
962 if (err) {
963 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700964 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700965 }
966
967 nla_nest_end(skb, port_self);
968
969 return 0;
970}
971
David Gibsonc53864f2014-04-24 10:22:36 +1000972static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
973 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700974{
975 int err;
976
David Gibsonc53864f2014-04-24 10:22:36 +1000977 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
978 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700979 return 0;
980
981 err = rtnl_port_self_fill(skb, dev);
982 if (err)
983 return err;
984
985 if (dev_num_vf(dev->dev.parent)) {
986 err = rtnl_vf_ports_fill(skb, dev);
987 if (err)
988 return err;
989 }
990
991 return 0;
992}
993
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200994static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
995{
996 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100997 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200998
999 err = dev_get_phys_port_id(dev, &ppid);
1000 if (err) {
1001 if (err == -EOPNOTSUPP)
1002 return 0;
1003 return err;
1004 }
1005
1006 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1007 return -EMSGSIZE;
1008
1009 return 0;
1010}
1011
David Aherndb24a902015-03-17 20:23:15 -06001012static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1013{
1014 char name[IFNAMSIZ];
1015 int err;
1016
1017 err = dev_get_phys_port_name(dev, name, sizeof(name));
1018 if (err) {
1019 if (err == -EOPNOTSUPP)
1020 return 0;
1021 return err;
1022 }
1023
1024 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1025 return -EMSGSIZE;
1026
1027 return 0;
1028}
1029
Jiri Pirko82f28412014-11-28 14:34:18 +01001030static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1031{
1032 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001033 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001034 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001035 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001036 .flags = SWITCHDEV_F_NO_RECURSE,
1037 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001038
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001039 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001040 if (err) {
1041 if (err == -EOPNOTSUPP)
1042 return 0;
1043 return err;
1044 }
1045
Scott Feldman42275bd2015-05-13 11:16:50 -07001046 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1047 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001048 return -EMSGSIZE;
1049
1050 return 0;
1051}
1052
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001053static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1054 struct net_device *dev)
1055{
1056 const struct rtnl_link_stats64 *stats;
1057 struct rtnl_link_stats64 temp;
1058 struct nlattr *attr;
1059
1060 stats = dev_get_stats(dev, &temp);
1061
1062 attr = nla_reserve(skb, IFLA_STATS,
1063 sizeof(struct rtnl_link_stats));
1064 if (!attr)
1065 return -EMSGSIZE;
1066
1067 copy_rtnl_link_stats(nla_data(attr), stats);
1068
1069 attr = nla_reserve(skb, IFLA_STATS64,
1070 sizeof(struct rtnl_link_stats64));
1071 if (!attr)
1072 return -EMSGSIZE;
1073
1074 copy_rtnl_link_stats64(nla_data(attr), stats);
1075
1076 return 0;
1077}
1078
1079static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1080 struct net_device *dev,
1081 int vfs_num,
1082 struct nlattr *vfinfo)
1083{
1084 struct ifla_vf_rss_query_en vf_rss_query_en;
1085 struct ifla_vf_link_state vf_linkstate;
1086 struct ifla_vf_spoofchk vf_spoofchk;
1087 struct ifla_vf_tx_rate vf_tx_rate;
1088 struct ifla_vf_stats vf_stats;
1089 struct ifla_vf_trust vf_trust;
1090 struct ifla_vf_vlan vf_vlan;
1091 struct ifla_vf_rate vf_rate;
1092 struct nlattr *vf, *vfstats;
1093 struct ifla_vf_mac vf_mac;
1094 struct ifla_vf_info ivi;
1095
1096 /* Not all SR-IOV capable drivers support the
1097 * spoofcheck and "RSS query enable" query. Preset to
1098 * -1 so the user space tool can detect that the driver
1099 * didn't report anything.
1100 */
1101 ivi.spoofchk = -1;
1102 ivi.rss_query_en = -1;
1103 ivi.trusted = -1;
1104 memset(ivi.mac, 0, sizeof(ivi.mac));
1105 /* The default value for VF link state is "auto"
1106 * IFLA_VF_LINK_STATE_AUTO which equals zero
1107 */
1108 ivi.linkstate = 0;
1109 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1110 return 0;
1111
1112 vf_mac.vf =
1113 vf_vlan.vf =
1114 vf_rate.vf =
1115 vf_tx_rate.vf =
1116 vf_spoofchk.vf =
1117 vf_linkstate.vf =
1118 vf_rss_query_en.vf =
1119 vf_trust.vf = ivi.vf;
1120
1121 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1122 vf_vlan.vlan = ivi.vlan;
1123 vf_vlan.qos = ivi.qos;
1124 vf_tx_rate.rate = ivi.max_tx_rate;
1125 vf_rate.min_tx_rate = ivi.min_tx_rate;
1126 vf_rate.max_tx_rate = ivi.max_tx_rate;
1127 vf_spoofchk.setting = ivi.spoofchk;
1128 vf_linkstate.link_state = ivi.linkstate;
1129 vf_rss_query_en.setting = ivi.rss_query_en;
1130 vf_trust.setting = ivi.trusted;
1131 vf = nla_nest_start(skb, IFLA_VF_INFO);
1132 if (!vf) {
1133 nla_nest_cancel(skb, vfinfo);
1134 return -EMSGSIZE;
1135 }
1136 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1137 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1138 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1139 &vf_rate) ||
1140 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1141 &vf_tx_rate) ||
1142 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1143 &vf_spoofchk) ||
1144 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1145 &vf_linkstate) ||
1146 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1147 sizeof(vf_rss_query_en),
1148 &vf_rss_query_en) ||
1149 nla_put(skb, IFLA_VF_TRUST,
1150 sizeof(vf_trust), &vf_trust))
1151 return -EMSGSIZE;
1152 memset(&vf_stats, 0, sizeof(vf_stats));
1153 if (dev->netdev_ops->ndo_get_vf_stats)
1154 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1155 &vf_stats);
1156 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1157 if (!vfstats) {
1158 nla_nest_cancel(skb, vf);
1159 nla_nest_cancel(skb, vfinfo);
1160 return -EMSGSIZE;
1161 }
1162 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1163 vf_stats.rx_packets) ||
1164 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1165 vf_stats.tx_packets) ||
1166 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1167 vf_stats.rx_bytes) ||
1168 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1169 vf_stats.tx_bytes) ||
1170 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1171 vf_stats.broadcast) ||
1172 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1173 vf_stats.multicast))
1174 return -EMSGSIZE;
1175 nla_nest_end(skb, vfstats);
1176 nla_nest_end(skb, vf);
1177 return 0;
1178}
1179
1180static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1181{
1182 struct rtnl_link_ifmap map = {
1183 .mem_start = dev->mem_start,
1184 .mem_end = dev->mem_end,
1185 .base_addr = dev->base_addr,
1186 .irq = dev->irq,
1187 .dma = dev->dma,
1188 .port = dev->if_port,
1189 };
1190 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1191 return -EMSGSIZE;
1192
1193 return 0;
1194}
1195
Thomas Grafb60c5112006-08-04 23:05:34 -07001196static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001197 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001198 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001199{
1200 struct ifinfomsg *ifm;
1201 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001202 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001203 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001204 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001205
Eric Dumazet2907c352011-05-25 07:34:04 +00001206 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001207 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1208 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001209 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001210
1211 ifm = nlmsg_data(nlh);
1212 ifm->ifi_family = AF_UNSPEC;
1213 ifm->__ifi_pad = 0;
1214 ifm->ifi_type = dev->type;
1215 ifm->ifi_index = dev->ifindex;
1216 ifm->ifi_flags = dev_get_flags(dev);
1217 ifm->ifi_change = change;
1218
David S. Millera6574342012-04-01 20:12:00 -04001219 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1220 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1221 nla_put_u8(skb, IFLA_OPERSTATE,
1222 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1223 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1224 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1225 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001226 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001227 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001228 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1229 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001230#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001231 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001232#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001233 (dev->ifindex != dev_get_iflink(dev) &&
1234 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001235 (upper_dev &&
1236 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001237 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001238 (dev->qdisc &&
1239 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1240 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001241 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1242 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001243 atomic_read(&dev->carrier_changes)) ||
1244 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001245 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001246
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001247 if (rtnl_fill_link_ifmap(skb, dev))
1248 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001251 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1252 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1253 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001256 if (rtnl_phys_port_id_fill(skb, dev))
1257 goto nla_put_failure;
1258
David Aherndb24a902015-03-17 20:23:15 -06001259 if (rtnl_phys_port_name_fill(skb, dev))
1260 goto nla_put_failure;
1261
Jiri Pirko82f28412014-11-28 14:34:18 +01001262 if (rtnl_phys_switch_id_fill(skb, dev))
1263 goto nla_put_failure;
1264
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001265 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001266 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
David S. Millera6574342012-04-01 20:12:00 -04001268 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1269 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1270 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001271
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001272 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1273 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001274 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001275 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001276 int num_vfs = dev_num_vf(dev->dev.parent);
1277
Chris Wrightc02db8c2010-05-16 01:05:45 -07001278 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1279 if (!vfinfo)
1280 goto nla_put_failure;
1281 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001282 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001283 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001284 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001285
Chris Wrightc02db8c2010-05-16 01:05:45 -07001286 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001287 }
Scott Feldman57b61082010-05-17 22:49:55 -07001288
David Gibsonc53864f2014-04-24 10:22:36 +10001289 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001290 goto nla_put_failure;
1291
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001292 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001293 if (rtnl_link_fill(skb, dev) < 0)
1294 goto nla_put_failure;
1295 }
1296
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001297 if (dev->rtnl_link_ops &&
1298 dev->rtnl_link_ops->get_link_net) {
1299 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1300
1301 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001302 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001303
1304 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1305 goto nla_put_failure;
1306 }
1307 }
1308
Thomas Graff8ff1822010-11-16 04:30:14 +00001309 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1310 goto nla_put_failure;
1311
1312 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1313 if (af_ops->fill_link_af) {
1314 struct nlattr *af;
1315 int err;
1316
1317 if (!(af = nla_nest_start(skb, af_ops->family)))
1318 goto nla_put_failure;
1319
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001320 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001321
1322 /*
1323 * Caller may return ENODATA to indicate that there
1324 * was no data to be dumped. This is not an error, it
1325 * means we should trim the attribute header and
1326 * continue.
1327 */
1328 if (err == -ENODATA)
1329 nla_nest_cancel(skb, af);
1330 else if (err < 0)
1331 goto nla_put_failure;
1332
1333 nla_nest_end(skb, af);
1334 }
1335 }
1336
1337 nla_nest_end(skb, af_spec);
1338
Johannes Berg053c0952015-01-16 22:09:00 +01001339 nlmsg_end(skb, nlh);
1340 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001341
1342nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001343 nlmsg_cancel(skb, nlh);
1344 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Jiri Pirkof7b12602014-02-18 20:53:18 +01001347static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1348 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1349 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1350 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1351 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1352 [IFLA_MTU] = { .type = NLA_U32 },
1353 [IFLA_LINK] = { .type = NLA_U32 },
1354 [IFLA_MASTER] = { .type = NLA_U32 },
1355 [IFLA_CARRIER] = { .type = NLA_U8 },
1356 [IFLA_TXQLEN] = { .type = NLA_U32 },
1357 [IFLA_WEIGHT] = { .type = NLA_U32 },
1358 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1359 [IFLA_LINKMODE] = { .type = NLA_U8 },
1360 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1361 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1362 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1363 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1364 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1365 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1366 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1367 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1368 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1369 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1370 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1371 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001372 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001373 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001374 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001375 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001376 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001377};
1378
1379static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1380 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1381 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1382 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1383 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1384};
1385
Jiri Pirkof7b12602014-02-18 20:53:18 +01001386static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001387 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1388 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1389 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1390 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1391 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1392 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001393 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001394 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001395 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001396 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1397 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001398};
1399
Jiri Pirkof7b12602014-02-18 20:53:18 +01001400static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1401 [IFLA_PORT_VF] = { .type = NLA_U32 },
1402 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1403 .len = PORT_PROFILE_MAX },
1404 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1405 .len = sizeof(struct ifla_port_vsi)},
1406 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1407 .len = PORT_UUID_MAX },
1408 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1409 .len = PORT_UUID_MAX },
1410 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1411 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1412};
1413
David Aherndc599f72016-02-02 08:17:07 -08001414static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1415{
1416 const struct rtnl_link_ops *ops = NULL;
1417 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1418
1419 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1420 return NULL;
1421
1422 if (linfo[IFLA_INFO_KIND]) {
1423 char kind[MODULE_NAME_LEN];
1424
1425 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1426 ops = rtnl_link_ops_get(kind);
1427 }
1428
1429 return ops;
1430}
1431
1432static bool link_master_filtered(struct net_device *dev, int master_idx)
1433{
1434 struct net_device *master;
1435
1436 if (!master_idx)
1437 return false;
1438
1439 master = netdev_master_upper_dev_get(dev);
1440 if (!master || master->ifindex != master_idx)
1441 return true;
1442
1443 return false;
1444}
1445
1446static bool link_kind_filtered(const struct net_device *dev,
1447 const struct rtnl_link_ops *kind_ops)
1448{
1449 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1450 return true;
1451
1452 return false;
1453}
1454
1455static bool link_dump_filtered(struct net_device *dev,
1456 int master_idx,
1457 const struct rtnl_link_ops *kind_ops)
1458{
1459 if (link_master_filtered(dev, master_idx) ||
1460 link_kind_filtered(dev, kind_ops))
1461 return true;
1462
1463 return false;
1464}
1465
Thomas Grafb60c5112006-08-04 23:05:34 -07001466static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001468 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001469 int h, s_h;
1470 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001472 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001473 struct nlattr *tb[IFLA_MAX+1];
1474 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001475 const struct rtnl_link_ops *kind_ops = NULL;
1476 unsigned int flags = NLM_F_MULTI;
1477 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001478 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001479 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001481 s_h = cb->args[0];
1482 s_idx = cb->args[1];
1483
Thomas Graf4e985ad2011-06-21 03:11:20 +00001484 cb->seq = net->dev_base_seq;
1485
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001486 /* A hack to preserve kernel<->userspace interface.
1487 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1488 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1489 * what iproute2 < v3.9.0 used.
1490 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1491 * attribute, its netlink message is shorter than struct ifinfomsg.
1492 */
1493 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1494 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1495
1496 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001497
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001498 if (tb[IFLA_EXT_MASK])
1499 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001500
1501 if (tb[IFLA_MASTER])
1502 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1503
1504 if (tb[IFLA_LINKINFO])
1505 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1506
1507 if (master_idx || kind_ops)
1508 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001509 }
Greg Rose115c9b82012-02-21 16:54:48 -05001510
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001511 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1512 idx = 0;
1513 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001514 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001515 if (link_dump_filtered(dev, master_idx, kind_ops))
1516 continue;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001517 if (idx < s_idx)
1518 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001519 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1520 NETLINK_CB(cb->skb).portid,
1521 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001522 flags,
David Gibson973462b2014-04-24 10:22:35 +10001523 ext_filter_mask);
1524 /* If we ran out of room on the first message,
1525 * we're in trouble
1526 */
1527 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1528
David S. Miller7b46a642015-01-18 23:36:08 -05001529 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001530 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001531
1532 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001533cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001534 idx++;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001537out:
1538 cb->args[1] = idx;
1539 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 return skb->len;
1542}
1543
Jiri Pirkof7b12602014-02-18 20:53:18 +01001544int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1545{
1546 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1547}
1548EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001549
Eric W. Biederman81adee42009-11-08 00:53:51 -08001550struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1551{
1552 struct net *net;
1553 /* Examine the link attributes and figure out which
1554 * network namespace we are talking about.
1555 */
1556 if (tb[IFLA_NET_NS_PID])
1557 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001558 else if (tb[IFLA_NET_NS_FD])
1559 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001560 else
1561 net = get_net(src_net);
1562 return net;
1563}
1564EXPORT_SYMBOL(rtnl_link_get_net);
1565
Thomas Graf1840bb12008-02-23 19:54:36 -08001566static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1567{
1568 if (dev) {
1569 if (tb[IFLA_ADDRESS] &&
1570 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1571 return -EINVAL;
1572
1573 if (tb[IFLA_BROADCAST] &&
1574 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1575 return -EINVAL;
1576 }
1577
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001578 if (tb[IFLA_AF_SPEC]) {
1579 struct nlattr *af;
1580 int rem, err;
1581
1582 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1583 const struct rtnl_af_ops *af_ops;
1584
1585 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1586 return -EAFNOSUPPORT;
1587
1588 if (!af_ops->set_link_af)
1589 return -EOPNOTSUPP;
1590
1591 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001592 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001593 if (err < 0)
1594 return err;
1595 }
1596 }
1597 }
1598
Thomas Graf1840bb12008-02-23 19:54:36 -08001599 return 0;
1600}
1601
Eli Cohencc8e27c2016-03-11 22:58:34 +02001602static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1603 int guid_type)
1604{
1605 const struct net_device_ops *ops = dev->netdev_ops;
1606
1607 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1608}
1609
1610static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1611{
1612 if (dev->type != ARPHRD_INFINIBAND)
1613 return -EOPNOTSUPP;
1614
1615 return handle_infiniband_guid(dev, ivt, guid_type);
1616}
1617
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001618static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001619{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001620 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001621 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001622
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001623 if (tb[IFLA_VF_MAC]) {
1624 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001625
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001626 err = -EOPNOTSUPP;
1627 if (ops->ndo_set_vf_mac)
1628 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1629 ivm->mac);
1630 if (err < 0)
1631 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001632 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001633
1634 if (tb[IFLA_VF_VLAN]) {
1635 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1636
1637 err = -EOPNOTSUPP;
1638 if (ops->ndo_set_vf_vlan)
1639 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1640 ivv->qos);
1641 if (err < 0)
1642 return err;
1643 }
1644
1645 if (tb[IFLA_VF_TX_RATE]) {
1646 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1647 struct ifla_vf_info ivf;
1648
1649 err = -EOPNOTSUPP;
1650 if (ops->ndo_get_vf_config)
1651 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1652 if (err < 0)
1653 return err;
1654
1655 err = -EOPNOTSUPP;
1656 if (ops->ndo_set_vf_rate)
1657 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1658 ivf.min_tx_rate,
1659 ivt->rate);
1660 if (err < 0)
1661 return err;
1662 }
1663
1664 if (tb[IFLA_VF_RATE]) {
1665 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1666
1667 err = -EOPNOTSUPP;
1668 if (ops->ndo_set_vf_rate)
1669 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1670 ivt->min_tx_rate,
1671 ivt->max_tx_rate);
1672 if (err < 0)
1673 return err;
1674 }
1675
1676 if (tb[IFLA_VF_SPOOFCHK]) {
1677 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1678
1679 err = -EOPNOTSUPP;
1680 if (ops->ndo_set_vf_spoofchk)
1681 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1682 ivs->setting);
1683 if (err < 0)
1684 return err;
1685 }
1686
1687 if (tb[IFLA_VF_LINK_STATE]) {
1688 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1689
1690 err = -EOPNOTSUPP;
1691 if (ops->ndo_set_vf_link_state)
1692 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1693 ivl->link_state);
1694 if (err < 0)
1695 return err;
1696 }
1697
1698 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1699 struct ifla_vf_rss_query_en *ivrssq_en;
1700
1701 err = -EOPNOTSUPP;
1702 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1703 if (ops->ndo_set_vf_rss_query_en)
1704 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1705 ivrssq_en->setting);
1706 if (err < 0)
1707 return err;
1708 }
1709
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001710 if (tb[IFLA_VF_TRUST]) {
1711 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1712
1713 err = -EOPNOTSUPP;
1714 if (ops->ndo_set_vf_trust)
1715 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1716 if (err < 0)
1717 return err;
1718 }
1719
Eli Cohencc8e27c2016-03-11 22:58:34 +02001720 if (tb[IFLA_VF_IB_NODE_GUID]) {
1721 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1722
1723 if (!ops->ndo_set_vf_guid)
1724 return -EOPNOTSUPP;
1725
1726 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1727 }
1728
1729 if (tb[IFLA_VF_IB_PORT_GUID]) {
1730 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1731
1732 if (!ops->ndo_set_vf_guid)
1733 return -EOPNOTSUPP;
1734
1735 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1736 }
1737
Chris Wrightc02db8c2010-05-16 01:05:45 -07001738 return err;
1739}
1740
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001741static int do_set_master(struct net_device *dev, int ifindex)
1742{
Jiri Pirko898e5062013-01-03 22:48:52 +00001743 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001744 const struct net_device_ops *ops;
1745 int err;
1746
Jiri Pirko898e5062013-01-03 22:48:52 +00001747 if (upper_dev) {
1748 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001749 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001750 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001751 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001752 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001753 if (err)
1754 return err;
1755 } else {
1756 return -EOPNOTSUPP;
1757 }
1758 }
1759
1760 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001761 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1762 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001763 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001764 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001765 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001766 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001767 if (err)
1768 return err;
1769 } else {
1770 return -EOPNOTSUPP;
1771 }
1772 }
1773 return 0;
1774}
1775
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001776#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001777/* notify flag means notify + modified. */
1778#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001779static int do_setlink(const struct sk_buff *skb,
1780 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001781 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001782{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001783 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001784 int err;
1785
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001786 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001787 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001788 if (IS_ERR(net)) {
1789 err = PTR_ERR(net);
1790 goto errout;
1791 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001792 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001793 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001794 err = -EPERM;
1795 goto errout;
1796 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001797 err = dev_change_net_namespace(dev, net, ifname);
1798 put_net(net);
1799 if (err)
1800 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001801 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001802 }
1803
Patrick McHardy0157f602007-06-13 12:03:36 -07001804 if (tb[IFLA_MAP]) {
1805 struct rtnl_link_ifmap *u_map;
1806 struct ifmap k_map;
1807
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001808 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001809 err = -EOPNOTSUPP;
1810 goto errout;
1811 }
1812
1813 if (!netif_device_present(dev)) {
1814 err = -ENODEV;
1815 goto errout;
1816 }
1817
1818 u_map = nla_data(tb[IFLA_MAP]);
1819 k_map.mem_start = (unsigned long) u_map->mem_start;
1820 k_map.mem_end = (unsigned long) u_map->mem_end;
1821 k_map.base_addr = (unsigned short) u_map->base_addr;
1822 k_map.irq = (unsigned char) u_map->irq;
1823 k_map.dma = (unsigned char) u_map->dma;
1824 k_map.port = (unsigned char) u_map->port;
1825
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001826 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001827 if (err < 0)
1828 goto errout;
1829
Nicolas Dichtelba998902014-09-01 16:07:29 +02001830 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001831 }
1832
1833 if (tb[IFLA_ADDRESS]) {
1834 struct sockaddr *sa;
1835 int len;
1836
Patrick McHardy0157f602007-06-13 12:03:36 -07001837 len = sizeof(sa_family_t) + dev->addr_len;
1838 sa = kmalloc(len, GFP_KERNEL);
1839 if (!sa) {
1840 err = -ENOMEM;
1841 goto errout;
1842 }
1843 sa->sa_family = dev->type;
1844 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1845 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001846 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001847 kfree(sa);
1848 if (err)
1849 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001850 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001851 }
1852
1853 if (tb[IFLA_MTU]) {
1854 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1855 if (err < 0)
1856 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001857 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001858 }
1859
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001860 if (tb[IFLA_GROUP]) {
1861 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001862 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001863 }
1864
Patrick McHardy0157f602007-06-13 12:03:36 -07001865 /*
1866 * Interface selected by interface index but interface
1867 * name provided implies that a name change has been
1868 * requested.
1869 */
1870 if (ifm->ifi_index > 0 && ifname[0]) {
1871 err = dev_change_name(dev, ifname);
1872 if (err < 0)
1873 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001874 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001875 }
1876
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001877 if (tb[IFLA_IFALIAS]) {
1878 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1879 nla_len(tb[IFLA_IFALIAS]));
1880 if (err < 0)
1881 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001882 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001883 }
1884
Patrick McHardy0157f602007-06-13 12:03:36 -07001885 if (tb[IFLA_BROADCAST]) {
1886 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001887 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001888 }
1889
1890 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001891 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001892 if (err < 0)
1893 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001894 }
1895
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001896 if (tb[IFLA_MASTER]) {
1897 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1898 if (err)
1899 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001900 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001901 }
1902
Jiri Pirko9a572472012-12-27 23:49:39 +00001903 if (tb[IFLA_CARRIER]) {
1904 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1905 if (err)
1906 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001907 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001908 }
1909
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001910 if (tb[IFLA_TXQLEN]) {
1911 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1912
1913 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001914 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001915
1916 dev->tx_queue_len = value;
1917 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001918
Patrick McHardy0157f602007-06-13 12:03:36 -07001919 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001920 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001921
1922 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001923 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1924
David S. Miller93b2d4a2008-02-17 18:35:07 -08001925 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001926 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001927 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001928 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001929 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001930 }
1931
Chris Wrightc02db8c2010-05-16 01:05:45 -07001932 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001933 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001934 struct nlattr *attr;
1935 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001936
Chris Wrightc02db8c2010-05-16 01:05:45 -07001937 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001938 if (nla_type(attr) != IFLA_VF_INFO ||
1939 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001940 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001941 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001942 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001943 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1944 ifla_vf_policy);
1945 if (err < 0)
1946 goto errout;
1947 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001948 if (err < 0)
1949 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001950 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001951 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001952 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001953 err = 0;
1954
Scott Feldman57b61082010-05-17 22:49:55 -07001955 if (tb[IFLA_VF_PORTS]) {
1956 struct nlattr *port[IFLA_PORT_MAX+1];
1957 struct nlattr *attr;
1958 int vf;
1959 int rem;
1960
1961 err = -EOPNOTSUPP;
1962 if (!ops->ndo_set_vf_port)
1963 goto errout;
1964
1965 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001966 if (nla_type(attr) != IFLA_VF_PORT ||
1967 nla_len(attr) < NLA_HDRLEN) {
1968 err = -EINVAL;
1969 goto errout;
1970 }
1971 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1972 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001973 if (err < 0)
1974 goto errout;
1975 if (!port[IFLA_PORT_VF]) {
1976 err = -EOPNOTSUPP;
1977 goto errout;
1978 }
1979 vf = nla_get_u32(port[IFLA_PORT_VF]);
1980 err = ops->ndo_set_vf_port(dev, vf, port);
1981 if (err < 0)
1982 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001983 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001984 }
1985 }
1986 err = 0;
1987
1988 if (tb[IFLA_PORT_SELF]) {
1989 struct nlattr *port[IFLA_PORT_MAX+1];
1990
1991 err = nla_parse_nested(port, IFLA_PORT_MAX,
1992 tb[IFLA_PORT_SELF], ifla_port_policy);
1993 if (err < 0)
1994 goto errout;
1995
1996 err = -EOPNOTSUPP;
1997 if (ops->ndo_set_vf_port)
1998 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1999 if (err < 0)
2000 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002001 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002002 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002003
2004 if (tb[IFLA_AF_SPEC]) {
2005 struct nlattr *af;
2006 int rem;
2007
2008 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2009 const struct rtnl_af_ops *af_ops;
2010
2011 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002012 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00002013
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002014 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00002015 if (err < 0)
2016 goto errout;
2017
Nicolas Dichtelba998902014-09-01 16:07:29 +02002018 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002019 }
2020 }
Scott Feldman57b61082010-05-17 22:49:55 -07002021 err = 0;
2022
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002023 if (tb[IFLA_PROTO_DOWN]) {
2024 err = dev_change_proto_down(dev,
2025 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2026 if (err)
2027 goto errout;
2028 status |= DO_SETLINK_NOTIFY;
2029 }
2030
Patrick McHardy0157f602007-06-13 12:03:36 -07002031errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002032 if (status & DO_SETLINK_MODIFIED) {
2033 if (status & DO_SETLINK_NOTIFY)
2034 netdev_state_change(dev);
2035
2036 if (err < 0)
2037 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",
2038 dev->name);
2039 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002040
Patrick McHardy0157f602007-06-13 12:03:36 -07002041 return err;
2042}
2043
Thomas Graf661d2962013-03-21 07:45:29 +00002044static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002046 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002047 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002049 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002050 struct nlattr *tb[IFLA_MAX+1];
2051 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Thomas Grafda5e0492006-08-10 21:17:37 -07002053 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2054 if (err < 0)
2055 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Thomas Graf5176f912006-08-26 20:13:18 -07002057 if (tb[IFLA_IFNAME])
2058 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002059 else
2060 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002063 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002064 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002065 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002066 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002067 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002068 else
2069 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Thomas Grafda5e0492006-08-10 21:17:37 -07002071 if (dev == NULL) {
2072 err = -ENODEV;
2073 goto errout;
2074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Eric Dumazete0d087a2009-11-07 01:26:17 -08002076 err = validate_linkmsg(dev, tb);
2077 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002078 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002079
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002080 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002081errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return err;
2083}
2084
WANG Cong66400d52015-03-24 11:53:31 -07002085static int rtnl_group_dellink(const struct net *net, int group)
2086{
2087 struct net_device *dev, *aux;
2088 LIST_HEAD(list_kill);
2089 bool found = false;
2090
2091 if (!group)
2092 return -EPERM;
2093
2094 for_each_netdev(net, dev) {
2095 if (dev->group == group) {
2096 const struct rtnl_link_ops *ops;
2097
2098 found = true;
2099 ops = dev->rtnl_link_ops;
2100 if (!ops || !ops->dellink)
2101 return -EOPNOTSUPP;
2102 }
2103 }
2104
2105 if (!found)
2106 return -ENODEV;
2107
2108 for_each_netdev_safe(net, dev, aux) {
2109 if (dev->group == group) {
2110 const struct rtnl_link_ops *ops;
2111
2112 ops = dev->rtnl_link_ops;
2113 ops->dellink(dev, &list_kill);
2114 }
2115 }
2116 unregister_netdevice_many(&list_kill);
2117
2118 return 0;
2119}
2120
Thomas Graf614732e2015-07-21 10:44:06 +02002121int rtnl_delete_link(struct net_device *dev)
2122{
2123 const struct rtnl_link_ops *ops;
2124 LIST_HEAD(list_kill);
2125
2126 ops = dev->rtnl_link_ops;
2127 if (!ops || !ops->dellink)
2128 return -EOPNOTSUPP;
2129
2130 ops->dellink(dev, &list_kill);
2131 unregister_netdevice_many(&list_kill);
2132
2133 return 0;
2134}
2135EXPORT_SYMBOL_GPL(rtnl_delete_link);
2136
Thomas Graf661d2962013-03-21 07:45:29 +00002137static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002138{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002139 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002140 struct net_device *dev;
2141 struct ifinfomsg *ifm;
2142 char ifname[IFNAMSIZ];
2143 struct nlattr *tb[IFLA_MAX+1];
2144 int err;
2145
2146 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2147 if (err < 0)
2148 return err;
2149
2150 if (tb[IFLA_IFNAME])
2151 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2152
2153 ifm = nlmsg_data(nlh);
2154 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002155 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002156 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002157 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002158 else if (tb[IFLA_GROUP])
2159 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002160 else
2161 return -EINVAL;
2162
2163 if (!dev)
2164 return -ENODEV;
2165
Thomas Graf614732e2015-07-21 10:44:06 +02002166 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002167}
2168
Patrick McHardy3729d502010-02-26 06:34:54 +00002169int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2170{
2171 unsigned int old_flags;
2172 int err;
2173
2174 old_flags = dev->flags;
2175 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2176 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2177 if (err < 0)
2178 return err;
2179 }
2180
2181 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002182
Nicolas Dichtela528c212013-09-25 12:02:44 +02002183 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002184 return 0;
2185}
2186EXPORT_SYMBOL(rtnl_configure_link);
2187
Rami Rosenc0713562012-11-30 01:08:47 +00002188struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002189 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002190 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002191{
2192 int err;
2193 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002194 unsigned int num_tx_queues = 1;
2195 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002196
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002197 if (tb[IFLA_NUM_TX_QUEUES])
2198 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2199 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002200 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002201
2202 if (tb[IFLA_NUM_RX_QUEUES])
2203 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2204 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002205 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002206
Pavel Emelianove7199282007-08-08 22:16:38 -07002207 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002208 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002209 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002210 if (!dev)
2211 goto err;
2212
Eric W. Biederman81adee42009-11-08 00:53:51 -08002213 dev_net_set(dev, net);
2214 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002215 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002216
Pavel Emelianove7199282007-08-08 22:16:38 -07002217 if (tb[IFLA_MTU])
2218 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002219 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002220 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2221 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002222 dev->addr_assign_type = NET_ADDR_SET;
2223 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002224 if (tb[IFLA_BROADCAST])
2225 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2226 nla_len(tb[IFLA_BROADCAST]));
2227 if (tb[IFLA_TXQLEN])
2228 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2229 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002230 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002231 if (tb[IFLA_LINKMODE])
2232 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002233 if (tb[IFLA_GROUP])
2234 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002235
2236 return dev;
2237
Pavel Emelianove7199282007-08-08 22:16:38 -07002238err:
2239 return ERR_PTR(err);
2240}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002241EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002242
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002243static int rtnl_group_changelink(const struct sk_buff *skb,
2244 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002245 struct ifinfomsg *ifm,
2246 struct nlattr **tb)
2247{
WANG Congd0795352015-03-23 16:31:09 -07002248 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002249 int err;
2250
WANG Congd0795352015-03-23 16:31:09 -07002251 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002252 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002253 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002254 if (err < 0)
2255 return err;
2256 }
2257 }
2258
2259 return 0;
2260}
2261
Thomas Graf661d2962013-03-21 07:45:29 +00002262static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002263{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002264 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002265 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002266 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002267 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002268 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002269 struct ifinfomsg *ifm;
2270 char kind[MODULE_NAME_LEN];
2271 char ifname[IFNAMSIZ];
2272 struct nlattr *tb[IFLA_MAX+1];
2273 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002274 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002275 int err;
2276
Johannes Berg95a5afc2008-10-16 15:24:51 -07002277#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002278replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002279#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002280 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2281 if (err < 0)
2282 return err;
2283
2284 if (tb[IFLA_IFNAME])
2285 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2286 else
2287 ifname[0] = '\0';
2288
2289 ifm = nlmsg_data(nlh);
2290 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002291 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002292 else {
2293 if (ifname[0])
2294 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002295 else
2296 dev = NULL;
2297 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002298
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002299 if (dev) {
2300 master_dev = netdev_master_upper_dev_get(dev);
2301 if (master_dev)
2302 m_ops = master_dev->rtnl_link_ops;
2303 }
2304
Eric Dumazete0d087a2009-11-07 01:26:17 -08002305 err = validate_linkmsg(dev, tb);
2306 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002307 return err;
2308
Patrick McHardy38f7b872007-06-13 12:03:51 -07002309 if (tb[IFLA_LINKINFO]) {
2310 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2311 tb[IFLA_LINKINFO], ifla_info_policy);
2312 if (err < 0)
2313 return err;
2314 } else
2315 memset(linkinfo, 0, sizeof(linkinfo));
2316
2317 if (linkinfo[IFLA_INFO_KIND]) {
2318 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2319 ops = rtnl_link_ops_get(kind);
2320 } else {
2321 kind[0] = '\0';
2322 ops = NULL;
2323 }
2324
2325 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002326 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2327 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002328 struct nlattr **data = NULL;
2329 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002330 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002331
2332 if (ops) {
2333 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2334 err = nla_parse_nested(attr, ops->maxtype,
2335 linkinfo[IFLA_INFO_DATA],
2336 ops->policy);
2337 if (err < 0)
2338 return err;
2339 data = attr;
2340 }
2341 if (ops->validate) {
2342 err = ops->validate(tb, data);
2343 if (err < 0)
2344 return err;
2345 }
2346 }
2347
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002348 if (m_ops) {
2349 if (m_ops->slave_maxtype &&
2350 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2351 err = nla_parse_nested(slave_attr,
2352 m_ops->slave_maxtype,
2353 linkinfo[IFLA_INFO_SLAVE_DATA],
2354 m_ops->slave_policy);
2355 if (err < 0)
2356 return err;
2357 slave_data = slave_attr;
2358 }
2359 if (m_ops->slave_validate) {
2360 err = m_ops->slave_validate(tb, slave_data);
2361 if (err < 0)
2362 return err;
2363 }
2364 }
2365
Patrick McHardy38f7b872007-06-13 12:03:51 -07002366 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002367 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002368
2369 if (nlh->nlmsg_flags & NLM_F_EXCL)
2370 return -EEXIST;
2371 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2372 return -EOPNOTSUPP;
2373
2374 if (linkinfo[IFLA_INFO_DATA]) {
2375 if (!ops || ops != dev->rtnl_link_ops ||
2376 !ops->changelink)
2377 return -EOPNOTSUPP;
2378
2379 err = ops->changelink(dev, tb, data);
2380 if (err < 0)
2381 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002382 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002383 }
2384
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002385 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2386 if (!m_ops || !m_ops->slave_changelink)
2387 return -EOPNOTSUPP;
2388
2389 err = m_ops->slave_changelink(master_dev, dev,
2390 tb, slave_data);
2391 if (err < 0)
2392 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002393 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002394 }
2395
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002396 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002397 }
2398
Patrick McHardyffa934f2011-01-20 03:00:42 +00002399 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2400 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002401 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002402 nla_get_u32(tb[IFLA_GROUP]),
2403 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002404 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002405 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002406
Patrick McHardy0e068772007-07-11 19:42:31 -07002407 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002408 return -EOPNOTSUPP;
2409
2410 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002411#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002412 if (kind[0]) {
2413 __rtnl_unlock();
2414 request_module("rtnl-link-%s", kind);
2415 rtnl_lock();
2416 ops = rtnl_link_ops_get(kind);
2417 if (ops)
2418 goto replay;
2419 }
2420#endif
2421 return -EOPNOTSUPP;
2422 }
2423
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002424 if (!ops->setup)
2425 return -EOPNOTSUPP;
2426
Tom Gundersen55177502014-07-14 16:37:25 +02002427 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002428 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002429 name_assign_type = NET_NAME_ENUM;
2430 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002431
Eric W. Biederman81adee42009-11-08 00:53:51 -08002432 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002433 if (IS_ERR(dest_net))
2434 return PTR_ERR(dest_net);
2435
Eric W. Biederman505ce412015-02-26 16:19:00 -06002436 err = -EPERM;
2437 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2438 goto out;
2439
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002440 if (tb[IFLA_LINK_NETNSID]) {
2441 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2442
2443 link_net = get_net_ns_by_id(dest_net, id);
2444 if (!link_net) {
2445 err = -EINVAL;
2446 goto out;
2447 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002448 err = -EPERM;
2449 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2450 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002451 }
2452
2453 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2454 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002455 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002456 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002457 goto out;
2458 }
2459
2460 dev->ifindex = ifm->ifi_index;
2461
Cong Wang0e0eee22014-02-11 15:51:30 -08002462 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002463 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002464 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002465 * and unregister it on failure after registration
2466 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002467 */
Cong Wange51fb152014-06-03 16:40:47 -07002468 if (err < 0) {
2469 /* If device is not registered at all, free it now */
2470 if (dev->reg_state == NETREG_UNINITIALIZED)
2471 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002472 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002473 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002474 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002475 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002476 if (err < 0) {
2477 free_netdev(dev);
2478 goto out;
2479 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002480 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002481 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002482 if (err < 0)
2483 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002484 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002485 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002486 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002487 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002488 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002489out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002490 if (link_net)
2491 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002492 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002493 return err;
David S. Miller43638902015-03-10 21:58:32 -04002494out_unregister:
2495 if (ops->newlink) {
2496 LIST_HEAD(list_kill);
2497
2498 ops->dellink(dev, &list_kill);
2499 unregister_netdevice_many(&list_kill);
2500 } else {
2501 unregister_netdevice(dev);
2502 }
2503 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002504 }
2505}
2506
Thomas Graf661d2962013-03-21 07:45:29 +00002507static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002508{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002509 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002510 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002511 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002512 struct nlattr *tb[IFLA_MAX+1];
2513 struct net_device *dev = NULL;
2514 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002515 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002516 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002517
Thomas Grafb60c5112006-08-04 23:05:34 -07002518 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2519 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002520 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002521
Eric Dumazeta3d12892009-10-21 10:59:31 +00002522 if (tb[IFLA_IFNAME])
2523 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2524
Greg Rose115c9b82012-02-21 16:54:48 -05002525 if (tb[IFLA_EXT_MASK])
2526 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2527
Thomas Grafb60c5112006-08-04 23:05:34 -07002528 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002529 if (ifm->ifi_index > 0)
2530 dev = __dev_get_by_index(net, ifm->ifi_index);
2531 else if (tb[IFLA_IFNAME])
2532 dev = __dev_get_by_name(net, ifname);
2533 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002534 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002535
Eric Dumazeta3d12892009-10-21 10:59:31 +00002536 if (dev == NULL)
2537 return -ENODEV;
2538
Greg Rose115c9b82012-02-21 16:54:48 -05002539 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002540 if (nskb == NULL)
2541 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002542
Eric W. Biederman15e47302012-09-07 20:12:54 +00002543 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002544 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002545 if (err < 0) {
2546 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2547 WARN_ON(err == -EMSGSIZE);
2548 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002549 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002550 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002551
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002552 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002553}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002554
Greg Rose115c9b82012-02-21 16:54:48 -05002555static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002556{
Greg Rose115c9b82012-02-21 16:54:48 -05002557 struct net *net = sock_net(skb->sk);
2558 struct net_device *dev;
2559 struct nlattr *tb[IFLA_MAX+1];
2560 u32 ext_filter_mask = 0;
2561 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002562 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002563
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002564 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2565 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2566 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2567
2568 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002569 if (tb[IFLA_EXT_MASK])
2570 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2571 }
Greg Rose115c9b82012-02-21 16:54:48 -05002572
2573 if (!ext_filter_mask)
2574 return NLMSG_GOODSIZE;
2575 /*
2576 * traverse the list of net devices and compute the minimum
2577 * buffer size based upon the filter mask.
2578 */
2579 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2580 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2581 if_nlmsg_size(dev,
2582 ext_filter_mask));
2583 }
2584
Greg Rosec7ac8672011-06-10 01:27:09 +00002585 return min_ifinfo_dump_size;
2586}
2587
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002588static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 int idx;
2591 int s_idx = cb->family;
2592
2593 if (s_idx == 0)
2594 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002595 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int type = cb->nlh->nlmsg_type-RTM_BASE;
2597 if (idx < s_idx || idx == PF_PACKET)
2598 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002599 if (rtnl_msg_handlers[idx] == NULL ||
2600 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002602 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002604 cb->prev_seq = 0;
2605 cb->seq = 0;
2606 }
Thomas Grafe2849862007-03-22 11:48:11 -07002607 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 break;
2609 }
2610 cb->family = idx;
2611
2612 return skb->len;
2613}
2614
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002615struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2616 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002618 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002620 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002621 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002623 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002624 if (skb == NULL)
2625 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Greg Rose115c9b82012-02-21 16:54:48 -05002627 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002628 if (err < 0) {
2629 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2630 WARN_ON(err == -EMSGSIZE);
2631 kfree_skb(skb);
2632 goto errout;
2633 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002634 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002635errout:
2636 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002637 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002638 return NULL;
2639}
2640
2641void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2642{
2643 struct net *net = dev_net(dev);
2644
2645 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2646}
2647
2648void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2649 gfp_t flags)
2650{
2651 struct sk_buff *skb;
2652
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002653 if (dev->reg_state != NETREG_REGISTERED)
2654 return;
2655
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002656 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2657 if (skb)
2658 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002660EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
John Fastabendd83b0602012-04-15 06:44:08 +00002662static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2663 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002664 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002665 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002666 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002667{
2668 struct nlmsghdr *nlh;
2669 struct ndmsg *ndm;
2670
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002671 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002672 if (!nlh)
2673 return -EMSGSIZE;
2674
2675 ndm = nlmsg_data(nlh);
2676 ndm->ndm_family = AF_BRIDGE;
2677 ndm->ndm_pad1 = 0;
2678 ndm->ndm_pad2 = 0;
2679 ndm->ndm_flags = flags;
2680 ndm->ndm_type = 0;
2681 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002682 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002683
2684 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2685 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002686 if (vid)
2687 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2688 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002689
Johannes Berg053c0952015-01-16 22:09:00 +01002690 nlmsg_end(skb, nlh);
2691 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002692
2693nla_put_failure:
2694 nlmsg_cancel(skb, nlh);
2695 return -EMSGSIZE;
2696}
2697
John Fastabend3ff661c2012-04-15 06:44:14 +00002698static inline size_t rtnl_fdb_nlmsg_size(void)
2699{
2700 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2701}
2702
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002703static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2704 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002705{
2706 struct net *net = dev_net(dev);
2707 struct sk_buff *skb;
2708 int err = -ENOBUFS;
2709
2710 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2711 if (!skb)
2712 goto errout;
2713
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002714 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002715 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002716 if (err < 0) {
2717 kfree_skb(skb);
2718 goto errout;
2719 }
2720
2721 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2722 return;
2723errout:
2724 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2725}
2726
Vlad Yasevich090096b2013-03-06 15:39:42 +00002727/**
2728 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2729 */
2730int ndo_dflt_fdb_add(struct ndmsg *ndm,
2731 struct nlattr *tb[],
2732 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002733 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002734 u16 flags)
2735{
2736 int err = -EINVAL;
2737
2738 /* If aging addresses are supported device will need to
2739 * implement its own handler for this.
2740 */
2741 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2742 pr_info("%s: FDB only supports static addresses\n", dev->name);
2743 return err;
2744 }
2745
Or Gerlitz65891fe2014-12-14 18:19:05 +02002746 if (vid) {
2747 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2748 return err;
2749 }
2750
Vlad Yasevich090096b2013-03-06 15:39:42 +00002751 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2752 err = dev_uc_add_excl(dev, addr);
2753 else if (is_multicast_ether_addr(addr))
2754 err = dev_mc_add_excl(dev, addr);
2755
2756 /* Only return duplicate errors if NLM_F_EXCL is set */
2757 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2758 err = 0;
2759
2760 return err;
2761}
2762EXPORT_SYMBOL(ndo_dflt_fdb_add);
2763
Jiri Pirkof6f64242014-11-28 14:34:15 +01002764static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2765{
2766 u16 vid = 0;
2767
2768 if (vlan_attr) {
2769 if (nla_len(vlan_attr) != sizeof(u16)) {
2770 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2771 return -EINVAL;
2772 }
2773
2774 vid = nla_get_u16(vlan_attr);
2775
2776 if (!vid || vid >= VLAN_VID_MASK) {
2777 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2778 vid);
2779 return -EINVAL;
2780 }
2781 }
2782 *p_vid = vid;
2783 return 0;
2784}
2785
Thomas Graf661d2962013-03-21 07:45:29 +00002786static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002787{
2788 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002789 struct ndmsg *ndm;
2790 struct nlattr *tb[NDA_MAX+1];
2791 struct net_device *dev;
2792 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002793 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002794 int err;
2795
2796 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2797 if (err < 0)
2798 return err;
2799
2800 ndm = nlmsg_data(nlh);
2801 if (ndm->ndm_ifindex == 0) {
2802 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2803 return -EINVAL;
2804 }
2805
2806 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2807 if (dev == NULL) {
2808 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2809 return -ENODEV;
2810 }
2811
2812 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2813 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2814 return -EINVAL;
2815 }
2816
2817 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002818
Jiri Pirkof6f64242014-11-28 14:34:15 +01002819 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2820 if (err)
2821 return err;
2822
John Fastabend77162022012-04-15 06:43:56 +00002823 err = -EOPNOTSUPP;
2824
2825 /* Support fdb on master device the net/bridge default case */
2826 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2827 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002828 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2829 const struct net_device_ops *ops = br_dev->netdev_ops;
2830
Jiri Pirkof6f64242014-11-28 14:34:15 +01002831 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2832 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002833 if (err)
2834 goto out;
2835 else
2836 ndm->ndm_flags &= ~NTF_MASTER;
2837 }
2838
2839 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002840 if ((ndm->ndm_flags & NTF_SELF)) {
2841 if (dev->netdev_ops->ndo_fdb_add)
2842 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002843 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002844 nlh->nlmsg_flags);
2845 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002846 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002847 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002848
John Fastabend3ff661c2012-04-15 06:44:14 +00002849 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002850 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2851 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002852 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002853 }
John Fastabend77162022012-04-15 06:43:56 +00002854 }
2855out:
2856 return err;
2857}
2858
Vlad Yasevich090096b2013-03-06 15:39:42 +00002859/**
2860 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2861 */
2862int ndo_dflt_fdb_del(struct ndmsg *ndm,
2863 struct nlattr *tb[],
2864 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002865 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002866{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002867 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002868
2869 /* If aging addresses are supported device will need to
2870 * implement its own handler for this.
2871 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002872 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002873 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002874 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002875 }
2876
2877 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2878 err = dev_uc_del(dev, addr);
2879 else if (is_multicast_ether_addr(addr))
2880 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002881
2882 return err;
2883}
2884EXPORT_SYMBOL(ndo_dflt_fdb_del);
2885
Thomas Graf661d2962013-03-21 07:45:29 +00002886static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002887{
2888 struct net *net = sock_net(skb->sk);
2889 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002890 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002891 struct net_device *dev;
2892 int err = -EINVAL;
2893 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002894 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002895
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002896 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002897 return -EPERM;
2898
2899 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2900 if (err < 0)
2901 return err;
John Fastabend77162022012-04-15 06:43:56 +00002902
2903 ndm = nlmsg_data(nlh);
2904 if (ndm->ndm_ifindex == 0) {
2905 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2906 return -EINVAL;
2907 }
2908
2909 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2910 if (dev == NULL) {
2911 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2912 return -ENODEV;
2913 }
2914
Vlad Yasevich1690be62013-02-13 12:00:18 +00002915 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2916 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002917 return -EINVAL;
2918 }
2919
Vlad Yasevich1690be62013-02-13 12:00:18 +00002920 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002921
Jiri Pirkof6f64242014-11-28 14:34:15 +01002922 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2923 if (err)
2924 return err;
2925
John Fastabend77162022012-04-15 06:43:56 +00002926 err = -EOPNOTSUPP;
2927
2928 /* Support fdb on master device the net/bridge default case */
2929 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2930 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002931 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2932 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002933
Jiri Pirko898e5062013-01-03 22:48:52 +00002934 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002935 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002936
2937 if (err)
2938 goto out;
2939 else
2940 ndm->ndm_flags &= ~NTF_MASTER;
2941 }
2942
2943 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002944 if (ndm->ndm_flags & NTF_SELF) {
2945 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002946 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2947 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002948 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002949 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002950
John Fastabend3ff661c2012-04-15 06:44:14 +00002951 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002952 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2953 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002954 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002955 }
John Fastabend77162022012-04-15 06:43:56 +00002956 }
2957out:
2958 return err;
2959}
2960
John Fastabendd83b0602012-04-15 06:44:08 +00002961static int nlmsg_populate_fdb(struct sk_buff *skb,
2962 struct netlink_callback *cb,
2963 struct net_device *dev,
2964 int *idx,
2965 struct netdev_hw_addr_list *list)
2966{
2967 struct netdev_hw_addr *ha;
2968 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002969 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002970
Eric W. Biederman15e47302012-09-07 20:12:54 +00002971 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002972 seq = cb->nlh->nlmsg_seq;
2973
2974 list_for_each_entry(ha, &list->list, list) {
2975 if (*idx < cb->args[0])
2976 goto skip;
2977
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002978 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002979 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002980 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002981 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00002982 if (err < 0)
2983 return err;
2984skip:
2985 *idx += 1;
2986 }
2987 return 0;
2988}
2989
2990/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002991 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002992 * @nlh: netlink message header
2993 * @dev: netdevice
2994 *
2995 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002996 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002997 */
2998int ndo_dflt_fdb_dump(struct sk_buff *skb,
2999 struct netlink_callback *cb,
3000 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003001 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00003002 int idx)
3003{
3004 int err;
3005
3006 netif_addr_lock_bh(dev);
3007 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3008 if (err)
3009 goto out;
3010 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3011out:
3012 netif_addr_unlock_bh(dev);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003013 cb->args[1] = err;
John Fastabendd83b0602012-04-15 06:44:08 +00003014 return idx;
3015}
3016EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3017
John Fastabend77162022012-04-15 06:43:56 +00003018static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3019{
John Fastabend77162022012-04-15 06:43:56 +00003020 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003021 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003022 struct net_device *br_dev = NULL;
3023 const struct net_device_ops *ops = NULL;
3024 const struct net_device_ops *cops = NULL;
3025 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3026 struct net *net = sock_net(skb->sk);
3027 int brport_idx = 0;
3028 int br_idx = 0;
3029 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003030
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003031 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3032 ifla_policy) == 0) {
3033 if (tb[IFLA_MASTER])
3034 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3035 }
John Fastabend77162022012-04-15 06:43:56 +00003036
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003037 brport_idx = ifm->ifi_index;
3038
3039 if (br_idx) {
3040 br_dev = __dev_get_by_index(net, br_idx);
3041 if (!br_dev)
3042 return -ENODEV;
3043
3044 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003045 }
3046
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003047 cb->args[1] = 0;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003048 for_each_netdev(net, dev) {
3049 if (brport_idx && (dev->ifindex != brport_idx))
3050 continue;
3051
3052 if (!br_idx) { /* user did not specify a specific bridge */
3053 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3054 br_dev = netdev_master_upper_dev_get(dev);
3055 cops = br_dev->netdev_ops;
3056 }
3057
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003058 } else {
3059 if (dev != br_dev &&
3060 !(dev->priv_flags & IFF_BRIDGE_PORT))
3061 continue;
3062
3063 if (br_dev != netdev_master_upper_dev_get(dev) &&
3064 !(dev->priv_flags & IFF_EBRIDGE))
3065 continue;
3066
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003067 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00003068 }
3069
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003070 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3071 if (cops && cops->ndo_fdb_dump)
3072 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3073 idx);
3074 }
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003075 if (cb->args[1] == -EMSGSIZE)
3076 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003077
John Fastabend77162022012-04-15 06:43:56 +00003078 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003079 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003080 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003081 else
3082 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
MINOURA Makoto / 箕浦 真472681d2016-02-25 14:20:48 +09003083 if (cb->args[1] == -EMSGSIZE)
3084 break;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003085
3086 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00003087 }
John Fastabend77162022012-04-15 06:43:56 +00003088
3089 cb->args[0] = idx;
3090 return skb->len;
3091}
3092
Scott Feldman2c3c0312014-11-28 14:34:25 +01003093static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3094 unsigned int attrnum, unsigned int flag)
3095{
3096 if (mask & flag)
3097 return nla_put_u8(skb, attrnum, !!(flags & flag));
3098 return 0;
3099}
3100
John Fastabend815cccb2012-10-24 08:13:09 +00003101int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003102 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003103 u32 flags, u32 mask, int nlflags,
3104 u32 filter_mask,
3105 int (*vlan_fill)(struct sk_buff *skb,
3106 struct net_device *dev,
3107 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003108{
3109 struct nlmsghdr *nlh;
3110 struct ifinfomsg *ifm;
3111 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003112 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003113 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003114 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003115 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003116
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003117 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003118 if (nlh == NULL)
3119 return -EMSGSIZE;
3120
3121 ifm = nlmsg_data(nlh);
3122 ifm->ifi_family = AF_BRIDGE;
3123 ifm->__ifi_pad = 0;
3124 ifm->ifi_type = dev->type;
3125 ifm->ifi_index = dev->ifindex;
3126 ifm->ifi_flags = dev_get_flags(dev);
3127 ifm->ifi_change = 0;
3128
3129
3130 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3131 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3132 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003133 (br_dev &&
3134 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003135 (dev->addr_len &&
3136 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003137 (dev->ifindex != dev_get_iflink(dev) &&
3138 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003139 goto nla_put_failure;
3140
3141 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3142 if (!br_afspec)
3143 goto nla_put_failure;
3144
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003145 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003146 nla_nest_cancel(skb, br_afspec);
3147 goto nla_put_failure;
3148 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003149
3150 if (mode != BRIDGE_MODE_UNDEF) {
3151 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3152 nla_nest_cancel(skb, br_afspec);
3153 goto nla_put_failure;
3154 }
3155 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003156 if (vlan_fill) {
3157 err = vlan_fill(skb, dev, filter_mask);
3158 if (err) {
3159 nla_nest_cancel(skb, br_afspec);
3160 goto nla_put_failure;
3161 }
3162 }
John Fastabend815cccb2012-10-24 08:13:09 +00003163 nla_nest_end(skb, br_afspec);
3164
Scott Feldman2c3c0312014-11-28 14:34:25 +01003165 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3166 if (!protinfo)
3167 goto nla_put_failure;
3168
3169 if (brport_nla_put_flag(skb, flags, mask,
3170 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3171 brport_nla_put_flag(skb, flags, mask,
3172 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3173 brport_nla_put_flag(skb, flags, mask,
3174 IFLA_BRPORT_FAST_LEAVE,
3175 BR_MULTICAST_FAST_LEAVE) ||
3176 brport_nla_put_flag(skb, flags, mask,
3177 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3178 brport_nla_put_flag(skb, flags, mask,
3179 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3180 brport_nla_put_flag(skb, flags, mask,
3181 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3182 brport_nla_put_flag(skb, flags, mask,
3183 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3184 brport_nla_put_flag(skb, flags, mask,
3185 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3186 nla_nest_cancel(skb, protinfo);
3187 goto nla_put_failure;
3188 }
3189
3190 nla_nest_end(skb, protinfo);
3191
Johannes Berg053c0952015-01-16 22:09:00 +01003192 nlmsg_end(skb, nlh);
3193 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003194nla_put_failure:
3195 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003196 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003197}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003198EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003199
John Fastabende5a55a82012-10-24 08:12:57 +00003200static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3201{
3202 struct net *net = sock_net(skb->sk);
3203 struct net_device *dev;
3204 int idx = 0;
3205 u32 portid = NETLINK_CB(cb->skb).portid;
3206 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003207 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003208 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003209
Thomas Grafaa68c202014-11-26 13:42:20 +01003210 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3211 struct nlattr *extfilt;
3212
3213 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3214 IFLA_EXT_MASK);
3215 if (extfilt) {
3216 if (nla_len(extfilt) < sizeof(filter_mask))
3217 return -EINVAL;
3218
3219 filter_mask = nla_get_u32(extfilt);
3220 }
3221 }
John Fastabende5a55a82012-10-24 08:12:57 +00003222
3223 rcu_read_lock();
3224 for_each_netdev_rcu(net, dev) {
3225 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003226 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003227
Jiri Pirko898e5062013-01-03 22:48:52 +00003228 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003229 if (idx >= cb->args[0]) {
3230 err = br_dev->netdev_ops->ndo_bridge_getlink(
3231 skb, portid, seq, dev,
3232 filter_mask, NLM_F_MULTI);
3233 if (err < 0 && err != -EOPNOTSUPP)
3234 break;
3235 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003236 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003237 }
3238
3239 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003240 if (idx >= cb->args[0]) {
3241 err = ops->ndo_bridge_getlink(skb, portid,
3242 seq, dev,
3243 filter_mask,
3244 NLM_F_MULTI);
3245 if (err < 0 && err != -EOPNOTSUPP)
3246 break;
3247 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003248 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003249 }
3250 }
3251 rcu_read_unlock();
3252 cb->args[0] = idx;
3253
3254 return skb->len;
3255}
3256
John Fastabend2469ffd2012-10-24 08:13:03 +00003257static inline size_t bridge_nlmsg_size(void)
3258{
3259 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3260 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3261 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3262 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3263 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3264 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3265 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3266 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3267 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3268 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3269 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3270}
3271
Roopa Prabhu02dba432015-01-14 20:02:25 -08003272static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003273{
3274 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003275 struct sk_buff *skb;
3276 int err = -EOPNOTSUPP;
3277
Roopa Prabhu02dba432015-01-14 20:02:25 -08003278 if (!dev->netdev_ops->ndo_bridge_getlink)
3279 return 0;
3280
John Fastabend2469ffd2012-10-24 08:13:03 +00003281 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3282 if (!skb) {
3283 err = -ENOMEM;
3284 goto errout;
3285 }
3286
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003287 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003288 if (err < 0)
3289 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003290
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003291 if (!skb->len)
3292 goto errout;
3293
John Fastabend2469ffd2012-10-24 08:13:03 +00003294 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3295 return 0;
3296errout:
3297 WARN_ON(err == -EMSGSIZE);
3298 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003299 if (err)
3300 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003301 return err;
3302}
3303
Thomas Graf661d2962013-03-21 07:45:29 +00003304static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003305{
3306 struct net *net = sock_net(skb->sk);
3307 struct ifinfomsg *ifm;
3308 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003309 struct nlattr *br_spec, *attr = NULL;
3310 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003311 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003312 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003313
3314 if (nlmsg_len(nlh) < sizeof(*ifm))
3315 return -EINVAL;
3316
3317 ifm = nlmsg_data(nlh);
3318 if (ifm->ifi_family != AF_BRIDGE)
3319 return -EPFNOSUPPORT;
3320
3321 dev = __dev_get_by_index(net, ifm->ifi_index);
3322 if (!dev) {
3323 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3324 return -ENODEV;
3325 }
3326
John Fastabend2469ffd2012-10-24 08:13:03 +00003327 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3328 if (br_spec) {
3329 nla_for_each_nested(attr, br_spec, rem) {
3330 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003331 if (nla_len(attr) < sizeof(flags))
3332 return -EINVAL;
3333
John Fastabendc38e01b2012-11-02 16:32:36 +00003334 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003335 flags = nla_get_u16(attr);
3336 break;
3337 }
3338 }
3339 }
3340
3341 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003342 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3343
3344 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003345 err = -EOPNOTSUPP;
3346 goto out;
3347 }
3348
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003349 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003350 if (err)
3351 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003352
3353 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003354 }
3355
John Fastabend2469ffd2012-10-24 08:13:03 +00003356 if ((flags & BRIDGE_FLAGS_SELF)) {
3357 if (!dev->netdev_ops->ndo_bridge_setlink)
3358 err = -EOPNOTSUPP;
3359 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003360 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3361 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003362 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003363 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003364
3365 /* Generate event to notify upper layer of bridge
3366 * change
3367 */
3368 err = rtnl_bridge_notify(dev);
3369 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003370 }
3371
John Fastabendc38e01b2012-11-02 16:32:36 +00003372 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003373 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003374out:
3375 return err;
3376}
3377
Thomas Graf661d2962013-03-21 07:45:29 +00003378static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003379{
3380 struct net *net = sock_net(skb->sk);
3381 struct ifinfomsg *ifm;
3382 struct net_device *dev;
3383 struct nlattr *br_spec, *attr = NULL;
3384 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003385 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003386 bool have_flags = false;
3387
3388 if (nlmsg_len(nlh) < sizeof(*ifm))
3389 return -EINVAL;
3390
3391 ifm = nlmsg_data(nlh);
3392 if (ifm->ifi_family != AF_BRIDGE)
3393 return -EPFNOSUPPORT;
3394
3395 dev = __dev_get_by_index(net, ifm->ifi_index);
3396 if (!dev) {
3397 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3398 return -ENODEV;
3399 }
3400
3401 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3402 if (br_spec) {
3403 nla_for_each_nested(attr, br_spec, rem) {
3404 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003405 if (nla_len(attr) < sizeof(flags))
3406 return -EINVAL;
3407
Vlad Yasevich407af322013-02-13 12:00:12 +00003408 have_flags = true;
3409 flags = nla_get_u16(attr);
3410 break;
3411 }
3412 }
3413 }
3414
Vlad Yasevich407af322013-02-13 12:00:12 +00003415 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3416 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3417
3418 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3419 err = -EOPNOTSUPP;
3420 goto out;
3421 }
3422
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003423 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003424 if (err)
3425 goto out;
3426
3427 flags &= ~BRIDGE_FLAGS_MASTER;
3428 }
3429
3430 if ((flags & BRIDGE_FLAGS_SELF)) {
3431 if (!dev->netdev_ops->ndo_bridge_dellink)
3432 err = -EOPNOTSUPP;
3433 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003434 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3435 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003436
Roopa Prabhu02dba432015-01-14 20:02:25 -08003437 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003438 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003439
3440 /* Generate event to notify upper layer of bridge
3441 * change
3442 */
3443 err = rtnl_bridge_notify(dev);
3444 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003445 }
3446
3447 if (have_flags)
3448 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003449out:
3450 return err;
3451}
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453/* Process one rtnetlink message. */
3454
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003455static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003457 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003458 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06003459 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 int family;
3461 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003462 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003466 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468 type -= RTM_BASE;
3469
3470 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003471 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return 0;
3473
Hong zhi guo573ce262013-03-27 06:47:04 +00003474 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 kind = type&3;
3476
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003477 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003478 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
David S. Millerb8f3ab42011-01-18 12:40:38 -08003480 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003481 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003482 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003483 rtnl_calcit_func calcit;
3484 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Thomas Grafe2849862007-03-22 11:48:11 -07003486 dumpit = rtnl_get_dumpit(family, type);
3487 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003488 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003489 calcit = rtnl_get_calcit(family, type);
3490 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003491 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Eric Dumazet2907c352011-05-25 07:34:04 +00003493 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003494 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003495 {
3496 struct netlink_dump_control c = {
3497 .dump = dumpit,
3498 .min_dump_alloc = min_dump_alloc,
3499 };
3500 err = netlink_dump_start(rtnl, skb, nlh, &c);
3501 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003502 rtnl_lock();
3503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505
Thomas Grafe2849862007-03-22 11:48:11 -07003506 doit = rtnl_get_doit(family, type);
3507 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003508 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Thomas Graf661d2962013-03-21 07:45:29 +00003510 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003513static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003515 rtnl_lock();
3516 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3517 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3521{
Jiri Pirko351638e2013-05-28 01:30:21 +00003522 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 case NETDEV_UP:
3526 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003527 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003528 case NETDEV_POST_INIT:
3529 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003531 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003533 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003534 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003535 case NETDEV_RELEASE:
3536 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003537 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 break;
3539 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003540 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 break;
3542 }
3543 return NOTIFY_DONE;
3544}
3545
3546static struct notifier_block rtnetlink_dev_notifier = {
3547 .notifier_call = rtnetlink_event,
3548};
3549
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003550
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003551static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003552{
3553 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003554 struct netlink_kernel_cfg cfg = {
3555 .groups = RTNLGRP_MAX,
3556 .input = rtnetlink_rcv,
3557 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003558 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003559 };
3560
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003561 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003562 if (!sk)
3563 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003564 net->rtnl = sk;
3565 return 0;
3566}
3567
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003568static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003569{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003570 netlink_kernel_release(net->rtnl);
3571 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003572}
3573
3574static struct pernet_operations rtnetlink_net_ops = {
3575 .init = rtnetlink_net_init,
3576 .exit = rtnetlink_net_exit,
3577};
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579void __init rtnetlink_init(void)
3580{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003581 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003585
Greg Rosec7ac8672011-06-10 01:27:09 +00003586 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3587 rtnl_dump_ifinfo, rtnl_calcit);
3588 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3589 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3590 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003591
Greg Rosec7ac8672011-06-10 01:27:09 +00003592 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3593 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003594
3595 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3596 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3597 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003598
3599 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003600 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003601 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}