blob: 62737f437c8e0f0e7d3faf0fdabed905bc2444cf [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 */
Thomas Graf339bf982006-11-10 14:10:15 -0800898 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700899 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700900 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100901 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500902 + nla_total_size(ext_filter_mask
903 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
904 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000905 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000906 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700907 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100908 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700909 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
910 + nla_total_size(1); /* IFLA_PROTO_DOWN */
911
Thomas Graf339bf982006-11-10 14:10:15 -0800912}
913
Scott Feldman57b61082010-05-17 22:49:55 -0700914static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
915{
916 struct nlattr *vf_ports;
917 struct nlattr *vf_port;
918 int vf;
919 int err;
920
921 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
922 if (!vf_ports)
923 return -EMSGSIZE;
924
925 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
926 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700927 if (!vf_port)
928 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400929 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
930 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700931 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700932 if (err == -EMSGSIZE)
933 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700934 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700935 nla_nest_cancel(skb, vf_port);
936 continue;
937 }
938 nla_nest_end(skb, vf_port);
939 }
940
941 nla_nest_end(skb, vf_ports);
942
943 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700944
945nla_put_failure:
946 nla_nest_cancel(skb, vf_ports);
947 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700948}
949
950static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
951{
952 struct nlattr *port_self;
953 int err;
954
955 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
956 if (!port_self)
957 return -EMSGSIZE;
958
959 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
960 if (err) {
961 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700962 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700963 }
964
965 nla_nest_end(skb, port_self);
966
967 return 0;
968}
969
David Gibsonc53864f2014-04-24 10:22:36 +1000970static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
971 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700972{
973 int err;
974
David Gibsonc53864f2014-04-24 10:22:36 +1000975 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
976 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700977 return 0;
978
979 err = rtnl_port_self_fill(skb, dev);
980 if (err)
981 return err;
982
983 if (dev_num_vf(dev->dev.parent)) {
984 err = rtnl_vf_ports_fill(skb, dev);
985 if (err)
986 return err;
987 }
988
989 return 0;
990}
991
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200992static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
993{
994 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100995 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200996
997 err = dev_get_phys_port_id(dev, &ppid);
998 if (err) {
999 if (err == -EOPNOTSUPP)
1000 return 0;
1001 return err;
1002 }
1003
1004 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1005 return -EMSGSIZE;
1006
1007 return 0;
1008}
1009
David Aherndb24a902015-03-17 20:23:15 -06001010static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1011{
1012 char name[IFNAMSIZ];
1013 int err;
1014
1015 err = dev_get_phys_port_name(dev, name, sizeof(name));
1016 if (err) {
1017 if (err == -EOPNOTSUPP)
1018 return 0;
1019 return err;
1020 }
1021
1022 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1023 return -EMSGSIZE;
1024
1025 return 0;
1026}
1027
Jiri Pirko82f28412014-11-28 14:34:18 +01001028static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1029{
1030 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001031 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001032 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001033 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001034 .flags = SWITCHDEV_F_NO_RECURSE,
1035 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001036
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001037 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001038 if (err) {
1039 if (err == -EOPNOTSUPP)
1040 return 0;
1041 return err;
1042 }
1043
Scott Feldman42275bd2015-05-13 11:16:50 -07001044 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1045 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001046 return -EMSGSIZE;
1047
1048 return 0;
1049}
1050
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001051static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1052 struct net_device *dev)
1053{
1054 const struct rtnl_link_stats64 *stats;
1055 struct rtnl_link_stats64 temp;
1056 struct nlattr *attr;
1057
1058 stats = dev_get_stats(dev, &temp);
1059
1060 attr = nla_reserve(skb, IFLA_STATS,
1061 sizeof(struct rtnl_link_stats));
1062 if (!attr)
1063 return -EMSGSIZE;
1064
1065 copy_rtnl_link_stats(nla_data(attr), stats);
1066
1067 attr = nla_reserve(skb, IFLA_STATS64,
1068 sizeof(struct rtnl_link_stats64));
1069 if (!attr)
1070 return -EMSGSIZE;
1071
1072 copy_rtnl_link_stats64(nla_data(attr), stats);
1073
1074 return 0;
1075}
1076
1077static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1078 struct net_device *dev,
1079 int vfs_num,
1080 struct nlattr *vfinfo)
1081{
1082 struct ifla_vf_rss_query_en vf_rss_query_en;
1083 struct ifla_vf_link_state vf_linkstate;
1084 struct ifla_vf_spoofchk vf_spoofchk;
1085 struct ifla_vf_tx_rate vf_tx_rate;
1086 struct ifla_vf_stats vf_stats;
1087 struct ifla_vf_trust vf_trust;
1088 struct ifla_vf_vlan vf_vlan;
1089 struct ifla_vf_rate vf_rate;
1090 struct nlattr *vf, *vfstats;
1091 struct ifla_vf_mac vf_mac;
1092 struct ifla_vf_info ivi;
1093
1094 /* Not all SR-IOV capable drivers support the
1095 * spoofcheck and "RSS query enable" query. Preset to
1096 * -1 so the user space tool can detect that the driver
1097 * didn't report anything.
1098 */
1099 ivi.spoofchk = -1;
1100 ivi.rss_query_en = -1;
1101 ivi.trusted = -1;
1102 memset(ivi.mac, 0, sizeof(ivi.mac));
1103 /* The default value for VF link state is "auto"
1104 * IFLA_VF_LINK_STATE_AUTO which equals zero
1105 */
1106 ivi.linkstate = 0;
1107 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1108 return 0;
1109
1110 vf_mac.vf =
1111 vf_vlan.vf =
1112 vf_rate.vf =
1113 vf_tx_rate.vf =
1114 vf_spoofchk.vf =
1115 vf_linkstate.vf =
1116 vf_rss_query_en.vf =
1117 vf_trust.vf = ivi.vf;
1118
1119 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1120 vf_vlan.vlan = ivi.vlan;
1121 vf_vlan.qos = ivi.qos;
1122 vf_tx_rate.rate = ivi.max_tx_rate;
1123 vf_rate.min_tx_rate = ivi.min_tx_rate;
1124 vf_rate.max_tx_rate = ivi.max_tx_rate;
1125 vf_spoofchk.setting = ivi.spoofchk;
1126 vf_linkstate.link_state = ivi.linkstate;
1127 vf_rss_query_en.setting = ivi.rss_query_en;
1128 vf_trust.setting = ivi.trusted;
1129 vf = nla_nest_start(skb, IFLA_VF_INFO);
1130 if (!vf) {
1131 nla_nest_cancel(skb, vfinfo);
1132 return -EMSGSIZE;
1133 }
1134 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1135 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1136 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1137 &vf_rate) ||
1138 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1139 &vf_tx_rate) ||
1140 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1141 &vf_spoofchk) ||
1142 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1143 &vf_linkstate) ||
1144 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1145 sizeof(vf_rss_query_en),
1146 &vf_rss_query_en) ||
1147 nla_put(skb, IFLA_VF_TRUST,
1148 sizeof(vf_trust), &vf_trust))
1149 return -EMSGSIZE;
1150 memset(&vf_stats, 0, sizeof(vf_stats));
1151 if (dev->netdev_ops->ndo_get_vf_stats)
1152 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1153 &vf_stats);
1154 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1155 if (!vfstats) {
1156 nla_nest_cancel(skb, vf);
1157 nla_nest_cancel(skb, vfinfo);
1158 return -EMSGSIZE;
1159 }
1160 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1161 vf_stats.rx_packets) ||
1162 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1163 vf_stats.tx_packets) ||
1164 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1165 vf_stats.rx_bytes) ||
1166 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1167 vf_stats.tx_bytes) ||
1168 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1169 vf_stats.broadcast) ||
1170 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1171 vf_stats.multicast))
1172 return -EMSGSIZE;
1173 nla_nest_end(skb, vfstats);
1174 nla_nest_end(skb, vf);
1175 return 0;
1176}
1177
1178static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1179{
1180 struct rtnl_link_ifmap map = {
1181 .mem_start = dev->mem_start,
1182 .mem_end = dev->mem_end,
1183 .base_addr = dev->base_addr,
1184 .irq = dev->irq,
1185 .dma = dev->dma,
1186 .port = dev->if_port,
1187 };
1188 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1189 return -EMSGSIZE;
1190
1191 return 0;
1192}
1193
Thomas Grafb60c5112006-08-04 23:05:34 -07001194static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001195 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001196 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001197{
1198 struct ifinfomsg *ifm;
1199 struct nlmsghdr *nlh;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001200 struct nlattr *af_spec;
Thomas Graff8ff1822010-11-16 04:30:14 +00001201 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001202 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001203
Eric Dumazet2907c352011-05-25 07:34:04 +00001204 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001205 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1206 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001207 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001208
1209 ifm = nlmsg_data(nlh);
1210 ifm->ifi_family = AF_UNSPEC;
1211 ifm->__ifi_pad = 0;
1212 ifm->ifi_type = dev->type;
1213 ifm->ifi_index = dev->ifindex;
1214 ifm->ifi_flags = dev_get_flags(dev);
1215 ifm->ifi_change = change;
1216
David S. Millera6574342012-04-01 20:12:00 -04001217 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1218 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1219 nla_put_u8(skb, IFLA_OPERSTATE,
1220 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1221 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1222 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1223 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001224 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001225 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001226#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001227 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001228#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001229 (dev->ifindex != dev_get_iflink(dev) &&
1230 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001231 (upper_dev &&
1232 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001233 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001234 (dev->qdisc &&
1235 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1236 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001237 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1238 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001239 atomic_read(&dev->carrier_changes)) ||
1240 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001241 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001242
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001243 if (rtnl_fill_link_ifmap(skb, dev))
1244 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001247 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1248 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1249 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001252 if (rtnl_phys_port_id_fill(skb, dev))
1253 goto nla_put_failure;
1254
David Aherndb24a902015-03-17 20:23:15 -06001255 if (rtnl_phys_port_name_fill(skb, dev))
1256 goto nla_put_failure;
1257
Jiri Pirko82f28412014-11-28 14:34:18 +01001258 if (rtnl_phys_switch_id_fill(skb, dev))
1259 goto nla_put_failure;
1260
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001261 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001262 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
David S. Millera6574342012-04-01 20:12:00 -04001264 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1265 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1266 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001267
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001268 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1269 ext_filter_mask & RTEXT_FILTER_VF) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001270 int i;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001271 struct nlattr *vfinfo;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001272 int num_vfs = dev_num_vf(dev->dev.parent);
1273
Chris Wrightc02db8c2010-05-16 01:05:45 -07001274 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1275 if (!vfinfo)
1276 goto nla_put_failure;
1277 for (i = 0; i < num_vfs; i++) {
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001278 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
Chris Wrightc02db8c2010-05-16 01:05:45 -07001279 goto nla_put_failure;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001280 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001281
Chris Wrightc02db8c2010-05-16 01:05:45 -07001282 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001283 }
Scott Feldman57b61082010-05-17 22:49:55 -07001284
David Gibsonc53864f2014-04-24 10:22:36 +10001285 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001286 goto nla_put_failure;
1287
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001288 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001289 if (rtnl_link_fill(skb, dev) < 0)
1290 goto nla_put_failure;
1291 }
1292
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001293 if (dev->rtnl_link_ops &&
1294 dev->rtnl_link_ops->get_link_net) {
1295 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1296
1297 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001298 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001299
1300 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1301 goto nla_put_failure;
1302 }
1303 }
1304
Thomas Graff8ff1822010-11-16 04:30:14 +00001305 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1306 goto nla_put_failure;
1307
1308 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1309 if (af_ops->fill_link_af) {
1310 struct nlattr *af;
1311 int err;
1312
1313 if (!(af = nla_nest_start(skb, af_ops->family)))
1314 goto nla_put_failure;
1315
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001316 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001317
1318 /*
1319 * Caller may return ENODATA to indicate that there
1320 * was no data to be dumped. This is not an error, it
1321 * means we should trim the attribute header and
1322 * continue.
1323 */
1324 if (err == -ENODATA)
1325 nla_nest_cancel(skb, af);
1326 else if (err < 0)
1327 goto nla_put_failure;
1328
1329 nla_nest_end(skb, af);
1330 }
1331 }
1332
1333 nla_nest_end(skb, af_spec);
1334
Johannes Berg053c0952015-01-16 22:09:00 +01001335 nlmsg_end(skb, nlh);
1336 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001337
1338nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001339 nlmsg_cancel(skb, nlh);
1340 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Jiri Pirkof7b12602014-02-18 20:53:18 +01001343static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1344 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1345 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1346 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1347 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1348 [IFLA_MTU] = { .type = NLA_U32 },
1349 [IFLA_LINK] = { .type = NLA_U32 },
1350 [IFLA_MASTER] = { .type = NLA_U32 },
1351 [IFLA_CARRIER] = { .type = NLA_U8 },
1352 [IFLA_TXQLEN] = { .type = NLA_U32 },
1353 [IFLA_WEIGHT] = { .type = NLA_U32 },
1354 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1355 [IFLA_LINKMODE] = { .type = NLA_U8 },
1356 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1357 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1358 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1359 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1360 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1361 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1362 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1363 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1364 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1365 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1366 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1367 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001368 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001369 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001370 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001371 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001372 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001373};
1374
1375static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1376 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1377 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1378 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1379 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1380};
1381
Jiri Pirkof7b12602014-02-18 20:53:18 +01001382static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001383 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1384 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1385 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1386 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1387 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1388 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001389 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001390 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001391 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001392};
1393
1394static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = {
1395 [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 },
1396 [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 },
1397 [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 },
1398 [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 },
1399 [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 },
1400 [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001401};
1402
1403static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1404 [IFLA_PORT_VF] = { .type = NLA_U32 },
1405 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1406 .len = PORT_PROFILE_MAX },
1407 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1408 .len = sizeof(struct ifla_port_vsi)},
1409 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1410 .len = PORT_UUID_MAX },
1411 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1412 .len = PORT_UUID_MAX },
1413 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1414 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1415};
1416
David Aherndc599f72016-02-02 08:17:07 -08001417static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1418{
1419 const struct rtnl_link_ops *ops = NULL;
1420 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1421
1422 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1423 return NULL;
1424
1425 if (linfo[IFLA_INFO_KIND]) {
1426 char kind[MODULE_NAME_LEN];
1427
1428 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1429 ops = rtnl_link_ops_get(kind);
1430 }
1431
1432 return ops;
1433}
1434
1435static bool link_master_filtered(struct net_device *dev, int master_idx)
1436{
1437 struct net_device *master;
1438
1439 if (!master_idx)
1440 return false;
1441
1442 master = netdev_master_upper_dev_get(dev);
1443 if (!master || master->ifindex != master_idx)
1444 return true;
1445
1446 return false;
1447}
1448
1449static bool link_kind_filtered(const struct net_device *dev,
1450 const struct rtnl_link_ops *kind_ops)
1451{
1452 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1453 return true;
1454
1455 return false;
1456}
1457
1458static bool link_dump_filtered(struct net_device *dev,
1459 int master_idx,
1460 const struct rtnl_link_ops *kind_ops)
1461{
1462 if (link_master_filtered(dev, master_idx) ||
1463 link_kind_filtered(dev, kind_ops))
1464 return true;
1465
1466 return false;
1467}
1468
Thomas Grafb60c5112006-08-04 23:05:34 -07001469static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001471 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001472 int h, s_h;
1473 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001475 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001476 struct nlattr *tb[IFLA_MAX+1];
1477 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001478 const struct rtnl_link_ops *kind_ops = NULL;
1479 unsigned int flags = NLM_F_MULTI;
1480 int master_idx = 0;
David Gibson973462b2014-04-24 10:22:35 +10001481 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001482 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001484 s_h = cb->args[0];
1485 s_idx = cb->args[1];
1486
Thomas Graf4e985ad2011-06-21 03:11:20 +00001487 cb->seq = net->dev_base_seq;
1488
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001489 /* A hack to preserve kernel<->userspace interface.
1490 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1491 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1492 * what iproute2 < v3.9.0 used.
1493 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1494 * attribute, its netlink message is shorter than struct ifinfomsg.
1495 */
1496 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1497 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1498
1499 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001500
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001501 if (tb[IFLA_EXT_MASK])
1502 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001503
1504 if (tb[IFLA_MASTER])
1505 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1506
1507 if (tb[IFLA_LINKINFO])
1508 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1509
1510 if (master_idx || kind_ops)
1511 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001512 }
Greg Rose115c9b82012-02-21 16:54:48 -05001513
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001514 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1515 idx = 0;
1516 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001517 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001518 if (link_dump_filtered(dev, master_idx, kind_ops))
1519 continue;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001520 if (idx < s_idx)
1521 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001522 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1523 NETLINK_CB(cb->skb).portid,
1524 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001525 flags,
David Gibson973462b2014-04-24 10:22:35 +10001526 ext_filter_mask);
1527 /* If we ran out of room on the first message,
1528 * we're in trouble
1529 */
1530 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1531
David S. Miller7b46a642015-01-18 23:36:08 -05001532 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001533 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001534
1535 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001536cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001537 idx++;
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001540out:
1541 cb->args[1] = idx;
1542 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 return skb->len;
1545}
1546
Jiri Pirkof7b12602014-02-18 20:53:18 +01001547int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1548{
1549 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1550}
1551EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001552
Eric W. Biederman81adee42009-11-08 00:53:51 -08001553struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1554{
1555 struct net *net;
1556 /* Examine the link attributes and figure out which
1557 * network namespace we are talking about.
1558 */
1559 if (tb[IFLA_NET_NS_PID])
1560 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001561 else if (tb[IFLA_NET_NS_FD])
1562 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001563 else
1564 net = get_net(src_net);
1565 return net;
1566}
1567EXPORT_SYMBOL(rtnl_link_get_net);
1568
Thomas Graf1840bb12008-02-23 19:54:36 -08001569static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1570{
1571 if (dev) {
1572 if (tb[IFLA_ADDRESS] &&
1573 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1574 return -EINVAL;
1575
1576 if (tb[IFLA_BROADCAST] &&
1577 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1578 return -EINVAL;
1579 }
1580
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001581 if (tb[IFLA_AF_SPEC]) {
1582 struct nlattr *af;
1583 int rem, err;
1584
1585 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1586 const struct rtnl_af_ops *af_ops;
1587
1588 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1589 return -EAFNOSUPPORT;
1590
1591 if (!af_ops->set_link_af)
1592 return -EOPNOTSUPP;
1593
1594 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001595 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001596 if (err < 0)
1597 return err;
1598 }
1599 }
1600 }
1601
Thomas Graf1840bb12008-02-23 19:54:36 -08001602 return 0;
1603}
1604
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001605static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001606{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001607 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001608 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001609
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001610 if (tb[IFLA_VF_MAC]) {
1611 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001612
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001613 err = -EOPNOTSUPP;
1614 if (ops->ndo_set_vf_mac)
1615 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1616 ivm->mac);
1617 if (err < 0)
1618 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001619 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001620
1621 if (tb[IFLA_VF_VLAN]) {
1622 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1623
1624 err = -EOPNOTSUPP;
1625 if (ops->ndo_set_vf_vlan)
1626 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1627 ivv->qos);
1628 if (err < 0)
1629 return err;
1630 }
1631
1632 if (tb[IFLA_VF_TX_RATE]) {
1633 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1634 struct ifla_vf_info ivf;
1635
1636 err = -EOPNOTSUPP;
1637 if (ops->ndo_get_vf_config)
1638 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1639 if (err < 0)
1640 return err;
1641
1642 err = -EOPNOTSUPP;
1643 if (ops->ndo_set_vf_rate)
1644 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1645 ivf.min_tx_rate,
1646 ivt->rate);
1647 if (err < 0)
1648 return err;
1649 }
1650
1651 if (tb[IFLA_VF_RATE]) {
1652 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1653
1654 err = -EOPNOTSUPP;
1655 if (ops->ndo_set_vf_rate)
1656 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1657 ivt->min_tx_rate,
1658 ivt->max_tx_rate);
1659 if (err < 0)
1660 return err;
1661 }
1662
1663 if (tb[IFLA_VF_SPOOFCHK]) {
1664 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1665
1666 err = -EOPNOTSUPP;
1667 if (ops->ndo_set_vf_spoofchk)
1668 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1669 ivs->setting);
1670 if (err < 0)
1671 return err;
1672 }
1673
1674 if (tb[IFLA_VF_LINK_STATE]) {
1675 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1676
1677 err = -EOPNOTSUPP;
1678 if (ops->ndo_set_vf_link_state)
1679 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1680 ivl->link_state);
1681 if (err < 0)
1682 return err;
1683 }
1684
1685 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1686 struct ifla_vf_rss_query_en *ivrssq_en;
1687
1688 err = -EOPNOTSUPP;
1689 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1690 if (ops->ndo_set_vf_rss_query_en)
1691 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1692 ivrssq_en->setting);
1693 if (err < 0)
1694 return err;
1695 }
1696
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001697 if (tb[IFLA_VF_TRUST]) {
1698 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1699
1700 err = -EOPNOTSUPP;
1701 if (ops->ndo_set_vf_trust)
1702 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1703 if (err < 0)
1704 return err;
1705 }
1706
Chris Wrightc02db8c2010-05-16 01:05:45 -07001707 return err;
1708}
1709
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001710static int do_set_master(struct net_device *dev, int ifindex)
1711{
Jiri Pirko898e5062013-01-03 22:48:52 +00001712 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001713 const struct net_device_ops *ops;
1714 int err;
1715
Jiri Pirko898e5062013-01-03 22:48:52 +00001716 if (upper_dev) {
1717 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001718 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001719 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001720 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001721 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001722 if (err)
1723 return err;
1724 } else {
1725 return -EOPNOTSUPP;
1726 }
1727 }
1728
1729 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001730 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1731 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001732 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001733 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001734 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001735 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001736 if (err)
1737 return err;
1738 } else {
1739 return -EOPNOTSUPP;
1740 }
1741 }
1742 return 0;
1743}
1744
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001745#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001746/* notify flag means notify + modified. */
1747#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001748static int do_setlink(const struct sk_buff *skb,
1749 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001750 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001751{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001752 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001753 int err;
1754
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001755 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001756 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001757 if (IS_ERR(net)) {
1758 err = PTR_ERR(net);
1759 goto errout;
1760 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001761 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001762 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001763 err = -EPERM;
1764 goto errout;
1765 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001766 err = dev_change_net_namespace(dev, net, ifname);
1767 put_net(net);
1768 if (err)
1769 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001770 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001771 }
1772
Patrick McHardy0157f602007-06-13 12:03:36 -07001773 if (tb[IFLA_MAP]) {
1774 struct rtnl_link_ifmap *u_map;
1775 struct ifmap k_map;
1776
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001777 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001778 err = -EOPNOTSUPP;
1779 goto errout;
1780 }
1781
1782 if (!netif_device_present(dev)) {
1783 err = -ENODEV;
1784 goto errout;
1785 }
1786
1787 u_map = nla_data(tb[IFLA_MAP]);
1788 k_map.mem_start = (unsigned long) u_map->mem_start;
1789 k_map.mem_end = (unsigned long) u_map->mem_end;
1790 k_map.base_addr = (unsigned short) u_map->base_addr;
1791 k_map.irq = (unsigned char) u_map->irq;
1792 k_map.dma = (unsigned char) u_map->dma;
1793 k_map.port = (unsigned char) u_map->port;
1794
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001795 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001796 if (err < 0)
1797 goto errout;
1798
Nicolas Dichtelba998902014-09-01 16:07:29 +02001799 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001800 }
1801
1802 if (tb[IFLA_ADDRESS]) {
1803 struct sockaddr *sa;
1804 int len;
1805
Patrick McHardy0157f602007-06-13 12:03:36 -07001806 len = sizeof(sa_family_t) + dev->addr_len;
1807 sa = kmalloc(len, GFP_KERNEL);
1808 if (!sa) {
1809 err = -ENOMEM;
1810 goto errout;
1811 }
1812 sa->sa_family = dev->type;
1813 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1814 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001815 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001816 kfree(sa);
1817 if (err)
1818 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001819 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001820 }
1821
1822 if (tb[IFLA_MTU]) {
1823 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1824 if (err < 0)
1825 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001826 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001827 }
1828
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001829 if (tb[IFLA_GROUP]) {
1830 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001831 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001832 }
1833
Patrick McHardy0157f602007-06-13 12:03:36 -07001834 /*
1835 * Interface selected by interface index but interface
1836 * name provided implies that a name change has been
1837 * requested.
1838 */
1839 if (ifm->ifi_index > 0 && ifname[0]) {
1840 err = dev_change_name(dev, ifname);
1841 if (err < 0)
1842 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001843 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001844 }
1845
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001846 if (tb[IFLA_IFALIAS]) {
1847 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1848 nla_len(tb[IFLA_IFALIAS]));
1849 if (err < 0)
1850 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001851 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001852 }
1853
Patrick McHardy0157f602007-06-13 12:03:36 -07001854 if (tb[IFLA_BROADCAST]) {
1855 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001856 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001857 }
1858
1859 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001860 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001861 if (err < 0)
1862 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001863 }
1864
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001865 if (tb[IFLA_MASTER]) {
1866 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1867 if (err)
1868 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001869 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001870 }
1871
Jiri Pirko9a572472012-12-27 23:49:39 +00001872 if (tb[IFLA_CARRIER]) {
1873 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1874 if (err)
1875 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001876 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001877 }
1878
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001879 if (tb[IFLA_TXQLEN]) {
1880 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1881
1882 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001883 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001884
1885 dev->tx_queue_len = value;
1886 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001887
Patrick McHardy0157f602007-06-13 12:03:36 -07001888 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001889 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001890
1891 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001892 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1893
David S. Miller93b2d4a2008-02-17 18:35:07 -08001894 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001895 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001896 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001897 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001898 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001899 }
1900
Chris Wrightc02db8c2010-05-16 01:05:45 -07001901 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001902 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001903 struct nlattr *attr;
1904 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001905
Chris Wrightc02db8c2010-05-16 01:05:45 -07001906 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001907 if (nla_type(attr) != IFLA_VF_INFO ||
1908 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001909 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001910 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001911 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001912 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1913 ifla_vf_policy);
1914 if (err < 0)
1915 goto errout;
1916 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001917 if (err < 0)
1918 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001919 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001920 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001921 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001922 err = 0;
1923
Scott Feldman57b61082010-05-17 22:49:55 -07001924 if (tb[IFLA_VF_PORTS]) {
1925 struct nlattr *port[IFLA_PORT_MAX+1];
1926 struct nlattr *attr;
1927 int vf;
1928 int rem;
1929
1930 err = -EOPNOTSUPP;
1931 if (!ops->ndo_set_vf_port)
1932 goto errout;
1933
1934 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001935 if (nla_type(attr) != IFLA_VF_PORT ||
1936 nla_len(attr) < NLA_HDRLEN) {
1937 err = -EINVAL;
1938 goto errout;
1939 }
1940 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1941 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001942 if (err < 0)
1943 goto errout;
1944 if (!port[IFLA_PORT_VF]) {
1945 err = -EOPNOTSUPP;
1946 goto errout;
1947 }
1948 vf = nla_get_u32(port[IFLA_PORT_VF]);
1949 err = ops->ndo_set_vf_port(dev, vf, port);
1950 if (err < 0)
1951 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001952 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001953 }
1954 }
1955 err = 0;
1956
1957 if (tb[IFLA_PORT_SELF]) {
1958 struct nlattr *port[IFLA_PORT_MAX+1];
1959
1960 err = nla_parse_nested(port, IFLA_PORT_MAX,
1961 tb[IFLA_PORT_SELF], ifla_port_policy);
1962 if (err < 0)
1963 goto errout;
1964
1965 err = -EOPNOTSUPP;
1966 if (ops->ndo_set_vf_port)
1967 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1968 if (err < 0)
1969 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001970 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001971 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001972
1973 if (tb[IFLA_AF_SPEC]) {
1974 struct nlattr *af;
1975 int rem;
1976
1977 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1978 const struct rtnl_af_ops *af_ops;
1979
1980 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001981 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001982
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001983 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001984 if (err < 0)
1985 goto errout;
1986
Nicolas Dichtelba998902014-09-01 16:07:29 +02001987 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001988 }
1989 }
Scott Feldman57b61082010-05-17 22:49:55 -07001990 err = 0;
1991
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001992 if (tb[IFLA_PROTO_DOWN]) {
1993 err = dev_change_proto_down(dev,
1994 nla_get_u8(tb[IFLA_PROTO_DOWN]));
1995 if (err)
1996 goto errout;
1997 status |= DO_SETLINK_NOTIFY;
1998 }
1999
Patrick McHardy0157f602007-06-13 12:03:36 -07002000errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002001 if (status & DO_SETLINK_MODIFIED) {
2002 if (status & DO_SETLINK_NOTIFY)
2003 netdev_state_change(dev);
2004
2005 if (err < 0)
2006 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",
2007 dev->name);
2008 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002009
Patrick McHardy0157f602007-06-13 12:03:36 -07002010 return err;
2011}
2012
Thomas Graf661d2962013-03-21 07:45:29 +00002013static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002015 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002016 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002018 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002019 struct nlattr *tb[IFLA_MAX+1];
2020 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Thomas Grafda5e0492006-08-10 21:17:37 -07002022 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2023 if (err < 0)
2024 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Thomas Graf5176f912006-08-26 20:13:18 -07002026 if (tb[IFLA_IFNAME])
2027 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002028 else
2029 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002032 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002033 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002034 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002035 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002036 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002037 else
2038 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Thomas Grafda5e0492006-08-10 21:17:37 -07002040 if (dev == NULL) {
2041 err = -ENODEV;
2042 goto errout;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Eric Dumazete0d087a2009-11-07 01:26:17 -08002045 err = validate_linkmsg(dev, tb);
2046 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002047 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002048
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002049 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002050errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return err;
2052}
2053
WANG Cong66400d52015-03-24 11:53:31 -07002054static int rtnl_group_dellink(const struct net *net, int group)
2055{
2056 struct net_device *dev, *aux;
2057 LIST_HEAD(list_kill);
2058 bool found = false;
2059
2060 if (!group)
2061 return -EPERM;
2062
2063 for_each_netdev(net, dev) {
2064 if (dev->group == group) {
2065 const struct rtnl_link_ops *ops;
2066
2067 found = true;
2068 ops = dev->rtnl_link_ops;
2069 if (!ops || !ops->dellink)
2070 return -EOPNOTSUPP;
2071 }
2072 }
2073
2074 if (!found)
2075 return -ENODEV;
2076
2077 for_each_netdev_safe(net, dev, aux) {
2078 if (dev->group == group) {
2079 const struct rtnl_link_ops *ops;
2080
2081 ops = dev->rtnl_link_ops;
2082 ops->dellink(dev, &list_kill);
2083 }
2084 }
2085 unregister_netdevice_many(&list_kill);
2086
2087 return 0;
2088}
2089
Thomas Graf614732e2015-07-21 10:44:06 +02002090int rtnl_delete_link(struct net_device *dev)
2091{
2092 const struct rtnl_link_ops *ops;
2093 LIST_HEAD(list_kill);
2094
2095 ops = dev->rtnl_link_ops;
2096 if (!ops || !ops->dellink)
2097 return -EOPNOTSUPP;
2098
2099 ops->dellink(dev, &list_kill);
2100 unregister_netdevice_many(&list_kill);
2101
2102 return 0;
2103}
2104EXPORT_SYMBOL_GPL(rtnl_delete_link);
2105
Thomas Graf661d2962013-03-21 07:45:29 +00002106static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002107{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002108 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002109 struct net_device *dev;
2110 struct ifinfomsg *ifm;
2111 char ifname[IFNAMSIZ];
2112 struct nlattr *tb[IFLA_MAX+1];
2113 int err;
2114
2115 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2116 if (err < 0)
2117 return err;
2118
2119 if (tb[IFLA_IFNAME])
2120 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2121
2122 ifm = nlmsg_data(nlh);
2123 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002124 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002125 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002126 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002127 else if (tb[IFLA_GROUP])
2128 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002129 else
2130 return -EINVAL;
2131
2132 if (!dev)
2133 return -ENODEV;
2134
Thomas Graf614732e2015-07-21 10:44:06 +02002135 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002136}
2137
Patrick McHardy3729d502010-02-26 06:34:54 +00002138int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2139{
2140 unsigned int old_flags;
2141 int err;
2142
2143 old_flags = dev->flags;
2144 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2145 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2146 if (err < 0)
2147 return err;
2148 }
2149
2150 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002151
Nicolas Dichtela528c212013-09-25 12:02:44 +02002152 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002153 return 0;
2154}
2155EXPORT_SYMBOL(rtnl_configure_link);
2156
Rami Rosenc0713562012-11-30 01:08:47 +00002157struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002158 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002159 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002160{
2161 int err;
2162 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002163 unsigned int num_tx_queues = 1;
2164 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002165
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002166 if (tb[IFLA_NUM_TX_QUEUES])
2167 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2168 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002169 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002170
2171 if (tb[IFLA_NUM_RX_QUEUES])
2172 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2173 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002174 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002175
Pavel Emelianove7199282007-08-08 22:16:38 -07002176 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002177 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002178 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002179 if (!dev)
2180 goto err;
2181
Eric W. Biederman81adee42009-11-08 00:53:51 -08002182 dev_net_set(dev, net);
2183 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002184 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002185
Pavel Emelianove7199282007-08-08 22:16:38 -07002186 if (tb[IFLA_MTU])
2187 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002188 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002189 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2190 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002191 dev->addr_assign_type = NET_ADDR_SET;
2192 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002193 if (tb[IFLA_BROADCAST])
2194 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2195 nla_len(tb[IFLA_BROADCAST]));
2196 if (tb[IFLA_TXQLEN])
2197 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2198 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002199 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002200 if (tb[IFLA_LINKMODE])
2201 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002202 if (tb[IFLA_GROUP])
2203 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002204
2205 return dev;
2206
Pavel Emelianove7199282007-08-08 22:16:38 -07002207err:
2208 return ERR_PTR(err);
2209}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002210EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002211
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002212static int rtnl_group_changelink(const struct sk_buff *skb,
2213 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002214 struct ifinfomsg *ifm,
2215 struct nlattr **tb)
2216{
WANG Congd0795352015-03-23 16:31:09 -07002217 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002218 int err;
2219
WANG Congd0795352015-03-23 16:31:09 -07002220 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002221 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002222 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002223 if (err < 0)
2224 return err;
2225 }
2226 }
2227
2228 return 0;
2229}
2230
Thomas Graf661d2962013-03-21 07:45:29 +00002231static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002232{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002233 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002234 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002235 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002236 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002237 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002238 struct ifinfomsg *ifm;
2239 char kind[MODULE_NAME_LEN];
2240 char ifname[IFNAMSIZ];
2241 struct nlattr *tb[IFLA_MAX+1];
2242 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002243 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002244 int err;
2245
Johannes Berg95a5afc2008-10-16 15:24:51 -07002246#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002247replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002248#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002249 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2250 if (err < 0)
2251 return err;
2252
2253 if (tb[IFLA_IFNAME])
2254 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2255 else
2256 ifname[0] = '\0';
2257
2258 ifm = nlmsg_data(nlh);
2259 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002260 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002261 else {
2262 if (ifname[0])
2263 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002264 else
2265 dev = NULL;
2266 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002267
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002268 if (dev) {
2269 master_dev = netdev_master_upper_dev_get(dev);
2270 if (master_dev)
2271 m_ops = master_dev->rtnl_link_ops;
2272 }
2273
Eric Dumazete0d087a2009-11-07 01:26:17 -08002274 err = validate_linkmsg(dev, tb);
2275 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002276 return err;
2277
Patrick McHardy38f7b872007-06-13 12:03:51 -07002278 if (tb[IFLA_LINKINFO]) {
2279 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2280 tb[IFLA_LINKINFO], ifla_info_policy);
2281 if (err < 0)
2282 return err;
2283 } else
2284 memset(linkinfo, 0, sizeof(linkinfo));
2285
2286 if (linkinfo[IFLA_INFO_KIND]) {
2287 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2288 ops = rtnl_link_ops_get(kind);
2289 } else {
2290 kind[0] = '\0';
2291 ops = NULL;
2292 }
2293
2294 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002295 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2296 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002297 struct nlattr **data = NULL;
2298 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002299 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002300
2301 if (ops) {
2302 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2303 err = nla_parse_nested(attr, ops->maxtype,
2304 linkinfo[IFLA_INFO_DATA],
2305 ops->policy);
2306 if (err < 0)
2307 return err;
2308 data = attr;
2309 }
2310 if (ops->validate) {
2311 err = ops->validate(tb, data);
2312 if (err < 0)
2313 return err;
2314 }
2315 }
2316
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002317 if (m_ops) {
2318 if (m_ops->slave_maxtype &&
2319 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2320 err = nla_parse_nested(slave_attr,
2321 m_ops->slave_maxtype,
2322 linkinfo[IFLA_INFO_SLAVE_DATA],
2323 m_ops->slave_policy);
2324 if (err < 0)
2325 return err;
2326 slave_data = slave_attr;
2327 }
2328 if (m_ops->slave_validate) {
2329 err = m_ops->slave_validate(tb, slave_data);
2330 if (err < 0)
2331 return err;
2332 }
2333 }
2334
Patrick McHardy38f7b872007-06-13 12:03:51 -07002335 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002336 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002337
2338 if (nlh->nlmsg_flags & NLM_F_EXCL)
2339 return -EEXIST;
2340 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2341 return -EOPNOTSUPP;
2342
2343 if (linkinfo[IFLA_INFO_DATA]) {
2344 if (!ops || ops != dev->rtnl_link_ops ||
2345 !ops->changelink)
2346 return -EOPNOTSUPP;
2347
2348 err = ops->changelink(dev, tb, data);
2349 if (err < 0)
2350 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002351 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002352 }
2353
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002354 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2355 if (!m_ops || !m_ops->slave_changelink)
2356 return -EOPNOTSUPP;
2357
2358 err = m_ops->slave_changelink(master_dev, dev,
2359 tb, slave_data);
2360 if (err < 0)
2361 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002362 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002363 }
2364
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002365 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002366 }
2367
Patrick McHardyffa934f2011-01-20 03:00:42 +00002368 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2369 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002370 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002371 nla_get_u32(tb[IFLA_GROUP]),
2372 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002373 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002374 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002375
Patrick McHardy0e068772007-07-11 19:42:31 -07002376 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002377 return -EOPNOTSUPP;
2378
2379 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002380#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002381 if (kind[0]) {
2382 __rtnl_unlock();
2383 request_module("rtnl-link-%s", kind);
2384 rtnl_lock();
2385 ops = rtnl_link_ops_get(kind);
2386 if (ops)
2387 goto replay;
2388 }
2389#endif
2390 return -EOPNOTSUPP;
2391 }
2392
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002393 if (!ops->setup)
2394 return -EOPNOTSUPP;
2395
Tom Gundersen55177502014-07-14 16:37:25 +02002396 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002397 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002398 name_assign_type = NET_NAME_ENUM;
2399 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002400
Eric W. Biederman81adee42009-11-08 00:53:51 -08002401 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002402 if (IS_ERR(dest_net))
2403 return PTR_ERR(dest_net);
2404
Eric W. Biederman505ce412015-02-26 16:19:00 -06002405 err = -EPERM;
2406 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2407 goto out;
2408
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002409 if (tb[IFLA_LINK_NETNSID]) {
2410 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2411
2412 link_net = get_net_ns_by_id(dest_net, id);
2413 if (!link_net) {
2414 err = -EINVAL;
2415 goto out;
2416 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002417 err = -EPERM;
2418 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2419 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002420 }
2421
2422 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2423 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002424 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002425 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002426 goto out;
2427 }
2428
2429 dev->ifindex = ifm->ifi_index;
2430
Cong Wang0e0eee22014-02-11 15:51:30 -08002431 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002432 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002433 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002434 * and unregister it on failure after registration
2435 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002436 */
Cong Wange51fb152014-06-03 16:40:47 -07002437 if (err < 0) {
2438 /* If device is not registered at all, free it now */
2439 if (dev->reg_state == NETREG_UNINITIALIZED)
2440 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002441 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002442 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002443 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002444 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002445 if (err < 0) {
2446 free_netdev(dev);
2447 goto out;
2448 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002449 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002450 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002451 if (err < 0)
2452 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002453 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002454 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002455 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002456 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002457 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002458out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002459 if (link_net)
2460 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002461 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002462 return err;
David S. Miller43638902015-03-10 21:58:32 -04002463out_unregister:
2464 if (ops->newlink) {
2465 LIST_HEAD(list_kill);
2466
2467 ops->dellink(dev, &list_kill);
2468 unregister_netdevice_many(&list_kill);
2469 } else {
2470 unregister_netdevice(dev);
2471 }
2472 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002473 }
2474}
2475
Thomas Graf661d2962013-03-21 07:45:29 +00002476static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002477{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002478 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002479 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002480 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002481 struct nlattr *tb[IFLA_MAX+1];
2482 struct net_device *dev = NULL;
2483 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002484 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002485 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002486
Thomas Grafb60c5112006-08-04 23:05:34 -07002487 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2488 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002489 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002490
Eric Dumazeta3d12892009-10-21 10:59:31 +00002491 if (tb[IFLA_IFNAME])
2492 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2493
Greg Rose115c9b82012-02-21 16:54:48 -05002494 if (tb[IFLA_EXT_MASK])
2495 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2496
Thomas Grafb60c5112006-08-04 23:05:34 -07002497 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002498 if (ifm->ifi_index > 0)
2499 dev = __dev_get_by_index(net, ifm->ifi_index);
2500 else if (tb[IFLA_IFNAME])
2501 dev = __dev_get_by_name(net, ifname);
2502 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002503 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002504
Eric Dumazeta3d12892009-10-21 10:59:31 +00002505 if (dev == NULL)
2506 return -ENODEV;
2507
Greg Rose115c9b82012-02-21 16:54:48 -05002508 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002509 if (nskb == NULL)
2510 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002511
Eric W. Biederman15e47302012-09-07 20:12:54 +00002512 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002513 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002514 if (err < 0) {
2515 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2516 WARN_ON(err == -EMSGSIZE);
2517 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002518 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002519 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002520
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002521 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002522}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002523
Greg Rose115c9b82012-02-21 16:54:48 -05002524static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002525{
Greg Rose115c9b82012-02-21 16:54:48 -05002526 struct net *net = sock_net(skb->sk);
2527 struct net_device *dev;
2528 struct nlattr *tb[IFLA_MAX+1];
2529 u32 ext_filter_mask = 0;
2530 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002531 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002532
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002533 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2534 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2535 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2536
2537 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002538 if (tb[IFLA_EXT_MASK])
2539 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2540 }
Greg Rose115c9b82012-02-21 16:54:48 -05002541
2542 if (!ext_filter_mask)
2543 return NLMSG_GOODSIZE;
2544 /*
2545 * traverse the list of net devices and compute the minimum
2546 * buffer size based upon the filter mask.
2547 */
2548 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2549 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2550 if_nlmsg_size(dev,
2551 ext_filter_mask));
2552 }
2553
Greg Rosec7ac8672011-06-10 01:27:09 +00002554 return min_ifinfo_dump_size;
2555}
2556
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002557static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 int idx;
2560 int s_idx = cb->family;
2561
2562 if (s_idx == 0)
2563 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002564 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 int type = cb->nlh->nlmsg_type-RTM_BASE;
2566 if (idx < s_idx || idx == PF_PACKET)
2567 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002568 if (rtnl_msg_handlers[idx] == NULL ||
2569 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002571 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002573 cb->prev_seq = 0;
2574 cb->seq = 0;
2575 }
Thomas Grafe2849862007-03-22 11:48:11 -07002576 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 break;
2578 }
2579 cb->family = idx;
2580
2581 return skb->len;
2582}
2583
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002584struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2585 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002587 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002589 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002590 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002592 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002593 if (skb == NULL)
2594 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Greg Rose115c9b82012-02-21 16:54:48 -05002596 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002597 if (err < 0) {
2598 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2599 WARN_ON(err == -EMSGSIZE);
2600 kfree_skb(skb);
2601 goto errout;
2602 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002603 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002604errout:
2605 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002606 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002607 return NULL;
2608}
2609
2610void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2611{
2612 struct net *net = dev_net(dev);
2613
2614 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2615}
2616
2617void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2618 gfp_t flags)
2619{
2620 struct sk_buff *skb;
2621
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002622 if (dev->reg_state != NETREG_REGISTERED)
2623 return;
2624
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002625 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2626 if (skb)
2627 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002629EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
John Fastabendd83b0602012-04-15 06:44:08 +00002631static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2632 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002633 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002634 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002635 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00002636{
2637 struct nlmsghdr *nlh;
2638 struct ndmsg *ndm;
2639
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002640 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002641 if (!nlh)
2642 return -EMSGSIZE;
2643
2644 ndm = nlmsg_data(nlh);
2645 ndm->ndm_family = AF_BRIDGE;
2646 ndm->ndm_pad1 = 0;
2647 ndm->ndm_pad2 = 0;
2648 ndm->ndm_flags = flags;
2649 ndm->ndm_type = 0;
2650 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002651 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00002652
2653 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2654 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002655 if (vid)
2656 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2657 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002658
Johannes Berg053c0952015-01-16 22:09:00 +01002659 nlmsg_end(skb, nlh);
2660 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002661
2662nla_put_failure:
2663 nlmsg_cancel(skb, nlh);
2664 return -EMSGSIZE;
2665}
2666
John Fastabend3ff661c2012-04-15 06:44:14 +00002667static inline size_t rtnl_fdb_nlmsg_size(void)
2668{
2669 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2670}
2671
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002672static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2673 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00002674{
2675 struct net *net = dev_net(dev);
2676 struct sk_buff *skb;
2677 int err = -ENOBUFS;
2678
2679 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2680 if (!skb)
2681 goto errout;
2682
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002683 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002684 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00002685 if (err < 0) {
2686 kfree_skb(skb);
2687 goto errout;
2688 }
2689
2690 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2691 return;
2692errout:
2693 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2694}
2695
Vlad Yasevich090096b2013-03-06 15:39:42 +00002696/**
2697 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2698 */
2699int ndo_dflt_fdb_add(struct ndmsg *ndm,
2700 struct nlattr *tb[],
2701 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002702 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002703 u16 flags)
2704{
2705 int err = -EINVAL;
2706
2707 /* If aging addresses are supported device will need to
2708 * implement its own handler for this.
2709 */
2710 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2711 pr_info("%s: FDB only supports static addresses\n", dev->name);
2712 return err;
2713 }
2714
Or Gerlitz65891fe2014-12-14 18:19:05 +02002715 if (vid) {
2716 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2717 return err;
2718 }
2719
Vlad Yasevich090096b2013-03-06 15:39:42 +00002720 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2721 err = dev_uc_add_excl(dev, addr);
2722 else if (is_multicast_ether_addr(addr))
2723 err = dev_mc_add_excl(dev, addr);
2724
2725 /* Only return duplicate errors if NLM_F_EXCL is set */
2726 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2727 err = 0;
2728
2729 return err;
2730}
2731EXPORT_SYMBOL(ndo_dflt_fdb_add);
2732
Jiri Pirkof6f64242014-11-28 14:34:15 +01002733static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2734{
2735 u16 vid = 0;
2736
2737 if (vlan_attr) {
2738 if (nla_len(vlan_attr) != sizeof(u16)) {
2739 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2740 return -EINVAL;
2741 }
2742
2743 vid = nla_get_u16(vlan_attr);
2744
2745 if (!vid || vid >= VLAN_VID_MASK) {
2746 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2747 vid);
2748 return -EINVAL;
2749 }
2750 }
2751 *p_vid = vid;
2752 return 0;
2753}
2754
Thomas Graf661d2962013-03-21 07:45:29 +00002755static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002756{
2757 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002758 struct ndmsg *ndm;
2759 struct nlattr *tb[NDA_MAX+1];
2760 struct net_device *dev;
2761 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002762 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002763 int err;
2764
2765 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2766 if (err < 0)
2767 return err;
2768
2769 ndm = nlmsg_data(nlh);
2770 if (ndm->ndm_ifindex == 0) {
2771 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2772 return -EINVAL;
2773 }
2774
2775 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2776 if (dev == NULL) {
2777 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2778 return -ENODEV;
2779 }
2780
2781 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2782 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2783 return -EINVAL;
2784 }
2785
2786 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002787
Jiri Pirkof6f64242014-11-28 14:34:15 +01002788 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2789 if (err)
2790 return err;
2791
John Fastabend77162022012-04-15 06:43:56 +00002792 err = -EOPNOTSUPP;
2793
2794 /* Support fdb on master device the net/bridge default case */
2795 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2796 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002797 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2798 const struct net_device_ops *ops = br_dev->netdev_ops;
2799
Jiri Pirkof6f64242014-11-28 14:34:15 +01002800 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2801 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002802 if (err)
2803 goto out;
2804 else
2805 ndm->ndm_flags &= ~NTF_MASTER;
2806 }
2807
2808 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002809 if ((ndm->ndm_flags & NTF_SELF)) {
2810 if (dev->netdev_ops->ndo_fdb_add)
2811 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002812 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002813 nlh->nlmsg_flags);
2814 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002815 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002816 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002817
John Fastabend3ff661c2012-04-15 06:44:14 +00002818 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002819 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2820 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002821 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002822 }
John Fastabend77162022012-04-15 06:43:56 +00002823 }
2824out:
2825 return err;
2826}
2827
Vlad Yasevich090096b2013-03-06 15:39:42 +00002828/**
2829 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2830 */
2831int ndo_dflt_fdb_del(struct ndmsg *ndm,
2832 struct nlattr *tb[],
2833 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002834 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002835{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002836 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002837
2838 /* If aging addresses are supported device will need to
2839 * implement its own handler for this.
2840 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002841 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002842 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002843 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002844 }
2845
2846 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2847 err = dev_uc_del(dev, addr);
2848 else if (is_multicast_ether_addr(addr))
2849 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002850
2851 return err;
2852}
2853EXPORT_SYMBOL(ndo_dflt_fdb_del);
2854
Thomas Graf661d2962013-03-21 07:45:29 +00002855static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002856{
2857 struct net *net = sock_net(skb->sk);
2858 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002859 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002860 struct net_device *dev;
2861 int err = -EINVAL;
2862 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002863 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002864
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002865 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002866 return -EPERM;
2867
2868 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2869 if (err < 0)
2870 return err;
John Fastabend77162022012-04-15 06:43:56 +00002871
2872 ndm = nlmsg_data(nlh);
2873 if (ndm->ndm_ifindex == 0) {
2874 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2875 return -EINVAL;
2876 }
2877
2878 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2879 if (dev == NULL) {
2880 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2881 return -ENODEV;
2882 }
2883
Vlad Yasevich1690be62013-02-13 12:00:18 +00002884 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2885 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002886 return -EINVAL;
2887 }
2888
Vlad Yasevich1690be62013-02-13 12:00:18 +00002889 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002890
Jiri Pirkof6f64242014-11-28 14:34:15 +01002891 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2892 if (err)
2893 return err;
2894
John Fastabend77162022012-04-15 06:43:56 +00002895 err = -EOPNOTSUPP;
2896
2897 /* Support fdb on master device the net/bridge default case */
2898 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2899 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002900 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2901 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002902
Jiri Pirko898e5062013-01-03 22:48:52 +00002903 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002904 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002905
2906 if (err)
2907 goto out;
2908 else
2909 ndm->ndm_flags &= ~NTF_MASTER;
2910 }
2911
2912 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002913 if (ndm->ndm_flags & NTF_SELF) {
2914 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002915 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2916 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002917 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002918 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002919
John Fastabend3ff661c2012-04-15 06:44:14 +00002920 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002921 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2922 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00002923 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002924 }
John Fastabend77162022012-04-15 06:43:56 +00002925 }
2926out:
2927 return err;
2928}
2929
John Fastabendd83b0602012-04-15 06:44:08 +00002930static int nlmsg_populate_fdb(struct sk_buff *skb,
2931 struct netlink_callback *cb,
2932 struct net_device *dev,
2933 int *idx,
2934 struct netdev_hw_addr_list *list)
2935{
2936 struct netdev_hw_addr *ha;
2937 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002938 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002939
Eric W. Biederman15e47302012-09-07 20:12:54 +00002940 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002941 seq = cb->nlh->nlmsg_seq;
2942
2943 list_for_each_entry(ha, &list->list, list) {
2944 if (*idx < cb->args[0])
2945 goto skip;
2946
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002947 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002948 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002949 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00002950 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00002951 if (err < 0)
2952 return err;
2953skip:
2954 *idx += 1;
2955 }
2956 return 0;
2957}
2958
2959/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002960 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002961 * @nlh: netlink message header
2962 * @dev: netdevice
2963 *
2964 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002965 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002966 */
2967int ndo_dflt_fdb_dump(struct sk_buff *skb,
2968 struct netlink_callback *cb,
2969 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002970 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002971 int idx)
2972{
2973 int err;
2974
2975 netif_addr_lock_bh(dev);
2976 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2977 if (err)
2978 goto out;
2979 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2980out:
2981 netif_addr_unlock_bh(dev);
2982 return idx;
2983}
2984EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2985
John Fastabend77162022012-04-15 06:43:56 +00002986static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2987{
John Fastabend77162022012-04-15 06:43:56 +00002988 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002989 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002990 struct net_device *br_dev = NULL;
2991 const struct net_device_ops *ops = NULL;
2992 const struct net_device_ops *cops = NULL;
2993 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2994 struct net *net = sock_net(skb->sk);
2995 int brport_idx = 0;
2996 int br_idx = 0;
2997 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002998
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002999 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3000 ifla_policy) == 0) {
3001 if (tb[IFLA_MASTER])
3002 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3003 }
John Fastabend77162022012-04-15 06:43:56 +00003004
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003005 brport_idx = ifm->ifi_index;
3006
3007 if (br_idx) {
3008 br_dev = __dev_get_by_index(net, br_idx);
3009 if (!br_dev)
3010 return -ENODEV;
3011
3012 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003013 }
3014
3015 for_each_netdev(net, dev) {
3016 if (brport_idx && (dev->ifindex != brport_idx))
3017 continue;
3018
3019 if (!br_idx) { /* user did not specify a specific bridge */
3020 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3021 br_dev = netdev_master_upper_dev_get(dev);
3022 cops = br_dev->netdev_ops;
3023 }
3024
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003025 } else {
3026 if (dev != br_dev &&
3027 !(dev->priv_flags & IFF_BRIDGE_PORT))
3028 continue;
3029
3030 if (br_dev != netdev_master_upper_dev_get(dev) &&
3031 !(dev->priv_flags & IFF_EBRIDGE))
3032 continue;
3033
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003034 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00003035 }
3036
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003037 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3038 if (cops && cops->ndo_fdb_dump)
3039 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3040 idx);
3041 }
3042
John Fastabend77162022012-04-15 06:43:56 +00003043 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003044 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003045 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00003046 else
3047 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003048
3049 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00003050 }
John Fastabend77162022012-04-15 06:43:56 +00003051
3052 cb->args[0] = idx;
3053 return skb->len;
3054}
3055
Scott Feldman2c3c0312014-11-28 14:34:25 +01003056static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3057 unsigned int attrnum, unsigned int flag)
3058{
3059 if (mask & flag)
3060 return nla_put_u8(skb, attrnum, !!(flags & flag));
3061 return 0;
3062}
3063
John Fastabend815cccb2012-10-24 08:13:09 +00003064int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003065 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003066 u32 flags, u32 mask, int nlflags,
3067 u32 filter_mask,
3068 int (*vlan_fill)(struct sk_buff *skb,
3069 struct net_device *dev,
3070 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003071{
3072 struct nlmsghdr *nlh;
3073 struct ifinfomsg *ifm;
3074 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003075 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003076 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003077 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003078 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003079
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003080 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003081 if (nlh == NULL)
3082 return -EMSGSIZE;
3083
3084 ifm = nlmsg_data(nlh);
3085 ifm->ifi_family = AF_BRIDGE;
3086 ifm->__ifi_pad = 0;
3087 ifm->ifi_type = dev->type;
3088 ifm->ifi_index = dev->ifindex;
3089 ifm->ifi_flags = dev_get_flags(dev);
3090 ifm->ifi_change = 0;
3091
3092
3093 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3094 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3095 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003096 (br_dev &&
3097 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003098 (dev->addr_len &&
3099 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003100 (dev->ifindex != dev_get_iflink(dev) &&
3101 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003102 goto nla_put_failure;
3103
3104 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3105 if (!br_afspec)
3106 goto nla_put_failure;
3107
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003108 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003109 nla_nest_cancel(skb, br_afspec);
3110 goto nla_put_failure;
3111 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003112
3113 if (mode != BRIDGE_MODE_UNDEF) {
3114 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3115 nla_nest_cancel(skb, br_afspec);
3116 goto nla_put_failure;
3117 }
3118 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003119 if (vlan_fill) {
3120 err = vlan_fill(skb, dev, filter_mask);
3121 if (err) {
3122 nla_nest_cancel(skb, br_afspec);
3123 goto nla_put_failure;
3124 }
3125 }
John Fastabend815cccb2012-10-24 08:13:09 +00003126 nla_nest_end(skb, br_afspec);
3127
Scott Feldman2c3c0312014-11-28 14:34:25 +01003128 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3129 if (!protinfo)
3130 goto nla_put_failure;
3131
3132 if (brport_nla_put_flag(skb, flags, mask,
3133 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3134 brport_nla_put_flag(skb, flags, mask,
3135 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3136 brport_nla_put_flag(skb, flags, mask,
3137 IFLA_BRPORT_FAST_LEAVE,
3138 BR_MULTICAST_FAST_LEAVE) ||
3139 brport_nla_put_flag(skb, flags, mask,
3140 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3141 brport_nla_put_flag(skb, flags, mask,
3142 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3143 brport_nla_put_flag(skb, flags, mask,
3144 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3145 brport_nla_put_flag(skb, flags, mask,
3146 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3147 brport_nla_put_flag(skb, flags, mask,
3148 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3149 nla_nest_cancel(skb, protinfo);
3150 goto nla_put_failure;
3151 }
3152
3153 nla_nest_end(skb, protinfo);
3154
Johannes Berg053c0952015-01-16 22:09:00 +01003155 nlmsg_end(skb, nlh);
3156 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003157nla_put_failure:
3158 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003159 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003160}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003161EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003162
John Fastabende5a55a82012-10-24 08:12:57 +00003163static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3164{
3165 struct net *net = sock_net(skb->sk);
3166 struct net_device *dev;
3167 int idx = 0;
3168 u32 portid = NETLINK_CB(cb->skb).portid;
3169 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003170 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003171 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003172
Thomas Grafaa68c202014-11-26 13:42:20 +01003173 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3174 struct nlattr *extfilt;
3175
3176 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3177 IFLA_EXT_MASK);
3178 if (extfilt) {
3179 if (nla_len(extfilt) < sizeof(filter_mask))
3180 return -EINVAL;
3181
3182 filter_mask = nla_get_u32(extfilt);
3183 }
3184 }
John Fastabende5a55a82012-10-24 08:12:57 +00003185
3186 rcu_read_lock();
3187 for_each_netdev_rcu(net, dev) {
3188 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003189 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003190
Jiri Pirko898e5062013-01-03 22:48:52 +00003191 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003192 if (idx >= cb->args[0]) {
3193 err = br_dev->netdev_ops->ndo_bridge_getlink(
3194 skb, portid, seq, dev,
3195 filter_mask, NLM_F_MULTI);
3196 if (err < 0 && err != -EOPNOTSUPP)
3197 break;
3198 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003199 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003200 }
3201
3202 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003203 if (idx >= cb->args[0]) {
3204 err = ops->ndo_bridge_getlink(skb, portid,
3205 seq, dev,
3206 filter_mask,
3207 NLM_F_MULTI);
3208 if (err < 0 && err != -EOPNOTSUPP)
3209 break;
3210 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003211 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003212 }
3213 }
3214 rcu_read_unlock();
3215 cb->args[0] = idx;
3216
3217 return skb->len;
3218}
3219
John Fastabend2469ffd2012-10-24 08:13:03 +00003220static inline size_t bridge_nlmsg_size(void)
3221{
3222 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3223 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3224 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3225 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3226 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3227 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3228 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3229 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3230 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3231 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3232 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3233}
3234
Roopa Prabhu02dba432015-01-14 20:02:25 -08003235static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003236{
3237 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003238 struct sk_buff *skb;
3239 int err = -EOPNOTSUPP;
3240
Roopa Prabhu02dba432015-01-14 20:02:25 -08003241 if (!dev->netdev_ops->ndo_bridge_getlink)
3242 return 0;
3243
John Fastabend2469ffd2012-10-24 08:13:03 +00003244 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3245 if (!skb) {
3246 err = -ENOMEM;
3247 goto errout;
3248 }
3249
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003250 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003251 if (err < 0)
3252 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003253
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003254 if (!skb->len)
3255 goto errout;
3256
John Fastabend2469ffd2012-10-24 08:13:03 +00003257 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3258 return 0;
3259errout:
3260 WARN_ON(err == -EMSGSIZE);
3261 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003262 if (err)
3263 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003264 return err;
3265}
3266
Thomas Graf661d2962013-03-21 07:45:29 +00003267static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003268{
3269 struct net *net = sock_net(skb->sk);
3270 struct ifinfomsg *ifm;
3271 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003272 struct nlattr *br_spec, *attr = NULL;
3273 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003274 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003275 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003276
3277 if (nlmsg_len(nlh) < sizeof(*ifm))
3278 return -EINVAL;
3279
3280 ifm = nlmsg_data(nlh);
3281 if (ifm->ifi_family != AF_BRIDGE)
3282 return -EPFNOSUPPORT;
3283
3284 dev = __dev_get_by_index(net, ifm->ifi_index);
3285 if (!dev) {
3286 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3287 return -ENODEV;
3288 }
3289
John Fastabend2469ffd2012-10-24 08:13:03 +00003290 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3291 if (br_spec) {
3292 nla_for_each_nested(attr, br_spec, rem) {
3293 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003294 if (nla_len(attr) < sizeof(flags))
3295 return -EINVAL;
3296
John Fastabendc38e01b2012-11-02 16:32:36 +00003297 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003298 flags = nla_get_u16(attr);
3299 break;
3300 }
3301 }
3302 }
3303
3304 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003305 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3306
3307 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003308 err = -EOPNOTSUPP;
3309 goto out;
3310 }
3311
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003312 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003313 if (err)
3314 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003315
3316 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003317 }
3318
John Fastabend2469ffd2012-10-24 08:13:03 +00003319 if ((flags & BRIDGE_FLAGS_SELF)) {
3320 if (!dev->netdev_ops->ndo_bridge_setlink)
3321 err = -EOPNOTSUPP;
3322 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003323 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3324 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003325 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003326 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003327
3328 /* Generate event to notify upper layer of bridge
3329 * change
3330 */
3331 err = rtnl_bridge_notify(dev);
3332 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003333 }
3334
John Fastabendc38e01b2012-11-02 16:32:36 +00003335 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003336 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003337out:
3338 return err;
3339}
3340
Thomas Graf661d2962013-03-21 07:45:29 +00003341static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003342{
3343 struct net *net = sock_net(skb->sk);
3344 struct ifinfomsg *ifm;
3345 struct net_device *dev;
3346 struct nlattr *br_spec, *attr = NULL;
3347 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003348 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003349 bool have_flags = false;
3350
3351 if (nlmsg_len(nlh) < sizeof(*ifm))
3352 return -EINVAL;
3353
3354 ifm = nlmsg_data(nlh);
3355 if (ifm->ifi_family != AF_BRIDGE)
3356 return -EPFNOSUPPORT;
3357
3358 dev = __dev_get_by_index(net, ifm->ifi_index);
3359 if (!dev) {
3360 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3361 return -ENODEV;
3362 }
3363
3364 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3365 if (br_spec) {
3366 nla_for_each_nested(attr, br_spec, rem) {
3367 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003368 if (nla_len(attr) < sizeof(flags))
3369 return -EINVAL;
3370
Vlad Yasevich407af322013-02-13 12:00:12 +00003371 have_flags = true;
3372 flags = nla_get_u16(attr);
3373 break;
3374 }
3375 }
3376 }
3377
Vlad Yasevich407af322013-02-13 12:00:12 +00003378 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3379 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3380
3381 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3382 err = -EOPNOTSUPP;
3383 goto out;
3384 }
3385
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003386 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003387 if (err)
3388 goto out;
3389
3390 flags &= ~BRIDGE_FLAGS_MASTER;
3391 }
3392
3393 if ((flags & BRIDGE_FLAGS_SELF)) {
3394 if (!dev->netdev_ops->ndo_bridge_dellink)
3395 err = -EOPNOTSUPP;
3396 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003397 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3398 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003399
Roopa Prabhu02dba432015-01-14 20:02:25 -08003400 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003401 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003402
3403 /* Generate event to notify upper layer of bridge
3404 * change
3405 */
3406 err = rtnl_bridge_notify(dev);
3407 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003408 }
3409
3410 if (have_flags)
3411 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003412out:
3413 return err;
3414}
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416/* Process one rtnetlink message. */
3417
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003418static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003420 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003421 rtnl_doit_func doit;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06003422 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 int family;
3424 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003425 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003429 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 type -= RTM_BASE;
3432
3433 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003434 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return 0;
3436
Hong zhi guo573ce262013-03-27 06:47:04 +00003437 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 kind = type&3;
3439
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003440 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003441 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
David S. Millerb8f3ab42011-01-18 12:40:38 -08003443 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003444 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003445 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003446 rtnl_calcit_func calcit;
3447 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Thomas Grafe2849862007-03-22 11:48:11 -07003449 dumpit = rtnl_get_dumpit(family, type);
3450 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003451 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003452 calcit = rtnl_get_calcit(family, type);
3453 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003454 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Eric Dumazet2907c352011-05-25 07:34:04 +00003456 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003457 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003458 {
3459 struct netlink_dump_control c = {
3460 .dump = dumpit,
3461 .min_dump_alloc = min_dump_alloc,
3462 };
3463 err = netlink_dump_start(rtnl, skb, nlh, &c);
3464 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003465 rtnl_lock();
3466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
3468
Thomas Grafe2849862007-03-22 11:48:11 -07003469 doit = rtnl_get_doit(family, type);
3470 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003471 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Thomas Graf661d2962013-03-21 07:45:29 +00003473 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003476static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003478 rtnl_lock();
3479 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3480 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481}
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3484{
Jiri Pirko351638e2013-05-28 01:30:21 +00003485 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 case NETDEV_UP:
3489 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003490 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003491 case NETDEV_POST_INIT:
3492 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003494 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003496 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003497 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003498 case NETDEV_RELEASE:
3499 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003500 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 break;
3502 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003503 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 break;
3505 }
3506 return NOTIFY_DONE;
3507}
3508
3509static struct notifier_block rtnetlink_dev_notifier = {
3510 .notifier_call = rtnetlink_event,
3511};
3512
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003513
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003514static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003515{
3516 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003517 struct netlink_kernel_cfg cfg = {
3518 .groups = RTNLGRP_MAX,
3519 .input = rtnetlink_rcv,
3520 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003521 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003522 };
3523
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003524 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003525 if (!sk)
3526 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003527 net->rtnl = sk;
3528 return 0;
3529}
3530
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003531static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003532{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003533 netlink_kernel_release(net->rtnl);
3534 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003535}
3536
3537static struct pernet_operations rtnetlink_net_ops = {
3538 .init = rtnetlink_net_init,
3539 .exit = rtnetlink_net_exit,
3540};
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542void __init rtnetlink_init(void)
3543{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003544 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003548
Greg Rosec7ac8672011-06-10 01:27:09 +00003549 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3550 rtnl_dump_ifinfo, rtnl_calcit);
3551 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3552 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3553 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003554
Greg Rosec7ac8672011-06-10 01:27:09 +00003555 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3556 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003557
3558 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3559 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3560 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003561
3562 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003563 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003564 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565}