blob: 7c78b5aca944d56eb063716aec41944f862cdc02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/string.h>
26#include <linux/sockios.h>
27#include <linux/net.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/capability.h>
33#include <linux/skbuff.h>
34#include <linux/init.h>
35#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080036#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070037#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000038#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010039#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000040#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000041#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/inet.h>
46#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010047#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/ip.h>
49#include <net/protocol.h>
50#include <net/arp.h>
51#include <net/route.h>
52#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010053#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070056#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070057#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000058#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Eric Dumazete0d087a2009-11-07 01:26:17 -080060struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070061 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000063 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070064};
65
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68void rtnl_lock(void)
69{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Eric Dumazete0d087a2009-11-07 01:26:17 -080072EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void rtnl_unlock(void)
80{
Herbert Xu58ec3b42008-10-07 15:50:03 -070081 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 netdev_run_todo();
83}
Eric Dumazete0d087a2009-11-07 01:26:17 -080084EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086int rtnl_trylock(void)
87{
88 return mutex_trylock(&rtnl_mutex);
89}
Eric Dumazete0d087a2009-11-07 01:26:17 -080090EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080091
Patrick McHardyc9c10142008-04-23 22:10:48 -070092int rtnl_is_locked(void)
93{
94 return mutex_is_locked(&rtnl_mutex);
95}
Eric Dumazete0d087a2009-11-07 01:26:17 -080096EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070097
Paul E. McKenneya898def2010-02-22 17:04:49 -080098#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +080099bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800100{
101 return lockdep_is_held(&rtnl_mutex);
102}
103EXPORT_SYMBOL(lockdep_rtnl_is_held);
104#endif /* #ifdef CONFIG_PROVE_LOCKING */
105
Patrick McHardy25239ce2010-04-26 16:02:05 +0200106static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700107
108static inline int rtm_msgindex(int msgtype)
109{
110 int msgindex = msgtype - RTM_BASE;
111
112 /*
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
116 */
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118
119 return msgindex;
120}
121
122static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
123{
124 struct rtnl_link *tab;
125
Patrick McHardy25239ce2010-04-26 16:02:05 +0200126 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
130
Thomas Graf51057f22007-03-22 21:41:06 -0700131 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700132 tab = rtnl_msg_handlers[PF_UNSPEC];
133
Hans Zhangc80bbea2012-10-22 22:21:23 +0000134 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700135}
136
137static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
138{
139 struct rtnl_link *tab;
140
Patrick McHardy25239ce2010-04-26 16:02:05 +0200141 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
145
Thomas Graf51057f22007-03-22 21:41:06 -0700146 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700147 tab = rtnl_msg_handlers[PF_UNSPEC];
148
Hans Zhangc80bbea2012-10-22 22:21:23 +0000149 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700150}
151
Greg Rosec7ac8672011-06-10 01:27:09 +0000152static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
153{
154 struct rtnl_link *tab;
155
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
160
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
163
Hans Zhangc80bbea2012-10-22 22:21:23 +0000164 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000165}
166
Thomas Grafe2849862007-03-22 11:48:11 -0700167/**
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000173 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700174 *
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
178 *
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
182 *
183 * Returns 0 on success or a negative error code.
184 */
185int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700188{
189 struct rtnl_link *tab;
190 int msgindex;
191
Patrick McHardy25239ce2010-04-26 16:02:05 +0200192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700193 msgindex = rtm_msgindex(msgtype);
194
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
200
201 rtnl_msg_handlers[protocol] = tab;
202 }
203
204 if (doit)
205 tab[msgindex].doit = doit;
206
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
209
Greg Rosec7ac8672011-06-10 01:27:09 +0000210 if (calcit)
211 tab[msgindex].calcit = calcit;
212
Thomas Grafe2849862007-03-22 11:48:11 -0700213 return 0;
214}
Thomas Grafe2849862007-03-22 11:48:11 -0700215EXPORT_SYMBOL_GPL(__rtnl_register);
216
217/**
218 * rtnl_register - Register a rtnetlink message type
219 *
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700225 */
226void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700229{
Greg Rosec7ac8672011-06-10 01:27:09 +0000230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
234}
Thomas Grafe2849862007-03-22 11:48:11 -0700235EXPORT_SYMBOL_GPL(rtnl_register);
236
237/**
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
241 *
242 * Returns 0 on success or a negative error code.
243 */
244int rtnl_unregister(int protocol, int msgtype)
245{
246 int msgindex;
247
Patrick McHardy25239ce2010-04-26 16:02:05 +0200248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700249 msgindex = rtm_msgindex(msgtype);
250
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
253
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256
257 return 0;
258}
Thomas Grafe2849862007-03-22 11:48:11 -0700259EXPORT_SYMBOL_GPL(rtnl_unregister);
260
261/**
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
264 *
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
267 */
268void rtnl_unregister_all(int protocol)
269{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700271
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
274}
Thomas Grafe2849862007-03-22 11:48:11 -0700275EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick McHardy38f7b872007-06-13 12:03:51 -0700277static LIST_HEAD(link_ops);
278
Eric Dumazetc63044f2011-12-13 11:38:00 +0000279static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
280{
281 const struct rtnl_link_ops *ops;
282
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
286 }
287 return NULL;
288}
289
Patrick McHardy38f7b872007-06-13 12:03:51 -0700290/**
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
293 *
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
297 *
298 * Returns 0 on success or a negative error code.
299 */
300int __rtnl_link_register(struct rtnl_link_ops *ops)
301{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
304
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
309 */
310 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000311 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700312
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313 list_add_tail(&ops->list, &link_ops);
314 return 0;
315}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700316EXPORT_SYMBOL_GPL(__rtnl_link_register);
317
318/**
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
321 *
322 * Returns 0 on success or a negative error code.
323 */
324int rtnl_link_register(struct rtnl_link_ops *ops)
325{
326 int err;
327
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
332}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700333EXPORT_SYMBOL_GPL(rtnl_link_register);
334
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
336{
337 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 LIST_HEAD(list_kill);
339
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700343 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000344 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700345}
346
Patrick McHardy38f7b872007-06-13 12:03:51 -0700347/**
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
350 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700351 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352 */
353void __rtnl_link_unregister(struct rtnl_link_ops *ops)
354{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700355 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356
Eric W. Biederman881d9662007-09-17 11:56:21 -0700357 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700358 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700359 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700360 list_del(&ops->list);
361}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700362EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
363
Cong Wang200b9162014-05-12 15:11:20 -0700364/* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
366 */
367static void rtnl_lock_unregistering_all(void)
368{
369 struct net *net;
370 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700372
Peter Zijlstraff960a72014-10-29 17:04:56 +0100373 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700374 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
381 }
382 }
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100386
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700388 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100389 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700390}
391
Patrick McHardy38f7b872007-06-13 12:03:51 -0700392/**
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
395 */
396void rtnl_link_unregister(struct rtnl_link_ops *ops)
397{
Cong Wang200b9162014-05-12 15:11:20 -0700398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700403 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405EXPORT_SYMBOL_GPL(rtnl_link_unregister);
406
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100407static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
408{
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
411
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900416 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
421}
422
Patrick McHardy38f7b872007-06-13 12:03:51 -0700423static size_t rtnl_link_get_size(const struct net_device *dev)
424{
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
427
428 if (!ops)
429 return 0;
430
Thomas Graf369cf772010-11-11 15:47:59 +0000431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700433
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000436 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700437 ops->get_size(dev);
438
439 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100443 size += rtnl_link_get_slave_info_data_size(dev);
444
Patrick McHardy38f7b872007-06-13 12:03:51 -0700445 return size;
446}
447
Thomas Graff8ff1822010-11-16 04:30:14 +0000448static LIST_HEAD(rtnl_af_ops);
449
450static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
451{
452 const struct rtnl_af_ops *ops;
453
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
457 }
458
459 return NULL;
460}
461
462/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
465 *
466 * Returns 0 on success or a negative error code.
467 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800468void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000469{
Thomas Graff8ff1822010-11-16 04:30:14 +0000470 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800471 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000473}
474EXPORT_SYMBOL_GPL(rtnl_af_register);
475
476/**
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
479 *
480 * The caller must hold the rtnl_mutex.
481 */
482void __rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 list_del(&ops->list);
485}
486EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
487
488/**
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
491 */
492void rtnl_af_unregister(struct rtnl_af_ops *ops)
493{
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
497}
498EXPORT_SYMBOL_GPL(rtnl_af_unregister);
499
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700500static size_t rtnl_link_get_af_size(const struct net_device *dev,
501 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000502{
503 struct rtnl_af_ops *af_ops;
504 size_t size;
505
506 /* IFLA_AF_SPEC */
507 size = nla_total_size(sizeof(struct nlattr));
508
509 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
510 if (af_ops->get_link_af_size) {
511 /* AF_* + nested data */
512 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700513 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000514 }
515 }
516
517 return size;
518}
519
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100520static bool rtnl_have_link_slave_info(const struct net_device *dev)
521{
522 struct net_device *master_dev;
523
524 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100525 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100526 return true;
527 return false;
528}
529
530static int rtnl_link_slave_info_fill(struct sk_buff *skb,
531 const struct net_device *dev)
532{
533 struct net_device *master_dev;
534 const struct rtnl_link_ops *ops;
535 struct nlattr *slave_data;
536 int err;
537
538 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
539 if (!master_dev)
540 return 0;
541 ops = master_dev->rtnl_link_ops;
542 if (!ops)
543 return 0;
544 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
545 return -EMSGSIZE;
546 if (ops->fill_slave_info) {
547 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
548 if (!slave_data)
549 return -EMSGSIZE;
550 err = ops->fill_slave_info(skb, master_dev, dev);
551 if (err < 0)
552 goto err_cancel_slave_data;
553 nla_nest_end(skb, slave_data);
554 }
555 return 0;
556
557err_cancel_slave_data:
558 nla_nest_cancel(skb, slave_data);
559 return err;
560}
561
562static int rtnl_link_info_fill(struct sk_buff *skb,
563 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700564{
565 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100566 struct nlattr *data;
567 int err;
568
569 if (!ops)
570 return 0;
571 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
572 return -EMSGSIZE;
573 if (ops->fill_xstats) {
574 err = ops->fill_xstats(skb, dev);
575 if (err < 0)
576 return err;
577 }
578 if (ops->fill_info) {
579 data = nla_nest_start(skb, IFLA_INFO_DATA);
580 if (data == NULL)
581 return -EMSGSIZE;
582 err = ops->fill_info(skb, dev);
583 if (err < 0)
584 goto err_cancel_data;
585 nla_nest_end(skb, data);
586 }
587 return 0;
588
589err_cancel_data:
590 nla_nest_cancel(skb, data);
591 return err;
592}
593
594static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
595{
596 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700597 int err = -EMSGSIZE;
598
599 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
600 if (linkinfo == NULL)
601 goto out;
602
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100603 err = rtnl_link_info_fill(skb, dev);
604 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700605 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100606
607 err = rtnl_link_slave_info_fill(skb, dev);
608 if (err < 0)
609 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700610
611 nla_nest_end(skb, linkinfo);
612 return 0;
613
Patrick McHardy38f7b872007-06-13 12:03:51 -0700614err_cancel_link:
615 nla_nest_cancel(skb, linkinfo);
616out:
617 return err;
618}
619
Eric Dumazet95c96172012-04-15 05:58:06 +0000620int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800622 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int err = 0;
624
Patrick McHardyac6d4392005-08-14 19:29:52 -0700625 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (echo)
627 atomic_inc(&skb->users);
628 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
629 if (echo)
630 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
631 return err;
632}
633
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800634int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700635{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800636 struct sock *rtnl = net->rtnl;
637
Thomas Graf2942e902006-08-15 00:30:25 -0700638 return nlmsg_unicast(rtnl, skb, pid);
639}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800640EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700641
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800642void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
643 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700644{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800645 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700646 int report = 0;
647
648 if (nlh)
649 report = nlmsg_report(nlh);
650
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800651 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700652}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800653EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700654
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800655void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700656{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800657 struct sock *rtnl = net->rtnl;
658
Thomas Graf97676b62006-08-15 00:31:41 -0700659 netlink_set_err(rtnl, 0, group, error);
660}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800661EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
664{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700665 struct nlattr *mx;
666 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Thomas Graf2d7202b2006-08-22 00:01:27 -0700668 mx = nla_nest_start(skb, RTA_METRICS);
669 if (mx == NULL)
670 return -ENOBUFS;
671
672 for (i = 0; i < RTAX_MAX; i++) {
673 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100674 if (i == RTAX_CC_ALGO - 1) {
675 char tmp[TCP_CA_NAME_MAX], *name;
676
677 name = tcp_ca_get_name_by_key(metrics[i], tmp);
678 if (!name)
679 continue;
680 if (nla_put_string(skb, i + 1, name))
681 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200682 } else if (i == RTAX_FEATURES - 1) {
683 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
684
685 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 if (nla_put_u32(skb, i + 1, user_features))
687 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100688 } else {
689 if (nla_put_u32(skb, i + 1, metrics[i]))
690 goto nla_put_failure;
691 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700692 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David S. Millera57d27f2006-08-22 22:20:14 -0700696 if (!valid) {
697 nla_nest_cancel(skb, mx);
698 return 0;
699 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700700
701 return nla_nest_end(skb, mx);
702
703nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700704 nla_nest_cancel(skb, mx);
705 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800707EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Thomas Grafe3703b32006-11-27 09:27:07 -0800709int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700710 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800711{
712 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000713 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800714 .rta_used = dst->__use,
715 .rta_clntref = atomic_read(&(dst->__refcnt)),
716 .rta_error = error,
717 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800718 };
719
Li Wei82539472012-07-29 16:01:30 +0000720 if (expires) {
721 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800722
Li Wei82539472012-07-29 16:01:30 +0000723 clock = jiffies_to_clock_t(abs(expires));
724 clock = min_t(unsigned long, clock, INT_MAX);
725 ci.rta_expires = (expires > 0) ? clock : -clock;
726 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800727 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
728}
Thomas Grafe3703b32006-11-27 09:27:07 -0800729EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
David S. Miller93b2d4a2008-02-17 18:35:07 -0800731static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800732{
733 unsigned char operstate = dev->operstate;
734
Eric Dumazete0d087a2009-11-07 01:26:17 -0800735 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800736 case IF_OPER_UP:
737 if ((operstate == IF_OPER_DORMANT ||
738 operstate == IF_OPER_UNKNOWN) &&
739 !netif_dormant(dev))
740 operstate = IF_OPER_UP;
741 break;
742
743 case IF_OPER_DORMANT:
744 if (operstate == IF_OPER_UP ||
745 operstate == IF_OPER_UNKNOWN)
746 operstate = IF_OPER_DORMANT;
747 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700748 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800749
750 if (dev->operstate != operstate) {
751 write_lock_bh(&dev_base_lock);
752 dev->operstate = operstate;
753 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800754 netdev_state_change(dev);
755 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800756}
757
Jiri Bencb1beb682012-07-27 02:58:22 +0000758static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
759{
760 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
762}
763
Patrick McHardy3729d502010-02-26 06:34:54 +0000764static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 const struct ifinfomsg *ifm)
766{
767 unsigned int flags = ifm->ifi_flags;
768
769 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 if (ifm->ifi_change)
771 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000772 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000773
774 return flags;
775}
776
Thomas Grafb60c5112006-08-04 23:05:34 -0700777static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000778 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Thomas Grafb60c5112006-08-04 23:05:34 -0700780 a->rx_packets = b->rx_packets;
781 a->tx_packets = b->tx_packets;
782 a->rx_bytes = b->rx_bytes;
783 a->tx_bytes = b->tx_bytes;
784 a->rx_errors = b->rx_errors;
785 a->tx_errors = b->tx_errors;
786 a->rx_dropped = b->rx_dropped;
787 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Grafb60c5112006-08-04 23:05:34 -0700789 a->multicast = b->multicast;
790 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Grafb60c5112006-08-04 23:05:34 -0700792 a->rx_length_errors = b->rx_length_errors;
793 a->rx_over_errors = b->rx_over_errors;
794 a->rx_crc_errors = b->rx_crc_errors;
795 a->rx_frame_errors = b->rx_frame_errors;
796 a->rx_fifo_errors = b->rx_fifo_errors;
797 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Thomas Grafb60c5112006-08-04 23:05:34 -0700799 a->tx_aborted_errors = b->tx_aborted_errors;
800 a->tx_carrier_errors = b->tx_carrier_errors;
801 a->tx_fifo_errors = b->tx_fifo_errors;
802 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Thomas Grafb60c5112006-08-04 23:05:34 -0700805 a->rx_compressed = b->rx_compressed;
806 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000807}
808
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000809static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000810{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000811 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000812}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Chris Wrightc02db8c2010-05-16 01:05:45 -0700814/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500815static inline int rtnl_vfinfo_size(const struct net_device *dev,
816 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000817{
Greg Rose115c9b82012-02-21 16:54:48 -0500818 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
819 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700820 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700821 size_t size = nla_total_size(sizeof(struct nlattr));
822 size += nla_total_size(num_vfs * sizeof(struct nlattr));
823 size += num_vfs *
824 (nla_total_size(sizeof(struct ifla_vf_mac)) +
825 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400826 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200827 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300828 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300829 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
830 /* IFLA_VF_STATS_RX_PACKETS */
831 nla_total_size(sizeof(__u64)) +
832 /* IFLA_VF_STATS_TX_PACKETS */
833 nla_total_size(sizeof(__u64)) +
834 /* IFLA_VF_STATS_RX_BYTES */
835 nla_total_size(sizeof(__u64)) +
836 /* IFLA_VF_STATS_TX_BYTES */
837 nla_total_size(sizeof(__u64)) +
838 /* IFLA_VF_STATS_BROADCAST */
839 nla_total_size(sizeof(__u64)) +
840 /* IFLA_VF_STATS_MULTICAST */
841 nla_total_size(sizeof(__u64)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700842 return size;
843 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000844 return 0;
845}
846
David Gibsonc53864f2014-04-24 10:22:36 +1000847static size_t rtnl_port_size(const struct net_device *dev,
848 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700849{
850 size_t port_size = nla_total_size(4) /* PORT_VF */
851 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
852 + nla_total_size(sizeof(struct ifla_port_vsi))
853 /* PORT_VSI_TYPE */
854 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
855 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
856 + nla_total_size(1) /* PROT_VDP_REQUEST */
857 + nla_total_size(2); /* PORT_VDP_RESPONSE */
858 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
859 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
860 + port_size;
861 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
862 + port_size;
863
David Gibsonc53864f2014-04-24 10:22:36 +1000864 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
865 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700866 return 0;
867 if (dev_num_vf(dev->dev.parent))
868 return port_self_size + vf_ports_size +
869 vf_port_size * dev_num_vf(dev->dev.parent);
870 else
871 return port_self_size;
872}
873
Greg Rose115c9b82012-02-21 16:54:48 -0500874static noinline size_t if_nlmsg_size(const struct net_device *dev,
875 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800876{
877 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
878 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700879 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800880 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
881 + nla_total_size(sizeof(struct rtnl_link_ifmap))
882 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700883 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800884 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
885 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
886 + nla_total_size(4) /* IFLA_TXQLEN */
887 + nla_total_size(4) /* IFLA_WEIGHT */
888 + nla_total_size(4) /* IFLA_MTU */
889 + nla_total_size(4) /* IFLA_LINK */
890 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000891 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000892 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000893 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
894 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800895 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700896 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700897 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100898 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500899 + nla_total_size(ext_filter_mask
900 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
901 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000902 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000903 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700904 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100905 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700906 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
907 + nla_total_size(1); /* IFLA_PROTO_DOWN */
908
Thomas Graf339bf982006-11-10 14:10:15 -0800909}
910
Scott Feldman57b61082010-05-17 22:49:55 -0700911static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
912{
913 struct nlattr *vf_ports;
914 struct nlattr *vf_port;
915 int vf;
916 int err;
917
918 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
919 if (!vf_ports)
920 return -EMSGSIZE;
921
922 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
923 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700924 if (!vf_port)
925 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400926 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
927 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700928 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700929 if (err == -EMSGSIZE)
930 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700931 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700932 nla_nest_cancel(skb, vf_port);
933 continue;
934 }
935 nla_nest_end(skb, vf_port);
936 }
937
938 nla_nest_end(skb, vf_ports);
939
940 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700941
942nla_put_failure:
943 nla_nest_cancel(skb, vf_ports);
944 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700945}
946
947static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
948{
949 struct nlattr *port_self;
950 int err;
951
952 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
953 if (!port_self)
954 return -EMSGSIZE;
955
956 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
957 if (err) {
958 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700959 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700960 }
961
962 nla_nest_end(skb, port_self);
963
964 return 0;
965}
966
David Gibsonc53864f2014-04-24 10:22:36 +1000967static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
968 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700969{
970 int err;
971
David Gibsonc53864f2014-04-24 10:22:36 +1000972 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
973 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700974 return 0;
975
976 err = rtnl_port_self_fill(skb, dev);
977 if (err)
978 return err;
979
980 if (dev_num_vf(dev->dev.parent)) {
981 err = rtnl_vf_ports_fill(skb, dev);
982 if (err)
983 return err;
984 }
985
986 return 0;
987}
988
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200989static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
990{
991 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100992 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200993
994 err = dev_get_phys_port_id(dev, &ppid);
995 if (err) {
996 if (err == -EOPNOTSUPP)
997 return 0;
998 return err;
999 }
1000
1001 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1002 return -EMSGSIZE;
1003
1004 return 0;
1005}
1006
David Aherndb24a902015-03-17 20:23:15 -06001007static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1008{
1009 char name[IFNAMSIZ];
1010 int err;
1011
1012 err = dev_get_phys_port_name(dev, name, sizeof(name));
1013 if (err) {
1014 if (err == -EOPNOTSUPP)
1015 return 0;
1016 return err;
1017 }
1018
1019 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1020 return -EMSGSIZE;
1021
1022 return 0;
1023}
1024
Jiri Pirko82f28412014-11-28 14:34:18 +01001025static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1026{
1027 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001028 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001029 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001030 .flags = SWITCHDEV_F_NO_RECURSE,
1031 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001032
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001033 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001034 if (err) {
1035 if (err == -EOPNOTSUPP)
1036 return 0;
1037 return err;
1038 }
1039
Scott Feldman42275bd2015-05-13 11:16:50 -07001040 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1041 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001042 return -EMSGSIZE;
1043
1044 return 0;
1045}
1046
Thomas Grafb60c5112006-08-04 23:05:34 -07001047static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001048 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001049 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001050{
1051 struct ifinfomsg *ifm;
1052 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001053 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001054 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001055 struct nlattr *attr, *af_spec;
1056 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001057 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001058
Eric Dumazet2907c352011-05-25 07:34:04 +00001059 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001060 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1061 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001062 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001063
1064 ifm = nlmsg_data(nlh);
1065 ifm->ifi_family = AF_UNSPEC;
1066 ifm->__ifi_pad = 0;
1067 ifm->ifi_type = dev->type;
1068 ifm->ifi_index = dev->ifindex;
1069 ifm->ifi_flags = dev_get_flags(dev);
1070 ifm->ifi_change = change;
1071
David S. Millera6574342012-04-01 20:12:00 -04001072 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1073 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1074 nla_put_u8(skb, IFLA_OPERSTATE,
1075 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1076 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1077 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1078 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001079 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001080 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001081#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001082 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001083#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001084 (dev->ifindex != dev_get_iflink(dev) &&
1085 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001086 (upper_dev &&
1087 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001088 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001089 (dev->qdisc &&
1090 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1091 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001092 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1093 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001094 atomic_read(&dev->carrier_changes)) ||
1095 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001096 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001097
Stefan Rompfb00055a2006-03-20 17:09:11 -08001098 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct rtnl_link_ifmap map = {
1100 .mem_start = dev->mem_start,
1101 .mem_end = dev->mem_end,
1102 .base_addr = dev->base_addr,
1103 .irq = dev->irq,
1104 .dma = dev->dma,
1105 .port = dev->if_port,
1106 };
David S. Millera6574342012-04-01 20:12:00 -04001107 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1108 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
1111 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001112 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1113 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1114 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001117 if (rtnl_phys_port_id_fill(skb, dev))
1118 goto nla_put_failure;
1119
David Aherndb24a902015-03-17 20:23:15 -06001120 if (rtnl_phys_port_name_fill(skb, dev))
1121 goto nla_put_failure;
1122
Jiri Pirko82f28412014-11-28 14:34:18 +01001123 if (rtnl_phys_switch_id_fill(skb, dev))
1124 goto nla_put_failure;
1125
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001126 attr = nla_reserve(skb, IFLA_STATS,
1127 sizeof(struct rtnl_link_stats));
1128 if (attr == NULL)
1129 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Eric Dumazet28172732010-07-07 14:58:56 -07001131 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001132 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Jan Engelhardt10708f32010-03-11 09:57:29 +00001134 attr = nla_reserve(skb, IFLA_STATS64,
1135 sizeof(struct rtnl_link_stats64));
1136 if (attr == NULL)
1137 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001138 copy_rtnl_link_stats64(nla_data(attr), stats);
1139
David S. Millera6574342012-04-01 20:12:00 -04001140 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1141 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1142 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001143
Greg Rose115c9b82012-02-21 16:54:48 -05001144 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1145 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001146 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001147
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001148 struct nlattr *vfinfo, *vf, *vfstats;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001149 int num_vfs = dev_num_vf(dev->dev.parent);
1150
Chris Wrightc02db8c2010-05-16 01:05:45 -07001151 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1152 if (!vfinfo)
1153 goto nla_put_failure;
1154 for (i = 0; i < num_vfs; i++) {
1155 struct ifla_vf_info ivi;
1156 struct ifla_vf_mac vf_mac;
1157 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001158 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001159 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001160 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001161 struct ifla_vf_link_state vf_linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001162 struct ifla_vf_rss_query_en vf_rss_query_en;
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001163 struct ifla_vf_stats vf_stats;
Greg Rose5f8444a2011-10-08 03:05:24 +00001164
1165 /*
1166 * Not all SR-IOV capable drivers support the
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001167 * spoofcheck and "RSS query enable" query. Preset to
1168 * -1 so the user space tool can detect that the driver
1169 * didn't report anything.
Greg Rose5f8444a2011-10-08 03:05:24 +00001170 */
1171 ivi.spoofchk = -1;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001172 ivi.rss_query_en = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001173 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001174 /* The default value for VF link state is "auto"
1175 * IFLA_VF_LINK_STATE_AUTO which equals zero
1176 */
1177 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001178 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1179 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001180 vf_mac.vf =
1181 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001182 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001183 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001184 vf_spoofchk.vf =
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001185 vf_linkstate.vf =
1186 vf_rss_query_en.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001187
Chris Wrightc02db8c2010-05-16 01:05:45 -07001188 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1189 vf_vlan.vlan = ivi.vlan;
1190 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001191 vf_tx_rate.rate = ivi.max_tx_rate;
1192 vf_rate.min_tx_rate = ivi.min_tx_rate;
1193 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001194 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001195 vf_linkstate.link_state = ivi.linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001196 vf_rss_query_en.setting = ivi.rss_query_en;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001197 vf = nla_nest_start(skb, IFLA_VF_INFO);
1198 if (!vf) {
1199 nla_nest_cancel(skb, vfinfo);
1200 goto nla_put_failure;
1201 }
David S. Millera6574342012-04-01 20:12:00 -04001202 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1203 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001204 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1205 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001206 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1207 &vf_tx_rate) ||
1208 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001209 &vf_spoofchk) ||
1210 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001211 &vf_linkstate) ||
1212 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1213 sizeof(vf_rss_query_en),
1214 &vf_rss_query_en))
David S. Millera6574342012-04-01 20:12:00 -04001215 goto nla_put_failure;
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001216 memset(&vf_stats, 0, sizeof(vf_stats));
1217 if (dev->netdev_ops->ndo_get_vf_stats)
1218 dev->netdev_ops->ndo_get_vf_stats(dev, i,
1219 &vf_stats);
1220 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1221 if (!vfstats) {
1222 nla_nest_cancel(skb, vf);
1223 nla_nest_cancel(skb, vfinfo);
1224 goto nla_put_failure;
1225 }
1226 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1227 vf_stats.rx_packets) ||
1228 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1229 vf_stats.tx_packets) ||
1230 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1231 vf_stats.rx_bytes) ||
1232 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1233 vf_stats.tx_bytes) ||
1234 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1235 vf_stats.broadcast) ||
1236 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1237 vf_stats.multicast))
1238 goto nla_put_failure;
1239 nla_nest_end(skb, vfstats);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001240 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001241 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001242 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001243 }
Scott Feldman57b61082010-05-17 22:49:55 -07001244
David Gibsonc53864f2014-04-24 10:22:36 +10001245 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001246 goto nla_put_failure;
1247
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001248 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001249 if (rtnl_link_fill(skb, dev) < 0)
1250 goto nla_put_failure;
1251 }
1252
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001253 if (dev->rtnl_link_ops &&
1254 dev->rtnl_link_ops->get_link_net) {
1255 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1256
1257 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001258 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001259
1260 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1261 goto nla_put_failure;
1262 }
1263 }
1264
Thomas Graff8ff1822010-11-16 04:30:14 +00001265 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1266 goto nla_put_failure;
1267
1268 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1269 if (af_ops->fill_link_af) {
1270 struct nlattr *af;
1271 int err;
1272
1273 if (!(af = nla_nest_start(skb, af_ops->family)))
1274 goto nla_put_failure;
1275
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04001276 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +00001277
1278 /*
1279 * Caller may return ENODATA to indicate that there
1280 * was no data to be dumped. This is not an error, it
1281 * means we should trim the attribute header and
1282 * continue.
1283 */
1284 if (err == -ENODATA)
1285 nla_nest_cancel(skb, af);
1286 else if (err < 0)
1287 goto nla_put_failure;
1288
1289 nla_nest_end(skb, af);
1290 }
1291 }
1292
1293 nla_nest_end(skb, af_spec);
1294
Johannes Berg053c0952015-01-16 22:09:00 +01001295 nlmsg_end(skb, nlh);
1296 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001297
1298nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001299 nlmsg_cancel(skb, nlh);
1300 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Jiri Pirkof7b12602014-02-18 20:53:18 +01001303static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1304 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1305 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1306 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1307 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1308 [IFLA_MTU] = { .type = NLA_U32 },
1309 [IFLA_LINK] = { .type = NLA_U32 },
1310 [IFLA_MASTER] = { .type = NLA_U32 },
1311 [IFLA_CARRIER] = { .type = NLA_U8 },
1312 [IFLA_TXQLEN] = { .type = NLA_U32 },
1313 [IFLA_WEIGHT] = { .type = NLA_U32 },
1314 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1315 [IFLA_LINKMODE] = { .type = NLA_U8 },
1316 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1317 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1318 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1319 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1320 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1321 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1322 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1323 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1324 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1325 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1326 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1327 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001328 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001329 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001330 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001331 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001332 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001333};
1334
1335static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1336 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1337 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1338 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1339 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1340};
1341
Jiri Pirkof7b12602014-02-18 20:53:18 +01001342static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001343 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1344 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1345 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1346 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1347 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1348 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001349 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001350 [IFLA_VF_STATS] = { .type = NLA_NESTED },
1351};
1352
1353static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = {
1354 [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 },
1355 [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 },
1356 [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 },
1357 [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 },
1358 [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 },
1359 [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001360};
1361
1362static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1363 [IFLA_PORT_VF] = { .type = NLA_U32 },
1364 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1365 .len = PORT_PROFILE_MAX },
1366 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1367 .len = sizeof(struct ifla_port_vsi)},
1368 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1369 .len = PORT_UUID_MAX },
1370 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1371 .len = PORT_UUID_MAX },
1372 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1373 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1374};
1375
Thomas Grafb60c5112006-08-04 23:05:34 -07001376static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001378 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001379 int h, s_h;
1380 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001382 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001383 struct nlattr *tb[IFLA_MAX+1];
1384 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001385 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001386 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001388 s_h = cb->args[0];
1389 s_idx = cb->args[1];
1390
Thomas Graf4e985ad2011-06-21 03:11:20 +00001391 cb->seq = net->dev_base_seq;
1392
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001393 /* A hack to preserve kernel<->userspace interface.
1394 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1395 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1396 * what iproute2 < v3.9.0 used.
1397 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1398 * attribute, its netlink message is shorter than struct ifinfomsg.
1399 */
1400 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1401 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1402
1403 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001404
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001405 if (tb[IFLA_EXT_MASK])
1406 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1407 }
Greg Rose115c9b82012-02-21 16:54:48 -05001408
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001409 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1410 idx = 0;
1411 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001412 hlist_for_each_entry(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001413 if (idx < s_idx)
1414 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001415 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1416 NETLINK_CB(cb->skb).portid,
1417 cb->nlh->nlmsg_seq, 0,
1418 NLM_F_MULTI,
1419 ext_filter_mask);
1420 /* If we ran out of room on the first message,
1421 * we're in trouble
1422 */
1423 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1424
David S. Miller7b46a642015-01-18 23:36:08 -05001425 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001426 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001427
1428 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001429cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001430 idx++;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001433out:
1434 cb->args[1] = idx;
1435 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 return skb->len;
1438}
1439
Jiri Pirkof7b12602014-02-18 20:53:18 +01001440int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1441{
1442 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1443}
1444EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001445
Eric W. Biederman81adee42009-11-08 00:53:51 -08001446struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1447{
1448 struct net *net;
1449 /* Examine the link attributes and figure out which
1450 * network namespace we are talking about.
1451 */
1452 if (tb[IFLA_NET_NS_PID])
1453 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001454 else if (tb[IFLA_NET_NS_FD])
1455 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001456 else
1457 net = get_net(src_net);
1458 return net;
1459}
1460EXPORT_SYMBOL(rtnl_link_get_net);
1461
Thomas Graf1840bb12008-02-23 19:54:36 -08001462static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1463{
1464 if (dev) {
1465 if (tb[IFLA_ADDRESS] &&
1466 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1467 return -EINVAL;
1468
1469 if (tb[IFLA_BROADCAST] &&
1470 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1471 return -EINVAL;
1472 }
1473
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001474 if (tb[IFLA_AF_SPEC]) {
1475 struct nlattr *af;
1476 int rem, err;
1477
1478 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1479 const struct rtnl_af_ops *af_ops;
1480
1481 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1482 return -EAFNOSUPPORT;
1483
1484 if (!af_ops->set_link_af)
1485 return -EOPNOTSUPP;
1486
1487 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001488 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001489 if (err < 0)
1490 return err;
1491 }
1492 }
1493 }
1494
Thomas Graf1840bb12008-02-23 19:54:36 -08001495 return 0;
1496}
1497
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001498static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001499{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001500 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001501 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001502
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001503 if (tb[IFLA_VF_MAC]) {
1504 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001505
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001506 err = -EOPNOTSUPP;
1507 if (ops->ndo_set_vf_mac)
1508 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1509 ivm->mac);
1510 if (err < 0)
1511 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001512 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001513
1514 if (tb[IFLA_VF_VLAN]) {
1515 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1516
1517 err = -EOPNOTSUPP;
1518 if (ops->ndo_set_vf_vlan)
1519 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1520 ivv->qos);
1521 if (err < 0)
1522 return err;
1523 }
1524
1525 if (tb[IFLA_VF_TX_RATE]) {
1526 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1527 struct ifla_vf_info ivf;
1528
1529 err = -EOPNOTSUPP;
1530 if (ops->ndo_get_vf_config)
1531 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1532 if (err < 0)
1533 return err;
1534
1535 err = -EOPNOTSUPP;
1536 if (ops->ndo_set_vf_rate)
1537 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1538 ivf.min_tx_rate,
1539 ivt->rate);
1540 if (err < 0)
1541 return err;
1542 }
1543
1544 if (tb[IFLA_VF_RATE]) {
1545 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1546
1547 err = -EOPNOTSUPP;
1548 if (ops->ndo_set_vf_rate)
1549 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1550 ivt->min_tx_rate,
1551 ivt->max_tx_rate);
1552 if (err < 0)
1553 return err;
1554 }
1555
1556 if (tb[IFLA_VF_SPOOFCHK]) {
1557 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1558
1559 err = -EOPNOTSUPP;
1560 if (ops->ndo_set_vf_spoofchk)
1561 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1562 ivs->setting);
1563 if (err < 0)
1564 return err;
1565 }
1566
1567 if (tb[IFLA_VF_LINK_STATE]) {
1568 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1569
1570 err = -EOPNOTSUPP;
1571 if (ops->ndo_set_vf_link_state)
1572 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1573 ivl->link_state);
1574 if (err < 0)
1575 return err;
1576 }
1577
1578 if (tb[IFLA_VF_RSS_QUERY_EN]) {
1579 struct ifla_vf_rss_query_en *ivrssq_en;
1580
1581 err = -EOPNOTSUPP;
1582 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1583 if (ops->ndo_set_vf_rss_query_en)
1584 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1585 ivrssq_en->setting);
1586 if (err < 0)
1587 return err;
1588 }
1589
Chris Wrightc02db8c2010-05-16 01:05:45 -07001590 return err;
1591}
1592
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001593static int do_set_master(struct net_device *dev, int ifindex)
1594{
Jiri Pirko898e5062013-01-03 22:48:52 +00001595 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001596 const struct net_device_ops *ops;
1597 int err;
1598
Jiri Pirko898e5062013-01-03 22:48:52 +00001599 if (upper_dev) {
1600 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001601 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001602 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001603 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001604 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001605 if (err)
1606 return err;
1607 } else {
1608 return -EOPNOTSUPP;
1609 }
1610 }
1611
1612 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001613 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1614 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001615 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001616 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001617 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001618 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001619 if (err)
1620 return err;
1621 } else {
1622 return -EOPNOTSUPP;
1623 }
1624 }
1625 return 0;
1626}
1627
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001628#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001629/* notify flag means notify + modified. */
1630#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001631static int do_setlink(const struct sk_buff *skb,
1632 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001633 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001634{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001635 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001636 int err;
1637
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001638 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001639 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001640 if (IS_ERR(net)) {
1641 err = PTR_ERR(net);
1642 goto errout;
1643 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001644 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001645 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001646 err = -EPERM;
1647 goto errout;
1648 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001649 err = dev_change_net_namespace(dev, net, ifname);
1650 put_net(net);
1651 if (err)
1652 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001653 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001654 }
1655
Patrick McHardy0157f602007-06-13 12:03:36 -07001656 if (tb[IFLA_MAP]) {
1657 struct rtnl_link_ifmap *u_map;
1658 struct ifmap k_map;
1659
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001660 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001661 err = -EOPNOTSUPP;
1662 goto errout;
1663 }
1664
1665 if (!netif_device_present(dev)) {
1666 err = -ENODEV;
1667 goto errout;
1668 }
1669
1670 u_map = nla_data(tb[IFLA_MAP]);
1671 k_map.mem_start = (unsigned long) u_map->mem_start;
1672 k_map.mem_end = (unsigned long) u_map->mem_end;
1673 k_map.base_addr = (unsigned short) u_map->base_addr;
1674 k_map.irq = (unsigned char) u_map->irq;
1675 k_map.dma = (unsigned char) u_map->dma;
1676 k_map.port = (unsigned char) u_map->port;
1677
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001678 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001679 if (err < 0)
1680 goto errout;
1681
Nicolas Dichtelba998902014-09-01 16:07:29 +02001682 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001683 }
1684
1685 if (tb[IFLA_ADDRESS]) {
1686 struct sockaddr *sa;
1687 int len;
1688
Patrick McHardy0157f602007-06-13 12:03:36 -07001689 len = sizeof(sa_family_t) + dev->addr_len;
1690 sa = kmalloc(len, GFP_KERNEL);
1691 if (!sa) {
1692 err = -ENOMEM;
1693 goto errout;
1694 }
1695 sa->sa_family = dev->type;
1696 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1697 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001698 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001699 kfree(sa);
1700 if (err)
1701 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001702 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001703 }
1704
1705 if (tb[IFLA_MTU]) {
1706 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1707 if (err < 0)
1708 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001709 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001710 }
1711
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001712 if (tb[IFLA_GROUP]) {
1713 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001714 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001715 }
1716
Patrick McHardy0157f602007-06-13 12:03:36 -07001717 /*
1718 * Interface selected by interface index but interface
1719 * name provided implies that a name change has been
1720 * requested.
1721 */
1722 if (ifm->ifi_index > 0 && ifname[0]) {
1723 err = dev_change_name(dev, ifname);
1724 if (err < 0)
1725 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001726 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001727 }
1728
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001729 if (tb[IFLA_IFALIAS]) {
1730 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1731 nla_len(tb[IFLA_IFALIAS]));
1732 if (err < 0)
1733 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001734 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001735 }
1736
Patrick McHardy0157f602007-06-13 12:03:36 -07001737 if (tb[IFLA_BROADCAST]) {
1738 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001739 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001740 }
1741
1742 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001743 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001744 if (err < 0)
1745 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001746 }
1747
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001748 if (tb[IFLA_MASTER]) {
1749 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1750 if (err)
1751 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001752 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001753 }
1754
Jiri Pirko9a572472012-12-27 23:49:39 +00001755 if (tb[IFLA_CARRIER]) {
1756 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1757 if (err)
1758 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001759 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001760 }
1761
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001762 if (tb[IFLA_TXQLEN]) {
1763 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1764
1765 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001766 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001767
1768 dev->tx_queue_len = value;
1769 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001770
Patrick McHardy0157f602007-06-13 12:03:36 -07001771 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001772 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001773
1774 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001775 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1776
David S. Miller93b2d4a2008-02-17 18:35:07 -08001777 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001778 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001779 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001780 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001781 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001782 }
1783
Chris Wrightc02db8c2010-05-16 01:05:45 -07001784 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001785 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07001786 struct nlattr *attr;
1787 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001788
Chris Wrightc02db8c2010-05-16 01:05:45 -07001789 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001790 if (nla_type(attr) != IFLA_VF_INFO ||
1791 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00001792 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001793 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001794 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001795 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1796 ifla_vf_policy);
1797 if (err < 0)
1798 goto errout;
1799 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001800 if (err < 0)
1801 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001802 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001803 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001804 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001805 err = 0;
1806
Scott Feldman57b61082010-05-17 22:49:55 -07001807 if (tb[IFLA_VF_PORTS]) {
1808 struct nlattr *port[IFLA_PORT_MAX+1];
1809 struct nlattr *attr;
1810 int vf;
1811 int rem;
1812
1813 err = -EOPNOTSUPP;
1814 if (!ops->ndo_set_vf_port)
1815 goto errout;
1816
1817 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02001818 if (nla_type(attr) != IFLA_VF_PORT ||
1819 nla_len(attr) < NLA_HDRLEN) {
1820 err = -EINVAL;
1821 goto errout;
1822 }
1823 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1824 ifla_port_policy);
Scott Feldman57b61082010-05-17 22:49:55 -07001825 if (err < 0)
1826 goto errout;
1827 if (!port[IFLA_PORT_VF]) {
1828 err = -EOPNOTSUPP;
1829 goto errout;
1830 }
1831 vf = nla_get_u32(port[IFLA_PORT_VF]);
1832 err = ops->ndo_set_vf_port(dev, vf, port);
1833 if (err < 0)
1834 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001835 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001836 }
1837 }
1838 err = 0;
1839
1840 if (tb[IFLA_PORT_SELF]) {
1841 struct nlattr *port[IFLA_PORT_MAX+1];
1842
1843 err = nla_parse_nested(port, IFLA_PORT_MAX,
1844 tb[IFLA_PORT_SELF], ifla_port_policy);
1845 if (err < 0)
1846 goto errout;
1847
1848 err = -EOPNOTSUPP;
1849 if (ops->ndo_set_vf_port)
1850 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1851 if (err < 0)
1852 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001853 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001854 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001855
1856 if (tb[IFLA_AF_SPEC]) {
1857 struct nlattr *af;
1858 int rem;
1859
1860 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1861 const struct rtnl_af_ops *af_ops;
1862
1863 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001864 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001865
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001866 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001867 if (err < 0)
1868 goto errout;
1869
Nicolas Dichtelba998902014-09-01 16:07:29 +02001870 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001871 }
1872 }
Scott Feldman57b61082010-05-17 22:49:55 -07001873 err = 0;
1874
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001875 if (tb[IFLA_PROTO_DOWN]) {
1876 err = dev_change_proto_down(dev,
1877 nla_get_u8(tb[IFLA_PROTO_DOWN]));
1878 if (err)
1879 goto errout;
1880 status |= DO_SETLINK_NOTIFY;
1881 }
1882
Patrick McHardy0157f602007-06-13 12:03:36 -07001883errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001884 if (status & DO_SETLINK_MODIFIED) {
1885 if (status & DO_SETLINK_NOTIFY)
1886 netdev_state_change(dev);
1887
1888 if (err < 0)
1889 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",
1890 dev->name);
1891 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001892
Patrick McHardy0157f602007-06-13 12:03:36 -07001893 return err;
1894}
1895
Thomas Graf661d2962013-03-21 07:45:29 +00001896static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001898 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001899 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001901 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001902 struct nlattr *tb[IFLA_MAX+1];
1903 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Thomas Grafda5e0492006-08-10 21:17:37 -07001905 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1906 if (err < 0)
1907 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Thomas Graf5176f912006-08-26 20:13:18 -07001909 if (tb[IFLA_IFNAME])
1910 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001911 else
1912 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001915 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001916 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001917 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001918 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001919 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001920 else
1921 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Thomas Grafda5e0492006-08-10 21:17:37 -07001923 if (dev == NULL) {
1924 err = -ENODEV;
1925 goto errout;
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Eric Dumazete0d087a2009-11-07 01:26:17 -08001928 err = validate_linkmsg(dev, tb);
1929 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001930 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001931
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001932 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001933errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return err;
1935}
1936
WANG Cong66400d52015-03-24 11:53:31 -07001937static int rtnl_group_dellink(const struct net *net, int group)
1938{
1939 struct net_device *dev, *aux;
1940 LIST_HEAD(list_kill);
1941 bool found = false;
1942
1943 if (!group)
1944 return -EPERM;
1945
1946 for_each_netdev(net, dev) {
1947 if (dev->group == group) {
1948 const struct rtnl_link_ops *ops;
1949
1950 found = true;
1951 ops = dev->rtnl_link_ops;
1952 if (!ops || !ops->dellink)
1953 return -EOPNOTSUPP;
1954 }
1955 }
1956
1957 if (!found)
1958 return -ENODEV;
1959
1960 for_each_netdev_safe(net, dev, aux) {
1961 if (dev->group == group) {
1962 const struct rtnl_link_ops *ops;
1963
1964 ops = dev->rtnl_link_ops;
1965 ops->dellink(dev, &list_kill);
1966 }
1967 }
1968 unregister_netdevice_many(&list_kill);
1969
1970 return 0;
1971}
1972
Thomas Graf614732e2015-07-21 10:44:06 +02001973int rtnl_delete_link(struct net_device *dev)
1974{
1975 const struct rtnl_link_ops *ops;
1976 LIST_HEAD(list_kill);
1977
1978 ops = dev->rtnl_link_ops;
1979 if (!ops || !ops->dellink)
1980 return -EOPNOTSUPP;
1981
1982 ops->dellink(dev, &list_kill);
1983 unregister_netdevice_many(&list_kill);
1984
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(rtnl_delete_link);
1988
Thomas Graf661d2962013-03-21 07:45:29 +00001989static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001990{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001991 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001992 struct net_device *dev;
1993 struct ifinfomsg *ifm;
1994 char ifname[IFNAMSIZ];
1995 struct nlattr *tb[IFLA_MAX+1];
1996 int err;
1997
1998 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1999 if (err < 0)
2000 return err;
2001
2002 if (tb[IFLA_IFNAME])
2003 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2004
2005 ifm = nlmsg_data(nlh);
2006 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002007 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002008 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002009 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002010 else if (tb[IFLA_GROUP])
2011 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002012 else
2013 return -EINVAL;
2014
2015 if (!dev)
2016 return -ENODEV;
2017
Thomas Graf614732e2015-07-21 10:44:06 +02002018 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002019}
2020
Patrick McHardy3729d502010-02-26 06:34:54 +00002021int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2022{
2023 unsigned int old_flags;
2024 int err;
2025
2026 old_flags = dev->flags;
2027 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2028 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2029 if (err < 0)
2030 return err;
2031 }
2032
2033 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002034
Nicolas Dichtela528c212013-09-25 12:02:44 +02002035 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002036 return 0;
2037}
2038EXPORT_SYMBOL(rtnl_configure_link);
2039
Rami Rosenc0713562012-11-30 01:08:47 +00002040struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002041 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002042 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002043{
2044 int err;
2045 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002046 unsigned int num_tx_queues = 1;
2047 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002048
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002049 if (tb[IFLA_NUM_TX_QUEUES])
2050 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2051 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002052 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002053
2054 if (tb[IFLA_NUM_RX_QUEUES])
2055 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2056 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002057 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002058
Pavel Emelianove7199282007-08-08 22:16:38 -07002059 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02002060 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002061 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002062 if (!dev)
2063 goto err;
2064
Eric W. Biederman81adee42009-11-08 00:53:51 -08002065 dev_net_set(dev, net);
2066 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002067 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002068
Pavel Emelianove7199282007-08-08 22:16:38 -07002069 if (tb[IFLA_MTU])
2070 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002071 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002072 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2073 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002074 dev->addr_assign_type = NET_ADDR_SET;
2075 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002076 if (tb[IFLA_BROADCAST])
2077 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2078 nla_len(tb[IFLA_BROADCAST]));
2079 if (tb[IFLA_TXQLEN])
2080 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2081 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002082 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002083 if (tb[IFLA_LINKMODE])
2084 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002085 if (tb[IFLA_GROUP])
2086 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002087
2088 return dev;
2089
Pavel Emelianove7199282007-08-08 22:16:38 -07002090err:
2091 return ERR_PTR(err);
2092}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002093EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002094
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002095static int rtnl_group_changelink(const struct sk_buff *skb,
2096 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002097 struct ifinfomsg *ifm,
2098 struct nlattr **tb)
2099{
WANG Congd0795352015-03-23 16:31:09 -07002100 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002101 int err;
2102
WANG Congd0795352015-03-23 16:31:09 -07002103 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002104 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002105 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002106 if (err < 0)
2107 return err;
2108 }
2109 }
2110
2111 return 0;
2112}
2113
Thomas Graf661d2962013-03-21 07:45:29 +00002114static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002115{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002116 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002117 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002118 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002119 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002120 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002121 struct ifinfomsg *ifm;
2122 char kind[MODULE_NAME_LEN];
2123 char ifname[IFNAMSIZ];
2124 struct nlattr *tb[IFLA_MAX+1];
2125 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002126 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002127 int err;
2128
Johannes Berg95a5afc2008-10-16 15:24:51 -07002129#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002130replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002131#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002132 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2133 if (err < 0)
2134 return err;
2135
2136 if (tb[IFLA_IFNAME])
2137 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2138 else
2139 ifname[0] = '\0';
2140
2141 ifm = nlmsg_data(nlh);
2142 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002143 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002144 else {
2145 if (ifname[0])
2146 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002147 else
2148 dev = NULL;
2149 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002150
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002151 if (dev) {
2152 master_dev = netdev_master_upper_dev_get(dev);
2153 if (master_dev)
2154 m_ops = master_dev->rtnl_link_ops;
2155 }
2156
Eric Dumazete0d087a2009-11-07 01:26:17 -08002157 err = validate_linkmsg(dev, tb);
2158 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002159 return err;
2160
Patrick McHardy38f7b872007-06-13 12:03:51 -07002161 if (tb[IFLA_LINKINFO]) {
2162 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2163 tb[IFLA_LINKINFO], ifla_info_policy);
2164 if (err < 0)
2165 return err;
2166 } else
2167 memset(linkinfo, 0, sizeof(linkinfo));
2168
2169 if (linkinfo[IFLA_INFO_KIND]) {
2170 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2171 ops = rtnl_link_ops_get(kind);
2172 } else {
2173 kind[0] = '\0';
2174 ops = NULL;
2175 }
2176
2177 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002178 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2179 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002180 struct nlattr **data = NULL;
2181 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002182 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002183
2184 if (ops) {
2185 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2186 err = nla_parse_nested(attr, ops->maxtype,
2187 linkinfo[IFLA_INFO_DATA],
2188 ops->policy);
2189 if (err < 0)
2190 return err;
2191 data = attr;
2192 }
2193 if (ops->validate) {
2194 err = ops->validate(tb, data);
2195 if (err < 0)
2196 return err;
2197 }
2198 }
2199
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002200 if (m_ops) {
2201 if (m_ops->slave_maxtype &&
2202 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2203 err = nla_parse_nested(slave_attr,
2204 m_ops->slave_maxtype,
2205 linkinfo[IFLA_INFO_SLAVE_DATA],
2206 m_ops->slave_policy);
2207 if (err < 0)
2208 return err;
2209 slave_data = slave_attr;
2210 }
2211 if (m_ops->slave_validate) {
2212 err = m_ops->slave_validate(tb, slave_data);
2213 if (err < 0)
2214 return err;
2215 }
2216 }
2217
Patrick McHardy38f7b872007-06-13 12:03:51 -07002218 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002219 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002220
2221 if (nlh->nlmsg_flags & NLM_F_EXCL)
2222 return -EEXIST;
2223 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2224 return -EOPNOTSUPP;
2225
2226 if (linkinfo[IFLA_INFO_DATA]) {
2227 if (!ops || ops != dev->rtnl_link_ops ||
2228 !ops->changelink)
2229 return -EOPNOTSUPP;
2230
2231 err = ops->changelink(dev, tb, data);
2232 if (err < 0)
2233 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002234 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002235 }
2236
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002237 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2238 if (!m_ops || !m_ops->slave_changelink)
2239 return -EOPNOTSUPP;
2240
2241 err = m_ops->slave_changelink(master_dev, dev,
2242 tb, slave_data);
2243 if (err < 0)
2244 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002245 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002246 }
2247
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002248 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002249 }
2250
Patrick McHardyffa934f2011-01-20 03:00:42 +00002251 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2252 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002253 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002254 nla_get_u32(tb[IFLA_GROUP]),
2255 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002256 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002257 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002258
Patrick McHardy0e068772007-07-11 19:42:31 -07002259 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002260 return -EOPNOTSUPP;
2261
2262 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002263#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002264 if (kind[0]) {
2265 __rtnl_unlock();
2266 request_module("rtnl-link-%s", kind);
2267 rtnl_lock();
2268 ops = rtnl_link_ops_get(kind);
2269 if (ops)
2270 goto replay;
2271 }
2272#endif
2273 return -EOPNOTSUPP;
2274 }
2275
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002276 if (!ops->setup)
2277 return -EOPNOTSUPP;
2278
Tom Gundersen55177502014-07-14 16:37:25 +02002279 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002280 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002281 name_assign_type = NET_NAME_ENUM;
2282 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002283
Eric W. Biederman81adee42009-11-08 00:53:51 -08002284 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002285 if (IS_ERR(dest_net))
2286 return PTR_ERR(dest_net);
2287
Eric W. Biederman505ce412015-02-26 16:19:00 -06002288 err = -EPERM;
2289 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2290 goto out;
2291
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002292 if (tb[IFLA_LINK_NETNSID]) {
2293 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2294
2295 link_net = get_net_ns_by_id(dest_net, id);
2296 if (!link_net) {
2297 err = -EINVAL;
2298 goto out;
2299 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002300 err = -EPERM;
2301 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2302 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002303 }
2304
2305 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2306 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002307 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002308 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002309 goto out;
2310 }
2311
2312 dev->ifindex = ifm->ifi_index;
2313
Cong Wang0e0eee22014-02-11 15:51:30 -08002314 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002315 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002316 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002317 * and unregister it on failure after registration
2318 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002319 */
Cong Wange51fb152014-06-03 16:40:47 -07002320 if (err < 0) {
2321 /* If device is not registered at all, free it now */
2322 if (dev->reg_state == NETREG_UNINITIALIZED)
2323 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002324 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002325 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002326 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002327 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002328 if (err < 0) {
2329 free_netdev(dev);
2330 goto out;
2331 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002332 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002333 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002334 if (err < 0)
2335 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002336 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002337 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002338 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002339 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002340 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002341out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002342 if (link_net)
2343 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002344 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002345 return err;
David S. Miller43638902015-03-10 21:58:32 -04002346out_unregister:
2347 if (ops->newlink) {
2348 LIST_HEAD(list_kill);
2349
2350 ops->dellink(dev, &list_kill);
2351 unregister_netdevice_many(&list_kill);
2352 } else {
2353 unregister_netdevice(dev);
2354 }
2355 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002356 }
2357}
2358
Thomas Graf661d2962013-03-21 07:45:29 +00002359static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002360{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002361 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002362 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002363 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002364 struct nlattr *tb[IFLA_MAX+1];
2365 struct net_device *dev = NULL;
2366 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002367 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002368 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002369
Thomas Grafb60c5112006-08-04 23:05:34 -07002370 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2371 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002372 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002373
Eric Dumazeta3d12892009-10-21 10:59:31 +00002374 if (tb[IFLA_IFNAME])
2375 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2376
Greg Rose115c9b82012-02-21 16:54:48 -05002377 if (tb[IFLA_EXT_MASK])
2378 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2379
Thomas Grafb60c5112006-08-04 23:05:34 -07002380 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002381 if (ifm->ifi_index > 0)
2382 dev = __dev_get_by_index(net, ifm->ifi_index);
2383 else if (tb[IFLA_IFNAME])
2384 dev = __dev_get_by_name(net, ifname);
2385 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002386 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002387
Eric Dumazeta3d12892009-10-21 10:59:31 +00002388 if (dev == NULL)
2389 return -ENODEV;
2390
Greg Rose115c9b82012-02-21 16:54:48 -05002391 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002392 if (nskb == NULL)
2393 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002394
Eric W. Biederman15e47302012-09-07 20:12:54 +00002395 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002396 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002397 if (err < 0) {
2398 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2399 WARN_ON(err == -EMSGSIZE);
2400 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002401 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002402 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002403
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002404 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002405}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002406
Greg Rose115c9b82012-02-21 16:54:48 -05002407static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002408{
Greg Rose115c9b82012-02-21 16:54:48 -05002409 struct net *net = sock_net(skb->sk);
2410 struct net_device *dev;
2411 struct nlattr *tb[IFLA_MAX+1];
2412 u32 ext_filter_mask = 0;
2413 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002414 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002415
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002416 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2417 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2418 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2419
2420 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002421 if (tb[IFLA_EXT_MASK])
2422 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2423 }
Greg Rose115c9b82012-02-21 16:54:48 -05002424
2425 if (!ext_filter_mask)
2426 return NLMSG_GOODSIZE;
2427 /*
2428 * traverse the list of net devices and compute the minimum
2429 * buffer size based upon the filter mask.
2430 */
2431 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2432 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2433 if_nlmsg_size(dev,
2434 ext_filter_mask));
2435 }
2436
Greg Rosec7ac8672011-06-10 01:27:09 +00002437 return min_ifinfo_dump_size;
2438}
2439
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002440static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
2442 int idx;
2443 int s_idx = cb->family;
2444
2445 if (s_idx == 0)
2446 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002447 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 int type = cb->nlh->nlmsg_type-RTM_BASE;
2449 if (idx < s_idx || idx == PF_PACKET)
2450 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002451 if (rtnl_msg_handlers[idx] == NULL ||
2452 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002454 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002456 cb->prev_seq = 0;
2457 cb->seq = 0;
2458 }
Thomas Grafe2849862007-03-22 11:48:11 -07002459 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 break;
2461 }
2462 cb->family = idx;
2463
2464 return skb->len;
2465}
2466
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002467struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2468 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002470 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002472 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002473 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002475 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002476 if (skb == NULL)
2477 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Greg Rose115c9b82012-02-21 16:54:48 -05002479 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002480 if (err < 0) {
2481 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2482 WARN_ON(err == -EMSGSIZE);
2483 kfree_skb(skb);
2484 goto errout;
2485 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002486 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002487errout:
2488 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002489 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002490 return NULL;
2491}
2492
2493void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2494{
2495 struct net *net = dev_net(dev);
2496
2497 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2498}
2499
2500void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2501 gfp_t flags)
2502{
2503 struct sk_buff *skb;
2504
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002505 if (dev->reg_state != NETREG_REGISTERED)
2506 return;
2507
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002508 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2509 if (skb)
2510 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002512EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
John Fastabendd83b0602012-04-15 06:44:08 +00002514static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2515 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002516 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002517 int type, unsigned int flags,
2518 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002519{
2520 struct nlmsghdr *nlh;
2521 struct ndmsg *ndm;
2522
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002523 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002524 if (!nlh)
2525 return -EMSGSIZE;
2526
2527 ndm = nlmsg_data(nlh);
2528 ndm->ndm_family = AF_BRIDGE;
2529 ndm->ndm_pad1 = 0;
2530 ndm->ndm_pad2 = 0;
2531 ndm->ndm_flags = flags;
2532 ndm->ndm_type = 0;
2533 ndm->ndm_ifindex = dev->ifindex;
2534 ndm->ndm_state = NUD_PERMANENT;
2535
2536 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2537 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002538 if (vid)
2539 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2540 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002541
Johannes Berg053c0952015-01-16 22:09:00 +01002542 nlmsg_end(skb, nlh);
2543 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002544
2545nla_put_failure:
2546 nlmsg_cancel(skb, nlh);
2547 return -EMSGSIZE;
2548}
2549
John Fastabend3ff661c2012-04-15 06:44:14 +00002550static inline size_t rtnl_fdb_nlmsg_size(void)
2551{
2552 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2553}
2554
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002555static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
John Fastabend3ff661c2012-04-15 06:44:14 +00002556{
2557 struct net *net = dev_net(dev);
2558 struct sk_buff *skb;
2559 int err = -ENOBUFS;
2560
2561 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2562 if (!skb)
2563 goto errout;
2564
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002565 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2566 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002567 if (err < 0) {
2568 kfree_skb(skb);
2569 goto errout;
2570 }
2571
2572 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2573 return;
2574errout:
2575 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2576}
2577
Vlad Yasevich090096b2013-03-06 15:39:42 +00002578/**
2579 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2580 */
2581int ndo_dflt_fdb_add(struct ndmsg *ndm,
2582 struct nlattr *tb[],
2583 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002584 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002585 u16 flags)
2586{
2587 int err = -EINVAL;
2588
2589 /* If aging addresses are supported device will need to
2590 * implement its own handler for this.
2591 */
2592 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2593 pr_info("%s: FDB only supports static addresses\n", dev->name);
2594 return err;
2595 }
2596
Or Gerlitz65891fe2014-12-14 18:19:05 +02002597 if (vid) {
2598 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2599 return err;
2600 }
2601
Vlad Yasevich090096b2013-03-06 15:39:42 +00002602 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2603 err = dev_uc_add_excl(dev, addr);
2604 else if (is_multicast_ether_addr(addr))
2605 err = dev_mc_add_excl(dev, addr);
2606
2607 /* Only return duplicate errors if NLM_F_EXCL is set */
2608 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2609 err = 0;
2610
2611 return err;
2612}
2613EXPORT_SYMBOL(ndo_dflt_fdb_add);
2614
Jiri Pirkof6f64242014-11-28 14:34:15 +01002615static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2616{
2617 u16 vid = 0;
2618
2619 if (vlan_attr) {
2620 if (nla_len(vlan_attr) != sizeof(u16)) {
2621 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2622 return -EINVAL;
2623 }
2624
2625 vid = nla_get_u16(vlan_attr);
2626
2627 if (!vid || vid >= VLAN_VID_MASK) {
2628 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2629 vid);
2630 return -EINVAL;
2631 }
2632 }
2633 *p_vid = vid;
2634 return 0;
2635}
2636
Thomas Graf661d2962013-03-21 07:45:29 +00002637static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002638{
2639 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002640 struct ndmsg *ndm;
2641 struct nlattr *tb[NDA_MAX+1];
2642 struct net_device *dev;
2643 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002644 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002645 int err;
2646
2647 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2648 if (err < 0)
2649 return err;
2650
2651 ndm = nlmsg_data(nlh);
2652 if (ndm->ndm_ifindex == 0) {
2653 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2654 return -EINVAL;
2655 }
2656
2657 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2658 if (dev == NULL) {
2659 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2660 return -ENODEV;
2661 }
2662
2663 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2664 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2665 return -EINVAL;
2666 }
2667
2668 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002669
Jiri Pirkof6f64242014-11-28 14:34:15 +01002670 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2671 if (err)
2672 return err;
2673
John Fastabend77162022012-04-15 06:43:56 +00002674 err = -EOPNOTSUPP;
2675
2676 /* Support fdb on master device the net/bridge default case */
2677 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2678 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002679 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2680 const struct net_device_ops *ops = br_dev->netdev_ops;
2681
Jiri Pirkof6f64242014-11-28 14:34:15 +01002682 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2683 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002684 if (err)
2685 goto out;
2686 else
2687 ndm->ndm_flags &= ~NTF_MASTER;
2688 }
2689
2690 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002691 if ((ndm->ndm_flags & NTF_SELF)) {
2692 if (dev->netdev_ops->ndo_fdb_add)
2693 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002694 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002695 nlh->nlmsg_flags);
2696 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002697 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002698 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002699
John Fastabend3ff661c2012-04-15 06:44:14 +00002700 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002701 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002702 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002703 }
John Fastabend77162022012-04-15 06:43:56 +00002704 }
2705out:
2706 return err;
2707}
2708
Vlad Yasevich090096b2013-03-06 15:39:42 +00002709/**
2710 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2711 */
2712int ndo_dflt_fdb_del(struct ndmsg *ndm,
2713 struct nlattr *tb[],
2714 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002715 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002716{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002717 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002718
2719 /* If aging addresses are supported device will need to
2720 * implement its own handler for this.
2721 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002722 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002723 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002724 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002725 }
2726
2727 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2728 err = dev_uc_del(dev, addr);
2729 else if (is_multicast_ether_addr(addr))
2730 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002731
2732 return err;
2733}
2734EXPORT_SYMBOL(ndo_dflt_fdb_del);
2735
Thomas Graf661d2962013-03-21 07:45:29 +00002736static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002737{
2738 struct net *net = sock_net(skb->sk);
2739 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002740 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002741 struct net_device *dev;
2742 int err = -EINVAL;
2743 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002744 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002745
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002746 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002747 return -EPERM;
2748
2749 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2750 if (err < 0)
2751 return err;
John Fastabend77162022012-04-15 06:43:56 +00002752
2753 ndm = nlmsg_data(nlh);
2754 if (ndm->ndm_ifindex == 0) {
2755 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2756 return -EINVAL;
2757 }
2758
2759 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2760 if (dev == NULL) {
2761 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2762 return -ENODEV;
2763 }
2764
Vlad Yasevich1690be62013-02-13 12:00:18 +00002765 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2766 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002767 return -EINVAL;
2768 }
2769
Vlad Yasevich1690be62013-02-13 12:00:18 +00002770 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002771
Jiri Pirkof6f64242014-11-28 14:34:15 +01002772 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2773 if (err)
2774 return err;
2775
John Fastabend77162022012-04-15 06:43:56 +00002776 err = -EOPNOTSUPP;
2777
2778 /* Support fdb on master device the net/bridge default case */
2779 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2780 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002781 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2782 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002783
Jiri Pirko898e5062013-01-03 22:48:52 +00002784 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002785 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002786
2787 if (err)
2788 goto out;
2789 else
2790 ndm->ndm_flags &= ~NTF_MASTER;
2791 }
2792
2793 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002794 if (ndm->ndm_flags & NTF_SELF) {
2795 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002796 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2797 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002798 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002799 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002800
John Fastabend3ff661c2012-04-15 06:44:14 +00002801 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002802 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002803 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002804 }
John Fastabend77162022012-04-15 06:43:56 +00002805 }
2806out:
2807 return err;
2808}
2809
John Fastabendd83b0602012-04-15 06:44:08 +00002810static int nlmsg_populate_fdb(struct sk_buff *skb,
2811 struct netlink_callback *cb,
2812 struct net_device *dev,
2813 int *idx,
2814 struct netdev_hw_addr_list *list)
2815{
2816 struct netdev_hw_addr *ha;
2817 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002818 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002819
Eric W. Biederman15e47302012-09-07 20:12:54 +00002820 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002821 seq = cb->nlh->nlmsg_seq;
2822
2823 list_for_each_entry(ha, &list->list, list) {
2824 if (*idx < cb->args[0])
2825 goto skip;
2826
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002827 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002828 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002829 RTM_NEWNEIGH, NTF_SELF,
2830 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002831 if (err < 0)
2832 return err;
2833skip:
2834 *idx += 1;
2835 }
2836 return 0;
2837}
2838
2839/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002840 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002841 * @nlh: netlink message header
2842 * @dev: netdevice
2843 *
2844 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002845 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002846 */
2847int ndo_dflt_fdb_dump(struct sk_buff *skb,
2848 struct netlink_callback *cb,
2849 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002850 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002851 int idx)
2852{
2853 int err;
2854
2855 netif_addr_lock_bh(dev);
2856 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2857 if (err)
2858 goto out;
2859 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2860out:
2861 netif_addr_unlock_bh(dev);
2862 return idx;
2863}
2864EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2865
John Fastabend77162022012-04-15 06:43:56 +00002866static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2867{
John Fastabend77162022012-04-15 06:43:56 +00002868 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002869 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002870 struct net_device *br_dev = NULL;
2871 const struct net_device_ops *ops = NULL;
2872 const struct net_device_ops *cops = NULL;
2873 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2874 struct net *net = sock_net(skb->sk);
2875 int brport_idx = 0;
2876 int br_idx = 0;
2877 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002878
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002879 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2880 ifla_policy) == 0) {
2881 if (tb[IFLA_MASTER])
2882 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2883 }
John Fastabend77162022012-04-15 06:43:56 +00002884
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002885 brport_idx = ifm->ifi_index;
2886
2887 if (br_idx) {
2888 br_dev = __dev_get_by_index(net, br_idx);
2889 if (!br_dev)
2890 return -ENODEV;
2891
2892 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002893 }
2894
2895 for_each_netdev(net, dev) {
2896 if (brport_idx && (dev->ifindex != brport_idx))
2897 continue;
2898
2899 if (!br_idx) { /* user did not specify a specific bridge */
2900 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2901 br_dev = netdev_master_upper_dev_get(dev);
2902 cops = br_dev->netdev_ops;
2903 }
2904
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002905 } else {
2906 if (dev != br_dev &&
2907 !(dev->priv_flags & IFF_BRIDGE_PORT))
2908 continue;
2909
2910 if (br_dev != netdev_master_upper_dev_get(dev) &&
2911 !(dev->priv_flags & IFF_EBRIDGE))
2912 continue;
2913
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002914 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002915 }
2916
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002917 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2918 if (cops && cops->ndo_fdb_dump)
2919 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2920 idx);
2921 }
2922
John Fastabend77162022012-04-15 06:43:56 +00002923 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002924 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002925 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002926 else
2927 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002928
2929 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002930 }
John Fastabend77162022012-04-15 06:43:56 +00002931
2932 cb->args[0] = idx;
2933 return skb->len;
2934}
2935
Scott Feldman2c3c0312014-11-28 14:34:25 +01002936static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2937 unsigned int attrnum, unsigned int flag)
2938{
2939 if (mask & flag)
2940 return nla_put_u8(skb, attrnum, !!(flags & flag));
2941 return 0;
2942}
2943
John Fastabend815cccb2012-10-24 08:13:09 +00002944int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002945 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002946 u32 flags, u32 mask, int nlflags,
2947 u32 filter_mask,
2948 int (*vlan_fill)(struct sk_buff *skb,
2949 struct net_device *dev,
2950 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00002951{
2952 struct nlmsghdr *nlh;
2953 struct ifinfomsg *ifm;
2954 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002955 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002956 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002957 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002958 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00002959
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002960 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00002961 if (nlh == NULL)
2962 return -EMSGSIZE;
2963
2964 ifm = nlmsg_data(nlh);
2965 ifm->ifi_family = AF_BRIDGE;
2966 ifm->__ifi_pad = 0;
2967 ifm->ifi_type = dev->type;
2968 ifm->ifi_index = dev->ifindex;
2969 ifm->ifi_flags = dev_get_flags(dev);
2970 ifm->ifi_change = 0;
2971
2972
2973 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2974 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2975 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002976 (br_dev &&
2977 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002978 (dev->addr_len &&
2979 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02002980 (dev->ifindex != dev_get_iflink(dev) &&
2981 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00002982 goto nla_put_failure;
2983
2984 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2985 if (!br_afspec)
2986 goto nla_put_failure;
2987
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002988 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002989 nla_nest_cancel(skb, br_afspec);
2990 goto nla_put_failure;
2991 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002992
2993 if (mode != BRIDGE_MODE_UNDEF) {
2994 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2995 nla_nest_cancel(skb, br_afspec);
2996 goto nla_put_failure;
2997 }
2998 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07002999 if (vlan_fill) {
3000 err = vlan_fill(skb, dev, filter_mask);
3001 if (err) {
3002 nla_nest_cancel(skb, br_afspec);
3003 goto nla_put_failure;
3004 }
3005 }
John Fastabend815cccb2012-10-24 08:13:09 +00003006 nla_nest_end(skb, br_afspec);
3007
Scott Feldman2c3c0312014-11-28 14:34:25 +01003008 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3009 if (!protinfo)
3010 goto nla_put_failure;
3011
3012 if (brport_nla_put_flag(skb, flags, mask,
3013 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3014 brport_nla_put_flag(skb, flags, mask,
3015 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3016 brport_nla_put_flag(skb, flags, mask,
3017 IFLA_BRPORT_FAST_LEAVE,
3018 BR_MULTICAST_FAST_LEAVE) ||
3019 brport_nla_put_flag(skb, flags, mask,
3020 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3021 brport_nla_put_flag(skb, flags, mask,
3022 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3023 brport_nla_put_flag(skb, flags, mask,
3024 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3025 brport_nla_put_flag(skb, flags, mask,
3026 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3027 brport_nla_put_flag(skb, flags, mask,
3028 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3029 nla_nest_cancel(skb, protinfo);
3030 goto nla_put_failure;
3031 }
3032
3033 nla_nest_end(skb, protinfo);
3034
Johannes Berg053c0952015-01-16 22:09:00 +01003035 nlmsg_end(skb, nlh);
3036 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003037nla_put_failure:
3038 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003039 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003040}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003041EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003042
John Fastabende5a55a82012-10-24 08:12:57 +00003043static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3044{
3045 struct net *net = sock_net(skb->sk);
3046 struct net_device *dev;
3047 int idx = 0;
3048 u32 portid = NETLINK_CB(cb->skb).portid;
3049 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003050 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003051 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003052
Thomas Grafaa68c202014-11-26 13:42:20 +01003053 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3054 struct nlattr *extfilt;
3055
3056 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3057 IFLA_EXT_MASK);
3058 if (extfilt) {
3059 if (nla_len(extfilt) < sizeof(filter_mask))
3060 return -EINVAL;
3061
3062 filter_mask = nla_get_u32(extfilt);
3063 }
3064 }
John Fastabende5a55a82012-10-24 08:12:57 +00003065
3066 rcu_read_lock();
3067 for_each_netdev_rcu(net, dev) {
3068 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003069 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003070
Jiri Pirko898e5062013-01-03 22:48:52 +00003071 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003072 if (idx >= cb->args[0]) {
3073 err = br_dev->netdev_ops->ndo_bridge_getlink(
3074 skb, portid, seq, dev,
3075 filter_mask, NLM_F_MULTI);
3076 if (err < 0 && err != -EOPNOTSUPP)
3077 break;
3078 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003079 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003080 }
3081
3082 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003083 if (idx >= cb->args[0]) {
3084 err = ops->ndo_bridge_getlink(skb, portid,
3085 seq, dev,
3086 filter_mask,
3087 NLM_F_MULTI);
3088 if (err < 0 && err != -EOPNOTSUPP)
3089 break;
3090 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003091 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003092 }
3093 }
3094 rcu_read_unlock();
3095 cb->args[0] = idx;
3096
3097 return skb->len;
3098}
3099
John Fastabend2469ffd2012-10-24 08:13:03 +00003100static inline size_t bridge_nlmsg_size(void)
3101{
3102 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3103 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3104 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3105 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3106 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3107 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3108 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3109 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3110 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3111 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3112 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3113}
3114
Roopa Prabhu02dba432015-01-14 20:02:25 -08003115static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003116{
3117 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003118 struct sk_buff *skb;
3119 int err = -EOPNOTSUPP;
3120
Roopa Prabhu02dba432015-01-14 20:02:25 -08003121 if (!dev->netdev_ops->ndo_bridge_getlink)
3122 return 0;
3123
John Fastabend2469ffd2012-10-24 08:13:03 +00003124 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3125 if (!skb) {
3126 err = -ENOMEM;
3127 goto errout;
3128 }
3129
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003130 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003131 if (err < 0)
3132 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003133
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003134 if (!skb->len)
3135 goto errout;
3136
John Fastabend2469ffd2012-10-24 08:13:03 +00003137 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3138 return 0;
3139errout:
3140 WARN_ON(err == -EMSGSIZE);
3141 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003142 if (err)
3143 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003144 return err;
3145}
3146
Thomas Graf661d2962013-03-21 07:45:29 +00003147static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003148{
3149 struct net *net = sock_net(skb->sk);
3150 struct ifinfomsg *ifm;
3151 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003152 struct nlattr *br_spec, *attr = NULL;
3153 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003154 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003155 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003156
3157 if (nlmsg_len(nlh) < sizeof(*ifm))
3158 return -EINVAL;
3159
3160 ifm = nlmsg_data(nlh);
3161 if (ifm->ifi_family != AF_BRIDGE)
3162 return -EPFNOSUPPORT;
3163
3164 dev = __dev_get_by_index(net, ifm->ifi_index);
3165 if (!dev) {
3166 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3167 return -ENODEV;
3168 }
3169
John Fastabend2469ffd2012-10-24 08:13:03 +00003170 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3171 if (br_spec) {
3172 nla_for_each_nested(attr, br_spec, rem) {
3173 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003174 if (nla_len(attr) < sizeof(flags))
3175 return -EINVAL;
3176
John Fastabendc38e01b2012-11-02 16:32:36 +00003177 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003178 flags = nla_get_u16(attr);
3179 break;
3180 }
3181 }
3182 }
3183
3184 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003185 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3186
3187 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003188 err = -EOPNOTSUPP;
3189 goto out;
3190 }
3191
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003192 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003193 if (err)
3194 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003195
3196 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003197 }
3198
John Fastabend2469ffd2012-10-24 08:13:03 +00003199 if ((flags & BRIDGE_FLAGS_SELF)) {
3200 if (!dev->netdev_ops->ndo_bridge_setlink)
3201 err = -EOPNOTSUPP;
3202 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003203 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3204 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003205 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003206 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003207
3208 /* Generate event to notify upper layer of bridge
3209 * change
3210 */
3211 err = rtnl_bridge_notify(dev);
3212 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003213 }
3214
John Fastabendc38e01b2012-11-02 16:32:36 +00003215 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003216 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003217out:
3218 return err;
3219}
3220
Thomas Graf661d2962013-03-21 07:45:29 +00003221static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003222{
3223 struct net *net = sock_net(skb->sk);
3224 struct ifinfomsg *ifm;
3225 struct net_device *dev;
3226 struct nlattr *br_spec, *attr = NULL;
3227 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003228 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003229 bool have_flags = false;
3230
3231 if (nlmsg_len(nlh) < sizeof(*ifm))
3232 return -EINVAL;
3233
3234 ifm = nlmsg_data(nlh);
3235 if (ifm->ifi_family != AF_BRIDGE)
3236 return -EPFNOSUPPORT;
3237
3238 dev = __dev_get_by_index(net, ifm->ifi_index);
3239 if (!dev) {
3240 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3241 return -ENODEV;
3242 }
3243
3244 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3245 if (br_spec) {
3246 nla_for_each_nested(attr, br_spec, rem) {
3247 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003248 if (nla_len(attr) < sizeof(flags))
3249 return -EINVAL;
3250
Vlad Yasevich407af322013-02-13 12:00:12 +00003251 have_flags = true;
3252 flags = nla_get_u16(attr);
3253 break;
3254 }
3255 }
3256 }
3257
Vlad Yasevich407af322013-02-13 12:00:12 +00003258 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3259 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3260
3261 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3262 err = -EOPNOTSUPP;
3263 goto out;
3264 }
3265
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003266 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003267 if (err)
3268 goto out;
3269
3270 flags &= ~BRIDGE_FLAGS_MASTER;
3271 }
3272
3273 if ((flags & BRIDGE_FLAGS_SELF)) {
3274 if (!dev->netdev_ops->ndo_bridge_dellink)
3275 err = -EOPNOTSUPP;
3276 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003277 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3278 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003279
Roopa Prabhu02dba432015-01-14 20:02:25 -08003280 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003281 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003282
3283 /* Generate event to notify upper layer of bridge
3284 * change
3285 */
3286 err = rtnl_bridge_notify(dev);
3287 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003288 }
3289
3290 if (have_flags)
3291 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003292out:
3293 return err;
3294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/* Process one rtnetlink message. */
3297
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003298static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003300 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003301 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 int family;
3304 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003305 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003309 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 type -= RTM_BASE;
3312
3313 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003314 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316
Hong zhi guo573ce262013-03-27 06:47:04 +00003317 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 sz_idx = type>>2;
3319 kind = type&3;
3320
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003321 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003322 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
David S. Millerb8f3ab42011-01-18 12:40:38 -08003324 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003325 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003326 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003327 rtnl_calcit_func calcit;
3328 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Thomas Grafe2849862007-03-22 11:48:11 -07003330 dumpit = rtnl_get_dumpit(family, type);
3331 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003332 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003333 calcit = rtnl_get_calcit(family, type);
3334 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003335 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Eric Dumazet2907c352011-05-25 07:34:04 +00003337 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003338 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003339 {
3340 struct netlink_dump_control c = {
3341 .dump = dumpit,
3342 .min_dump_alloc = min_dump_alloc,
3343 };
3344 err = netlink_dump_start(rtnl, skb, nlh, &c);
3345 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003346 rtnl_lock();
3347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
3349
Thomas Grafe2849862007-03-22 11:48:11 -07003350 doit = rtnl_get_doit(family, type);
3351 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003352 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
Thomas Graf661d2962013-03-21 07:45:29 +00003354 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003357static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003359 rtnl_lock();
3360 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3361 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3365{
Jiri Pirko351638e2013-05-28 01:30:21 +00003366 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 case NETDEV_UP:
3370 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003371 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003372 case NETDEV_POST_INIT:
3373 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003375 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003377 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003378 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003379 case NETDEV_RELEASE:
3380 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003381 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 break;
3383 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003384 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 break;
3386 }
3387 return NOTIFY_DONE;
3388}
3389
3390static struct notifier_block rtnetlink_dev_notifier = {
3391 .notifier_call = rtnetlink_event,
3392};
3393
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003394
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003395static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003396{
3397 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003398 struct netlink_kernel_cfg cfg = {
3399 .groups = RTNLGRP_MAX,
3400 .input = rtnetlink_rcv,
3401 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003402 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003403 };
3404
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003405 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003406 if (!sk)
3407 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003408 net->rtnl = sk;
3409 return 0;
3410}
3411
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003412static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003413{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003414 netlink_kernel_release(net->rtnl);
3415 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003416}
3417
3418static struct pernet_operations rtnetlink_net_ops = {
3419 .init = rtnetlink_net_init,
3420 .exit = rtnetlink_net_exit,
3421};
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423void __init rtnetlink_init(void)
3424{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003425 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003429
Greg Rosec7ac8672011-06-10 01:27:09 +00003430 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3431 rtnl_dump_ifinfo, rtnl_calcit);
3432 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3433 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3434 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003435
Greg Rosec7ac8672011-06-10 01:27:09 +00003436 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3437 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003438
3439 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3440 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3441 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003442
3443 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003444 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003445 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}