blob: eadc5c0e2dfa99e81d4201593bb8b2dbb3b8d42d [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
99int lockdep_rtnl_is_held(void)
100{
101 return lockdep_is_held(&rtnl_mutex);
102}
103EXPORT_SYMBOL(lockdep_rtnl_is_held);
104#endif /* #ifdef CONFIG_PROVE_LOCKING */
105
Patrick McHardy25239ce2010-04-26 16:02:05 +0200106static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700107
108static inline int rtm_msgindex(int msgtype)
109{
110 int msgindex = msgtype - RTM_BASE;
111
112 /*
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
116 */
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118
119 return msgindex;
120}
121
122static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
123{
124 struct rtnl_link *tab;
125
Patrick McHardy25239ce2010-04-26 16:02:05 +0200126 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
130
Thomas Graf51057f22007-03-22 21:41:06 -0700131 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700132 tab = rtnl_msg_handlers[PF_UNSPEC];
133
Hans Zhangc80bbea2012-10-22 22:21:23 +0000134 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700135}
136
137static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
138{
139 struct rtnl_link *tab;
140
Patrick McHardy25239ce2010-04-26 16:02:05 +0200141 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
145
Thomas Graf51057f22007-03-22 21:41:06 -0700146 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700147 tab = rtnl_msg_handlers[PF_UNSPEC];
148
Hans Zhangc80bbea2012-10-22 22:21:23 +0000149 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700150}
151
Greg Rosec7ac8672011-06-10 01:27:09 +0000152static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
153{
154 struct rtnl_link *tab;
155
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
160
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
163
Hans Zhangc80bbea2012-10-22 22:21:23 +0000164 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000165}
166
Thomas Grafe2849862007-03-22 11:48:11 -0700167/**
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000173 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700174 *
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
178 *
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
182 *
183 * Returns 0 on success or a negative error code.
184 */
185int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700188{
189 struct rtnl_link *tab;
190 int msgindex;
191
Patrick McHardy25239ce2010-04-26 16:02:05 +0200192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700193 msgindex = rtm_msgindex(msgtype);
194
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
200
201 rtnl_msg_handlers[protocol] = tab;
202 }
203
204 if (doit)
205 tab[msgindex].doit = doit;
206
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
209
Greg Rosec7ac8672011-06-10 01:27:09 +0000210 if (calcit)
211 tab[msgindex].calcit = calcit;
212
Thomas Grafe2849862007-03-22 11:48:11 -0700213 return 0;
214}
Thomas Grafe2849862007-03-22 11:48:11 -0700215EXPORT_SYMBOL_GPL(__rtnl_register);
216
217/**
218 * rtnl_register - Register a rtnetlink message type
219 *
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700225 */
226void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700229{
Greg Rosec7ac8672011-06-10 01:27:09 +0000230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
234}
Thomas Grafe2849862007-03-22 11:48:11 -0700235EXPORT_SYMBOL_GPL(rtnl_register);
236
237/**
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
241 *
242 * Returns 0 on success or a negative error code.
243 */
244int rtnl_unregister(int protocol, int msgtype)
245{
246 int msgindex;
247
Patrick McHardy25239ce2010-04-26 16:02:05 +0200248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700249 msgindex = rtm_msgindex(msgtype);
250
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
253
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256
257 return 0;
258}
Thomas Grafe2849862007-03-22 11:48:11 -0700259EXPORT_SYMBOL_GPL(rtnl_unregister);
260
261/**
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
264 *
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
267 */
268void rtnl_unregister_all(int protocol)
269{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700271
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
274}
Thomas Grafe2849862007-03-22 11:48:11 -0700275EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick McHardy38f7b872007-06-13 12:03:51 -0700277static LIST_HEAD(link_ops);
278
Eric Dumazetc63044f2011-12-13 11:38:00 +0000279static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
280{
281 const struct rtnl_link_ops *ops;
282
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
286 }
287 return NULL;
288}
289
Patrick McHardy38f7b872007-06-13 12:03:51 -0700290/**
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
293 *
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
297 *
298 * Returns 0 on success or a negative error code.
299 */
300int __rtnl_link_register(struct rtnl_link_ops *ops)
301{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
304
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
309 */
310 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000311 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700312
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313 list_add_tail(&ops->list, &link_ops);
314 return 0;
315}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700316EXPORT_SYMBOL_GPL(__rtnl_link_register);
317
318/**
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
321 *
322 * Returns 0 on success or a negative error code.
323 */
324int rtnl_link_register(struct rtnl_link_ops *ops)
325{
326 int err;
327
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
332}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700333EXPORT_SYMBOL_GPL(rtnl_link_register);
334
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
336{
337 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 LIST_HEAD(list_kill);
339
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700343 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000344 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700345}
346
Patrick McHardy38f7b872007-06-13 12:03:51 -0700347/**
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
350 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700351 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352 */
353void __rtnl_link_unregister(struct rtnl_link_ops *ops)
354{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700355 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356
Eric W. Biederman881d9662007-09-17 11:56:21 -0700357 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700358 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700359 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700360 list_del(&ops->list);
361}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700362EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
363
Cong Wang200b9162014-05-12 15:11:20 -0700364/* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
366 */
367static void rtnl_lock_unregistering_all(void)
368{
369 struct net *net;
370 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700372
Peter Zijlstraff960a72014-10-29 17:04:56 +0100373 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700374 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
381 }
382 }
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100386
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700388 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100389 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700390}
391
Patrick McHardy38f7b872007-06-13 12:03:51 -0700392/**
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
395 */
396void rtnl_link_unregister(struct rtnl_link_ops *ops)
397{
Cong Wang200b9162014-05-12 15:11:20 -0700398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700403 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405EXPORT_SYMBOL_GPL(rtnl_link_unregister);
406
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100407static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
408{
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
411
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900416 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
421}
422
Patrick McHardy38f7b872007-06-13 12:03:51 -0700423static size_t rtnl_link_get_size(const struct net_device *dev)
424{
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
427
428 if (!ops)
429 return 0;
430
Thomas Graf369cf772010-11-11 15:47:59 +0000431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700433
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000436 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700437 ops->get_size(dev);
438
439 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100443 size += rtnl_link_get_slave_info_data_size(dev);
444
Patrick McHardy38f7b872007-06-13 12:03:51 -0700445 return size;
446}
447
Thomas Graff8ff1822010-11-16 04:30:14 +0000448static LIST_HEAD(rtnl_af_ops);
449
450static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
451{
452 const struct rtnl_af_ops *ops;
453
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
457 }
458
459 return NULL;
460}
461
462/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
465 *
466 * Returns 0 on success or a negative error code.
467 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800468void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000469{
Thomas Graff8ff1822010-11-16 04:30:14 +0000470 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800471 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000473}
474EXPORT_SYMBOL_GPL(rtnl_af_register);
475
476/**
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
479 *
480 * The caller must hold the rtnl_mutex.
481 */
482void __rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 list_del(&ops->list);
485}
486EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
487
488/**
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
491 */
492void rtnl_af_unregister(struct rtnl_af_ops *ops)
493{
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
497}
498EXPORT_SYMBOL_GPL(rtnl_af_unregister);
499
500static size_t rtnl_link_get_af_size(const struct net_device *dev)
501{
502 struct rtnl_af_ops *af_ops;
503 size_t size;
504
505 /* IFLA_AF_SPEC */
506 size = nla_total_size(sizeof(struct nlattr));
507
508 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
509 if (af_ops->get_link_af_size) {
510 /* AF_* + nested data */
511 size += nla_total_size(sizeof(struct nlattr)) +
512 af_ops->get_link_af_size(dev);
513 }
514 }
515
516 return size;
517}
518
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100519static bool rtnl_have_link_slave_info(const struct net_device *dev)
520{
521 struct net_device *master_dev;
522
523 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100524 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100525 return true;
526 return false;
527}
528
529static int rtnl_link_slave_info_fill(struct sk_buff *skb,
530 const struct net_device *dev)
531{
532 struct net_device *master_dev;
533 const struct rtnl_link_ops *ops;
534 struct nlattr *slave_data;
535 int err;
536
537 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
538 if (!master_dev)
539 return 0;
540 ops = master_dev->rtnl_link_ops;
541 if (!ops)
542 return 0;
543 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
544 return -EMSGSIZE;
545 if (ops->fill_slave_info) {
546 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
547 if (!slave_data)
548 return -EMSGSIZE;
549 err = ops->fill_slave_info(skb, master_dev, dev);
550 if (err < 0)
551 goto err_cancel_slave_data;
552 nla_nest_end(skb, slave_data);
553 }
554 return 0;
555
556err_cancel_slave_data:
557 nla_nest_cancel(skb, slave_data);
558 return err;
559}
560
561static int rtnl_link_info_fill(struct sk_buff *skb,
562 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700563{
564 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100565 struct nlattr *data;
566 int err;
567
568 if (!ops)
569 return 0;
570 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
571 return -EMSGSIZE;
572 if (ops->fill_xstats) {
573 err = ops->fill_xstats(skb, dev);
574 if (err < 0)
575 return err;
576 }
577 if (ops->fill_info) {
578 data = nla_nest_start(skb, IFLA_INFO_DATA);
579 if (data == NULL)
580 return -EMSGSIZE;
581 err = ops->fill_info(skb, dev);
582 if (err < 0)
583 goto err_cancel_data;
584 nla_nest_end(skb, data);
585 }
586 return 0;
587
588err_cancel_data:
589 nla_nest_cancel(skb, data);
590 return err;
591}
592
593static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
594{
595 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700596 int err = -EMSGSIZE;
597
598 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
599 if (linkinfo == NULL)
600 goto out;
601
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100602 err = rtnl_link_info_fill(skb, dev);
603 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700604 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605
606 err = rtnl_link_slave_info_fill(skb, dev);
607 if (err < 0)
608 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700609
610 nla_nest_end(skb, linkinfo);
611 return 0;
612
Patrick McHardy38f7b872007-06-13 12:03:51 -0700613err_cancel_link:
614 nla_nest_cancel(skb, linkinfo);
615out:
616 return err;
617}
618
Eric Dumazet95c96172012-04-15 05:58:06 +0000619int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800621 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int err = 0;
623
Patrick McHardyac6d4392005-08-14 19:29:52 -0700624 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (echo)
626 atomic_inc(&skb->users);
627 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
628 if (echo)
629 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
630 return err;
631}
632
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800633int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700634{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800635 struct sock *rtnl = net->rtnl;
636
Thomas Graf2942e902006-08-15 00:30:25 -0700637 return nlmsg_unicast(rtnl, skb, pid);
638}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800639EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700640
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800641void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
642 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700643{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800644 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700645 int report = 0;
646
647 if (nlh)
648 report = nlmsg_report(nlh);
649
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800650 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700651}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800652EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700653
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800654void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700655{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800656 struct sock *rtnl = net->rtnl;
657
Thomas Graf97676b62006-08-15 00:31:41 -0700658 netlink_set_err(rtnl, 0, group, error);
659}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800660EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
663{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700664 struct nlattr *mx;
665 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 mx = nla_nest_start(skb, RTA_METRICS);
668 if (mx == NULL)
669 return -ENOBUFS;
670
671 for (i = 0; i < RTAX_MAX; i++) {
672 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100673 if (i == RTAX_CC_ALGO - 1) {
674 char tmp[TCP_CA_NAME_MAX], *name;
675
676 name = tcp_ca_get_name_by_key(metrics[i], tmp);
677 if (!name)
678 continue;
679 if (nla_put_string(skb, i + 1, name))
680 goto nla_put_failure;
681 } else {
682 if (nla_put_u32(skb, i + 1, metrics[i]))
683 goto nla_put_failure;
684 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700685 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Millera57d27f2006-08-22 22:20:14 -0700689 if (!valid) {
690 nla_nest_cancel(skb, mx);
691 return 0;
692 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693
694 return nla_nest_end(skb, mx);
695
696nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700697 nla_nest_cancel(skb, mx);
698 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800700EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Thomas Grafe3703b32006-11-27 09:27:07 -0800702int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700703 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800704{
705 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000706 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800707 .rta_used = dst->__use,
708 .rta_clntref = atomic_read(&(dst->__refcnt)),
709 .rta_error = error,
710 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800711 };
712
Li Wei82539472012-07-29 16:01:30 +0000713 if (expires) {
714 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800715
Li Wei82539472012-07-29 16:01:30 +0000716 clock = jiffies_to_clock_t(abs(expires));
717 clock = min_t(unsigned long, clock, INT_MAX);
718 ci.rta_expires = (expires > 0) ? clock : -clock;
719 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800720 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
721}
Thomas Grafe3703b32006-11-27 09:27:07 -0800722EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David S. Miller93b2d4a2008-02-17 18:35:07 -0800724static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800725{
726 unsigned char operstate = dev->operstate;
727
Eric Dumazete0d087a2009-11-07 01:26:17 -0800728 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800729 case IF_OPER_UP:
730 if ((operstate == IF_OPER_DORMANT ||
731 operstate == IF_OPER_UNKNOWN) &&
732 !netif_dormant(dev))
733 operstate = IF_OPER_UP;
734 break;
735
736 case IF_OPER_DORMANT:
737 if (operstate == IF_OPER_UP ||
738 operstate == IF_OPER_UNKNOWN)
739 operstate = IF_OPER_DORMANT;
740 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700741 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800742
743 if (dev->operstate != operstate) {
744 write_lock_bh(&dev_base_lock);
745 dev->operstate = operstate;
746 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800747 netdev_state_change(dev);
748 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800749}
750
Jiri Bencb1beb682012-07-27 02:58:22 +0000751static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
752{
753 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
754 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
755}
756
Patrick McHardy3729d502010-02-26 06:34:54 +0000757static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
758 const struct ifinfomsg *ifm)
759{
760 unsigned int flags = ifm->ifi_flags;
761
762 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
763 if (ifm->ifi_change)
764 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000765 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000766
767 return flags;
768}
769
Thomas Grafb60c5112006-08-04 23:05:34 -0700770static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000771 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Thomas Grafb60c5112006-08-04 23:05:34 -0700773 a->rx_packets = b->rx_packets;
774 a->tx_packets = b->tx_packets;
775 a->rx_bytes = b->rx_bytes;
776 a->tx_bytes = b->tx_bytes;
777 a->rx_errors = b->rx_errors;
778 a->tx_errors = b->tx_errors;
779 a->rx_dropped = b->rx_dropped;
780 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Thomas Grafb60c5112006-08-04 23:05:34 -0700782 a->multicast = b->multicast;
783 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Thomas Grafb60c5112006-08-04 23:05:34 -0700785 a->rx_length_errors = b->rx_length_errors;
786 a->rx_over_errors = b->rx_over_errors;
787 a->rx_crc_errors = b->rx_crc_errors;
788 a->rx_frame_errors = b->rx_frame_errors;
789 a->rx_fifo_errors = b->rx_fifo_errors;
790 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Grafb60c5112006-08-04 23:05:34 -0700792 a->tx_aborted_errors = b->tx_aborted_errors;
793 a->tx_carrier_errors = b->tx_carrier_errors;
794 a->tx_fifo_errors = b->tx_fifo_errors;
795 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
796 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Thomas Grafb60c5112006-08-04 23:05:34 -0700798 a->rx_compressed = b->rx_compressed;
799 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000800}
801
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000802static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000803{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000804 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000805}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chris Wrightc02db8c2010-05-16 01:05:45 -0700807/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500808static inline int rtnl_vfinfo_size(const struct net_device *dev,
809 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000810{
Greg Rose115c9b82012-02-21 16:54:48 -0500811 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
812 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700813 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700814 size_t size = nla_total_size(sizeof(struct nlattr));
815 size += nla_total_size(num_vfs * sizeof(struct nlattr));
816 size += num_vfs *
817 (nla_total_size(sizeof(struct ifla_vf_mac)) +
818 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400819 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200820 nla_total_size(sizeof(struct ifla_vf_rate)) +
821 nla_total_size(sizeof(struct ifla_vf_link_state)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700822 return size;
823 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000824 return 0;
825}
826
David Gibsonc53864f2014-04-24 10:22:36 +1000827static size_t rtnl_port_size(const struct net_device *dev,
828 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700829{
830 size_t port_size = nla_total_size(4) /* PORT_VF */
831 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
832 + nla_total_size(sizeof(struct ifla_port_vsi))
833 /* PORT_VSI_TYPE */
834 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
835 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
836 + nla_total_size(1) /* PROT_VDP_REQUEST */
837 + nla_total_size(2); /* PORT_VDP_RESPONSE */
838 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
839 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
840 + port_size;
841 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
842 + port_size;
843
David Gibsonc53864f2014-04-24 10:22:36 +1000844 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
845 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700846 return 0;
847 if (dev_num_vf(dev->dev.parent))
848 return port_self_size + vf_ports_size +
849 vf_port_size * dev_num_vf(dev->dev.parent);
850 else
851 return port_self_size;
852}
853
Greg Rose115c9b82012-02-21 16:54:48 -0500854static noinline size_t if_nlmsg_size(const struct net_device *dev,
855 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800856{
857 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
858 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700859 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800860 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
861 + nla_total_size(sizeof(struct rtnl_link_ifmap))
862 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700863 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800864 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
865 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
866 + nla_total_size(4) /* IFLA_TXQLEN */
867 + nla_total_size(4) /* IFLA_WEIGHT */
868 + nla_total_size(4) /* IFLA_MTU */
869 + nla_total_size(4) /* IFLA_LINK */
870 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000871 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000872 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000873 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
874 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800875 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700876 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700877 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Greg Rose115c9b82012-02-21 16:54:48 -0500878 + nla_total_size(ext_filter_mask
879 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
880 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000881 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000882 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200883 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100884 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
885 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800886}
887
Scott Feldman57b61082010-05-17 22:49:55 -0700888static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
889{
890 struct nlattr *vf_ports;
891 struct nlattr *vf_port;
892 int vf;
893 int err;
894
895 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
896 if (!vf_ports)
897 return -EMSGSIZE;
898
899 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
900 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700901 if (!vf_port)
902 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400903 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
904 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700905 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700906 if (err == -EMSGSIZE)
907 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700908 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700909 nla_nest_cancel(skb, vf_port);
910 continue;
911 }
912 nla_nest_end(skb, vf_port);
913 }
914
915 nla_nest_end(skb, vf_ports);
916
917 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700918
919nla_put_failure:
920 nla_nest_cancel(skb, vf_ports);
921 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700922}
923
924static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
925{
926 struct nlattr *port_self;
927 int err;
928
929 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
930 if (!port_self)
931 return -EMSGSIZE;
932
933 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
934 if (err) {
935 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700936 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700937 }
938
939 nla_nest_end(skb, port_self);
940
941 return 0;
942}
943
David Gibsonc53864f2014-04-24 10:22:36 +1000944static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
945 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700946{
947 int err;
948
David Gibsonc53864f2014-04-24 10:22:36 +1000949 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
950 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700951 return 0;
952
953 err = rtnl_port_self_fill(skb, dev);
954 if (err)
955 return err;
956
957 if (dev_num_vf(dev->dev.parent)) {
958 err = rtnl_vf_ports_fill(skb, dev);
959 if (err)
960 return err;
961 }
962
963 return 0;
964}
965
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200966static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
967{
968 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100969 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200970
971 err = dev_get_phys_port_id(dev, &ppid);
972 if (err) {
973 if (err == -EOPNOTSUPP)
974 return 0;
975 return err;
976 }
977
978 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
979 return -EMSGSIZE;
980
981 return 0;
982}
983
Jiri Pirko82f28412014-11-28 14:34:18 +0100984static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
985{
986 int err;
987 struct netdev_phys_item_id psid;
988
989 err = netdev_switch_parent_id_get(dev, &psid);
990 if (err) {
991 if (err == -EOPNOTSUPP)
992 return 0;
993 return err;
994 }
995
996 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
997 return -EMSGSIZE;
998
999 return 0;
1000}
1001
Thomas Grafb60c5112006-08-04 23:05:34 -07001002static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001003 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001004 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001005{
1006 struct ifinfomsg *ifm;
1007 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001008 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001009 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001010 struct nlattr *attr, *af_spec;
1011 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001012 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001013
Eric Dumazet2907c352011-05-25 07:34:04 +00001014 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001015 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1016 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001017 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001018
1019 ifm = nlmsg_data(nlh);
1020 ifm->ifi_family = AF_UNSPEC;
1021 ifm->__ifi_pad = 0;
1022 ifm->ifi_type = dev->type;
1023 ifm->ifi_index = dev->ifindex;
1024 ifm->ifi_flags = dev_get_flags(dev);
1025 ifm->ifi_change = change;
1026
David S. Millera6574342012-04-01 20:12:00 -04001027 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1028 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1029 nla_put_u8(skb, IFLA_OPERSTATE,
1030 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1031 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1032 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1033 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001034 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001035 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001036#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001037 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001038#endif
David S. Millera6574342012-04-01 20:12:00 -04001039 (dev->ifindex != dev->iflink &&
1040 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001041 (upper_dev &&
1042 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001043 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001044 (dev->qdisc &&
1045 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1046 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001047 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1048 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1049 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001050 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001051
Stefan Rompfb00055a2006-03-20 17:09:11 -08001052 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct rtnl_link_ifmap map = {
1054 .mem_start = dev->mem_start,
1055 .mem_end = dev->mem_end,
1056 .base_addr = dev->base_addr,
1057 .irq = dev->irq,
1058 .dma = dev->dma,
1059 .port = dev->if_port,
1060 };
David S. Millera6574342012-04-01 20:12:00 -04001061 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1062 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
1065 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001066 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1067 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1068 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001071 if (rtnl_phys_port_id_fill(skb, dev))
1072 goto nla_put_failure;
1073
Jiri Pirko82f28412014-11-28 14:34:18 +01001074 if (rtnl_phys_switch_id_fill(skb, dev))
1075 goto nla_put_failure;
1076
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001077 attr = nla_reserve(skb, IFLA_STATS,
1078 sizeof(struct rtnl_link_stats));
1079 if (attr == NULL)
1080 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Eric Dumazet28172732010-07-07 14:58:56 -07001082 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001083 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Jan Engelhardt10708f32010-03-11 09:57:29 +00001085 attr = nla_reserve(skb, IFLA_STATS64,
1086 sizeof(struct rtnl_link_stats64));
1087 if (attr == NULL)
1088 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001089 copy_rtnl_link_stats64(nla_data(attr), stats);
1090
David S. Millera6574342012-04-01 20:12:00 -04001091 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1092 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1093 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001094
Greg Rose115c9b82012-02-21 16:54:48 -05001095 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1096 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001097 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001098
Chris Wrightc02db8c2010-05-16 01:05:45 -07001099 struct nlattr *vfinfo, *vf;
1100 int num_vfs = dev_num_vf(dev->dev.parent);
1101
Chris Wrightc02db8c2010-05-16 01:05:45 -07001102 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1103 if (!vfinfo)
1104 goto nla_put_failure;
1105 for (i = 0; i < num_vfs; i++) {
1106 struct ifla_vf_info ivi;
1107 struct ifla_vf_mac vf_mac;
1108 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001109 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001110 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001111 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001112 struct ifla_vf_link_state vf_linkstate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001113
1114 /*
1115 * Not all SR-IOV capable drivers support the
1116 * spoofcheck query. Preset to -1 so the user
1117 * space tool can detect that the driver didn't
1118 * report anything.
1119 */
1120 ivi.spoofchk = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001121 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001122 /* The default value for VF link state is "auto"
1123 * IFLA_VF_LINK_STATE_AUTO which equals zero
1124 */
1125 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001126 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1127 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001128 vf_mac.vf =
1129 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001130 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001131 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001132 vf_spoofchk.vf =
1133 vf_linkstate.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001134
Chris Wrightc02db8c2010-05-16 01:05:45 -07001135 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1136 vf_vlan.vlan = ivi.vlan;
1137 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001138 vf_tx_rate.rate = ivi.max_tx_rate;
1139 vf_rate.min_tx_rate = ivi.min_tx_rate;
1140 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001141 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001142 vf_linkstate.link_state = ivi.linkstate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001143 vf = nla_nest_start(skb, IFLA_VF_INFO);
1144 if (!vf) {
1145 nla_nest_cancel(skb, vfinfo);
1146 goto nla_put_failure;
1147 }
David S. Millera6574342012-04-01 20:12:00 -04001148 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1149 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001150 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1151 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001152 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1153 &vf_tx_rate) ||
1154 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001155 &vf_spoofchk) ||
1156 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1157 &vf_linkstate))
David S. Millera6574342012-04-01 20:12:00 -04001158 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001159 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001160 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001161 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001162 }
Scott Feldman57b61082010-05-17 22:49:55 -07001163
David Gibsonc53864f2014-04-24 10:22:36 +10001164 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001165 goto nla_put_failure;
1166
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001167 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001168 if (rtnl_link_fill(skb, dev) < 0)
1169 goto nla_put_failure;
1170 }
1171
Thomas Graff8ff1822010-11-16 04:30:14 +00001172 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1173 goto nla_put_failure;
1174
1175 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1176 if (af_ops->fill_link_af) {
1177 struct nlattr *af;
1178 int err;
1179
1180 if (!(af = nla_nest_start(skb, af_ops->family)))
1181 goto nla_put_failure;
1182
1183 err = af_ops->fill_link_af(skb, dev);
1184
1185 /*
1186 * Caller may return ENODATA to indicate that there
1187 * was no data to be dumped. This is not an error, it
1188 * means we should trim the attribute header and
1189 * continue.
1190 */
1191 if (err == -ENODATA)
1192 nla_nest_cancel(skb, af);
1193 else if (err < 0)
1194 goto nla_put_failure;
1195
1196 nla_nest_end(skb, af);
1197 }
1198 }
1199
1200 nla_nest_end(skb, af_spec);
1201
Thomas Grafb60c5112006-08-04 23:05:34 -07001202 return nlmsg_end(skb, nlh);
1203
1204nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001205 nlmsg_cancel(skb, nlh);
1206 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Jiri Pirkof7b12602014-02-18 20:53:18 +01001209static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1210 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1211 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1212 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1213 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1214 [IFLA_MTU] = { .type = NLA_U32 },
1215 [IFLA_LINK] = { .type = NLA_U32 },
1216 [IFLA_MASTER] = { .type = NLA_U32 },
1217 [IFLA_CARRIER] = { .type = NLA_U8 },
1218 [IFLA_TXQLEN] = { .type = NLA_U32 },
1219 [IFLA_WEIGHT] = { .type = NLA_U32 },
1220 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1221 [IFLA_LINKMODE] = { .type = NLA_U8 },
1222 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1223 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1224 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1225 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1226 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1227 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1228 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1229 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1230 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1231 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1232 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1233 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001234 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001235 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001236 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001237};
1238
1239static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1240 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1241 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1242 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1243 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1244};
1245
1246static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1247 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1248};
1249
1250static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1251 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1252 .len = sizeof(struct ifla_vf_mac) },
1253 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1254 .len = sizeof(struct ifla_vf_vlan) },
1255 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1256 .len = sizeof(struct ifla_vf_tx_rate) },
1257 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1258 .len = sizeof(struct ifla_vf_spoofchk) },
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001259 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1260 .len = sizeof(struct ifla_vf_rate) },
Doug Ledfordc5b46162014-06-11 10:38:03 -04001261 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1262 .len = sizeof(struct ifla_vf_link_state) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001263};
1264
1265static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1266 [IFLA_PORT_VF] = { .type = NLA_U32 },
1267 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1268 .len = PORT_PROFILE_MAX },
1269 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1270 .len = sizeof(struct ifla_port_vsi)},
1271 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1272 .len = PORT_UUID_MAX },
1273 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1274 .len = PORT_UUID_MAX },
1275 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1276 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1277};
1278
Thomas Grafb60c5112006-08-04 23:05:34 -07001279static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001281 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001282 int h, s_h;
1283 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001285 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001286 struct nlattr *tb[IFLA_MAX+1];
1287 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001288 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001289 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001291 s_h = cb->args[0];
1292 s_idx = cb->args[1];
1293
Eric Dumazete67f88d2011-04-27 22:56:07 +00001294 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001295 cb->seq = net->dev_base_seq;
1296
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001297 /* A hack to preserve kernel<->userspace interface.
1298 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1299 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1300 * what iproute2 < v3.9.0 used.
1301 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1302 * attribute, its netlink message is shorter than struct ifinfomsg.
1303 */
1304 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1305 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1306
1307 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001308
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001309 if (tb[IFLA_EXT_MASK])
1310 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1311 }
Greg Rose115c9b82012-02-21 16:54:48 -05001312
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001313 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1314 idx = 0;
1315 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001316 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001317 if (idx < s_idx)
1318 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001319 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1320 NETLINK_CB(cb->skb).portid,
1321 cb->nlh->nlmsg_seq, 0,
1322 NLM_F_MULTI,
1323 ext_filter_mask);
1324 /* If we ran out of room on the first message,
1325 * we're in trouble
1326 */
1327 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1328
1329 if (err <= 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001330 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001331
1332 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001333cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001334 idx++;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001337out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001338 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001339 cb->args[1] = idx;
1340 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 return skb->len;
1343}
1344
Jiri Pirkof7b12602014-02-18 20:53:18 +01001345int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1346{
1347 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1348}
1349EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001350
Eric W. Biederman81adee42009-11-08 00:53:51 -08001351struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1352{
1353 struct net *net;
1354 /* Examine the link attributes and figure out which
1355 * network namespace we are talking about.
1356 */
1357 if (tb[IFLA_NET_NS_PID])
1358 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001359 else if (tb[IFLA_NET_NS_FD])
1360 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001361 else
1362 net = get_net(src_net);
1363 return net;
1364}
1365EXPORT_SYMBOL(rtnl_link_get_net);
1366
Thomas Graf1840bb12008-02-23 19:54:36 -08001367static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1368{
1369 if (dev) {
1370 if (tb[IFLA_ADDRESS] &&
1371 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1372 return -EINVAL;
1373
1374 if (tb[IFLA_BROADCAST] &&
1375 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1376 return -EINVAL;
1377 }
1378
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001379 if (tb[IFLA_AF_SPEC]) {
1380 struct nlattr *af;
1381 int rem, err;
1382
1383 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1384 const struct rtnl_af_ops *af_ops;
1385
1386 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1387 return -EAFNOSUPPORT;
1388
1389 if (!af_ops->set_link_af)
1390 return -EOPNOTSUPP;
1391
1392 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001393 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001394 if (err < 0)
1395 return err;
1396 }
1397 }
1398 }
1399
Thomas Graf1840bb12008-02-23 19:54:36 -08001400 return 0;
1401}
1402
Chris Wrightc02db8c2010-05-16 01:05:45 -07001403static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1404{
1405 int rem, err = -EINVAL;
1406 struct nlattr *vf;
1407 const struct net_device_ops *ops = dev->netdev_ops;
1408
1409 nla_for_each_nested(vf, attr, rem) {
1410 switch (nla_type(vf)) {
1411 case IFLA_VF_MAC: {
1412 struct ifla_vf_mac *ivm;
1413 ivm = nla_data(vf);
1414 err = -EOPNOTSUPP;
1415 if (ops->ndo_set_vf_mac)
1416 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1417 ivm->mac);
1418 break;
1419 }
1420 case IFLA_VF_VLAN: {
1421 struct ifla_vf_vlan *ivv;
1422 ivv = nla_data(vf);
1423 err = -EOPNOTSUPP;
1424 if (ops->ndo_set_vf_vlan)
1425 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1426 ivv->vlan,
1427 ivv->qos);
1428 break;
1429 }
1430 case IFLA_VF_TX_RATE: {
1431 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001432 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001433 ivt = nla_data(vf);
1434 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001435 if (ops->ndo_get_vf_config)
1436 err = ops->ndo_get_vf_config(dev, ivt->vf,
1437 &ivf);
1438 if (err)
1439 break;
1440 err = -EOPNOTSUPP;
1441 if (ops->ndo_set_vf_rate)
1442 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1443 ivf.min_tx_rate,
1444 ivt->rate);
1445 break;
1446 }
1447 case IFLA_VF_RATE: {
1448 struct ifla_vf_rate *ivt;
1449 ivt = nla_data(vf);
1450 err = -EOPNOTSUPP;
1451 if (ops->ndo_set_vf_rate)
1452 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1453 ivt->min_tx_rate,
1454 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001455 break;
1456 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001457 case IFLA_VF_SPOOFCHK: {
1458 struct ifla_vf_spoofchk *ivs;
1459 ivs = nla_data(vf);
1460 err = -EOPNOTSUPP;
1461 if (ops->ndo_set_vf_spoofchk)
1462 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1463 ivs->setting);
1464 break;
1465 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001466 case IFLA_VF_LINK_STATE: {
1467 struct ifla_vf_link_state *ivl;
1468 ivl = nla_data(vf);
1469 err = -EOPNOTSUPP;
1470 if (ops->ndo_set_vf_link_state)
1471 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1472 ivl->link_state);
1473 break;
1474 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001475 default:
1476 err = -EINVAL;
1477 break;
1478 }
1479 if (err)
1480 break;
1481 }
1482 return err;
1483}
1484
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001485static int do_set_master(struct net_device *dev, int ifindex)
1486{
Jiri Pirko898e5062013-01-03 22:48:52 +00001487 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001488 const struct net_device_ops *ops;
1489 int err;
1490
Jiri Pirko898e5062013-01-03 22:48:52 +00001491 if (upper_dev) {
1492 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001493 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001494 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001495 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001496 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001497 if (err)
1498 return err;
1499 } else {
1500 return -EOPNOTSUPP;
1501 }
1502 }
1503
1504 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001505 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1506 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001507 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001508 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001509 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001510 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001511 if (err)
1512 return err;
1513 } else {
1514 return -EOPNOTSUPP;
1515 }
1516 }
1517 return 0;
1518}
1519
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001520#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001521/* notify flag means notify + modified. */
1522#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001523static int do_setlink(const struct sk_buff *skb,
1524 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001525 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001526{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001527 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001528 int err;
1529
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001530 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001531 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001532 if (IS_ERR(net)) {
1533 err = PTR_ERR(net);
1534 goto errout;
1535 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001536 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001537 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001538 err = -EPERM;
1539 goto errout;
1540 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001541 err = dev_change_net_namespace(dev, net, ifname);
1542 put_net(net);
1543 if (err)
1544 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001545 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001546 }
1547
Patrick McHardy0157f602007-06-13 12:03:36 -07001548 if (tb[IFLA_MAP]) {
1549 struct rtnl_link_ifmap *u_map;
1550 struct ifmap k_map;
1551
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001552 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001553 err = -EOPNOTSUPP;
1554 goto errout;
1555 }
1556
1557 if (!netif_device_present(dev)) {
1558 err = -ENODEV;
1559 goto errout;
1560 }
1561
1562 u_map = nla_data(tb[IFLA_MAP]);
1563 k_map.mem_start = (unsigned long) u_map->mem_start;
1564 k_map.mem_end = (unsigned long) u_map->mem_end;
1565 k_map.base_addr = (unsigned short) u_map->base_addr;
1566 k_map.irq = (unsigned char) u_map->irq;
1567 k_map.dma = (unsigned char) u_map->dma;
1568 k_map.port = (unsigned char) u_map->port;
1569
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001570 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001571 if (err < 0)
1572 goto errout;
1573
Nicolas Dichtelba998902014-09-01 16:07:29 +02001574 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001575 }
1576
1577 if (tb[IFLA_ADDRESS]) {
1578 struct sockaddr *sa;
1579 int len;
1580
Patrick McHardy0157f602007-06-13 12:03:36 -07001581 len = sizeof(sa_family_t) + dev->addr_len;
1582 sa = kmalloc(len, GFP_KERNEL);
1583 if (!sa) {
1584 err = -ENOMEM;
1585 goto errout;
1586 }
1587 sa->sa_family = dev->type;
1588 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1589 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001590 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001591 kfree(sa);
1592 if (err)
1593 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001594 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001595 }
1596
1597 if (tb[IFLA_MTU]) {
1598 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1599 if (err < 0)
1600 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001601 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001602 }
1603
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001604 if (tb[IFLA_GROUP]) {
1605 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001606 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001607 }
1608
Patrick McHardy0157f602007-06-13 12:03:36 -07001609 /*
1610 * Interface selected by interface index but interface
1611 * name provided implies that a name change has been
1612 * requested.
1613 */
1614 if (ifm->ifi_index > 0 && ifname[0]) {
1615 err = dev_change_name(dev, ifname);
1616 if (err < 0)
1617 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001618 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001619 }
1620
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001621 if (tb[IFLA_IFALIAS]) {
1622 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1623 nla_len(tb[IFLA_IFALIAS]));
1624 if (err < 0)
1625 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001626 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001627 }
1628
Patrick McHardy0157f602007-06-13 12:03:36 -07001629 if (tb[IFLA_BROADCAST]) {
1630 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001631 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001632 }
1633
1634 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001635 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001636 if (err < 0)
1637 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001638 }
1639
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001640 if (tb[IFLA_MASTER]) {
1641 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1642 if (err)
1643 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001644 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001645 }
1646
Jiri Pirko9a572472012-12-27 23:49:39 +00001647 if (tb[IFLA_CARRIER]) {
1648 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1649 if (err)
1650 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001651 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001652 }
1653
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001654 if (tb[IFLA_TXQLEN]) {
1655 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1656
1657 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001658 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001659
1660 dev->tx_queue_len = value;
1661 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001662
Patrick McHardy0157f602007-06-13 12:03:36 -07001663 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001664 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001665
1666 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001667 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1668
David S. Miller93b2d4a2008-02-17 18:35:07 -08001669 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001670 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001671 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001672 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001673 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001674 }
1675
Chris Wrightc02db8c2010-05-16 01:05:45 -07001676 if (tb[IFLA_VFINFO_LIST]) {
1677 struct nlattr *attr;
1678 int rem;
1679 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001680 if (nla_type(attr) != IFLA_VF_INFO) {
1681 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001682 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001683 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001684 err = do_setvfinfo(dev, attr);
1685 if (err < 0)
1686 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001687 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001688 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001689 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001690 err = 0;
1691
Scott Feldman57b61082010-05-17 22:49:55 -07001692 if (tb[IFLA_VF_PORTS]) {
1693 struct nlattr *port[IFLA_PORT_MAX+1];
1694 struct nlattr *attr;
1695 int vf;
1696 int rem;
1697
1698 err = -EOPNOTSUPP;
1699 if (!ops->ndo_set_vf_port)
1700 goto errout;
1701
1702 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1703 if (nla_type(attr) != IFLA_VF_PORT)
1704 continue;
1705 err = nla_parse_nested(port, IFLA_PORT_MAX,
1706 attr, ifla_port_policy);
1707 if (err < 0)
1708 goto errout;
1709 if (!port[IFLA_PORT_VF]) {
1710 err = -EOPNOTSUPP;
1711 goto errout;
1712 }
1713 vf = nla_get_u32(port[IFLA_PORT_VF]);
1714 err = ops->ndo_set_vf_port(dev, vf, port);
1715 if (err < 0)
1716 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001717 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001718 }
1719 }
1720 err = 0;
1721
1722 if (tb[IFLA_PORT_SELF]) {
1723 struct nlattr *port[IFLA_PORT_MAX+1];
1724
1725 err = nla_parse_nested(port, IFLA_PORT_MAX,
1726 tb[IFLA_PORT_SELF], ifla_port_policy);
1727 if (err < 0)
1728 goto errout;
1729
1730 err = -EOPNOTSUPP;
1731 if (ops->ndo_set_vf_port)
1732 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1733 if (err < 0)
1734 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001735 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001736 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001737
1738 if (tb[IFLA_AF_SPEC]) {
1739 struct nlattr *af;
1740 int rem;
1741
1742 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1743 const struct rtnl_af_ops *af_ops;
1744
1745 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001746 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001747
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001748 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001749 if (err < 0)
1750 goto errout;
1751
Nicolas Dichtelba998902014-09-01 16:07:29 +02001752 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001753 }
1754 }
Scott Feldman57b61082010-05-17 22:49:55 -07001755 err = 0;
1756
Patrick McHardy0157f602007-06-13 12:03:36 -07001757errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001758 if (status & DO_SETLINK_MODIFIED) {
1759 if (status & DO_SETLINK_NOTIFY)
1760 netdev_state_change(dev);
1761
1762 if (err < 0)
1763 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",
1764 dev->name);
1765 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001766
Patrick McHardy0157f602007-06-13 12:03:36 -07001767 return err;
1768}
1769
Thomas Graf661d2962013-03-21 07:45:29 +00001770static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001772 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001773 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001775 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001776 struct nlattr *tb[IFLA_MAX+1];
1777 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Thomas Grafda5e0492006-08-10 21:17:37 -07001779 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1780 if (err < 0)
1781 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Thomas Graf5176f912006-08-26 20:13:18 -07001783 if (tb[IFLA_IFNAME])
1784 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001785 else
1786 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001789 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001790 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001791 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001792 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001793 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001794 else
1795 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Thomas Grafda5e0492006-08-10 21:17:37 -07001797 if (dev == NULL) {
1798 err = -ENODEV;
1799 goto errout;
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Eric Dumazete0d087a2009-11-07 01:26:17 -08001802 err = validate_linkmsg(dev, tb);
1803 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001804 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001805
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001806 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001807errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809}
1810
Thomas Graf661d2962013-03-21 07:45:29 +00001811static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001812{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001813 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001814 const struct rtnl_link_ops *ops;
1815 struct net_device *dev;
1816 struct ifinfomsg *ifm;
1817 char ifname[IFNAMSIZ];
1818 struct nlattr *tb[IFLA_MAX+1];
1819 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001820 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001821
1822 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1823 if (err < 0)
1824 return err;
1825
1826 if (tb[IFLA_IFNAME])
1827 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1828
1829 ifm = nlmsg_data(nlh);
1830 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001831 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001832 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001833 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001834 else
1835 return -EINVAL;
1836
1837 if (!dev)
1838 return -ENODEV;
1839
1840 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001841 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001842 return -EOPNOTSUPP;
1843
Eric Dumazet226bd342011-05-08 23:17:57 +00001844 ops->dellink(dev, &list_kill);
1845 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001846 return 0;
1847}
1848
Patrick McHardy3729d502010-02-26 06:34:54 +00001849int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1850{
1851 unsigned int old_flags;
1852 int err;
1853
1854 old_flags = dev->flags;
1855 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1856 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1857 if (err < 0)
1858 return err;
1859 }
1860
1861 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001862
Nicolas Dichtela528c212013-09-25 12:02:44 +02001863 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001864 return 0;
1865}
1866EXPORT_SYMBOL(rtnl_configure_link);
1867
Rami Rosenc0713562012-11-30 01:08:47 +00001868struct net_device *rtnl_create_link(struct net *net,
Tom Gundersen55177502014-07-14 16:37:25 +02001869 char *ifname, unsigned char name_assign_type,
1870 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001871{
1872 int err;
1873 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001874 unsigned int num_tx_queues = 1;
1875 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001876
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001877 if (tb[IFLA_NUM_TX_QUEUES])
1878 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1879 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001880 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001881
1882 if (tb[IFLA_NUM_RX_QUEUES])
1883 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1884 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001885 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001886
Pavel Emelianove7199282007-08-08 22:16:38 -07001887 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001888 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001889 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001890 if (!dev)
1891 goto err;
1892
Eric W. Biederman81adee42009-11-08 00:53:51 -08001893 dev_net_set(dev, net);
1894 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001895 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001896
Pavel Emelianove7199282007-08-08 22:16:38 -07001897 if (tb[IFLA_MTU])
1898 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001899 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001900 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1901 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001902 dev->addr_assign_type = NET_ADDR_SET;
1903 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001904 if (tb[IFLA_BROADCAST])
1905 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1906 nla_len(tb[IFLA_BROADCAST]));
1907 if (tb[IFLA_TXQLEN])
1908 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1909 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001910 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001911 if (tb[IFLA_LINKMODE])
1912 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001913 if (tb[IFLA_GROUP])
1914 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001915
1916 return dev;
1917
Pavel Emelianove7199282007-08-08 22:16:38 -07001918err:
1919 return ERR_PTR(err);
1920}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001921EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001922
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001923static int rtnl_group_changelink(const struct sk_buff *skb,
1924 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001925 struct ifinfomsg *ifm,
1926 struct nlattr **tb)
1927{
1928 struct net_device *dev;
1929 int err;
1930
1931 for_each_netdev(net, dev) {
1932 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001933 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001934 if (err < 0)
1935 return err;
1936 }
1937 }
1938
1939 return 0;
1940}
1941
Thomas Graf661d2962013-03-21 07:45:29 +00001942static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001943{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001944 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001945 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001946 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001947 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001948 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001949 struct ifinfomsg *ifm;
1950 char kind[MODULE_NAME_LEN];
1951 char ifname[IFNAMSIZ];
1952 struct nlattr *tb[IFLA_MAX+1];
1953 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02001954 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001955 int err;
1956
Johannes Berg95a5afc2008-10-16 15:24:51 -07001957#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001958replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001959#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001960 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1961 if (err < 0)
1962 return err;
1963
1964 if (tb[IFLA_IFNAME])
1965 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1966 else
1967 ifname[0] = '\0';
1968
1969 ifm = nlmsg_data(nlh);
1970 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001971 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001972 else {
1973 if (ifname[0])
1974 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001975 else
1976 dev = NULL;
1977 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001978
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001979 if (dev) {
1980 master_dev = netdev_master_upper_dev_get(dev);
1981 if (master_dev)
1982 m_ops = master_dev->rtnl_link_ops;
1983 }
1984
Eric Dumazete0d087a2009-11-07 01:26:17 -08001985 err = validate_linkmsg(dev, tb);
1986 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001987 return err;
1988
Patrick McHardy38f7b872007-06-13 12:03:51 -07001989 if (tb[IFLA_LINKINFO]) {
1990 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1991 tb[IFLA_LINKINFO], ifla_info_policy);
1992 if (err < 0)
1993 return err;
1994 } else
1995 memset(linkinfo, 0, sizeof(linkinfo));
1996
1997 if (linkinfo[IFLA_INFO_KIND]) {
1998 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1999 ops = rtnl_link_ops_get(kind);
2000 } else {
2001 kind[0] = '\0';
2002 ops = NULL;
2003 }
2004
2005 if (1) {
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002006 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
2007 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
2008 struct nlattr **data = NULL;
2009 struct nlattr **slave_data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002010 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002011
2012 if (ops) {
2013 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2014 err = nla_parse_nested(attr, ops->maxtype,
2015 linkinfo[IFLA_INFO_DATA],
2016 ops->policy);
2017 if (err < 0)
2018 return err;
2019 data = attr;
2020 }
2021 if (ops->validate) {
2022 err = ops->validate(tb, data);
2023 if (err < 0)
2024 return err;
2025 }
2026 }
2027
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002028 if (m_ops) {
2029 if (m_ops->slave_maxtype &&
2030 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2031 err = nla_parse_nested(slave_attr,
2032 m_ops->slave_maxtype,
2033 linkinfo[IFLA_INFO_SLAVE_DATA],
2034 m_ops->slave_policy);
2035 if (err < 0)
2036 return err;
2037 slave_data = slave_attr;
2038 }
2039 if (m_ops->slave_validate) {
2040 err = m_ops->slave_validate(tb, slave_data);
2041 if (err < 0)
2042 return err;
2043 }
2044 }
2045
Patrick McHardy38f7b872007-06-13 12:03:51 -07002046 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002047 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002048
2049 if (nlh->nlmsg_flags & NLM_F_EXCL)
2050 return -EEXIST;
2051 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2052 return -EOPNOTSUPP;
2053
2054 if (linkinfo[IFLA_INFO_DATA]) {
2055 if (!ops || ops != dev->rtnl_link_ops ||
2056 !ops->changelink)
2057 return -EOPNOTSUPP;
2058
2059 err = ops->changelink(dev, tb, data);
2060 if (err < 0)
2061 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002062 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002063 }
2064
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002065 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2066 if (!m_ops || !m_ops->slave_changelink)
2067 return -EOPNOTSUPP;
2068
2069 err = m_ops->slave_changelink(master_dev, dev,
2070 tb, slave_data);
2071 if (err < 0)
2072 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002073 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002074 }
2075
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002076 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002077 }
2078
Patrick McHardyffa934f2011-01-20 03:00:42 +00002079 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2080 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002081 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002082 nla_get_u32(tb[IFLA_GROUP]),
2083 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002084 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002085 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002086
Patrick McHardy0e068772007-07-11 19:42:31 -07002087 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002088 return -EOPNOTSUPP;
2089
2090 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002091#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002092 if (kind[0]) {
2093 __rtnl_unlock();
2094 request_module("rtnl-link-%s", kind);
2095 rtnl_lock();
2096 ops = rtnl_link_ops_get(kind);
2097 if (ops)
2098 goto replay;
2099 }
2100#endif
2101 return -EOPNOTSUPP;
2102 }
2103
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002104 if (!ops->setup)
2105 return -EOPNOTSUPP;
2106
Tom Gundersen55177502014-07-14 16:37:25 +02002107 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002108 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002109 name_assign_type = NET_NAME_ENUM;
2110 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002111
Eric W. Biederman81adee42009-11-08 00:53:51 -08002112 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002113 if (IS_ERR(dest_net))
2114 return PTR_ERR(dest_net);
2115
Tom Gundersen55177502014-07-14 16:37:25 +02002116 dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002117 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002118 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002119 goto out;
2120 }
2121
2122 dev->ifindex = ifm->ifi_index;
2123
Cong Wang0e0eee22014-02-11 15:51:30 -08002124 if (ops->newlink) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002125 err = ops->newlink(net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002126 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002127 * and unregister it on failure after registration
2128 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002129 */
Cong Wange51fb152014-06-03 16:40:47 -07002130 if (err < 0) {
2131 /* If device is not registered at all, free it now */
2132 if (dev->reg_state == NETREG_UNINITIALIZED)
2133 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002134 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002135 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002136 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002137 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002138 if (err < 0) {
2139 free_netdev(dev);
2140 goto out;
2141 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002142 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002143 err = rtnl_configure_link(dev, ifm);
2144 if (err < 0)
2145 unregister_netdevice(dev);
2146out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08002147 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002148 return err;
2149 }
2150}
2151
Thomas Graf661d2962013-03-21 07:45:29 +00002152static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002153{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002154 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002155 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002156 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002157 struct nlattr *tb[IFLA_MAX+1];
2158 struct net_device *dev = NULL;
2159 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002160 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002161 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002162
Thomas Grafb60c5112006-08-04 23:05:34 -07002163 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2164 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002165 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002166
Eric Dumazeta3d12892009-10-21 10:59:31 +00002167 if (tb[IFLA_IFNAME])
2168 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2169
Greg Rose115c9b82012-02-21 16:54:48 -05002170 if (tb[IFLA_EXT_MASK])
2171 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2172
Thomas Grafb60c5112006-08-04 23:05:34 -07002173 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002174 if (ifm->ifi_index > 0)
2175 dev = __dev_get_by_index(net, ifm->ifi_index);
2176 else if (tb[IFLA_IFNAME])
2177 dev = __dev_get_by_name(net, ifname);
2178 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002179 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002180
Eric Dumazeta3d12892009-10-21 10:59:31 +00002181 if (dev == NULL)
2182 return -ENODEV;
2183
Greg Rose115c9b82012-02-21 16:54:48 -05002184 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002185 if (nskb == NULL)
2186 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002187
Eric W. Biederman15e47302012-09-07 20:12:54 +00002188 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002189 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002190 if (err < 0) {
2191 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2192 WARN_ON(err == -EMSGSIZE);
2193 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002194 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002195 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002196
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002197 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002198}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002199
Greg Rose115c9b82012-02-21 16:54:48 -05002200static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002201{
Greg Rose115c9b82012-02-21 16:54:48 -05002202 struct net *net = sock_net(skb->sk);
2203 struct net_device *dev;
2204 struct nlattr *tb[IFLA_MAX+1];
2205 u32 ext_filter_mask = 0;
2206 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002207 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002208
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002209 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2210 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2211 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2212
2213 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002214 if (tb[IFLA_EXT_MASK])
2215 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2216 }
Greg Rose115c9b82012-02-21 16:54:48 -05002217
2218 if (!ext_filter_mask)
2219 return NLMSG_GOODSIZE;
2220 /*
2221 * traverse the list of net devices and compute the minimum
2222 * buffer size based upon the filter mask.
2223 */
2224 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2225 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2226 if_nlmsg_size(dev,
2227 ext_filter_mask));
2228 }
2229
Greg Rosec7ac8672011-06-10 01:27:09 +00002230 return min_ifinfo_dump_size;
2231}
2232
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002233static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 int idx;
2236 int s_idx = cb->family;
2237
2238 if (s_idx == 0)
2239 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002240 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int type = cb->nlh->nlmsg_type-RTM_BASE;
2242 if (idx < s_idx || idx == PF_PACKET)
2243 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002244 if (rtnl_msg_handlers[idx] == NULL ||
2245 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002247 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002249 cb->prev_seq = 0;
2250 cb->seq = 0;
2251 }
Thomas Grafe2849862007-03-22 11:48:11 -07002252 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 break;
2254 }
2255 cb->family = idx;
2256
2257 return skb->len;
2258}
2259
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002260struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2261 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002263 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002265 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002266 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002268 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002269 if (skb == NULL)
2270 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Greg Rose115c9b82012-02-21 16:54:48 -05002272 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002273 if (err < 0) {
2274 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2275 WARN_ON(err == -EMSGSIZE);
2276 kfree_skb(skb);
2277 goto errout;
2278 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002279 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002280errout:
2281 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002282 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002283 return NULL;
2284}
2285
2286void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2287{
2288 struct net *net = dev_net(dev);
2289
2290 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2291}
2292
2293void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2294 gfp_t flags)
2295{
2296 struct sk_buff *skb;
2297
2298 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2299 if (skb)
2300 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002302EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
John Fastabendd83b0602012-04-15 06:44:08 +00002304static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2305 struct net_device *dev,
2306 u8 *addr, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002307 int type, unsigned int flags,
2308 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002309{
2310 struct nlmsghdr *nlh;
2311 struct ndmsg *ndm;
2312
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002313 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002314 if (!nlh)
2315 return -EMSGSIZE;
2316
2317 ndm = nlmsg_data(nlh);
2318 ndm->ndm_family = AF_BRIDGE;
2319 ndm->ndm_pad1 = 0;
2320 ndm->ndm_pad2 = 0;
2321 ndm->ndm_flags = flags;
2322 ndm->ndm_type = 0;
2323 ndm->ndm_ifindex = dev->ifindex;
2324 ndm->ndm_state = NUD_PERMANENT;
2325
2326 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2327 goto nla_put_failure;
2328
2329 return nlmsg_end(skb, nlh);
2330
2331nla_put_failure:
2332 nlmsg_cancel(skb, nlh);
2333 return -EMSGSIZE;
2334}
2335
John Fastabend3ff661c2012-04-15 06:44:14 +00002336static inline size_t rtnl_fdb_nlmsg_size(void)
2337{
2338 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2339}
2340
2341static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2342{
2343 struct net *net = dev_net(dev);
2344 struct sk_buff *skb;
2345 int err = -ENOBUFS;
2346
2347 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2348 if (!skb)
2349 goto errout;
2350
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002351 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002352 if (err < 0) {
2353 kfree_skb(skb);
2354 goto errout;
2355 }
2356
2357 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2358 return;
2359errout:
2360 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2361}
2362
Vlad Yasevich090096b2013-03-06 15:39:42 +00002363/**
2364 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2365 */
2366int ndo_dflt_fdb_add(struct ndmsg *ndm,
2367 struct nlattr *tb[],
2368 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002369 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002370 u16 flags)
2371{
2372 int err = -EINVAL;
2373
2374 /* If aging addresses are supported device will need to
2375 * implement its own handler for this.
2376 */
2377 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2378 pr_info("%s: FDB only supports static addresses\n", dev->name);
2379 return err;
2380 }
2381
Or Gerlitz65891fe2014-12-14 18:19:05 +02002382 if (vid) {
2383 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2384 return err;
2385 }
2386
Vlad Yasevich090096b2013-03-06 15:39:42 +00002387 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2388 err = dev_uc_add_excl(dev, addr);
2389 else if (is_multicast_ether_addr(addr))
2390 err = dev_mc_add_excl(dev, addr);
2391
2392 /* Only return duplicate errors if NLM_F_EXCL is set */
2393 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2394 err = 0;
2395
2396 return err;
2397}
2398EXPORT_SYMBOL(ndo_dflt_fdb_add);
2399
Jiri Pirkof6f64242014-11-28 14:34:15 +01002400static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2401{
2402 u16 vid = 0;
2403
2404 if (vlan_attr) {
2405 if (nla_len(vlan_attr) != sizeof(u16)) {
2406 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2407 return -EINVAL;
2408 }
2409
2410 vid = nla_get_u16(vlan_attr);
2411
2412 if (!vid || vid >= VLAN_VID_MASK) {
2413 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2414 vid);
2415 return -EINVAL;
2416 }
2417 }
2418 *p_vid = vid;
2419 return 0;
2420}
2421
Thomas Graf661d2962013-03-21 07:45:29 +00002422static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002423{
2424 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002425 struct ndmsg *ndm;
2426 struct nlattr *tb[NDA_MAX+1];
2427 struct net_device *dev;
2428 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002429 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002430 int err;
2431
2432 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2433 if (err < 0)
2434 return err;
2435
2436 ndm = nlmsg_data(nlh);
2437 if (ndm->ndm_ifindex == 0) {
2438 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2439 return -EINVAL;
2440 }
2441
2442 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2443 if (dev == NULL) {
2444 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2445 return -ENODEV;
2446 }
2447
2448 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2449 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2450 return -EINVAL;
2451 }
2452
2453 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002454
Jiri Pirkof6f64242014-11-28 14:34:15 +01002455 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2456 if (err)
2457 return err;
2458
John Fastabend77162022012-04-15 06:43:56 +00002459 err = -EOPNOTSUPP;
2460
2461 /* Support fdb on master device the net/bridge default case */
2462 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2463 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002464 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2465 const struct net_device_ops *ops = br_dev->netdev_ops;
2466
Jiri Pirkof6f64242014-11-28 14:34:15 +01002467 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2468 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002469 if (err)
2470 goto out;
2471 else
2472 ndm->ndm_flags &= ~NTF_MASTER;
2473 }
2474
2475 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002476 if ((ndm->ndm_flags & NTF_SELF)) {
2477 if (dev->netdev_ops->ndo_fdb_add)
2478 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002479 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002480 nlh->nlmsg_flags);
2481 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002482 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002483 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002484
John Fastabend3ff661c2012-04-15 06:44:14 +00002485 if (!err) {
2486 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002487 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002488 }
John Fastabend77162022012-04-15 06:43:56 +00002489 }
2490out:
2491 return err;
2492}
2493
Vlad Yasevich090096b2013-03-06 15:39:42 +00002494/**
2495 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2496 */
2497int ndo_dflt_fdb_del(struct ndmsg *ndm,
2498 struct nlattr *tb[],
2499 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002500 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002501{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002502 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002503
2504 /* If aging addresses are supported device will need to
2505 * implement its own handler for this.
2506 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002507 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002508 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002509 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002510 }
2511
2512 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2513 err = dev_uc_del(dev, addr);
2514 else if (is_multicast_ether_addr(addr))
2515 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002516
2517 return err;
2518}
2519EXPORT_SYMBOL(ndo_dflt_fdb_del);
2520
Thomas Graf661d2962013-03-21 07:45:29 +00002521static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002522{
2523 struct net *net = sock_net(skb->sk);
2524 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002525 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002526 struct net_device *dev;
2527 int err = -EINVAL;
2528 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002529 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002530
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002531 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002532 return -EPERM;
2533
2534 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2535 if (err < 0)
2536 return err;
John Fastabend77162022012-04-15 06:43:56 +00002537
2538 ndm = nlmsg_data(nlh);
2539 if (ndm->ndm_ifindex == 0) {
2540 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2541 return -EINVAL;
2542 }
2543
2544 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2545 if (dev == NULL) {
2546 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2547 return -ENODEV;
2548 }
2549
Vlad Yasevich1690be62013-02-13 12:00:18 +00002550 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2551 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002552 return -EINVAL;
2553 }
2554
Vlad Yasevich1690be62013-02-13 12:00:18 +00002555 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002556
Jiri Pirkof6f64242014-11-28 14:34:15 +01002557 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2558 if (err)
2559 return err;
2560
John Fastabend77162022012-04-15 06:43:56 +00002561 err = -EOPNOTSUPP;
2562
2563 /* Support fdb on master device the net/bridge default case */
2564 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2565 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002566 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2567 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002568
Jiri Pirko898e5062013-01-03 22:48:52 +00002569 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002570 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002571
2572 if (err)
2573 goto out;
2574 else
2575 ndm->ndm_flags &= ~NTF_MASTER;
2576 }
2577
2578 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002579 if (ndm->ndm_flags & NTF_SELF) {
2580 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002581 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2582 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002583 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002584 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002585
John Fastabend3ff661c2012-04-15 06:44:14 +00002586 if (!err) {
2587 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002588 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002589 }
John Fastabend77162022012-04-15 06:43:56 +00002590 }
2591out:
2592 return err;
2593}
2594
John Fastabendd83b0602012-04-15 06:44:08 +00002595static int nlmsg_populate_fdb(struct sk_buff *skb,
2596 struct netlink_callback *cb,
2597 struct net_device *dev,
2598 int *idx,
2599 struct netdev_hw_addr_list *list)
2600{
2601 struct netdev_hw_addr *ha;
2602 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002603 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002604
Eric W. Biederman15e47302012-09-07 20:12:54 +00002605 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002606 seq = cb->nlh->nlmsg_seq;
2607
2608 list_for_each_entry(ha, &list->list, list) {
2609 if (*idx < cb->args[0])
2610 goto skip;
2611
2612 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
John Fastabenda7a558f2012-11-01 16:23:10 +00002613 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002614 RTM_NEWNEIGH, NTF_SELF,
2615 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002616 if (err < 0)
2617 return err;
2618skip:
2619 *idx += 1;
2620 }
2621 return 0;
2622}
2623
2624/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002625 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002626 * @nlh: netlink message header
2627 * @dev: netdevice
2628 *
2629 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002630 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002631 */
2632int ndo_dflt_fdb_dump(struct sk_buff *skb,
2633 struct netlink_callback *cb,
2634 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002635 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002636 int idx)
2637{
2638 int err;
2639
2640 netif_addr_lock_bh(dev);
2641 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2642 if (err)
2643 goto out;
2644 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2645out:
2646 netif_addr_unlock_bh(dev);
2647 return idx;
2648}
2649EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2650
John Fastabend77162022012-04-15 06:43:56 +00002651static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2652{
John Fastabend77162022012-04-15 06:43:56 +00002653 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002654 struct nlattr *tb[IFLA_MAX+1];
2655 struct net_device *bdev = NULL;
2656 struct net_device *br_dev = NULL;
2657 const struct net_device_ops *ops = NULL;
2658 const struct net_device_ops *cops = NULL;
2659 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2660 struct net *net = sock_net(skb->sk);
2661 int brport_idx = 0;
2662 int br_idx = 0;
2663 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002664
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002665 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2666 ifla_policy) == 0) {
2667 if (tb[IFLA_MASTER])
2668 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2669 }
John Fastabend77162022012-04-15 06:43:56 +00002670
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002671 brport_idx = ifm->ifi_index;
2672
2673 if (br_idx) {
2674 br_dev = __dev_get_by_index(net, br_idx);
2675 if (!br_dev)
2676 return -ENODEV;
2677
2678 ops = br_dev->netdev_ops;
2679 bdev = br_dev;
2680 }
2681
2682 for_each_netdev(net, dev) {
2683 if (brport_idx && (dev->ifindex != brport_idx))
2684 continue;
2685
2686 if (!br_idx) { /* user did not specify a specific bridge */
2687 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2688 br_dev = netdev_master_upper_dev_get(dev);
2689 cops = br_dev->netdev_ops;
2690 }
2691
2692 bdev = dev;
2693 } else {
2694 if (dev != br_dev &&
2695 !(dev->priv_flags & IFF_BRIDGE_PORT))
2696 continue;
2697
2698 if (br_dev != netdev_master_upper_dev_get(dev) &&
2699 !(dev->priv_flags & IFF_EBRIDGE))
2700 continue;
2701
2702 bdev = br_dev;
2703 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002704 }
2705
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002706 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2707 if (cops && cops->ndo_fdb_dump)
2708 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2709 idx);
2710 }
2711
John Fastabend77162022012-04-15 06:43:56 +00002712 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002713 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002714 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002715 else
2716 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002717
2718 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002719 }
John Fastabend77162022012-04-15 06:43:56 +00002720
2721 cb->args[0] = idx;
2722 return skb->len;
2723}
2724
Scott Feldman2c3c0312014-11-28 14:34:25 +01002725static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2726 unsigned int attrnum, unsigned int flag)
2727{
2728 if (mask & flag)
2729 return nla_put_u8(skb, attrnum, !!(flags & flag));
2730 return 0;
2731}
2732
John Fastabend815cccb2012-10-24 08:13:09 +00002733int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002734 struct net_device *dev, u16 mode,
2735 u32 flags, u32 mask)
John Fastabend815cccb2012-10-24 08:13:09 +00002736{
2737 struct nlmsghdr *nlh;
2738 struct ifinfomsg *ifm;
2739 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002740 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002741 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002742 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002743
2744 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2745 if (nlh == NULL)
2746 return -EMSGSIZE;
2747
2748 ifm = nlmsg_data(nlh);
2749 ifm->ifi_family = AF_BRIDGE;
2750 ifm->__ifi_pad = 0;
2751 ifm->ifi_type = dev->type;
2752 ifm->ifi_index = dev->ifindex;
2753 ifm->ifi_flags = dev_get_flags(dev);
2754 ifm->ifi_change = 0;
2755
2756
2757 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2758 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2759 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002760 (br_dev &&
2761 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002762 (dev->addr_len &&
2763 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2764 (dev->ifindex != dev->iflink &&
2765 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2766 goto nla_put_failure;
2767
2768 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2769 if (!br_afspec)
2770 goto nla_put_failure;
2771
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002772 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002773 nla_nest_cancel(skb, br_afspec);
2774 goto nla_put_failure;
2775 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002776
2777 if (mode != BRIDGE_MODE_UNDEF) {
2778 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2779 nla_nest_cancel(skb, br_afspec);
2780 goto nla_put_failure;
2781 }
2782 }
John Fastabend815cccb2012-10-24 08:13:09 +00002783 nla_nest_end(skb, br_afspec);
2784
Scott Feldman2c3c0312014-11-28 14:34:25 +01002785 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2786 if (!protinfo)
2787 goto nla_put_failure;
2788
2789 if (brport_nla_put_flag(skb, flags, mask,
2790 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2791 brport_nla_put_flag(skb, flags, mask,
2792 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2793 brport_nla_put_flag(skb, flags, mask,
2794 IFLA_BRPORT_FAST_LEAVE,
2795 BR_MULTICAST_FAST_LEAVE) ||
2796 brport_nla_put_flag(skb, flags, mask,
2797 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2798 brport_nla_put_flag(skb, flags, mask,
2799 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2800 brport_nla_put_flag(skb, flags, mask,
2801 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2802 brport_nla_put_flag(skb, flags, mask,
2803 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2804 brport_nla_put_flag(skb, flags, mask,
2805 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2806 nla_nest_cancel(skb, protinfo);
2807 goto nla_put_failure;
2808 }
2809
2810 nla_nest_end(skb, protinfo);
2811
John Fastabend815cccb2012-10-24 08:13:09 +00002812 return nlmsg_end(skb, nlh);
2813nla_put_failure:
2814 nlmsg_cancel(skb, nlh);
2815 return -EMSGSIZE;
2816}
2817EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2818
John Fastabende5a55a82012-10-24 08:12:57 +00002819static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2820{
2821 struct net *net = sock_net(skb->sk);
2822 struct net_device *dev;
2823 int idx = 0;
2824 u32 portid = NETLINK_CB(cb->skb).portid;
2825 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002826 u32 filter_mask = 0;
2827
Thomas Grafaa68c202014-11-26 13:42:20 +01002828 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2829 struct nlattr *extfilt;
2830
2831 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2832 IFLA_EXT_MASK);
2833 if (extfilt) {
2834 if (nla_len(extfilt) < sizeof(filter_mask))
2835 return -EINVAL;
2836
2837 filter_mask = nla_get_u32(extfilt);
2838 }
2839 }
John Fastabende5a55a82012-10-24 08:12:57 +00002840
2841 rcu_read_lock();
2842 for_each_netdev_rcu(net, dev) {
2843 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002844 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002845
Jiri Pirko898e5062013-01-03 22:48:52 +00002846 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002847 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002848 br_dev->netdev_ops->ndo_bridge_getlink(
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002849 skb, portid, seq, dev, filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002850 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002851 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002852 }
2853
2854 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002855 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002856 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2857 filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002858 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002859 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002860 }
2861 }
2862 rcu_read_unlock();
2863 cb->args[0] = idx;
2864
2865 return skb->len;
2866}
2867
John Fastabend2469ffd2012-10-24 08:13:03 +00002868static inline size_t bridge_nlmsg_size(void)
2869{
2870 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2871 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2872 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2873 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2874 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2875 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2876 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2877 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2878 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2879 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2880 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2881}
2882
Roopa Prabhu02dba432015-01-14 20:02:25 -08002883static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00002884{
2885 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00002886 struct sk_buff *skb;
2887 int err = -EOPNOTSUPP;
2888
Roopa Prabhu02dba432015-01-14 20:02:25 -08002889 if (!dev->netdev_ops->ndo_bridge_getlink)
2890 return 0;
2891
John Fastabend2469ffd2012-10-24 08:13:03 +00002892 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2893 if (!skb) {
2894 err = -ENOMEM;
2895 goto errout;
2896 }
2897
Roopa Prabhu02dba432015-01-14 20:02:25 -08002898 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2899 if (err < 0)
2900 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00002901
2902 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2903 return 0;
2904errout:
2905 WARN_ON(err == -EMSGSIZE);
2906 kfree_skb(skb);
2907 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2908 return err;
2909}
2910
Thomas Graf661d2962013-03-21 07:45:29 +00002911static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00002912{
2913 struct net *net = sock_net(skb->sk);
2914 struct ifinfomsg *ifm;
2915 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00002916 struct nlattr *br_spec, *attr = NULL;
2917 int rem, err = -EOPNOTSUPP;
John Fastabendc38e01b2012-11-02 16:32:36 +00002918 u16 oflags, flags = 0;
2919 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00002920
2921 if (nlmsg_len(nlh) < sizeof(*ifm))
2922 return -EINVAL;
2923
2924 ifm = nlmsg_data(nlh);
2925 if (ifm->ifi_family != AF_BRIDGE)
2926 return -EPFNOSUPPORT;
2927
2928 dev = __dev_get_by_index(net, ifm->ifi_index);
2929 if (!dev) {
2930 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2931 return -ENODEV;
2932 }
2933
John Fastabend2469ffd2012-10-24 08:13:03 +00002934 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2935 if (br_spec) {
2936 nla_for_each_nested(attr, br_spec, rem) {
2937 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002938 if (nla_len(attr) < sizeof(flags))
2939 return -EINVAL;
2940
John Fastabendc38e01b2012-11-02 16:32:36 +00002941 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00002942 flags = nla_get_u16(attr);
2943 break;
2944 }
2945 }
2946 }
2947
John Fastabendc38e01b2012-11-02 16:32:36 +00002948 oflags = flags;
2949
John Fastabend2469ffd2012-10-24 08:13:03 +00002950 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002951 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2952
2953 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002954 err = -EOPNOTSUPP;
2955 goto out;
2956 }
2957
Jiri Pirko898e5062013-01-03 22:48:52 +00002958 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002959 if (err)
2960 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00002961
2962 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00002963 }
2964
John Fastabend2469ffd2012-10-24 08:13:03 +00002965 if ((flags & BRIDGE_FLAGS_SELF)) {
2966 if (!dev->netdev_ops->ndo_bridge_setlink)
2967 err = -EOPNOTSUPP;
2968 else
2969 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
Roopa Prabhu02dba432015-01-14 20:02:25 -08002970 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002971 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08002972
2973 /* Generate event to notify upper layer of bridge
2974 * change
2975 */
2976 err = rtnl_bridge_notify(dev);
2977 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002978 }
2979
John Fastabendc38e01b2012-11-02 16:32:36 +00002980 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00002981 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00002982out:
2983 return err;
2984}
2985
Thomas Graf661d2962013-03-21 07:45:29 +00002986static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00002987{
2988 struct net *net = sock_net(skb->sk);
2989 struct ifinfomsg *ifm;
2990 struct net_device *dev;
2991 struct nlattr *br_spec, *attr = NULL;
2992 int rem, err = -EOPNOTSUPP;
2993 u16 oflags, flags = 0;
2994 bool have_flags = false;
2995
2996 if (nlmsg_len(nlh) < sizeof(*ifm))
2997 return -EINVAL;
2998
2999 ifm = nlmsg_data(nlh);
3000 if (ifm->ifi_family != AF_BRIDGE)
3001 return -EPFNOSUPPORT;
3002
3003 dev = __dev_get_by_index(net, ifm->ifi_index);
3004 if (!dev) {
3005 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3006 return -ENODEV;
3007 }
3008
3009 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3010 if (br_spec) {
3011 nla_for_each_nested(attr, br_spec, rem) {
3012 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003013 if (nla_len(attr) < sizeof(flags))
3014 return -EINVAL;
3015
Vlad Yasevich407af322013-02-13 12:00:12 +00003016 have_flags = true;
3017 flags = nla_get_u16(attr);
3018 break;
3019 }
3020 }
3021 }
3022
3023 oflags = flags;
3024
3025 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3026 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3027
3028 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3029 err = -EOPNOTSUPP;
3030 goto out;
3031 }
3032
3033 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3034 if (err)
3035 goto out;
3036
3037 flags &= ~BRIDGE_FLAGS_MASTER;
3038 }
3039
3040 if ((flags & BRIDGE_FLAGS_SELF)) {
3041 if (!dev->netdev_ops->ndo_bridge_dellink)
3042 err = -EOPNOTSUPP;
3043 else
3044 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3045
Roopa Prabhu02dba432015-01-14 20:02:25 -08003046 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003047 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003048
3049 /* Generate event to notify upper layer of bridge
3050 * change
3051 */
3052 err = rtnl_bridge_notify(dev);
3053 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003054 }
3055
3056 if (have_flags)
3057 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003058out:
3059 return err;
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062/* Process one rtnetlink message. */
3063
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003064static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003066 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003067 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 int family;
3070 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003075 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 type -= RTM_BASE;
3078
3079 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003080 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082
Hong zhi guo573ce262013-03-27 06:47:04 +00003083 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 sz_idx = type>>2;
3085 kind = type&3;
3086
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003087 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003088 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
David S. Millerb8f3ab42011-01-18 12:40:38 -08003090 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003091 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003092 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003093 rtnl_calcit_func calcit;
3094 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Thomas Grafe2849862007-03-22 11:48:11 -07003096 dumpit = rtnl_get_dumpit(family, type);
3097 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003098 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003099 calcit = rtnl_get_calcit(family, type);
3100 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003101 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Eric Dumazet2907c352011-05-25 07:34:04 +00003103 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003104 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003105 {
3106 struct netlink_dump_control c = {
3107 .dump = dumpit,
3108 .min_dump_alloc = min_dump_alloc,
3109 };
3110 err = netlink_dump_start(rtnl, skb, nlh, &c);
3111 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003112 rtnl_lock();
3113 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115
Thomas Grafe2849862007-03-22 11:48:11 -07003116 doit = rtnl_get_doit(family, type);
3117 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003118 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Thomas Graf661d2962013-03-21 07:45:29 +00003120 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003123static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003125 rtnl_lock();
3126 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3127 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128}
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3131{
Jiri Pirko351638e2013-05-28 01:30:21 +00003132 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 case NETDEV_UP:
3136 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003137 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003138 case NETDEV_POST_INIT:
3139 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003141 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003143 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003144 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003145 case NETDEV_RELEASE:
3146 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 break;
3148 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003149 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 break;
3151 }
3152 return NOTIFY_DONE;
3153}
3154
3155static struct notifier_block rtnetlink_dev_notifier = {
3156 .notifier_call = rtnetlink_event,
3157};
3158
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003159
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003160static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003161{
3162 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003163 struct netlink_kernel_cfg cfg = {
3164 .groups = RTNLGRP_MAX,
3165 .input = rtnetlink_rcv,
3166 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003167 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003168 };
3169
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003170 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003171 if (!sk)
3172 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003173 net->rtnl = sk;
3174 return 0;
3175}
3176
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003177static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003178{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003179 netlink_kernel_release(net->rtnl);
3180 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003181}
3182
3183static struct pernet_operations rtnetlink_net_ops = {
3184 .init = rtnetlink_net_init,
3185 .exit = rtnetlink_net_exit,
3186};
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188void __init rtnetlink_init(void)
3189{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003190 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003194
Greg Rosec7ac8672011-06-10 01:27:09 +00003195 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3196 rtnl_dump_ifinfo, rtnl_calcit);
3197 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3198 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3199 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003200
Greg Rosec7ac8672011-06-10 01:27:09 +00003201 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3202 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003203
3204 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3205 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3206 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003207
3208 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003209 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003210 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212