blob: ab293a3066b34bc4f6af71701f0c12b9ab6e5a34 [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 */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100878 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500879 + nla_total_size(ext_filter_mask
880 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
881 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000882 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000883 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200884 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100885 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
886 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800887}
888
Scott Feldman57b61082010-05-17 22:49:55 -0700889static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
890{
891 struct nlattr *vf_ports;
892 struct nlattr *vf_port;
893 int vf;
894 int err;
895
896 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
897 if (!vf_ports)
898 return -EMSGSIZE;
899
900 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
901 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700902 if (!vf_port)
903 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400904 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
905 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700906 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700907 if (err == -EMSGSIZE)
908 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700909 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700910 nla_nest_cancel(skb, vf_port);
911 continue;
912 }
913 nla_nest_end(skb, vf_port);
914 }
915
916 nla_nest_end(skb, vf_ports);
917
918 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700919
920nla_put_failure:
921 nla_nest_cancel(skb, vf_ports);
922 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700923}
924
925static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
926{
927 struct nlattr *port_self;
928 int err;
929
930 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
931 if (!port_self)
932 return -EMSGSIZE;
933
934 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
935 if (err) {
936 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700937 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700938 }
939
940 nla_nest_end(skb, port_self);
941
942 return 0;
943}
944
David Gibsonc53864f2014-04-24 10:22:36 +1000945static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
946 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700947{
948 int err;
949
David Gibsonc53864f2014-04-24 10:22:36 +1000950 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
951 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700952 return 0;
953
954 err = rtnl_port_self_fill(skb, dev);
955 if (err)
956 return err;
957
958 if (dev_num_vf(dev->dev.parent)) {
959 err = rtnl_vf_ports_fill(skb, dev);
960 if (err)
961 return err;
962 }
963
964 return 0;
965}
966
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200967static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
968{
969 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100970 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200971
972 err = dev_get_phys_port_id(dev, &ppid);
973 if (err) {
974 if (err == -EOPNOTSUPP)
975 return 0;
976 return err;
977 }
978
979 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
980 return -EMSGSIZE;
981
982 return 0;
983}
984
Jiri Pirko82f28412014-11-28 14:34:18 +0100985static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
986{
987 int err;
988 struct netdev_phys_item_id psid;
989
990 err = netdev_switch_parent_id_get(dev, &psid);
991 if (err) {
992 if (err == -EOPNOTSUPP)
993 return 0;
994 return err;
995 }
996
997 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
998 return -EMSGSIZE;
999
1000 return 0;
1001}
1002
Thomas Grafb60c5112006-08-04 23:05:34 -07001003static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001004 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001005 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001006{
1007 struct ifinfomsg *ifm;
1008 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001009 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001010 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001011 struct nlattr *attr, *af_spec;
1012 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001013 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001014
Eric Dumazet2907c352011-05-25 07:34:04 +00001015 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001016 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1017 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001018 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001019
1020 ifm = nlmsg_data(nlh);
1021 ifm->ifi_family = AF_UNSPEC;
1022 ifm->__ifi_pad = 0;
1023 ifm->ifi_type = dev->type;
1024 ifm->ifi_index = dev->ifindex;
1025 ifm->ifi_flags = dev_get_flags(dev);
1026 ifm->ifi_change = change;
1027
David S. Millera6574342012-04-01 20:12:00 -04001028 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1029 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1030 nla_put_u8(skb, IFLA_OPERSTATE,
1031 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1032 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1033 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1034 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001035 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001036 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001037#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001038 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001039#endif
David S. Millera6574342012-04-01 20:12:00 -04001040 (dev->ifindex != dev->iflink &&
1041 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001042 (upper_dev &&
1043 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001044 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001045 (dev->qdisc &&
1046 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1047 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001048 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1049 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1050 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001051 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001052
Stefan Rompfb00055a2006-03-20 17:09:11 -08001053 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct rtnl_link_ifmap map = {
1055 .mem_start = dev->mem_start,
1056 .mem_end = dev->mem_end,
1057 .base_addr = dev->base_addr,
1058 .irq = dev->irq,
1059 .dma = dev->dma,
1060 .port = dev->if_port,
1061 };
David S. Millera6574342012-04-01 20:12:00 -04001062 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1063 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001067 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1068 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1069 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001072 if (rtnl_phys_port_id_fill(skb, dev))
1073 goto nla_put_failure;
1074
Jiri Pirko82f28412014-11-28 14:34:18 +01001075 if (rtnl_phys_switch_id_fill(skb, dev))
1076 goto nla_put_failure;
1077
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001078 attr = nla_reserve(skb, IFLA_STATS,
1079 sizeof(struct rtnl_link_stats));
1080 if (attr == NULL)
1081 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Eric Dumazet28172732010-07-07 14:58:56 -07001083 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001084 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Jan Engelhardt10708f32010-03-11 09:57:29 +00001086 attr = nla_reserve(skb, IFLA_STATS64,
1087 sizeof(struct rtnl_link_stats64));
1088 if (attr == NULL)
1089 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001090 copy_rtnl_link_stats64(nla_data(attr), stats);
1091
David S. Millera6574342012-04-01 20:12:00 -04001092 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1093 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1094 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001095
Greg Rose115c9b82012-02-21 16:54:48 -05001096 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1097 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001098 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001099
Chris Wrightc02db8c2010-05-16 01:05:45 -07001100 struct nlattr *vfinfo, *vf;
1101 int num_vfs = dev_num_vf(dev->dev.parent);
1102
Chris Wrightc02db8c2010-05-16 01:05:45 -07001103 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1104 if (!vfinfo)
1105 goto nla_put_failure;
1106 for (i = 0; i < num_vfs; i++) {
1107 struct ifla_vf_info ivi;
1108 struct ifla_vf_mac vf_mac;
1109 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001110 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001111 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001112 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001113 struct ifla_vf_link_state vf_linkstate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001114
1115 /*
1116 * Not all SR-IOV capable drivers support the
1117 * spoofcheck query. Preset to -1 so the user
1118 * space tool can detect that the driver didn't
1119 * report anything.
1120 */
1121 ivi.spoofchk = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001122 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001123 /* The default value for VF link state is "auto"
1124 * IFLA_VF_LINK_STATE_AUTO which equals zero
1125 */
1126 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001127 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1128 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001129 vf_mac.vf =
1130 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001131 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001132 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001133 vf_spoofchk.vf =
1134 vf_linkstate.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001135
Chris Wrightc02db8c2010-05-16 01:05:45 -07001136 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1137 vf_vlan.vlan = ivi.vlan;
1138 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001139 vf_tx_rate.rate = ivi.max_tx_rate;
1140 vf_rate.min_tx_rate = ivi.min_tx_rate;
1141 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001142 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001143 vf_linkstate.link_state = ivi.linkstate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001144 vf = nla_nest_start(skb, IFLA_VF_INFO);
1145 if (!vf) {
1146 nla_nest_cancel(skb, vfinfo);
1147 goto nla_put_failure;
1148 }
David S. Millera6574342012-04-01 20:12:00 -04001149 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1150 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001151 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1152 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001153 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1154 &vf_tx_rate) ||
1155 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001156 &vf_spoofchk) ||
1157 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1158 &vf_linkstate))
David S. Millera6574342012-04-01 20:12:00 -04001159 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001160 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001161 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001162 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001163 }
Scott Feldman57b61082010-05-17 22:49:55 -07001164
David Gibsonc53864f2014-04-24 10:22:36 +10001165 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001166 goto nla_put_failure;
1167
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001168 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001169 if (rtnl_link_fill(skb, dev) < 0)
1170 goto nla_put_failure;
1171 }
1172
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001173 if (dev->rtnl_link_ops &&
1174 dev->rtnl_link_ops->get_link_net) {
1175 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1176
1177 if (!net_eq(dev_net(dev), link_net)) {
1178 int id = peernet2id(dev_net(dev), link_net);
1179
1180 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1181 goto nla_put_failure;
1182 }
1183 }
1184
Thomas Graff8ff1822010-11-16 04:30:14 +00001185 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1186 goto nla_put_failure;
1187
1188 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1189 if (af_ops->fill_link_af) {
1190 struct nlattr *af;
1191 int err;
1192
1193 if (!(af = nla_nest_start(skb, af_ops->family)))
1194 goto nla_put_failure;
1195
1196 err = af_ops->fill_link_af(skb, dev);
1197
1198 /*
1199 * Caller may return ENODATA to indicate that there
1200 * was no data to be dumped. This is not an error, it
1201 * means we should trim the attribute header and
1202 * continue.
1203 */
1204 if (err == -ENODATA)
1205 nla_nest_cancel(skb, af);
1206 else if (err < 0)
1207 goto nla_put_failure;
1208
1209 nla_nest_end(skb, af);
1210 }
1211 }
1212
1213 nla_nest_end(skb, af_spec);
1214
Johannes Berg053c0952015-01-16 22:09:00 +01001215 nlmsg_end(skb, nlh);
1216 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001217
1218nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001219 nlmsg_cancel(skb, nlh);
1220 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Jiri Pirkof7b12602014-02-18 20:53:18 +01001223static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1224 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1225 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1226 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1227 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1228 [IFLA_MTU] = { .type = NLA_U32 },
1229 [IFLA_LINK] = { .type = NLA_U32 },
1230 [IFLA_MASTER] = { .type = NLA_U32 },
1231 [IFLA_CARRIER] = { .type = NLA_U8 },
1232 [IFLA_TXQLEN] = { .type = NLA_U32 },
1233 [IFLA_WEIGHT] = { .type = NLA_U32 },
1234 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1235 [IFLA_LINKMODE] = { .type = NLA_U8 },
1236 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1237 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1238 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1239 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1240 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1241 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1242 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1243 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1244 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1245 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1246 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1247 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001248 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001249 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001250 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001251 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001252};
1253
1254static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1255 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1256 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1257 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1258 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1259};
1260
1261static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1262 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1263};
1264
1265static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001266 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1267 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1268 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1269 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1270 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1271 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001272};
1273
1274static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1275 [IFLA_PORT_VF] = { .type = NLA_U32 },
1276 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1277 .len = PORT_PROFILE_MAX },
1278 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1279 .len = sizeof(struct ifla_port_vsi)},
1280 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1281 .len = PORT_UUID_MAX },
1282 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1283 .len = PORT_UUID_MAX },
1284 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1285 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1286};
1287
Thomas Grafb60c5112006-08-04 23:05:34 -07001288static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001290 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001291 int h, s_h;
1292 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001294 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001295 struct nlattr *tb[IFLA_MAX+1];
1296 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001297 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001298 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001300 s_h = cb->args[0];
1301 s_idx = cb->args[1];
1302
Eric Dumazete67f88d2011-04-27 22:56:07 +00001303 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001304 cb->seq = net->dev_base_seq;
1305
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001306 /* A hack to preserve kernel<->userspace interface.
1307 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1308 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1309 * what iproute2 < v3.9.0 used.
1310 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1311 * attribute, its netlink message is shorter than struct ifinfomsg.
1312 */
1313 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1314 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1315
1316 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001317
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001318 if (tb[IFLA_EXT_MASK])
1319 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1320 }
Greg Rose115c9b82012-02-21 16:54:48 -05001321
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001322 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1323 idx = 0;
1324 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001325 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001326 if (idx < s_idx)
1327 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001328 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1329 NETLINK_CB(cb->skb).portid,
1330 cb->nlh->nlmsg_seq, 0,
1331 NLM_F_MULTI,
1332 ext_filter_mask);
1333 /* If we ran out of room on the first message,
1334 * we're in trouble
1335 */
1336 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1337
David S. Miller7b46a642015-01-18 23:36:08 -05001338 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001339 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001340
1341 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001342cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001343 idx++;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001346out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001347 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001348 cb->args[1] = idx;
1349 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 return skb->len;
1352}
1353
Jiri Pirkof7b12602014-02-18 20:53:18 +01001354int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1355{
1356 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1357}
1358EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001359
Eric W. Biederman81adee42009-11-08 00:53:51 -08001360struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1361{
1362 struct net *net;
1363 /* Examine the link attributes and figure out which
1364 * network namespace we are talking about.
1365 */
1366 if (tb[IFLA_NET_NS_PID])
1367 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001368 else if (tb[IFLA_NET_NS_FD])
1369 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001370 else
1371 net = get_net(src_net);
1372 return net;
1373}
1374EXPORT_SYMBOL(rtnl_link_get_net);
1375
Thomas Graf1840bb12008-02-23 19:54:36 -08001376static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1377{
1378 if (dev) {
1379 if (tb[IFLA_ADDRESS] &&
1380 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1381 return -EINVAL;
1382
1383 if (tb[IFLA_BROADCAST] &&
1384 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1385 return -EINVAL;
1386 }
1387
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001388 if (tb[IFLA_AF_SPEC]) {
1389 struct nlattr *af;
1390 int rem, err;
1391
1392 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1393 const struct rtnl_af_ops *af_ops;
1394
1395 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1396 return -EAFNOSUPPORT;
1397
1398 if (!af_ops->set_link_af)
1399 return -EOPNOTSUPP;
1400
1401 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001402 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001403 if (err < 0)
1404 return err;
1405 }
1406 }
1407 }
1408
Thomas Graf1840bb12008-02-23 19:54:36 -08001409 return 0;
1410}
1411
Chris Wrightc02db8c2010-05-16 01:05:45 -07001412static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1413{
1414 int rem, err = -EINVAL;
1415 struct nlattr *vf;
1416 const struct net_device_ops *ops = dev->netdev_ops;
1417
1418 nla_for_each_nested(vf, attr, rem) {
1419 switch (nla_type(vf)) {
1420 case IFLA_VF_MAC: {
1421 struct ifla_vf_mac *ivm;
1422 ivm = nla_data(vf);
1423 err = -EOPNOTSUPP;
1424 if (ops->ndo_set_vf_mac)
1425 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1426 ivm->mac);
1427 break;
1428 }
1429 case IFLA_VF_VLAN: {
1430 struct ifla_vf_vlan *ivv;
1431 ivv = nla_data(vf);
1432 err = -EOPNOTSUPP;
1433 if (ops->ndo_set_vf_vlan)
1434 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1435 ivv->vlan,
1436 ivv->qos);
1437 break;
1438 }
1439 case IFLA_VF_TX_RATE: {
1440 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001441 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001442 ivt = nla_data(vf);
1443 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001444 if (ops->ndo_get_vf_config)
1445 err = ops->ndo_get_vf_config(dev, ivt->vf,
1446 &ivf);
1447 if (err)
1448 break;
1449 err = -EOPNOTSUPP;
1450 if (ops->ndo_set_vf_rate)
1451 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1452 ivf.min_tx_rate,
1453 ivt->rate);
1454 break;
1455 }
1456 case IFLA_VF_RATE: {
1457 struct ifla_vf_rate *ivt;
1458 ivt = nla_data(vf);
1459 err = -EOPNOTSUPP;
1460 if (ops->ndo_set_vf_rate)
1461 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1462 ivt->min_tx_rate,
1463 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001464 break;
1465 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001466 case IFLA_VF_SPOOFCHK: {
1467 struct ifla_vf_spoofchk *ivs;
1468 ivs = nla_data(vf);
1469 err = -EOPNOTSUPP;
1470 if (ops->ndo_set_vf_spoofchk)
1471 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1472 ivs->setting);
1473 break;
1474 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001475 case IFLA_VF_LINK_STATE: {
1476 struct ifla_vf_link_state *ivl;
1477 ivl = nla_data(vf);
1478 err = -EOPNOTSUPP;
1479 if (ops->ndo_set_vf_link_state)
1480 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1481 ivl->link_state);
1482 break;
1483 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001484 default:
1485 err = -EINVAL;
1486 break;
1487 }
1488 if (err)
1489 break;
1490 }
1491 return err;
1492}
1493
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001494static int do_set_master(struct net_device *dev, int ifindex)
1495{
Jiri Pirko898e5062013-01-03 22:48:52 +00001496 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001497 const struct net_device_ops *ops;
1498 int err;
1499
Jiri Pirko898e5062013-01-03 22:48:52 +00001500 if (upper_dev) {
1501 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001502 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001503 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001504 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001505 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001506 if (err)
1507 return err;
1508 } else {
1509 return -EOPNOTSUPP;
1510 }
1511 }
1512
1513 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001514 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1515 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001516 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001517 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001518 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001519 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001520 if (err)
1521 return err;
1522 } else {
1523 return -EOPNOTSUPP;
1524 }
1525 }
1526 return 0;
1527}
1528
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001529#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001530/* notify flag means notify + modified. */
1531#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001532static int do_setlink(const struct sk_buff *skb,
1533 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001534 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001535{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001536 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001537 int err;
1538
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001539 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001540 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001541 if (IS_ERR(net)) {
1542 err = PTR_ERR(net);
1543 goto errout;
1544 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001545 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001546 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001547 err = -EPERM;
1548 goto errout;
1549 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001550 err = dev_change_net_namespace(dev, net, ifname);
1551 put_net(net);
1552 if (err)
1553 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001554 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001555 }
1556
Patrick McHardy0157f602007-06-13 12:03:36 -07001557 if (tb[IFLA_MAP]) {
1558 struct rtnl_link_ifmap *u_map;
1559 struct ifmap k_map;
1560
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001561 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001562 err = -EOPNOTSUPP;
1563 goto errout;
1564 }
1565
1566 if (!netif_device_present(dev)) {
1567 err = -ENODEV;
1568 goto errout;
1569 }
1570
1571 u_map = nla_data(tb[IFLA_MAP]);
1572 k_map.mem_start = (unsigned long) u_map->mem_start;
1573 k_map.mem_end = (unsigned long) u_map->mem_end;
1574 k_map.base_addr = (unsigned short) u_map->base_addr;
1575 k_map.irq = (unsigned char) u_map->irq;
1576 k_map.dma = (unsigned char) u_map->dma;
1577 k_map.port = (unsigned char) u_map->port;
1578
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001579 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001580 if (err < 0)
1581 goto errout;
1582
Nicolas Dichtelba998902014-09-01 16:07:29 +02001583 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001584 }
1585
1586 if (tb[IFLA_ADDRESS]) {
1587 struct sockaddr *sa;
1588 int len;
1589
Patrick McHardy0157f602007-06-13 12:03:36 -07001590 len = sizeof(sa_family_t) + dev->addr_len;
1591 sa = kmalloc(len, GFP_KERNEL);
1592 if (!sa) {
1593 err = -ENOMEM;
1594 goto errout;
1595 }
1596 sa->sa_family = dev->type;
1597 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1598 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001599 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001600 kfree(sa);
1601 if (err)
1602 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001603 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001604 }
1605
1606 if (tb[IFLA_MTU]) {
1607 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1608 if (err < 0)
1609 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001610 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001611 }
1612
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001613 if (tb[IFLA_GROUP]) {
1614 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001615 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001616 }
1617
Patrick McHardy0157f602007-06-13 12:03:36 -07001618 /*
1619 * Interface selected by interface index but interface
1620 * name provided implies that a name change has been
1621 * requested.
1622 */
1623 if (ifm->ifi_index > 0 && ifname[0]) {
1624 err = dev_change_name(dev, ifname);
1625 if (err < 0)
1626 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001627 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001628 }
1629
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001630 if (tb[IFLA_IFALIAS]) {
1631 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1632 nla_len(tb[IFLA_IFALIAS]));
1633 if (err < 0)
1634 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001635 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001636 }
1637
Patrick McHardy0157f602007-06-13 12:03:36 -07001638 if (tb[IFLA_BROADCAST]) {
1639 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001640 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001641 }
1642
1643 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001644 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001645 if (err < 0)
1646 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001647 }
1648
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001649 if (tb[IFLA_MASTER]) {
1650 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1651 if (err)
1652 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001653 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001654 }
1655
Jiri Pirko9a572472012-12-27 23:49:39 +00001656 if (tb[IFLA_CARRIER]) {
1657 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1658 if (err)
1659 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001660 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001661 }
1662
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001663 if (tb[IFLA_TXQLEN]) {
1664 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1665
1666 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001667 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001668
1669 dev->tx_queue_len = value;
1670 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001671
Patrick McHardy0157f602007-06-13 12:03:36 -07001672 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001673 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001674
1675 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001676 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1677
David S. Miller93b2d4a2008-02-17 18:35:07 -08001678 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001679 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001680 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001681 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001682 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001683 }
1684
Chris Wrightc02db8c2010-05-16 01:05:45 -07001685 if (tb[IFLA_VFINFO_LIST]) {
1686 struct nlattr *attr;
1687 int rem;
1688 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001689 if (nla_type(attr) != IFLA_VF_INFO) {
1690 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001691 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001692 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001693 err = do_setvfinfo(dev, attr);
1694 if (err < 0)
1695 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001696 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001697 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001698 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001699 err = 0;
1700
Scott Feldman57b61082010-05-17 22:49:55 -07001701 if (tb[IFLA_VF_PORTS]) {
1702 struct nlattr *port[IFLA_PORT_MAX+1];
1703 struct nlattr *attr;
1704 int vf;
1705 int rem;
1706
1707 err = -EOPNOTSUPP;
1708 if (!ops->ndo_set_vf_port)
1709 goto errout;
1710
1711 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1712 if (nla_type(attr) != IFLA_VF_PORT)
1713 continue;
1714 err = nla_parse_nested(port, IFLA_PORT_MAX,
1715 attr, ifla_port_policy);
1716 if (err < 0)
1717 goto errout;
1718 if (!port[IFLA_PORT_VF]) {
1719 err = -EOPNOTSUPP;
1720 goto errout;
1721 }
1722 vf = nla_get_u32(port[IFLA_PORT_VF]);
1723 err = ops->ndo_set_vf_port(dev, vf, port);
1724 if (err < 0)
1725 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001726 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001727 }
1728 }
1729 err = 0;
1730
1731 if (tb[IFLA_PORT_SELF]) {
1732 struct nlattr *port[IFLA_PORT_MAX+1];
1733
1734 err = nla_parse_nested(port, IFLA_PORT_MAX,
1735 tb[IFLA_PORT_SELF], ifla_port_policy);
1736 if (err < 0)
1737 goto errout;
1738
1739 err = -EOPNOTSUPP;
1740 if (ops->ndo_set_vf_port)
1741 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1742 if (err < 0)
1743 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001744 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001745 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001746
1747 if (tb[IFLA_AF_SPEC]) {
1748 struct nlattr *af;
1749 int rem;
1750
1751 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1752 const struct rtnl_af_ops *af_ops;
1753
1754 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001755 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001756
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001757 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001758 if (err < 0)
1759 goto errout;
1760
Nicolas Dichtelba998902014-09-01 16:07:29 +02001761 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001762 }
1763 }
Scott Feldman57b61082010-05-17 22:49:55 -07001764 err = 0;
1765
Patrick McHardy0157f602007-06-13 12:03:36 -07001766errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001767 if (status & DO_SETLINK_MODIFIED) {
1768 if (status & DO_SETLINK_NOTIFY)
1769 netdev_state_change(dev);
1770
1771 if (err < 0)
1772 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",
1773 dev->name);
1774 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001775
Patrick McHardy0157f602007-06-13 12:03:36 -07001776 return err;
1777}
1778
Thomas Graf661d2962013-03-21 07:45:29 +00001779static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001781 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001782 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001784 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001785 struct nlattr *tb[IFLA_MAX+1];
1786 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Thomas Grafda5e0492006-08-10 21:17:37 -07001788 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1789 if (err < 0)
1790 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Thomas Graf5176f912006-08-26 20:13:18 -07001792 if (tb[IFLA_IFNAME])
1793 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001794 else
1795 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001798 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001799 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001800 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001801 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001802 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001803 else
1804 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Thomas Grafda5e0492006-08-10 21:17:37 -07001806 if (dev == NULL) {
1807 err = -ENODEV;
1808 goto errout;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Eric Dumazete0d087a2009-11-07 01:26:17 -08001811 err = validate_linkmsg(dev, tb);
1812 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001813 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001814
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001815 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001816errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return err;
1818}
1819
Thomas Graf661d2962013-03-21 07:45:29 +00001820static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001821{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001822 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001823 const struct rtnl_link_ops *ops;
1824 struct net_device *dev;
1825 struct ifinfomsg *ifm;
1826 char ifname[IFNAMSIZ];
1827 struct nlattr *tb[IFLA_MAX+1];
1828 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001829 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001830
1831 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1832 if (err < 0)
1833 return err;
1834
1835 if (tb[IFLA_IFNAME])
1836 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1837
1838 ifm = nlmsg_data(nlh);
1839 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001840 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001841 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001842 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001843 else
1844 return -EINVAL;
1845
1846 if (!dev)
1847 return -ENODEV;
1848
1849 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001850 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001851 return -EOPNOTSUPP;
1852
Eric Dumazet226bd342011-05-08 23:17:57 +00001853 ops->dellink(dev, &list_kill);
1854 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001855 return 0;
1856}
1857
Patrick McHardy3729d502010-02-26 06:34:54 +00001858int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1859{
1860 unsigned int old_flags;
1861 int err;
1862
1863 old_flags = dev->flags;
1864 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1865 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1866 if (err < 0)
1867 return err;
1868 }
1869
1870 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001871
Nicolas Dichtela528c212013-09-25 12:02:44 +02001872 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001873 return 0;
1874}
1875EXPORT_SYMBOL(rtnl_configure_link);
1876
Rami Rosenc0713562012-11-30 01:08:47 +00001877struct net_device *rtnl_create_link(struct net *net,
Tom Gundersen55177502014-07-14 16:37:25 +02001878 char *ifname, unsigned char name_assign_type,
1879 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001880{
1881 int err;
1882 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001883 unsigned int num_tx_queues = 1;
1884 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001885
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001886 if (tb[IFLA_NUM_TX_QUEUES])
1887 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1888 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001889 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001890
1891 if (tb[IFLA_NUM_RX_QUEUES])
1892 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1893 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001894 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001895
Pavel Emelianove7199282007-08-08 22:16:38 -07001896 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001897 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001898 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001899 if (!dev)
1900 goto err;
1901
Eric W. Biederman81adee42009-11-08 00:53:51 -08001902 dev_net_set(dev, net);
1903 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001904 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001905
Pavel Emelianove7199282007-08-08 22:16:38 -07001906 if (tb[IFLA_MTU])
1907 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001908 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001909 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1910 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001911 dev->addr_assign_type = NET_ADDR_SET;
1912 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001913 if (tb[IFLA_BROADCAST])
1914 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1915 nla_len(tb[IFLA_BROADCAST]));
1916 if (tb[IFLA_TXQLEN])
1917 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1918 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001919 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001920 if (tb[IFLA_LINKMODE])
1921 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001922 if (tb[IFLA_GROUP])
1923 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001924
1925 return dev;
1926
Pavel Emelianove7199282007-08-08 22:16:38 -07001927err:
1928 return ERR_PTR(err);
1929}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001930EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001931
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001932static int rtnl_group_changelink(const struct sk_buff *skb,
1933 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001934 struct ifinfomsg *ifm,
1935 struct nlattr **tb)
1936{
1937 struct net_device *dev;
1938 int err;
1939
1940 for_each_netdev(net, dev) {
1941 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001942 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001943 if (err < 0)
1944 return err;
1945 }
1946 }
1947
1948 return 0;
1949}
1950
Thomas Graf661d2962013-03-21 07:45:29 +00001951static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001952{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001953 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001954 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001955 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001956 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001957 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001958 struct ifinfomsg *ifm;
1959 char kind[MODULE_NAME_LEN];
1960 char ifname[IFNAMSIZ];
1961 struct nlattr *tb[IFLA_MAX+1];
1962 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02001963 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001964 int err;
1965
Johannes Berg95a5afc2008-10-16 15:24:51 -07001966#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001967replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001968#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001969 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1970 if (err < 0)
1971 return err;
1972
1973 if (tb[IFLA_IFNAME])
1974 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1975 else
1976 ifname[0] = '\0';
1977
1978 ifm = nlmsg_data(nlh);
1979 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001980 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001981 else {
1982 if (ifname[0])
1983 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001984 else
1985 dev = NULL;
1986 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001987
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001988 if (dev) {
1989 master_dev = netdev_master_upper_dev_get(dev);
1990 if (master_dev)
1991 m_ops = master_dev->rtnl_link_ops;
1992 }
1993
Eric Dumazete0d087a2009-11-07 01:26:17 -08001994 err = validate_linkmsg(dev, tb);
1995 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001996 return err;
1997
Patrick McHardy38f7b872007-06-13 12:03:51 -07001998 if (tb[IFLA_LINKINFO]) {
1999 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2000 tb[IFLA_LINKINFO], ifla_info_policy);
2001 if (err < 0)
2002 return err;
2003 } else
2004 memset(linkinfo, 0, sizeof(linkinfo));
2005
2006 if (linkinfo[IFLA_INFO_KIND]) {
2007 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2008 ops = rtnl_link_ops_get(kind);
2009 } else {
2010 kind[0] = '\0';
2011 ops = NULL;
2012 }
2013
2014 if (1) {
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002015 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
2016 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
2017 struct nlattr **data = NULL;
2018 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002019 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002020
2021 if (ops) {
2022 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2023 err = nla_parse_nested(attr, ops->maxtype,
2024 linkinfo[IFLA_INFO_DATA],
2025 ops->policy);
2026 if (err < 0)
2027 return err;
2028 data = attr;
2029 }
2030 if (ops->validate) {
2031 err = ops->validate(tb, data);
2032 if (err < 0)
2033 return err;
2034 }
2035 }
2036
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002037 if (m_ops) {
2038 if (m_ops->slave_maxtype &&
2039 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2040 err = nla_parse_nested(slave_attr,
2041 m_ops->slave_maxtype,
2042 linkinfo[IFLA_INFO_SLAVE_DATA],
2043 m_ops->slave_policy);
2044 if (err < 0)
2045 return err;
2046 slave_data = slave_attr;
2047 }
2048 if (m_ops->slave_validate) {
2049 err = m_ops->slave_validate(tb, slave_data);
2050 if (err < 0)
2051 return err;
2052 }
2053 }
2054
Patrick McHardy38f7b872007-06-13 12:03:51 -07002055 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002056 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002057
2058 if (nlh->nlmsg_flags & NLM_F_EXCL)
2059 return -EEXIST;
2060 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2061 return -EOPNOTSUPP;
2062
2063 if (linkinfo[IFLA_INFO_DATA]) {
2064 if (!ops || ops != dev->rtnl_link_ops ||
2065 !ops->changelink)
2066 return -EOPNOTSUPP;
2067
2068 err = ops->changelink(dev, tb, data);
2069 if (err < 0)
2070 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002071 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002072 }
2073
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002074 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2075 if (!m_ops || !m_ops->slave_changelink)
2076 return -EOPNOTSUPP;
2077
2078 err = m_ops->slave_changelink(master_dev, dev,
2079 tb, slave_data);
2080 if (err < 0)
2081 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002082 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002083 }
2084
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002085 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002086 }
2087
Patrick McHardyffa934f2011-01-20 03:00:42 +00002088 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2089 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002090 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002091 nla_get_u32(tb[IFLA_GROUP]),
2092 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002093 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002094 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002095
Patrick McHardy0e068772007-07-11 19:42:31 -07002096 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002097 return -EOPNOTSUPP;
2098
2099 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002100#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002101 if (kind[0]) {
2102 __rtnl_unlock();
2103 request_module("rtnl-link-%s", kind);
2104 rtnl_lock();
2105 ops = rtnl_link_ops_get(kind);
2106 if (ops)
2107 goto replay;
2108 }
2109#endif
2110 return -EOPNOTSUPP;
2111 }
2112
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002113 if (!ops->setup)
2114 return -EOPNOTSUPP;
2115
Tom Gundersen55177502014-07-14 16:37:25 +02002116 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002117 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002118 name_assign_type = NET_NAME_ENUM;
2119 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002120
Eric W. Biederman81adee42009-11-08 00:53:51 -08002121 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002122 if (IS_ERR(dest_net))
2123 return PTR_ERR(dest_net);
2124
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002125 if (tb[IFLA_LINK_NETNSID]) {
2126 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2127
2128 link_net = get_net_ns_by_id(dest_net, id);
2129 if (!link_net) {
2130 err = -EINVAL;
2131 goto out;
2132 }
2133 }
2134
2135 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2136 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002137 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002138 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002139 goto out;
2140 }
2141
2142 dev->ifindex = ifm->ifi_index;
2143
Cong Wang0e0eee22014-02-11 15:51:30 -08002144 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002145 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002146 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002147 * and unregister it on failure after registration
2148 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002149 */
Cong Wange51fb152014-06-03 16:40:47 -07002150 if (err < 0) {
2151 /* If device is not registered at all, free it now */
2152 if (dev->reg_state == NETREG_UNINITIALIZED)
2153 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002154 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002155 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002156 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002157 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002158 if (err < 0) {
2159 free_netdev(dev);
2160 goto out;
2161 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002162 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002163 err = rtnl_configure_link(dev, ifm);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002164 if (err < 0) {
WANG Cong7afb8882015-02-13 13:56:53 -08002165 if (ops->newlink) {
2166 LIST_HEAD(list_kill);
2167
2168 ops->dellink(dev, &list_kill);
2169 unregister_netdevice_many(&list_kill);
2170 } else {
2171 unregister_netdevice(dev);
2172 }
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002173 goto out;
2174 }
2175
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002176 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002177 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002178 if (err < 0)
2179 unregister_netdevice(dev);
2180 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002181out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002182 if (link_net)
2183 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002184 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002185 return err;
2186 }
2187}
2188
Thomas Graf661d2962013-03-21 07:45:29 +00002189static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002190{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002191 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002192 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002193 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002194 struct nlattr *tb[IFLA_MAX+1];
2195 struct net_device *dev = NULL;
2196 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002197 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002198 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002199
Thomas Grafb60c5112006-08-04 23:05:34 -07002200 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2201 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002202 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002203
Eric Dumazeta3d12892009-10-21 10:59:31 +00002204 if (tb[IFLA_IFNAME])
2205 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2206
Greg Rose115c9b82012-02-21 16:54:48 -05002207 if (tb[IFLA_EXT_MASK])
2208 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2209
Thomas Grafb60c5112006-08-04 23:05:34 -07002210 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002211 if (ifm->ifi_index > 0)
2212 dev = __dev_get_by_index(net, ifm->ifi_index);
2213 else if (tb[IFLA_IFNAME])
2214 dev = __dev_get_by_name(net, ifname);
2215 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002216 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002217
Eric Dumazeta3d12892009-10-21 10:59:31 +00002218 if (dev == NULL)
2219 return -ENODEV;
2220
Greg Rose115c9b82012-02-21 16:54:48 -05002221 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002222 if (nskb == NULL)
2223 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002224
Eric W. Biederman15e47302012-09-07 20:12:54 +00002225 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002226 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002227 if (err < 0) {
2228 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2229 WARN_ON(err == -EMSGSIZE);
2230 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002231 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002232 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002233
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002234 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002235}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002236
Greg Rose115c9b82012-02-21 16:54:48 -05002237static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002238{
Greg Rose115c9b82012-02-21 16:54:48 -05002239 struct net *net = sock_net(skb->sk);
2240 struct net_device *dev;
2241 struct nlattr *tb[IFLA_MAX+1];
2242 u32 ext_filter_mask = 0;
2243 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002244 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002245
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002246 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2247 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2248 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2249
2250 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002251 if (tb[IFLA_EXT_MASK])
2252 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2253 }
Greg Rose115c9b82012-02-21 16:54:48 -05002254
2255 if (!ext_filter_mask)
2256 return NLMSG_GOODSIZE;
2257 /*
2258 * traverse the list of net devices and compute the minimum
2259 * buffer size based upon the filter mask.
2260 */
2261 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2262 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2263 if_nlmsg_size(dev,
2264 ext_filter_mask));
2265 }
2266
Greg Rosec7ac8672011-06-10 01:27:09 +00002267 return min_ifinfo_dump_size;
2268}
2269
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002270static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 int idx;
2273 int s_idx = cb->family;
2274
2275 if (s_idx == 0)
2276 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002277 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int type = cb->nlh->nlmsg_type-RTM_BASE;
2279 if (idx < s_idx || idx == PF_PACKET)
2280 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002281 if (rtnl_msg_handlers[idx] == NULL ||
2282 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002284 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002286 cb->prev_seq = 0;
2287 cb->seq = 0;
2288 }
Thomas Grafe2849862007-03-22 11:48:11 -07002289 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 break;
2291 }
2292 cb->family = idx;
2293
2294 return skb->len;
2295}
2296
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002297struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2298 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002300 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002302 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002303 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002305 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002306 if (skb == NULL)
2307 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Greg Rose115c9b82012-02-21 16:54:48 -05002309 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002310 if (err < 0) {
2311 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2312 WARN_ON(err == -EMSGSIZE);
2313 kfree_skb(skb);
2314 goto errout;
2315 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002316 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002317errout:
2318 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002319 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002320 return NULL;
2321}
2322
2323void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2324{
2325 struct net *net = dev_net(dev);
2326
2327 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2328}
2329
2330void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2331 gfp_t flags)
2332{
2333 struct sk_buff *skb;
2334
2335 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2336 if (skb)
2337 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002339EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
John Fastabendd83b0602012-04-15 06:44:08 +00002341static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2342 struct net_device *dev,
2343 u8 *addr, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002344 int type, unsigned int flags,
2345 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002346{
2347 struct nlmsghdr *nlh;
2348 struct ndmsg *ndm;
2349
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002350 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002351 if (!nlh)
2352 return -EMSGSIZE;
2353
2354 ndm = nlmsg_data(nlh);
2355 ndm->ndm_family = AF_BRIDGE;
2356 ndm->ndm_pad1 = 0;
2357 ndm->ndm_pad2 = 0;
2358 ndm->ndm_flags = flags;
2359 ndm->ndm_type = 0;
2360 ndm->ndm_ifindex = dev->ifindex;
2361 ndm->ndm_state = NUD_PERMANENT;
2362
2363 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2364 goto nla_put_failure;
2365
Johannes Berg053c0952015-01-16 22:09:00 +01002366 nlmsg_end(skb, nlh);
2367 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002368
2369nla_put_failure:
2370 nlmsg_cancel(skb, nlh);
2371 return -EMSGSIZE;
2372}
2373
John Fastabend3ff661c2012-04-15 06:44:14 +00002374static inline size_t rtnl_fdb_nlmsg_size(void)
2375{
2376 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2377}
2378
2379static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2380{
2381 struct net *net = dev_net(dev);
2382 struct sk_buff *skb;
2383 int err = -ENOBUFS;
2384
2385 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2386 if (!skb)
2387 goto errout;
2388
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002389 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002390 if (err < 0) {
2391 kfree_skb(skb);
2392 goto errout;
2393 }
2394
2395 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2396 return;
2397errout:
2398 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2399}
2400
Vlad Yasevich090096b2013-03-06 15:39:42 +00002401/**
2402 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2403 */
2404int ndo_dflt_fdb_add(struct ndmsg *ndm,
2405 struct nlattr *tb[],
2406 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002407 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002408 u16 flags)
2409{
2410 int err = -EINVAL;
2411
2412 /* If aging addresses are supported device will need to
2413 * implement its own handler for this.
2414 */
2415 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2416 pr_info("%s: FDB only supports static addresses\n", dev->name);
2417 return err;
2418 }
2419
Or Gerlitz65891fe2014-12-14 18:19:05 +02002420 if (vid) {
2421 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2422 return err;
2423 }
2424
Vlad Yasevich090096b2013-03-06 15:39:42 +00002425 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2426 err = dev_uc_add_excl(dev, addr);
2427 else if (is_multicast_ether_addr(addr))
2428 err = dev_mc_add_excl(dev, addr);
2429
2430 /* Only return duplicate errors if NLM_F_EXCL is set */
2431 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2432 err = 0;
2433
2434 return err;
2435}
2436EXPORT_SYMBOL(ndo_dflt_fdb_add);
2437
Jiri Pirkof6f64242014-11-28 14:34:15 +01002438static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2439{
2440 u16 vid = 0;
2441
2442 if (vlan_attr) {
2443 if (nla_len(vlan_attr) != sizeof(u16)) {
2444 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2445 return -EINVAL;
2446 }
2447
2448 vid = nla_get_u16(vlan_attr);
2449
2450 if (!vid || vid >= VLAN_VID_MASK) {
2451 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2452 vid);
2453 return -EINVAL;
2454 }
2455 }
2456 *p_vid = vid;
2457 return 0;
2458}
2459
Thomas Graf661d2962013-03-21 07:45:29 +00002460static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002461{
2462 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002463 struct ndmsg *ndm;
2464 struct nlattr *tb[NDA_MAX+1];
2465 struct net_device *dev;
2466 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002467 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002468 int err;
2469
2470 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2471 if (err < 0)
2472 return err;
2473
2474 ndm = nlmsg_data(nlh);
2475 if (ndm->ndm_ifindex == 0) {
2476 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2477 return -EINVAL;
2478 }
2479
2480 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2481 if (dev == NULL) {
2482 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2483 return -ENODEV;
2484 }
2485
2486 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2487 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2488 return -EINVAL;
2489 }
2490
2491 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002492
Jiri Pirkof6f64242014-11-28 14:34:15 +01002493 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2494 if (err)
2495 return err;
2496
John Fastabend77162022012-04-15 06:43:56 +00002497 err = -EOPNOTSUPP;
2498
2499 /* Support fdb on master device the net/bridge default case */
2500 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2501 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002502 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2503 const struct net_device_ops *ops = br_dev->netdev_ops;
2504
Jiri Pirkof6f64242014-11-28 14:34:15 +01002505 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2506 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002507 if (err)
2508 goto out;
2509 else
2510 ndm->ndm_flags &= ~NTF_MASTER;
2511 }
2512
2513 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002514 if ((ndm->ndm_flags & NTF_SELF)) {
2515 if (dev->netdev_ops->ndo_fdb_add)
2516 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002517 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002518 nlh->nlmsg_flags);
2519 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002520 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002521 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002522
John Fastabend3ff661c2012-04-15 06:44:14 +00002523 if (!err) {
2524 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002525 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002526 }
John Fastabend77162022012-04-15 06:43:56 +00002527 }
2528out:
2529 return err;
2530}
2531
Vlad Yasevich090096b2013-03-06 15:39:42 +00002532/**
2533 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2534 */
2535int ndo_dflt_fdb_del(struct ndmsg *ndm,
2536 struct nlattr *tb[],
2537 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002538 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002539{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002540 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002541
2542 /* If aging addresses are supported device will need to
2543 * implement its own handler for this.
2544 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002545 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002546 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002547 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002548 }
2549
2550 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2551 err = dev_uc_del(dev, addr);
2552 else if (is_multicast_ether_addr(addr))
2553 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002554
2555 return err;
2556}
2557EXPORT_SYMBOL(ndo_dflt_fdb_del);
2558
Thomas Graf661d2962013-03-21 07:45:29 +00002559static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002560{
2561 struct net *net = sock_net(skb->sk);
2562 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002563 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002564 struct net_device *dev;
2565 int err = -EINVAL;
2566 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002567 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002568
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002569 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002570 return -EPERM;
2571
2572 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2573 if (err < 0)
2574 return err;
John Fastabend77162022012-04-15 06:43:56 +00002575
2576 ndm = nlmsg_data(nlh);
2577 if (ndm->ndm_ifindex == 0) {
2578 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2579 return -EINVAL;
2580 }
2581
2582 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2583 if (dev == NULL) {
2584 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2585 return -ENODEV;
2586 }
2587
Vlad Yasevich1690be62013-02-13 12:00:18 +00002588 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2589 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002590 return -EINVAL;
2591 }
2592
Vlad Yasevich1690be62013-02-13 12:00:18 +00002593 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002594
Jiri Pirkof6f64242014-11-28 14:34:15 +01002595 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2596 if (err)
2597 return err;
2598
John Fastabend77162022012-04-15 06:43:56 +00002599 err = -EOPNOTSUPP;
2600
2601 /* Support fdb on master device the net/bridge default case */
2602 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2603 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002604 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2605 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002606
Jiri Pirko898e5062013-01-03 22:48:52 +00002607 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002608 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002609
2610 if (err)
2611 goto out;
2612 else
2613 ndm->ndm_flags &= ~NTF_MASTER;
2614 }
2615
2616 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002617 if (ndm->ndm_flags & NTF_SELF) {
2618 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002619 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2620 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002621 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002622 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002623
John Fastabend3ff661c2012-04-15 06:44:14 +00002624 if (!err) {
2625 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002626 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002627 }
John Fastabend77162022012-04-15 06:43:56 +00002628 }
2629out:
2630 return err;
2631}
2632
John Fastabendd83b0602012-04-15 06:44:08 +00002633static int nlmsg_populate_fdb(struct sk_buff *skb,
2634 struct netlink_callback *cb,
2635 struct net_device *dev,
2636 int *idx,
2637 struct netdev_hw_addr_list *list)
2638{
2639 struct netdev_hw_addr *ha;
2640 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002641 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002642
Eric W. Biederman15e47302012-09-07 20:12:54 +00002643 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002644 seq = cb->nlh->nlmsg_seq;
2645
2646 list_for_each_entry(ha, &list->list, list) {
2647 if (*idx < cb->args[0])
2648 goto skip;
2649
2650 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
John Fastabenda7a558f2012-11-01 16:23:10 +00002651 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002652 RTM_NEWNEIGH, NTF_SELF,
2653 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002654 if (err < 0)
2655 return err;
2656skip:
2657 *idx += 1;
2658 }
2659 return 0;
2660}
2661
2662/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002663 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002664 * @nlh: netlink message header
2665 * @dev: netdevice
2666 *
2667 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002668 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002669 */
2670int ndo_dflt_fdb_dump(struct sk_buff *skb,
2671 struct netlink_callback *cb,
2672 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002673 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002674 int idx)
2675{
2676 int err;
2677
2678 netif_addr_lock_bh(dev);
2679 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2680 if (err)
2681 goto out;
2682 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2683out:
2684 netif_addr_unlock_bh(dev);
2685 return idx;
2686}
2687EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2688
John Fastabend77162022012-04-15 06:43:56 +00002689static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2690{
John Fastabend77162022012-04-15 06:43:56 +00002691 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002692 struct nlattr *tb[IFLA_MAX+1];
2693 struct net_device *bdev = NULL;
2694 struct net_device *br_dev = NULL;
2695 const struct net_device_ops *ops = NULL;
2696 const struct net_device_ops *cops = NULL;
2697 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2698 struct net *net = sock_net(skb->sk);
2699 int brport_idx = 0;
2700 int br_idx = 0;
2701 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002702
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002703 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2704 ifla_policy) == 0) {
2705 if (tb[IFLA_MASTER])
2706 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2707 }
John Fastabend77162022012-04-15 06:43:56 +00002708
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002709 brport_idx = ifm->ifi_index;
2710
2711 if (br_idx) {
2712 br_dev = __dev_get_by_index(net, br_idx);
2713 if (!br_dev)
2714 return -ENODEV;
2715
2716 ops = br_dev->netdev_ops;
2717 bdev = br_dev;
2718 }
2719
2720 for_each_netdev(net, dev) {
2721 if (brport_idx && (dev->ifindex != brport_idx))
2722 continue;
2723
2724 if (!br_idx) { /* user did not specify a specific bridge */
2725 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2726 br_dev = netdev_master_upper_dev_get(dev);
2727 cops = br_dev->netdev_ops;
2728 }
2729
2730 bdev = dev;
2731 } else {
2732 if (dev != br_dev &&
2733 !(dev->priv_flags & IFF_BRIDGE_PORT))
2734 continue;
2735
2736 if (br_dev != netdev_master_upper_dev_get(dev) &&
2737 !(dev->priv_flags & IFF_EBRIDGE))
2738 continue;
2739
2740 bdev = br_dev;
2741 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002742 }
2743
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002744 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2745 if (cops && cops->ndo_fdb_dump)
2746 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2747 idx);
2748 }
2749
John Fastabend77162022012-04-15 06:43:56 +00002750 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002751 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002752 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002753 else
2754 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002755
2756 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002757 }
John Fastabend77162022012-04-15 06:43:56 +00002758
2759 cb->args[0] = idx;
2760 return skb->len;
2761}
2762
Scott Feldman2c3c0312014-11-28 14:34:25 +01002763static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2764 unsigned int attrnum, unsigned int flag)
2765{
2766 if (mask & flag)
2767 return nla_put_u8(skb, attrnum, !!(flags & flag));
2768 return 0;
2769}
2770
John Fastabend815cccb2012-10-24 08:13:09 +00002771int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002772 struct net_device *dev, u16 mode,
2773 u32 flags, u32 mask)
John Fastabend815cccb2012-10-24 08:13:09 +00002774{
2775 struct nlmsghdr *nlh;
2776 struct ifinfomsg *ifm;
2777 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002778 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002779 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002780 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002781
2782 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2783 if (nlh == NULL)
2784 return -EMSGSIZE;
2785
2786 ifm = nlmsg_data(nlh);
2787 ifm->ifi_family = AF_BRIDGE;
2788 ifm->__ifi_pad = 0;
2789 ifm->ifi_type = dev->type;
2790 ifm->ifi_index = dev->ifindex;
2791 ifm->ifi_flags = dev_get_flags(dev);
2792 ifm->ifi_change = 0;
2793
2794
2795 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2796 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2797 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002798 (br_dev &&
2799 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002800 (dev->addr_len &&
2801 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2802 (dev->ifindex != dev->iflink &&
2803 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2804 goto nla_put_failure;
2805
2806 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2807 if (!br_afspec)
2808 goto nla_put_failure;
2809
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002810 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002811 nla_nest_cancel(skb, br_afspec);
2812 goto nla_put_failure;
2813 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002814
2815 if (mode != BRIDGE_MODE_UNDEF) {
2816 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2817 nla_nest_cancel(skb, br_afspec);
2818 goto nla_put_failure;
2819 }
2820 }
John Fastabend815cccb2012-10-24 08:13:09 +00002821 nla_nest_end(skb, br_afspec);
2822
Scott Feldman2c3c0312014-11-28 14:34:25 +01002823 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2824 if (!protinfo)
2825 goto nla_put_failure;
2826
2827 if (brport_nla_put_flag(skb, flags, mask,
2828 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2829 brport_nla_put_flag(skb, flags, mask,
2830 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2831 brport_nla_put_flag(skb, flags, mask,
2832 IFLA_BRPORT_FAST_LEAVE,
2833 BR_MULTICAST_FAST_LEAVE) ||
2834 brport_nla_put_flag(skb, flags, mask,
2835 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2836 brport_nla_put_flag(skb, flags, mask,
2837 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2838 brport_nla_put_flag(skb, flags, mask,
2839 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2840 brport_nla_put_flag(skb, flags, mask,
2841 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2842 brport_nla_put_flag(skb, flags, mask,
2843 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2844 nla_nest_cancel(skb, protinfo);
2845 goto nla_put_failure;
2846 }
2847
2848 nla_nest_end(skb, protinfo);
2849
Johannes Berg053c0952015-01-16 22:09:00 +01002850 nlmsg_end(skb, nlh);
2851 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00002852nla_put_failure:
2853 nlmsg_cancel(skb, nlh);
2854 return -EMSGSIZE;
2855}
2856EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2857
John Fastabende5a55a82012-10-24 08:12:57 +00002858static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2859{
2860 struct net *net = sock_net(skb->sk);
2861 struct net_device *dev;
2862 int idx = 0;
2863 u32 portid = NETLINK_CB(cb->skb).portid;
2864 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002865 u32 filter_mask = 0;
2866
Thomas Grafaa68c202014-11-26 13:42:20 +01002867 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2868 struct nlattr *extfilt;
2869
2870 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2871 IFLA_EXT_MASK);
2872 if (extfilt) {
2873 if (nla_len(extfilt) < sizeof(filter_mask))
2874 return -EINVAL;
2875
2876 filter_mask = nla_get_u32(extfilt);
2877 }
2878 }
John Fastabende5a55a82012-10-24 08:12:57 +00002879
2880 rcu_read_lock();
2881 for_each_netdev_rcu(net, dev) {
2882 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002883 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002884
Jiri Pirko898e5062013-01-03 22:48:52 +00002885 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002886 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002887 br_dev->netdev_ops->ndo_bridge_getlink(
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002888 skb, portid, seq, dev, filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002889 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002890 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002891 }
2892
2893 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002894 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002895 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2896 filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002897 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002898 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002899 }
2900 }
2901 rcu_read_unlock();
2902 cb->args[0] = idx;
2903
2904 return skb->len;
2905}
2906
John Fastabend2469ffd2012-10-24 08:13:03 +00002907static inline size_t bridge_nlmsg_size(void)
2908{
2909 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2910 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2911 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2912 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2913 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2914 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2915 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2916 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2917 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2918 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2919 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2920}
2921
Roopa Prabhu02dba432015-01-14 20:02:25 -08002922static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00002923{
2924 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00002925 struct sk_buff *skb;
2926 int err = -EOPNOTSUPP;
2927
Roopa Prabhu02dba432015-01-14 20:02:25 -08002928 if (!dev->netdev_ops->ndo_bridge_getlink)
2929 return 0;
2930
John Fastabend2469ffd2012-10-24 08:13:03 +00002931 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2932 if (!skb) {
2933 err = -ENOMEM;
2934 goto errout;
2935 }
2936
Roopa Prabhu02dba432015-01-14 20:02:25 -08002937 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
2938 if (err < 0)
2939 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00002940
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08002941 if (!skb->len)
2942 goto errout;
2943
John Fastabend2469ffd2012-10-24 08:13:03 +00002944 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2945 return 0;
2946errout:
2947 WARN_ON(err == -EMSGSIZE);
2948 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08002949 if (err)
2950 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00002951 return err;
2952}
2953
Thomas Graf661d2962013-03-21 07:45:29 +00002954static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00002955{
2956 struct net *net = sock_net(skb->sk);
2957 struct ifinfomsg *ifm;
2958 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00002959 struct nlattr *br_spec, *attr = NULL;
2960 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02002961 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00002962 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00002963
2964 if (nlmsg_len(nlh) < sizeof(*ifm))
2965 return -EINVAL;
2966
2967 ifm = nlmsg_data(nlh);
2968 if (ifm->ifi_family != AF_BRIDGE)
2969 return -EPFNOSUPPORT;
2970
2971 dev = __dev_get_by_index(net, ifm->ifi_index);
2972 if (!dev) {
2973 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2974 return -ENODEV;
2975 }
2976
John Fastabend2469ffd2012-10-24 08:13:03 +00002977 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2978 if (br_spec) {
2979 nla_for_each_nested(attr, br_spec, rem) {
2980 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002981 if (nla_len(attr) < sizeof(flags))
2982 return -EINVAL;
2983
John Fastabendc38e01b2012-11-02 16:32:36 +00002984 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00002985 flags = nla_get_u16(attr);
2986 break;
2987 }
2988 }
2989 }
2990
2991 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002992 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2993
2994 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002995 err = -EOPNOTSUPP;
2996 goto out;
2997 }
2998
Roopa Prabhuadd511b2015-01-29 22:40:12 -08002999 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003000 if (err)
3001 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003002
3003 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003004 }
3005
John Fastabend2469ffd2012-10-24 08:13:03 +00003006 if ((flags & BRIDGE_FLAGS_SELF)) {
3007 if (!dev->netdev_ops->ndo_bridge_setlink)
3008 err = -EOPNOTSUPP;
3009 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003010 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3011 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003012 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003013 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003014
3015 /* Generate event to notify upper layer of bridge
3016 * change
3017 */
3018 err = rtnl_bridge_notify(dev);
3019 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003020 }
3021
John Fastabendc38e01b2012-11-02 16:32:36 +00003022 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003023 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003024out:
3025 return err;
3026}
3027
Thomas Graf661d2962013-03-21 07:45:29 +00003028static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003029{
3030 struct net *net = sock_net(skb->sk);
3031 struct ifinfomsg *ifm;
3032 struct net_device *dev;
3033 struct nlattr *br_spec, *attr = NULL;
3034 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003035 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003036 bool have_flags = false;
3037
3038 if (nlmsg_len(nlh) < sizeof(*ifm))
3039 return -EINVAL;
3040
3041 ifm = nlmsg_data(nlh);
3042 if (ifm->ifi_family != AF_BRIDGE)
3043 return -EPFNOSUPPORT;
3044
3045 dev = __dev_get_by_index(net, ifm->ifi_index);
3046 if (!dev) {
3047 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3048 return -ENODEV;
3049 }
3050
3051 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3052 if (br_spec) {
3053 nla_for_each_nested(attr, br_spec, rem) {
3054 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003055 if (nla_len(attr) < sizeof(flags))
3056 return -EINVAL;
3057
Vlad Yasevich407af322013-02-13 12:00:12 +00003058 have_flags = true;
3059 flags = nla_get_u16(attr);
3060 break;
3061 }
3062 }
3063 }
3064
Vlad Yasevich407af322013-02-13 12:00:12 +00003065 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3066 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3067
3068 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3069 err = -EOPNOTSUPP;
3070 goto out;
3071 }
3072
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003073 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003074 if (err)
3075 goto out;
3076
3077 flags &= ~BRIDGE_FLAGS_MASTER;
3078 }
3079
3080 if ((flags & BRIDGE_FLAGS_SELF)) {
3081 if (!dev->netdev_ops->ndo_bridge_dellink)
3082 err = -EOPNOTSUPP;
3083 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003084 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3085 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003086
Roopa Prabhu02dba432015-01-14 20:02:25 -08003087 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003088 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003089
3090 /* Generate event to notify upper layer of bridge
3091 * change
3092 */
3093 err = rtnl_bridge_notify(dev);
3094 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003095 }
3096
3097 if (have_flags)
3098 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003099out:
3100 return err;
3101}
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103/* Process one rtnetlink message. */
3104
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003105static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003107 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003108 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 int family;
3111 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003112 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003116 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 type -= RTM_BASE;
3119
3120 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003121 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return 0;
3123
Hong zhi guo573ce262013-03-27 06:47:04 +00003124 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 sz_idx = type>>2;
3126 kind = type&3;
3127
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003128 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003129 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
David S. Millerb8f3ab42011-01-18 12:40:38 -08003131 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003132 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003133 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003134 rtnl_calcit_func calcit;
3135 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Thomas Grafe2849862007-03-22 11:48:11 -07003137 dumpit = rtnl_get_dumpit(family, type);
3138 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003139 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003140 calcit = rtnl_get_calcit(family, type);
3141 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003142 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Eric Dumazet2907c352011-05-25 07:34:04 +00003144 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003145 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003146 {
3147 struct netlink_dump_control c = {
3148 .dump = dumpit,
3149 .min_dump_alloc = min_dump_alloc,
3150 };
3151 err = netlink_dump_start(rtnl, skb, nlh, &c);
3152 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003153 rtnl_lock();
3154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156
Thomas Grafe2849862007-03-22 11:48:11 -07003157 doit = rtnl_get_doit(family, type);
3158 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003159 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Thomas Graf661d2962013-03-21 07:45:29 +00003161 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003164static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003166 rtnl_lock();
3167 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3168 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3172{
Jiri Pirko351638e2013-05-28 01:30:21 +00003173 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 case NETDEV_UP:
3177 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003178 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003179 case NETDEV_POST_INIT:
3180 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003182 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003184 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003185 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003186 case NETDEV_RELEASE:
3187 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003188 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 break;
3190 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003191 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 break;
3193 }
3194 return NOTIFY_DONE;
3195}
3196
3197static struct notifier_block rtnetlink_dev_notifier = {
3198 .notifier_call = rtnetlink_event,
3199};
3200
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003201
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003202static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003203{
3204 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003205 struct netlink_kernel_cfg cfg = {
3206 .groups = RTNLGRP_MAX,
3207 .input = rtnetlink_rcv,
3208 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003209 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003210 };
3211
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003212 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003213 if (!sk)
3214 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003215 net->rtnl = sk;
3216 return 0;
3217}
3218
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003219static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003220{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003221 netlink_kernel_release(net->rtnl);
3222 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003223}
3224
3225static struct pernet_operations rtnetlink_net_ops = {
3226 .init = rtnetlink_net_init,
3227 .exit = rtnetlink_net_exit,
3228};
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230void __init rtnetlink_init(void)
3231{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003232 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003236
Greg Rosec7ac8672011-06-10 01:27:09 +00003237 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3238 rtnl_dump_ifinfo, rtnl_calcit);
3239 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3240 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3241 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003242
Greg Rosec7ac8672011-06-10 01:27:09 +00003243 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3244 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003245
3246 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3247 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3248 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003249
3250 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003251 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003252 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254