blob: 141ccc357e2e2071ee0dbc149cd56965db9d20c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/socket.h>
23#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/timer.h>
25#include <linux/string.h>
26#include <linux/sockios.h>
27#include <linux/net.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/capability.h>
33#include <linux/skbuff.h>
34#include <linux/init.h>
35#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080036#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070037#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000038#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010039#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000040#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000041#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <linux/inet.h>
46#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010047#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <net/ip.h>
49#include <net/protocol.h>
50#include <net/arp.h>
51#include <net/route.h>
52#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010053#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/sock.h>
55#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070056#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070057#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000058#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Eric Dumazete0d087a2009-11-07 01:26:17 -080060struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070061 rtnl_doit_func doit;
62 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000063 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070064};
65
Stephen Hemminger6756ae42006-03-20 22:23:58 -080066static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68void rtnl_lock(void)
69{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Eric Dumazete0d087a2009-11-07 01:26:17 -080072EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void rtnl_unlock(void)
80{
Herbert Xu58ec3b42008-10-07 15:50:03 -070081 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 netdev_run_todo();
83}
Eric Dumazete0d087a2009-11-07 01:26:17 -080084EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Stephen Hemminger6756ae42006-03-20 22:23:58 -080086int rtnl_trylock(void)
87{
88 return mutex_trylock(&rtnl_mutex);
89}
Eric Dumazete0d087a2009-11-07 01:26:17 -080090EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080091
Patrick McHardyc9c10142008-04-23 22:10:48 -070092int rtnl_is_locked(void)
93{
94 return mutex_is_locked(&rtnl_mutex);
95}
Eric Dumazete0d087a2009-11-07 01:26:17 -080096EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070097
Paul E. McKenneya898def2010-02-22 17:04:49 -080098#ifdef CONFIG_PROVE_LOCKING
99int lockdep_rtnl_is_held(void)
100{
101 return lockdep_is_held(&rtnl_mutex);
102}
103EXPORT_SYMBOL(lockdep_rtnl_is_held);
104#endif /* #ifdef CONFIG_PROVE_LOCKING */
105
Patrick McHardy25239ce2010-04-26 16:02:05 +0200106static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700107
108static inline int rtm_msgindex(int msgtype)
109{
110 int msgindex = msgtype - RTM_BASE;
111
112 /*
113 * msgindex < 0 implies someone tried to register a netlink
114 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 * the message type has not been added to linux/rtnetlink.h
116 */
117 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118
119 return msgindex;
120}
121
122static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
123{
124 struct rtnl_link *tab;
125
Patrick McHardy25239ce2010-04-26 16:02:05 +0200126 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000127 tab = rtnl_msg_handlers[protocol];
128 else
129 tab = NULL;
130
Thomas Graf51057f22007-03-22 21:41:06 -0700131 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700132 tab = rtnl_msg_handlers[PF_UNSPEC];
133
Hans Zhangc80bbea2012-10-22 22:21:23 +0000134 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700135}
136
137static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
138{
139 struct rtnl_link *tab;
140
Patrick McHardy25239ce2010-04-26 16:02:05 +0200141 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000142 tab = rtnl_msg_handlers[protocol];
143 else
144 tab = NULL;
145
Thomas Graf51057f22007-03-22 21:41:06 -0700146 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700147 tab = rtnl_msg_handlers[PF_UNSPEC];
148
Hans Zhangc80bbea2012-10-22 22:21:23 +0000149 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700150}
151
Greg Rosec7ac8672011-06-10 01:27:09 +0000152static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
153{
154 struct rtnl_link *tab;
155
156 if (protocol <= RTNL_FAMILY_MAX)
157 tab = rtnl_msg_handlers[protocol];
158 else
159 tab = NULL;
160
161 if (tab == NULL || tab[msgindex].calcit == NULL)
162 tab = rtnl_msg_handlers[PF_UNSPEC];
163
Hans Zhangc80bbea2012-10-22 22:21:23 +0000164 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000165}
166
Thomas Grafe2849862007-03-22 11:48:11 -0700167/**
168 * __rtnl_register - Register a rtnetlink message type
169 * @protocol: Protocol family or PF_UNSPEC
170 * @msgtype: rtnetlink message type
171 * @doit: Function pointer called for each request message
172 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000173 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700174 *
175 * Registers the specified function pointers (at least one of them has
176 * to be non-NULL) to be called whenever a request message for the
177 * specified protocol family and message type is received.
178 *
179 * The special protocol family PF_UNSPEC may be used to define fallback
180 * function pointers for the case when no entry for the specific protocol
181 * family exists.
182 *
183 * Returns 0 on success or a negative error code.
184 */
185int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000186 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700188{
189 struct rtnl_link *tab;
190 int msgindex;
191
Patrick McHardy25239ce2010-04-26 16:02:05 +0200192 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700193 msgindex = rtm_msgindex(msgtype);
194
195 tab = rtnl_msg_handlers[protocol];
196 if (tab == NULL) {
197 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 if (tab == NULL)
199 return -ENOBUFS;
200
201 rtnl_msg_handlers[protocol] = tab;
202 }
203
204 if (doit)
205 tab[msgindex].doit = doit;
206
207 if (dumpit)
208 tab[msgindex].dumpit = dumpit;
209
Greg Rosec7ac8672011-06-10 01:27:09 +0000210 if (calcit)
211 tab[msgindex].calcit = calcit;
212
Thomas Grafe2849862007-03-22 11:48:11 -0700213 return 0;
214}
Thomas Grafe2849862007-03-22 11:48:11 -0700215EXPORT_SYMBOL_GPL(__rtnl_register);
216
217/**
218 * rtnl_register - Register a rtnetlink message type
219 *
220 * Identical to __rtnl_register() but panics on failure. This is useful
221 * as failure of this function is very unlikely, it can only happen due
222 * to lack of memory when allocating the chain to store all message
223 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700225 */
226void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000227 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700229{
Greg Rosec7ac8672011-06-10 01:27:09 +0000230 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700231 panic("Unable to register rtnetlink message handler, "
232 "protocol = %d, message type = %d\n",
233 protocol, msgtype);
234}
Thomas Grafe2849862007-03-22 11:48:11 -0700235EXPORT_SYMBOL_GPL(rtnl_register);
236
237/**
238 * rtnl_unregister - Unregister a rtnetlink message type
239 * @protocol: Protocol family or PF_UNSPEC
240 * @msgtype: rtnetlink message type
241 *
242 * Returns 0 on success or a negative error code.
243 */
244int rtnl_unregister(int protocol, int msgtype)
245{
246 int msgindex;
247
Patrick McHardy25239ce2010-04-26 16:02:05 +0200248 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700249 msgindex = rtm_msgindex(msgtype);
250
251 if (rtnl_msg_handlers[protocol] == NULL)
252 return -ENOENT;
253
254 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256
257 return 0;
258}
Thomas Grafe2849862007-03-22 11:48:11 -0700259EXPORT_SYMBOL_GPL(rtnl_unregister);
260
261/**
262 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263 * @protocol : Protocol family or PF_UNSPEC
264 *
265 * Identical to calling rtnl_unregster() for all registered message types
266 * of a certain protocol family.
267 */
268void rtnl_unregister_all(int protocol)
269{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200270 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700271
272 kfree(rtnl_msg_handlers[protocol]);
273 rtnl_msg_handlers[protocol] = NULL;
274}
Thomas Grafe2849862007-03-22 11:48:11 -0700275EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick McHardy38f7b872007-06-13 12:03:51 -0700277static LIST_HEAD(link_ops);
278
Eric Dumazetc63044f2011-12-13 11:38:00 +0000279static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
280{
281 const struct rtnl_link_ops *ops;
282
283 list_for_each_entry(ops, &link_ops, list) {
284 if (!strcmp(ops->kind, kind))
285 return ops;
286 }
287 return NULL;
288}
289
Patrick McHardy38f7b872007-06-13 12:03:51 -0700290/**
291 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292 * @ops: struct rtnl_link_ops * to register
293 *
294 * The caller must hold the rtnl_mutex. This function should be used
295 * by drivers that create devices during module initialization. It
296 * must be called before registering the devices.
297 *
298 * Returns 0 on success or a negative error code.
299 */
300int __rtnl_link_register(struct rtnl_link_ops *ops)
301{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000302 if (rtnl_link_ops_get(ops->kind))
303 return -EEXIST;
304
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200305 /* The check for setup is here because if ops
306 * does not have that filled up, it is not possible
307 * to use the ops for creating device. So do not
308 * fill up dellink as well. That disables rtnl_dellink.
309 */
310 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000311 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700312
Patrick McHardy38f7b872007-06-13 12:03:51 -0700313 list_add_tail(&ops->list, &link_ops);
314 return 0;
315}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700316EXPORT_SYMBOL_GPL(__rtnl_link_register);
317
318/**
319 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320 * @ops: struct rtnl_link_ops * to register
321 *
322 * Returns 0 on success or a negative error code.
323 */
324int rtnl_link_register(struct rtnl_link_ops *ops)
325{
326 int err;
327
328 rtnl_lock();
329 err = __rtnl_link_register(ops);
330 rtnl_unlock();
331 return err;
332}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700333EXPORT_SYMBOL_GPL(rtnl_link_register);
334
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700335static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
336{
337 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000338 LIST_HEAD(list_kill);
339
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700340 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000341 if (dev->rtnl_link_ops == ops)
342 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700343 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000344 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700345}
346
Patrick McHardy38f7b872007-06-13 12:03:51 -0700347/**
348 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349 * @ops: struct rtnl_link_ops * to unregister
350 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700351 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700352 */
353void __rtnl_link_unregister(struct rtnl_link_ops *ops)
354{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700355 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700356
Eric W. Biederman881d9662007-09-17 11:56:21 -0700357 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700358 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700359 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700360 list_del(&ops->list);
361}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700362EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
363
Cong Wang200b9162014-05-12 15:11:20 -0700364/* Return with the rtnl_lock held when there are no network
365 * devices unregistering in any network namespace.
366 */
367static void rtnl_lock_unregistering_all(void)
368{
369 struct net *net;
370 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100371 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700372
Peter Zijlstraff960a72014-10-29 17:04:56 +0100373 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700374 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
381 }
382 }
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100386
387 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700388 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100389 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700390}
391
Patrick McHardy38f7b872007-06-13 12:03:51 -0700392/**
393 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394 * @ops: struct rtnl_link_ops * to unregister
395 */
396void rtnl_link_unregister(struct rtnl_link_ops *ops)
397{
Cong Wang200b9162014-05-12 15:11:20 -0700398 /* Close the race with cleanup_net() */
399 mutex_lock(&net_mutex);
400 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700401 __rtnl_link_unregister(ops);
402 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700403 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405EXPORT_SYMBOL_GPL(rtnl_link_unregister);
406
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100407static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
408{
409 struct net_device *master_dev;
410 const struct rtnl_link_ops *ops;
411
412 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 if (!master_dev)
414 return 0;
415 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900416 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100417 return 0;
418 /* IFLA_INFO_SLAVE_DATA + nested data */
419 return nla_total_size(sizeof(struct nlattr)) +
420 ops->get_slave_size(master_dev, dev);
421}
422
Patrick McHardy38f7b872007-06-13 12:03:51 -0700423static size_t rtnl_link_get_size(const struct net_device *dev)
424{
425 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 size_t size;
427
428 if (!ops)
429 return 0;
430
Thomas Graf369cf772010-11-11 15:47:59 +0000431 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700433
434 if (ops->get_size)
435 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000436 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700437 ops->get_size(dev);
438
439 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000440 /* IFLA_INFO_XSTATS */
441 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700442
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100443 size += rtnl_link_get_slave_info_data_size(dev);
444
Patrick McHardy38f7b872007-06-13 12:03:51 -0700445 return size;
446}
447
Thomas Graff8ff1822010-11-16 04:30:14 +0000448static LIST_HEAD(rtnl_af_ops);
449
450static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
451{
452 const struct rtnl_af_ops *ops;
453
454 list_for_each_entry(ops, &rtnl_af_ops, list) {
455 if (ops->family == family)
456 return ops;
457 }
458
459 return NULL;
460}
461
462/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000463 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464 * @ops: struct rtnl_af_ops * to register
465 *
466 * Returns 0 on success or a negative error code.
467 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800468void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000469{
Thomas Graff8ff1822010-11-16 04:30:14 +0000470 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800471 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000472 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000473}
474EXPORT_SYMBOL_GPL(rtnl_af_register);
475
476/**
477 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478 * @ops: struct rtnl_af_ops * to unregister
479 *
480 * The caller must hold the rtnl_mutex.
481 */
482void __rtnl_af_unregister(struct rtnl_af_ops *ops)
483{
484 list_del(&ops->list);
485}
486EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
487
488/**
489 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490 * @ops: struct rtnl_af_ops * to unregister
491 */
492void rtnl_af_unregister(struct rtnl_af_ops *ops)
493{
494 rtnl_lock();
495 __rtnl_af_unregister(ops);
496 rtnl_unlock();
497}
498EXPORT_SYMBOL_GPL(rtnl_af_unregister);
499
500static size_t rtnl_link_get_af_size(const struct net_device *dev)
501{
502 struct rtnl_af_ops *af_ops;
503 size_t size;
504
505 /* IFLA_AF_SPEC */
506 size = nla_total_size(sizeof(struct nlattr));
507
508 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
509 if (af_ops->get_link_af_size) {
510 /* AF_* + nested data */
511 size += nla_total_size(sizeof(struct nlattr)) +
512 af_ops->get_link_af_size(dev);
513 }
514 }
515
516 return size;
517}
518
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100519static bool rtnl_have_link_slave_info(const struct net_device *dev)
520{
521 struct net_device *master_dev;
522
523 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100524 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100525 return true;
526 return false;
527}
528
529static int rtnl_link_slave_info_fill(struct sk_buff *skb,
530 const struct net_device *dev)
531{
532 struct net_device *master_dev;
533 const struct rtnl_link_ops *ops;
534 struct nlattr *slave_data;
535 int err;
536
537 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
538 if (!master_dev)
539 return 0;
540 ops = master_dev->rtnl_link_ops;
541 if (!ops)
542 return 0;
543 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
544 return -EMSGSIZE;
545 if (ops->fill_slave_info) {
546 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
547 if (!slave_data)
548 return -EMSGSIZE;
549 err = ops->fill_slave_info(skb, master_dev, dev);
550 if (err < 0)
551 goto err_cancel_slave_data;
552 nla_nest_end(skb, slave_data);
553 }
554 return 0;
555
556err_cancel_slave_data:
557 nla_nest_cancel(skb, slave_data);
558 return err;
559}
560
561static int rtnl_link_info_fill(struct sk_buff *skb,
562 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700563{
564 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100565 struct nlattr *data;
566 int err;
567
568 if (!ops)
569 return 0;
570 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
571 return -EMSGSIZE;
572 if (ops->fill_xstats) {
573 err = ops->fill_xstats(skb, dev);
574 if (err < 0)
575 return err;
576 }
577 if (ops->fill_info) {
578 data = nla_nest_start(skb, IFLA_INFO_DATA);
579 if (data == NULL)
580 return -EMSGSIZE;
581 err = ops->fill_info(skb, dev);
582 if (err < 0)
583 goto err_cancel_data;
584 nla_nest_end(skb, data);
585 }
586 return 0;
587
588err_cancel_data:
589 nla_nest_cancel(skb, data);
590 return err;
591}
592
593static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
594{
595 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700596 int err = -EMSGSIZE;
597
598 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
599 if (linkinfo == NULL)
600 goto out;
601
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100602 err = rtnl_link_info_fill(skb, dev);
603 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700604 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100605
606 err = rtnl_link_slave_info_fill(skb, dev);
607 if (err < 0)
608 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700609
610 nla_nest_end(skb, linkinfo);
611 return 0;
612
Patrick McHardy38f7b872007-06-13 12:03:51 -0700613err_cancel_link:
614 nla_nest_cancel(skb, linkinfo);
615out:
616 return err;
617}
618
Eric Dumazet95c96172012-04-15 05:58:06 +0000619int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800621 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int err = 0;
623
Patrick McHardyac6d4392005-08-14 19:29:52 -0700624 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (echo)
626 atomic_inc(&skb->users);
627 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
628 if (echo)
629 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
630 return err;
631}
632
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800633int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700634{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800635 struct sock *rtnl = net->rtnl;
636
Thomas Graf2942e902006-08-15 00:30:25 -0700637 return nlmsg_unicast(rtnl, skb, pid);
638}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800639EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700640
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800641void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
642 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700643{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800644 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700645 int report = 0;
646
647 if (nlh)
648 report = nlmsg_report(nlh);
649
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800650 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700651}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800652EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700653
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800654void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700655{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800656 struct sock *rtnl = net->rtnl;
657
Thomas Graf97676b62006-08-15 00:31:41 -0700658 netlink_set_err(rtnl, 0, group, error);
659}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800660EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
663{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700664 struct nlattr *mx;
665 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Thomas Graf2d7202b2006-08-22 00:01:27 -0700667 mx = nla_nest_start(skb, RTA_METRICS);
668 if (mx == NULL)
669 return -ENOBUFS;
670
671 for (i = 0; i < RTAX_MAX; i++) {
672 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100673 if (i == RTAX_CC_ALGO - 1) {
674 char tmp[TCP_CA_NAME_MAX], *name;
675
676 name = tcp_ca_get_name_by_key(metrics[i], tmp);
677 if (!name)
678 continue;
679 if (nla_put_string(skb, i + 1, name))
680 goto nla_put_failure;
681 } else {
682 if (nla_put_u32(skb, i + 1, metrics[i]))
683 goto nla_put_failure;
684 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700685 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Millera57d27f2006-08-22 22:20:14 -0700689 if (!valid) {
690 nla_nest_cancel(skb, mx);
691 return 0;
692 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700693
694 return nla_nest_end(skb, mx);
695
696nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700697 nla_nest_cancel(skb, mx);
698 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800700EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Thomas Grafe3703b32006-11-27 09:27:07 -0800702int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700703 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800704{
705 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000706 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800707 .rta_used = dst->__use,
708 .rta_clntref = atomic_read(&(dst->__refcnt)),
709 .rta_error = error,
710 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800711 };
712
Li Wei82539472012-07-29 16:01:30 +0000713 if (expires) {
714 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800715
Li Wei82539472012-07-29 16:01:30 +0000716 clock = jiffies_to_clock_t(abs(expires));
717 clock = min_t(unsigned long, clock, INT_MAX);
718 ci.rta_expires = (expires > 0) ? clock : -clock;
719 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800720 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
721}
Thomas Grafe3703b32006-11-27 09:27:07 -0800722EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David S. Miller93b2d4a2008-02-17 18:35:07 -0800724static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800725{
726 unsigned char operstate = dev->operstate;
727
Eric Dumazete0d087a2009-11-07 01:26:17 -0800728 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800729 case IF_OPER_UP:
730 if ((operstate == IF_OPER_DORMANT ||
731 operstate == IF_OPER_UNKNOWN) &&
732 !netif_dormant(dev))
733 operstate = IF_OPER_UP;
734 break;
735
736 case IF_OPER_DORMANT:
737 if (operstate == IF_OPER_UP ||
738 operstate == IF_OPER_UNKNOWN)
739 operstate = IF_OPER_DORMANT;
740 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700741 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800742
743 if (dev->operstate != operstate) {
744 write_lock_bh(&dev_base_lock);
745 dev->operstate = operstate;
746 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800747 netdev_state_change(dev);
748 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800749}
750
Jiri Bencb1beb682012-07-27 02:58:22 +0000751static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
752{
753 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
754 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
755}
756
Patrick McHardy3729d502010-02-26 06:34:54 +0000757static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
758 const struct ifinfomsg *ifm)
759{
760 unsigned int flags = ifm->ifi_flags;
761
762 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
763 if (ifm->ifi_change)
764 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000765 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000766
767 return flags;
768}
769
Thomas Grafb60c5112006-08-04 23:05:34 -0700770static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000771 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Thomas Grafb60c5112006-08-04 23:05:34 -0700773 a->rx_packets = b->rx_packets;
774 a->tx_packets = b->tx_packets;
775 a->rx_bytes = b->rx_bytes;
776 a->tx_bytes = b->tx_bytes;
777 a->rx_errors = b->rx_errors;
778 a->tx_errors = b->tx_errors;
779 a->rx_dropped = b->rx_dropped;
780 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Thomas Grafb60c5112006-08-04 23:05:34 -0700782 a->multicast = b->multicast;
783 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Thomas Grafb60c5112006-08-04 23:05:34 -0700785 a->rx_length_errors = b->rx_length_errors;
786 a->rx_over_errors = b->rx_over_errors;
787 a->rx_crc_errors = b->rx_crc_errors;
788 a->rx_frame_errors = b->rx_frame_errors;
789 a->rx_fifo_errors = b->rx_fifo_errors;
790 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Grafb60c5112006-08-04 23:05:34 -0700792 a->tx_aborted_errors = b->tx_aborted_errors;
793 a->tx_carrier_errors = b->tx_carrier_errors;
794 a->tx_fifo_errors = b->tx_fifo_errors;
795 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
796 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Thomas Grafb60c5112006-08-04 23:05:34 -0700798 a->rx_compressed = b->rx_compressed;
799 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000800}
801
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000802static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000803{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000804 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000805}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chris Wrightc02db8c2010-05-16 01:05:45 -0700807/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500808static inline int rtnl_vfinfo_size(const struct net_device *dev,
809 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000810{
Greg Rose115c9b82012-02-21 16:54:48 -0500811 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
812 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700813 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700814 size_t size = nla_total_size(sizeof(struct nlattr));
815 size += nla_total_size(num_vfs * sizeof(struct nlattr));
816 size += num_vfs *
817 (nla_total_size(sizeof(struct ifla_vf_mac)) +
818 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400819 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200820 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300821 nla_total_size(sizeof(struct ifla_vf_link_state)) +
822 nla_total_size(sizeof(struct ifla_vf_rss_query_en)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700823 return size;
824 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000825 return 0;
826}
827
David Gibsonc53864f2014-04-24 10:22:36 +1000828static size_t rtnl_port_size(const struct net_device *dev,
829 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700830{
831 size_t port_size = nla_total_size(4) /* PORT_VF */
832 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
833 + nla_total_size(sizeof(struct ifla_port_vsi))
834 /* PORT_VSI_TYPE */
835 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
836 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
837 + nla_total_size(1) /* PROT_VDP_REQUEST */
838 + nla_total_size(2); /* PORT_VDP_RESPONSE */
839 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
840 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
841 + port_size;
842 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
843 + port_size;
844
David Gibsonc53864f2014-04-24 10:22:36 +1000845 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
846 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700847 return 0;
848 if (dev_num_vf(dev->dev.parent))
849 return port_self_size + vf_ports_size +
850 vf_port_size * dev_num_vf(dev->dev.parent);
851 else
852 return port_self_size;
853}
854
Greg Rose115c9b82012-02-21 16:54:48 -0500855static noinline size_t if_nlmsg_size(const struct net_device *dev,
856 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800857{
858 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
859 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700860 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800861 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
862 + nla_total_size(sizeof(struct rtnl_link_ifmap))
863 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700864 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800865 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
866 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
867 + nla_total_size(4) /* IFLA_TXQLEN */
868 + nla_total_size(4) /* IFLA_WEIGHT */
869 + nla_total_size(4) /* IFLA_MTU */
870 + nla_total_size(4) /* IFLA_LINK */
871 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000872 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000873 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000874 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
875 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800876 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700877 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700878 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100879 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Greg Rose115c9b82012-02-21 16:54:48 -0500880 + nla_total_size(ext_filter_mask
881 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
882 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000883 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000884 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200885 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100886 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
887 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800888}
889
Scott Feldman57b61082010-05-17 22:49:55 -0700890static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
891{
892 struct nlattr *vf_ports;
893 struct nlattr *vf_port;
894 int vf;
895 int err;
896
897 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
898 if (!vf_ports)
899 return -EMSGSIZE;
900
901 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
902 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700903 if (!vf_port)
904 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400905 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
906 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700907 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700908 if (err == -EMSGSIZE)
909 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700910 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700911 nla_nest_cancel(skb, vf_port);
912 continue;
913 }
914 nla_nest_end(skb, vf_port);
915 }
916
917 nla_nest_end(skb, vf_ports);
918
919 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700920
921nla_put_failure:
922 nla_nest_cancel(skb, vf_ports);
923 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700924}
925
926static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
927{
928 struct nlattr *port_self;
929 int err;
930
931 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
932 if (!port_self)
933 return -EMSGSIZE;
934
935 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
936 if (err) {
937 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700938 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700939 }
940
941 nla_nest_end(skb, port_self);
942
943 return 0;
944}
945
David Gibsonc53864f2014-04-24 10:22:36 +1000946static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
947 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700948{
949 int err;
950
David Gibsonc53864f2014-04-24 10:22:36 +1000951 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
952 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700953 return 0;
954
955 err = rtnl_port_self_fill(skb, dev);
956 if (err)
957 return err;
958
959 if (dev_num_vf(dev->dev.parent)) {
960 err = rtnl_vf_ports_fill(skb, dev);
961 if (err)
962 return err;
963 }
964
965 return 0;
966}
967
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200968static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
969{
970 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100971 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200972
973 err = dev_get_phys_port_id(dev, &ppid);
974 if (err) {
975 if (err == -EOPNOTSUPP)
976 return 0;
977 return err;
978 }
979
980 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
981 return -EMSGSIZE;
982
983 return 0;
984}
985
David Aherndb24a902015-03-17 20:23:15 -0600986static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
987{
988 char name[IFNAMSIZ];
989 int err;
990
991 err = dev_get_phys_port_name(dev, name, sizeof(name));
992 if (err) {
993 if (err == -EOPNOTSUPP)
994 return 0;
995 return err;
996 }
997
998 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
999 return -EMSGSIZE;
1000
1001 return 0;
1002}
1003
Jiri Pirko82f28412014-11-28 14:34:18 +01001004static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1005{
1006 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001007 struct switchdev_attr attr = {
1008 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1009 .flags = SWITCHDEV_F_NO_RECURSE,
1010 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001011
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001012 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001013 if (err) {
1014 if (err == -EOPNOTSUPP)
1015 return 0;
1016 return err;
1017 }
1018
Scott Feldman42275bd2015-05-13 11:16:50 -07001019 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1020 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001021 return -EMSGSIZE;
1022
1023 return 0;
1024}
1025
Thomas Grafb60c5112006-08-04 23:05:34 -07001026static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001027 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001028 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001029{
1030 struct ifinfomsg *ifm;
1031 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001032 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001033 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001034 struct nlattr *attr, *af_spec;
1035 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001036 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001037
Eric Dumazet2907c352011-05-25 07:34:04 +00001038 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001039 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1040 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001041 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001042
1043 ifm = nlmsg_data(nlh);
1044 ifm->ifi_family = AF_UNSPEC;
1045 ifm->__ifi_pad = 0;
1046 ifm->ifi_type = dev->type;
1047 ifm->ifi_index = dev->ifindex;
1048 ifm->ifi_flags = dev_get_flags(dev);
1049 ifm->ifi_change = change;
1050
David S. Millera6574342012-04-01 20:12:00 -04001051 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1052 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1053 nla_put_u8(skb, IFLA_OPERSTATE,
1054 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1055 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1056 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1057 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001058 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001059 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001060#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001061 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001062#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001063 (dev->ifindex != dev_get_iflink(dev) &&
1064 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001065 (upper_dev &&
1066 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001067 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001068 (dev->qdisc &&
1069 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1070 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001071 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1072 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1073 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001074 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001075
Stefan Rompfb00055a2006-03-20 17:09:11 -08001076 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct rtnl_link_ifmap map = {
1078 .mem_start = dev->mem_start,
1079 .mem_end = dev->mem_end,
1080 .base_addr = dev->base_addr,
1081 .irq = dev->irq,
1082 .dma = dev->dma,
1083 .port = dev->if_port,
1084 };
David S. Millera6574342012-04-01 20:12:00 -04001085 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1086 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001090 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1091 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1092 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001095 if (rtnl_phys_port_id_fill(skb, dev))
1096 goto nla_put_failure;
1097
David Aherndb24a902015-03-17 20:23:15 -06001098 if (rtnl_phys_port_name_fill(skb, dev))
1099 goto nla_put_failure;
1100
Jiri Pirko82f28412014-11-28 14:34:18 +01001101 if (rtnl_phys_switch_id_fill(skb, dev))
1102 goto nla_put_failure;
1103
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001104 attr = nla_reserve(skb, IFLA_STATS,
1105 sizeof(struct rtnl_link_stats));
1106 if (attr == NULL)
1107 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Dumazet28172732010-07-07 14:58:56 -07001109 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001110 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Jan Engelhardt10708f32010-03-11 09:57:29 +00001112 attr = nla_reserve(skb, IFLA_STATS64,
1113 sizeof(struct rtnl_link_stats64));
1114 if (attr == NULL)
1115 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001116 copy_rtnl_link_stats64(nla_data(attr), stats);
1117
David S. Millera6574342012-04-01 20:12:00 -04001118 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1119 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1120 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001121
Greg Rose115c9b82012-02-21 16:54:48 -05001122 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1123 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001124 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001125
Chris Wrightc02db8c2010-05-16 01:05:45 -07001126 struct nlattr *vfinfo, *vf;
1127 int num_vfs = dev_num_vf(dev->dev.parent);
1128
Chris Wrightc02db8c2010-05-16 01:05:45 -07001129 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1130 if (!vfinfo)
1131 goto nla_put_failure;
1132 for (i = 0; i < num_vfs; i++) {
1133 struct ifla_vf_info ivi;
1134 struct ifla_vf_mac vf_mac;
1135 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001136 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001137 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001138 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001139 struct ifla_vf_link_state vf_linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001140 struct ifla_vf_rss_query_en vf_rss_query_en;
Greg Rose5f8444a2011-10-08 03:05:24 +00001141
1142 /*
1143 * Not all SR-IOV capable drivers support the
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001144 * spoofcheck and "RSS query enable" query. Preset to
1145 * -1 so the user space tool can detect that the driver
1146 * didn't report anything.
Greg Rose5f8444a2011-10-08 03:05:24 +00001147 */
1148 ivi.spoofchk = -1;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001149 ivi.rss_query_en = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001150 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001151 /* The default value for VF link state is "auto"
1152 * IFLA_VF_LINK_STATE_AUTO which equals zero
1153 */
1154 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001155 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1156 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001157 vf_mac.vf =
1158 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001159 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001160 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001161 vf_spoofchk.vf =
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001162 vf_linkstate.vf =
1163 vf_rss_query_en.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001164
Chris Wrightc02db8c2010-05-16 01:05:45 -07001165 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1166 vf_vlan.vlan = ivi.vlan;
1167 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001168 vf_tx_rate.rate = ivi.max_tx_rate;
1169 vf_rate.min_tx_rate = ivi.min_tx_rate;
1170 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001171 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001172 vf_linkstate.link_state = ivi.linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001173 vf_rss_query_en.setting = ivi.rss_query_en;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001174 vf = nla_nest_start(skb, IFLA_VF_INFO);
1175 if (!vf) {
1176 nla_nest_cancel(skb, vfinfo);
1177 goto nla_put_failure;
1178 }
David S. Millera6574342012-04-01 20:12:00 -04001179 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1180 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001181 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1182 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001183 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1184 &vf_tx_rate) ||
1185 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001186 &vf_spoofchk) ||
1187 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001188 &vf_linkstate) ||
1189 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1190 sizeof(vf_rss_query_en),
1191 &vf_rss_query_en))
David S. Millera6574342012-04-01 20:12:00 -04001192 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001193 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001194 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001195 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001196 }
Scott Feldman57b61082010-05-17 22:49:55 -07001197
David Gibsonc53864f2014-04-24 10:22:36 +10001198 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001199 goto nla_put_failure;
1200
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001201 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001202 if (rtnl_link_fill(skb, dev) < 0)
1203 goto nla_put_failure;
1204 }
1205
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001206 if (dev->rtnl_link_ops &&
1207 dev->rtnl_link_ops->get_link_net) {
1208 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1209
1210 if (!net_eq(dev_net(dev), link_net)) {
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +02001211 int id = peernet2id_alloc(dev_net(dev), link_net);
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001212
1213 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1214 goto nla_put_failure;
1215 }
1216 }
1217
Thomas Graff8ff1822010-11-16 04:30:14 +00001218 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1219 goto nla_put_failure;
1220
1221 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1222 if (af_ops->fill_link_af) {
1223 struct nlattr *af;
1224 int err;
1225
1226 if (!(af = nla_nest_start(skb, af_ops->family)))
1227 goto nla_put_failure;
1228
1229 err = af_ops->fill_link_af(skb, dev);
1230
1231 /*
1232 * Caller may return ENODATA to indicate that there
1233 * was no data to be dumped. This is not an error, it
1234 * means we should trim the attribute header and
1235 * continue.
1236 */
1237 if (err == -ENODATA)
1238 nla_nest_cancel(skb, af);
1239 else if (err < 0)
1240 goto nla_put_failure;
1241
1242 nla_nest_end(skb, af);
1243 }
1244 }
1245
1246 nla_nest_end(skb, af_spec);
1247
Johannes Berg053c0952015-01-16 22:09:00 +01001248 nlmsg_end(skb, nlh);
1249 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001250
1251nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001252 nlmsg_cancel(skb, nlh);
1253 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Jiri Pirkof7b12602014-02-18 20:53:18 +01001256static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1257 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1258 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1259 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1260 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1261 [IFLA_MTU] = { .type = NLA_U32 },
1262 [IFLA_LINK] = { .type = NLA_U32 },
1263 [IFLA_MASTER] = { .type = NLA_U32 },
1264 [IFLA_CARRIER] = { .type = NLA_U8 },
1265 [IFLA_TXQLEN] = { .type = NLA_U32 },
1266 [IFLA_WEIGHT] = { .type = NLA_U32 },
1267 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1268 [IFLA_LINKMODE] = { .type = NLA_U8 },
1269 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1270 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1271 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1272 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1273 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1274 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1275 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1276 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1277 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1278 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1279 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1280 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001281 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001282 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001283 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001284 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001285};
1286
1287static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1288 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1289 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1290 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1291 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1292};
1293
1294static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1295 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1296};
1297
1298static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001299 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1300 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1301 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1302 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1303 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1304 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001305 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001306};
1307
1308static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1309 [IFLA_PORT_VF] = { .type = NLA_U32 },
1310 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1311 .len = PORT_PROFILE_MAX },
1312 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1313 .len = sizeof(struct ifla_port_vsi)},
1314 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1315 .len = PORT_UUID_MAX },
1316 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1317 .len = PORT_UUID_MAX },
1318 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1319 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1320};
1321
Thomas Grafb60c5112006-08-04 23:05:34 -07001322static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001324 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001325 int h, s_h;
1326 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001328 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001329 struct nlattr *tb[IFLA_MAX+1];
1330 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001331 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001332 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001334 s_h = cb->args[0];
1335 s_idx = cb->args[1];
1336
Thomas Graf4e985ad2011-06-21 03:11:20 +00001337 cb->seq = net->dev_base_seq;
1338
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001339 /* A hack to preserve kernel<->userspace interface.
1340 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1341 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1342 * what iproute2 < v3.9.0 used.
1343 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1344 * attribute, its netlink message is shorter than struct ifinfomsg.
1345 */
1346 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1347 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1348
1349 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001350
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001351 if (tb[IFLA_EXT_MASK])
1352 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1353 }
Greg Rose115c9b82012-02-21 16:54:48 -05001354
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001355 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1356 idx = 0;
1357 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001358 hlist_for_each_entry(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001359 if (idx < s_idx)
1360 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001361 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1362 NETLINK_CB(cb->skb).portid,
1363 cb->nlh->nlmsg_seq, 0,
1364 NLM_F_MULTI,
1365 ext_filter_mask);
1366 /* If we ran out of room on the first message,
1367 * we're in trouble
1368 */
1369 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1370
David S. Miller7b46a642015-01-18 23:36:08 -05001371 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001372 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001373
1374 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001375cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001376 idx++;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001379out:
1380 cb->args[1] = idx;
1381 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 return skb->len;
1384}
1385
Jiri Pirkof7b12602014-02-18 20:53:18 +01001386int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1387{
1388 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1389}
1390EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001391
Eric W. Biederman81adee42009-11-08 00:53:51 -08001392struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1393{
1394 struct net *net;
1395 /* Examine the link attributes and figure out which
1396 * network namespace we are talking about.
1397 */
1398 if (tb[IFLA_NET_NS_PID])
1399 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001400 else if (tb[IFLA_NET_NS_FD])
1401 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001402 else
1403 net = get_net(src_net);
1404 return net;
1405}
1406EXPORT_SYMBOL(rtnl_link_get_net);
1407
Thomas Graf1840bb12008-02-23 19:54:36 -08001408static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1409{
1410 if (dev) {
1411 if (tb[IFLA_ADDRESS] &&
1412 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1413 return -EINVAL;
1414
1415 if (tb[IFLA_BROADCAST] &&
1416 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1417 return -EINVAL;
1418 }
1419
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001420 if (tb[IFLA_AF_SPEC]) {
1421 struct nlattr *af;
1422 int rem, err;
1423
1424 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1425 const struct rtnl_af_ops *af_ops;
1426
1427 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1428 return -EAFNOSUPPORT;
1429
1430 if (!af_ops->set_link_af)
1431 return -EOPNOTSUPP;
1432
1433 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001434 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001435 if (err < 0)
1436 return err;
1437 }
1438 }
1439 }
1440
Thomas Graf1840bb12008-02-23 19:54:36 -08001441 return 0;
1442}
1443
Chris Wrightc02db8c2010-05-16 01:05:45 -07001444static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1445{
1446 int rem, err = -EINVAL;
1447 struct nlattr *vf;
1448 const struct net_device_ops *ops = dev->netdev_ops;
1449
1450 nla_for_each_nested(vf, attr, rem) {
1451 switch (nla_type(vf)) {
1452 case IFLA_VF_MAC: {
1453 struct ifla_vf_mac *ivm;
1454 ivm = nla_data(vf);
1455 err = -EOPNOTSUPP;
1456 if (ops->ndo_set_vf_mac)
1457 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1458 ivm->mac);
1459 break;
1460 }
1461 case IFLA_VF_VLAN: {
1462 struct ifla_vf_vlan *ivv;
1463 ivv = nla_data(vf);
1464 err = -EOPNOTSUPP;
1465 if (ops->ndo_set_vf_vlan)
1466 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1467 ivv->vlan,
1468 ivv->qos);
1469 break;
1470 }
1471 case IFLA_VF_TX_RATE: {
1472 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001473 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001474 ivt = nla_data(vf);
1475 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001476 if (ops->ndo_get_vf_config)
1477 err = ops->ndo_get_vf_config(dev, ivt->vf,
1478 &ivf);
1479 if (err)
1480 break;
1481 err = -EOPNOTSUPP;
1482 if (ops->ndo_set_vf_rate)
1483 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1484 ivf.min_tx_rate,
1485 ivt->rate);
1486 break;
1487 }
1488 case IFLA_VF_RATE: {
1489 struct ifla_vf_rate *ivt;
1490 ivt = nla_data(vf);
1491 err = -EOPNOTSUPP;
1492 if (ops->ndo_set_vf_rate)
1493 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1494 ivt->min_tx_rate,
1495 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001496 break;
1497 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001498 case IFLA_VF_SPOOFCHK: {
1499 struct ifla_vf_spoofchk *ivs;
1500 ivs = nla_data(vf);
1501 err = -EOPNOTSUPP;
1502 if (ops->ndo_set_vf_spoofchk)
1503 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1504 ivs->setting);
1505 break;
1506 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001507 case IFLA_VF_LINK_STATE: {
1508 struct ifla_vf_link_state *ivl;
1509 ivl = nla_data(vf);
1510 err = -EOPNOTSUPP;
1511 if (ops->ndo_set_vf_link_state)
1512 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1513 ivl->link_state);
1514 break;
1515 }
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001516 case IFLA_VF_RSS_QUERY_EN: {
1517 struct ifla_vf_rss_query_en *ivrssq_en;
1518
1519 ivrssq_en = nla_data(vf);
1520 err = -EOPNOTSUPP;
1521 if (ops->ndo_set_vf_rss_query_en)
1522 err = ops->ndo_set_vf_rss_query_en(dev,
1523 ivrssq_en->vf,
1524 ivrssq_en->setting);
1525 break;
1526 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001527 default:
1528 err = -EINVAL;
1529 break;
1530 }
1531 if (err)
1532 break;
1533 }
1534 return err;
1535}
1536
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001537static int do_set_master(struct net_device *dev, int ifindex)
1538{
Jiri Pirko898e5062013-01-03 22:48:52 +00001539 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001540 const struct net_device_ops *ops;
1541 int err;
1542
Jiri Pirko898e5062013-01-03 22:48:52 +00001543 if (upper_dev) {
1544 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001545 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001546 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001547 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001548 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001549 if (err)
1550 return err;
1551 } else {
1552 return -EOPNOTSUPP;
1553 }
1554 }
1555
1556 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001557 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1558 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001559 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001560 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001561 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001562 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001563 if (err)
1564 return err;
1565 } else {
1566 return -EOPNOTSUPP;
1567 }
1568 }
1569 return 0;
1570}
1571
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001572#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001573/* notify flag means notify + modified. */
1574#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001575static int do_setlink(const struct sk_buff *skb,
1576 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001577 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001578{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001579 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001580 int err;
1581
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001582 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001583 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001584 if (IS_ERR(net)) {
1585 err = PTR_ERR(net);
1586 goto errout;
1587 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001588 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001589 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001590 err = -EPERM;
1591 goto errout;
1592 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001593 err = dev_change_net_namespace(dev, net, ifname);
1594 put_net(net);
1595 if (err)
1596 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001597 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001598 }
1599
Patrick McHardy0157f602007-06-13 12:03:36 -07001600 if (tb[IFLA_MAP]) {
1601 struct rtnl_link_ifmap *u_map;
1602 struct ifmap k_map;
1603
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001604 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001605 err = -EOPNOTSUPP;
1606 goto errout;
1607 }
1608
1609 if (!netif_device_present(dev)) {
1610 err = -ENODEV;
1611 goto errout;
1612 }
1613
1614 u_map = nla_data(tb[IFLA_MAP]);
1615 k_map.mem_start = (unsigned long) u_map->mem_start;
1616 k_map.mem_end = (unsigned long) u_map->mem_end;
1617 k_map.base_addr = (unsigned short) u_map->base_addr;
1618 k_map.irq = (unsigned char) u_map->irq;
1619 k_map.dma = (unsigned char) u_map->dma;
1620 k_map.port = (unsigned char) u_map->port;
1621
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001622 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001623 if (err < 0)
1624 goto errout;
1625
Nicolas Dichtelba998902014-09-01 16:07:29 +02001626 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001627 }
1628
1629 if (tb[IFLA_ADDRESS]) {
1630 struct sockaddr *sa;
1631 int len;
1632
Patrick McHardy0157f602007-06-13 12:03:36 -07001633 len = sizeof(sa_family_t) + dev->addr_len;
1634 sa = kmalloc(len, GFP_KERNEL);
1635 if (!sa) {
1636 err = -ENOMEM;
1637 goto errout;
1638 }
1639 sa->sa_family = dev->type;
1640 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1641 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001642 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001643 kfree(sa);
1644 if (err)
1645 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001646 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001647 }
1648
1649 if (tb[IFLA_MTU]) {
1650 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1651 if (err < 0)
1652 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001653 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001654 }
1655
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001656 if (tb[IFLA_GROUP]) {
1657 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001658 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001659 }
1660
Patrick McHardy0157f602007-06-13 12:03:36 -07001661 /*
1662 * Interface selected by interface index but interface
1663 * name provided implies that a name change has been
1664 * requested.
1665 */
1666 if (ifm->ifi_index > 0 && ifname[0]) {
1667 err = dev_change_name(dev, ifname);
1668 if (err < 0)
1669 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001670 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001671 }
1672
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001673 if (tb[IFLA_IFALIAS]) {
1674 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1675 nla_len(tb[IFLA_IFALIAS]));
1676 if (err < 0)
1677 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001678 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001679 }
1680
Patrick McHardy0157f602007-06-13 12:03:36 -07001681 if (tb[IFLA_BROADCAST]) {
1682 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001683 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001684 }
1685
1686 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001687 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001688 if (err < 0)
1689 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001690 }
1691
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001692 if (tb[IFLA_MASTER]) {
1693 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1694 if (err)
1695 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001696 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001697 }
1698
Jiri Pirko9a572472012-12-27 23:49:39 +00001699 if (tb[IFLA_CARRIER]) {
1700 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1701 if (err)
1702 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001703 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001704 }
1705
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001706 if (tb[IFLA_TXQLEN]) {
1707 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1708
1709 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001710 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001711
1712 dev->tx_queue_len = value;
1713 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001714
Patrick McHardy0157f602007-06-13 12:03:36 -07001715 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001716 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001717
1718 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001719 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1720
David S. Miller93b2d4a2008-02-17 18:35:07 -08001721 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001722 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001723 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001724 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001725 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001726 }
1727
Chris Wrightc02db8c2010-05-16 01:05:45 -07001728 if (tb[IFLA_VFINFO_LIST]) {
1729 struct nlattr *attr;
1730 int rem;
1731 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001732 if (nla_type(attr) != IFLA_VF_INFO) {
1733 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001734 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001735 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001736 err = do_setvfinfo(dev, attr);
1737 if (err < 0)
1738 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001739 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001740 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001741 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001742 err = 0;
1743
Scott Feldman57b61082010-05-17 22:49:55 -07001744 if (tb[IFLA_VF_PORTS]) {
1745 struct nlattr *port[IFLA_PORT_MAX+1];
1746 struct nlattr *attr;
1747 int vf;
1748 int rem;
1749
1750 err = -EOPNOTSUPP;
1751 if (!ops->ndo_set_vf_port)
1752 goto errout;
1753
1754 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1755 if (nla_type(attr) != IFLA_VF_PORT)
1756 continue;
1757 err = nla_parse_nested(port, IFLA_PORT_MAX,
1758 attr, ifla_port_policy);
1759 if (err < 0)
1760 goto errout;
1761 if (!port[IFLA_PORT_VF]) {
1762 err = -EOPNOTSUPP;
1763 goto errout;
1764 }
1765 vf = nla_get_u32(port[IFLA_PORT_VF]);
1766 err = ops->ndo_set_vf_port(dev, vf, port);
1767 if (err < 0)
1768 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001769 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001770 }
1771 }
1772 err = 0;
1773
1774 if (tb[IFLA_PORT_SELF]) {
1775 struct nlattr *port[IFLA_PORT_MAX+1];
1776
1777 err = nla_parse_nested(port, IFLA_PORT_MAX,
1778 tb[IFLA_PORT_SELF], ifla_port_policy);
1779 if (err < 0)
1780 goto errout;
1781
1782 err = -EOPNOTSUPP;
1783 if (ops->ndo_set_vf_port)
1784 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1785 if (err < 0)
1786 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001787 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001788 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001789
1790 if (tb[IFLA_AF_SPEC]) {
1791 struct nlattr *af;
1792 int rem;
1793
1794 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1795 const struct rtnl_af_ops *af_ops;
1796
1797 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001798 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001799
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001800 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001801 if (err < 0)
1802 goto errout;
1803
Nicolas Dichtelba998902014-09-01 16:07:29 +02001804 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001805 }
1806 }
Scott Feldman57b61082010-05-17 22:49:55 -07001807 err = 0;
1808
Patrick McHardy0157f602007-06-13 12:03:36 -07001809errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001810 if (status & DO_SETLINK_MODIFIED) {
1811 if (status & DO_SETLINK_NOTIFY)
1812 netdev_state_change(dev);
1813
1814 if (err < 0)
1815 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",
1816 dev->name);
1817 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001818
Patrick McHardy0157f602007-06-13 12:03:36 -07001819 return err;
1820}
1821
Thomas Graf661d2962013-03-21 07:45:29 +00001822static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001824 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001825 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001827 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001828 struct nlattr *tb[IFLA_MAX+1];
1829 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Thomas Grafda5e0492006-08-10 21:17:37 -07001831 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1832 if (err < 0)
1833 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Thomas Graf5176f912006-08-26 20:13:18 -07001835 if (tb[IFLA_IFNAME])
1836 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001837 else
1838 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001841 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001842 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001843 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001844 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001845 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001846 else
1847 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Thomas Grafda5e0492006-08-10 21:17:37 -07001849 if (dev == NULL) {
1850 err = -ENODEV;
1851 goto errout;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Eric Dumazete0d087a2009-11-07 01:26:17 -08001854 err = validate_linkmsg(dev, tb);
1855 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001856 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001857
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001858 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001859errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return err;
1861}
1862
WANG Cong66400d52015-03-24 11:53:31 -07001863static int rtnl_group_dellink(const struct net *net, int group)
1864{
1865 struct net_device *dev, *aux;
1866 LIST_HEAD(list_kill);
1867 bool found = false;
1868
1869 if (!group)
1870 return -EPERM;
1871
1872 for_each_netdev(net, dev) {
1873 if (dev->group == group) {
1874 const struct rtnl_link_ops *ops;
1875
1876 found = true;
1877 ops = dev->rtnl_link_ops;
1878 if (!ops || !ops->dellink)
1879 return -EOPNOTSUPP;
1880 }
1881 }
1882
1883 if (!found)
1884 return -ENODEV;
1885
1886 for_each_netdev_safe(net, dev, aux) {
1887 if (dev->group == group) {
1888 const struct rtnl_link_ops *ops;
1889
1890 ops = dev->rtnl_link_ops;
1891 ops->dellink(dev, &list_kill);
1892 }
1893 }
1894 unregister_netdevice_many(&list_kill);
1895
1896 return 0;
1897}
1898
Thomas Graf661d2962013-03-21 07:45:29 +00001899static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001900{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001901 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001902 const struct rtnl_link_ops *ops;
1903 struct net_device *dev;
1904 struct ifinfomsg *ifm;
1905 char ifname[IFNAMSIZ];
1906 struct nlattr *tb[IFLA_MAX+1];
1907 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001908 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001909
1910 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1911 if (err < 0)
1912 return err;
1913
1914 if (tb[IFLA_IFNAME])
1915 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1916
1917 ifm = nlmsg_data(nlh);
1918 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001919 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001920 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001921 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07001922 else if (tb[IFLA_GROUP])
1923 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07001924 else
1925 return -EINVAL;
1926
1927 if (!dev)
1928 return -ENODEV;
1929
1930 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001931 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001932 return -EOPNOTSUPP;
1933
Eric Dumazet226bd342011-05-08 23:17:57 +00001934 ops->dellink(dev, &list_kill);
1935 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001936 return 0;
1937}
1938
Patrick McHardy3729d502010-02-26 06:34:54 +00001939int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1940{
1941 unsigned int old_flags;
1942 int err;
1943
1944 old_flags = dev->flags;
1945 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1946 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1947 if (err < 0)
1948 return err;
1949 }
1950
1951 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001952
Nicolas Dichtela528c212013-09-25 12:02:44 +02001953 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001954 return 0;
1955}
1956EXPORT_SYMBOL(rtnl_configure_link);
1957
Rami Rosenc0713562012-11-30 01:08:47 +00001958struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02001959 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02001960 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001961{
1962 int err;
1963 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001964 unsigned int num_tx_queues = 1;
1965 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001966
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001967 if (tb[IFLA_NUM_TX_QUEUES])
1968 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1969 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001970 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001971
1972 if (tb[IFLA_NUM_RX_QUEUES])
1973 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1974 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001975 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001976
Pavel Emelianove7199282007-08-08 22:16:38 -07001977 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001978 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001979 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001980 if (!dev)
1981 goto err;
1982
Eric W. Biederman81adee42009-11-08 00:53:51 -08001983 dev_net_set(dev, net);
1984 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001985 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001986
Pavel Emelianove7199282007-08-08 22:16:38 -07001987 if (tb[IFLA_MTU])
1988 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001989 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001990 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1991 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001992 dev->addr_assign_type = NET_ADDR_SET;
1993 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001994 if (tb[IFLA_BROADCAST])
1995 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1996 nla_len(tb[IFLA_BROADCAST]));
1997 if (tb[IFLA_TXQLEN])
1998 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1999 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002000 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002001 if (tb[IFLA_LINKMODE])
2002 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002003 if (tb[IFLA_GROUP])
2004 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002005
2006 return dev;
2007
Pavel Emelianove7199282007-08-08 22:16:38 -07002008err:
2009 return ERR_PTR(err);
2010}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002011EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002012
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002013static int rtnl_group_changelink(const struct sk_buff *skb,
2014 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002015 struct ifinfomsg *ifm,
2016 struct nlattr **tb)
2017{
WANG Congd0795352015-03-23 16:31:09 -07002018 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002019 int err;
2020
WANG Congd0795352015-03-23 16:31:09 -07002021 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002022 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002023 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002024 if (err < 0)
2025 return err;
2026 }
2027 }
2028
2029 return 0;
2030}
2031
Thomas Graf661d2962013-03-21 07:45:29 +00002032static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002033{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002034 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002035 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002036 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002037 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002038 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002039 struct ifinfomsg *ifm;
2040 char kind[MODULE_NAME_LEN];
2041 char ifname[IFNAMSIZ];
2042 struct nlattr *tb[IFLA_MAX+1];
2043 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002044 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002045 int err;
2046
Johannes Berg95a5afc2008-10-16 15:24:51 -07002047#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002048replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002049#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002050 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2051 if (err < 0)
2052 return err;
2053
2054 if (tb[IFLA_IFNAME])
2055 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2056 else
2057 ifname[0] = '\0';
2058
2059 ifm = nlmsg_data(nlh);
2060 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002061 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002062 else {
2063 if (ifname[0])
2064 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002065 else
2066 dev = NULL;
2067 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002068
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002069 if (dev) {
2070 master_dev = netdev_master_upper_dev_get(dev);
2071 if (master_dev)
2072 m_ops = master_dev->rtnl_link_ops;
2073 }
2074
Eric Dumazete0d087a2009-11-07 01:26:17 -08002075 err = validate_linkmsg(dev, tb);
2076 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002077 return err;
2078
Patrick McHardy38f7b872007-06-13 12:03:51 -07002079 if (tb[IFLA_LINKINFO]) {
2080 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2081 tb[IFLA_LINKINFO], ifla_info_policy);
2082 if (err < 0)
2083 return err;
2084 } else
2085 memset(linkinfo, 0, sizeof(linkinfo));
2086
2087 if (linkinfo[IFLA_INFO_KIND]) {
2088 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2089 ops = rtnl_link_ops_get(kind);
2090 } else {
2091 kind[0] = '\0';
2092 ops = NULL;
2093 }
2094
2095 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002096 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2097 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002098 struct nlattr **data = NULL;
2099 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002100 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002101
2102 if (ops) {
2103 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2104 err = nla_parse_nested(attr, ops->maxtype,
2105 linkinfo[IFLA_INFO_DATA],
2106 ops->policy);
2107 if (err < 0)
2108 return err;
2109 data = attr;
2110 }
2111 if (ops->validate) {
2112 err = ops->validate(tb, data);
2113 if (err < 0)
2114 return err;
2115 }
2116 }
2117
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002118 if (m_ops) {
2119 if (m_ops->slave_maxtype &&
2120 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2121 err = nla_parse_nested(slave_attr,
2122 m_ops->slave_maxtype,
2123 linkinfo[IFLA_INFO_SLAVE_DATA],
2124 m_ops->slave_policy);
2125 if (err < 0)
2126 return err;
2127 slave_data = slave_attr;
2128 }
2129 if (m_ops->slave_validate) {
2130 err = m_ops->slave_validate(tb, slave_data);
2131 if (err < 0)
2132 return err;
2133 }
2134 }
2135
Patrick McHardy38f7b872007-06-13 12:03:51 -07002136 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002137 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002138
2139 if (nlh->nlmsg_flags & NLM_F_EXCL)
2140 return -EEXIST;
2141 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2142 return -EOPNOTSUPP;
2143
2144 if (linkinfo[IFLA_INFO_DATA]) {
2145 if (!ops || ops != dev->rtnl_link_ops ||
2146 !ops->changelink)
2147 return -EOPNOTSUPP;
2148
2149 err = ops->changelink(dev, tb, data);
2150 if (err < 0)
2151 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002152 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002153 }
2154
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002155 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2156 if (!m_ops || !m_ops->slave_changelink)
2157 return -EOPNOTSUPP;
2158
2159 err = m_ops->slave_changelink(master_dev, dev,
2160 tb, slave_data);
2161 if (err < 0)
2162 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002163 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002164 }
2165
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002166 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002167 }
2168
Patrick McHardyffa934f2011-01-20 03:00:42 +00002169 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2170 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002171 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002172 nla_get_u32(tb[IFLA_GROUP]),
2173 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002174 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002175 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002176
Patrick McHardy0e068772007-07-11 19:42:31 -07002177 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002178 return -EOPNOTSUPP;
2179
2180 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002181#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002182 if (kind[0]) {
2183 __rtnl_unlock();
2184 request_module("rtnl-link-%s", kind);
2185 rtnl_lock();
2186 ops = rtnl_link_ops_get(kind);
2187 if (ops)
2188 goto replay;
2189 }
2190#endif
2191 return -EOPNOTSUPP;
2192 }
2193
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002194 if (!ops->setup)
2195 return -EOPNOTSUPP;
2196
Tom Gundersen55177502014-07-14 16:37:25 +02002197 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002198 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002199 name_assign_type = NET_NAME_ENUM;
2200 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002201
Eric W. Biederman81adee42009-11-08 00:53:51 -08002202 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002203 if (IS_ERR(dest_net))
2204 return PTR_ERR(dest_net);
2205
Eric W. Biederman505ce412015-02-26 16:19:00 -06002206 err = -EPERM;
2207 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2208 goto out;
2209
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002210 if (tb[IFLA_LINK_NETNSID]) {
2211 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2212
2213 link_net = get_net_ns_by_id(dest_net, id);
2214 if (!link_net) {
2215 err = -EINVAL;
2216 goto out;
2217 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002218 err = -EPERM;
2219 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2220 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002221 }
2222
2223 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2224 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002225 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002226 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002227 goto out;
2228 }
2229
2230 dev->ifindex = ifm->ifi_index;
2231
Cong Wang0e0eee22014-02-11 15:51:30 -08002232 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002233 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002234 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002235 * and unregister it on failure after registration
2236 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002237 */
Cong Wange51fb152014-06-03 16:40:47 -07002238 if (err < 0) {
2239 /* If device is not registered at all, free it now */
2240 if (dev->reg_state == NETREG_UNINITIALIZED)
2241 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002242 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002243 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002244 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002245 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002246 if (err < 0) {
2247 free_netdev(dev);
2248 goto out;
2249 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002250 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002251 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002252 if (err < 0)
2253 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002254 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002255 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002256 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002257 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002258 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002259out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002260 if (link_net)
2261 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002262 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002263 return err;
David S. Miller43638902015-03-10 21:58:32 -04002264out_unregister:
2265 if (ops->newlink) {
2266 LIST_HEAD(list_kill);
2267
2268 ops->dellink(dev, &list_kill);
2269 unregister_netdevice_many(&list_kill);
2270 } else {
2271 unregister_netdevice(dev);
2272 }
2273 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002274 }
2275}
2276
Thomas Graf661d2962013-03-21 07:45:29 +00002277static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002278{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002279 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002280 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002281 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002282 struct nlattr *tb[IFLA_MAX+1];
2283 struct net_device *dev = NULL;
2284 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002285 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002286 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002287
Thomas Grafb60c5112006-08-04 23:05:34 -07002288 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2289 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002290 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002291
Eric Dumazeta3d12892009-10-21 10:59:31 +00002292 if (tb[IFLA_IFNAME])
2293 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2294
Greg Rose115c9b82012-02-21 16:54:48 -05002295 if (tb[IFLA_EXT_MASK])
2296 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2297
Thomas Grafb60c5112006-08-04 23:05:34 -07002298 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002299 if (ifm->ifi_index > 0)
2300 dev = __dev_get_by_index(net, ifm->ifi_index);
2301 else if (tb[IFLA_IFNAME])
2302 dev = __dev_get_by_name(net, ifname);
2303 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002304 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002305
Eric Dumazeta3d12892009-10-21 10:59:31 +00002306 if (dev == NULL)
2307 return -ENODEV;
2308
Greg Rose115c9b82012-02-21 16:54:48 -05002309 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002310 if (nskb == NULL)
2311 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002312
Eric W. Biederman15e47302012-09-07 20:12:54 +00002313 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002314 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002315 if (err < 0) {
2316 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2317 WARN_ON(err == -EMSGSIZE);
2318 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002319 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002320 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002321
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002322 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002323}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002324
Greg Rose115c9b82012-02-21 16:54:48 -05002325static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002326{
Greg Rose115c9b82012-02-21 16:54:48 -05002327 struct net *net = sock_net(skb->sk);
2328 struct net_device *dev;
2329 struct nlattr *tb[IFLA_MAX+1];
2330 u32 ext_filter_mask = 0;
2331 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002332 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002333
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002334 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2335 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2336 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2337
2338 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002339 if (tb[IFLA_EXT_MASK])
2340 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2341 }
Greg Rose115c9b82012-02-21 16:54:48 -05002342
2343 if (!ext_filter_mask)
2344 return NLMSG_GOODSIZE;
2345 /*
2346 * traverse the list of net devices and compute the minimum
2347 * buffer size based upon the filter mask.
2348 */
2349 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2350 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2351 if_nlmsg_size(dev,
2352 ext_filter_mask));
2353 }
2354
Greg Rosec7ac8672011-06-10 01:27:09 +00002355 return min_ifinfo_dump_size;
2356}
2357
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002358static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 int idx;
2361 int s_idx = cb->family;
2362
2363 if (s_idx == 0)
2364 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002365 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 int type = cb->nlh->nlmsg_type-RTM_BASE;
2367 if (idx < s_idx || idx == PF_PACKET)
2368 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002369 if (rtnl_msg_handlers[idx] == NULL ||
2370 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002372 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002374 cb->prev_seq = 0;
2375 cb->seq = 0;
2376 }
Thomas Grafe2849862007-03-22 11:48:11 -07002377 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 break;
2379 }
2380 cb->family = idx;
2381
2382 return skb->len;
2383}
2384
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002385struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2386 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002388 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002390 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002391 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002393 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002394 if (skb == NULL)
2395 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Greg Rose115c9b82012-02-21 16:54:48 -05002397 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002398 if (err < 0) {
2399 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2400 WARN_ON(err == -EMSGSIZE);
2401 kfree_skb(skb);
2402 goto errout;
2403 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002404 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002405errout:
2406 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002407 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002408 return NULL;
2409}
2410
2411void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2412{
2413 struct net *net = dev_net(dev);
2414
2415 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2416}
2417
2418void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2419 gfp_t flags)
2420{
2421 struct sk_buff *skb;
2422
2423 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2424 if (skb)
2425 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002427EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
John Fastabendd83b0602012-04-15 06:44:08 +00002429static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2430 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002431 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002432 int type, unsigned int flags,
2433 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002434{
2435 struct nlmsghdr *nlh;
2436 struct ndmsg *ndm;
2437
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002438 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002439 if (!nlh)
2440 return -EMSGSIZE;
2441
2442 ndm = nlmsg_data(nlh);
2443 ndm->ndm_family = AF_BRIDGE;
2444 ndm->ndm_pad1 = 0;
2445 ndm->ndm_pad2 = 0;
2446 ndm->ndm_flags = flags;
2447 ndm->ndm_type = 0;
2448 ndm->ndm_ifindex = dev->ifindex;
2449 ndm->ndm_state = NUD_PERMANENT;
2450
2451 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2452 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002453 if (vid)
2454 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2455 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002456
Johannes Berg053c0952015-01-16 22:09:00 +01002457 nlmsg_end(skb, nlh);
2458 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002459
2460nla_put_failure:
2461 nlmsg_cancel(skb, nlh);
2462 return -EMSGSIZE;
2463}
2464
John Fastabend3ff661c2012-04-15 06:44:14 +00002465static inline size_t rtnl_fdb_nlmsg_size(void)
2466{
2467 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2468}
2469
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002470static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
John Fastabend3ff661c2012-04-15 06:44:14 +00002471{
2472 struct net *net = dev_net(dev);
2473 struct sk_buff *skb;
2474 int err = -ENOBUFS;
2475
2476 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2477 if (!skb)
2478 goto errout;
2479
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002480 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2481 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002482 if (err < 0) {
2483 kfree_skb(skb);
2484 goto errout;
2485 }
2486
2487 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2488 return;
2489errout:
2490 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2491}
2492
Vlad Yasevich090096b2013-03-06 15:39:42 +00002493/**
2494 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2495 */
2496int ndo_dflt_fdb_add(struct ndmsg *ndm,
2497 struct nlattr *tb[],
2498 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002499 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002500 u16 flags)
2501{
2502 int err = -EINVAL;
2503
2504 /* If aging addresses are supported device will need to
2505 * implement its own handler for this.
2506 */
2507 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2508 pr_info("%s: FDB only supports static addresses\n", dev->name);
2509 return err;
2510 }
2511
Or Gerlitz65891fe2014-12-14 18:19:05 +02002512 if (vid) {
2513 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2514 return err;
2515 }
2516
Vlad Yasevich090096b2013-03-06 15:39:42 +00002517 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2518 err = dev_uc_add_excl(dev, addr);
2519 else if (is_multicast_ether_addr(addr))
2520 err = dev_mc_add_excl(dev, addr);
2521
2522 /* Only return duplicate errors if NLM_F_EXCL is set */
2523 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2524 err = 0;
2525
2526 return err;
2527}
2528EXPORT_SYMBOL(ndo_dflt_fdb_add);
2529
Jiri Pirkof6f64242014-11-28 14:34:15 +01002530static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2531{
2532 u16 vid = 0;
2533
2534 if (vlan_attr) {
2535 if (nla_len(vlan_attr) != sizeof(u16)) {
2536 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2537 return -EINVAL;
2538 }
2539
2540 vid = nla_get_u16(vlan_attr);
2541
2542 if (!vid || vid >= VLAN_VID_MASK) {
2543 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2544 vid);
2545 return -EINVAL;
2546 }
2547 }
2548 *p_vid = vid;
2549 return 0;
2550}
2551
Thomas Graf661d2962013-03-21 07:45:29 +00002552static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002553{
2554 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002555 struct ndmsg *ndm;
2556 struct nlattr *tb[NDA_MAX+1];
2557 struct net_device *dev;
2558 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002559 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002560 int err;
2561
2562 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2563 if (err < 0)
2564 return err;
2565
2566 ndm = nlmsg_data(nlh);
2567 if (ndm->ndm_ifindex == 0) {
2568 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2569 return -EINVAL;
2570 }
2571
2572 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2573 if (dev == NULL) {
2574 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2575 return -ENODEV;
2576 }
2577
2578 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2579 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2580 return -EINVAL;
2581 }
2582
2583 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002584
Jiri Pirkof6f64242014-11-28 14:34:15 +01002585 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2586 if (err)
2587 return err;
2588
John Fastabend77162022012-04-15 06:43:56 +00002589 err = -EOPNOTSUPP;
2590
2591 /* Support fdb on master device the net/bridge default case */
2592 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2593 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002594 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2595 const struct net_device_ops *ops = br_dev->netdev_ops;
2596
Jiri Pirkof6f64242014-11-28 14:34:15 +01002597 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2598 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002599 if (err)
2600 goto out;
2601 else
2602 ndm->ndm_flags &= ~NTF_MASTER;
2603 }
2604
2605 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002606 if ((ndm->ndm_flags & NTF_SELF)) {
2607 if (dev->netdev_ops->ndo_fdb_add)
2608 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002609 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002610 nlh->nlmsg_flags);
2611 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002612 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002613 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002614
John Fastabend3ff661c2012-04-15 06:44:14 +00002615 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002616 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002617 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002618 }
John Fastabend77162022012-04-15 06:43:56 +00002619 }
2620out:
2621 return err;
2622}
2623
Vlad Yasevich090096b2013-03-06 15:39:42 +00002624/**
2625 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2626 */
2627int ndo_dflt_fdb_del(struct ndmsg *ndm,
2628 struct nlattr *tb[],
2629 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002630 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002631{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002632 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002633
2634 /* If aging addresses are supported device will need to
2635 * implement its own handler for this.
2636 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002637 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002638 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002639 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002640 }
2641
2642 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2643 err = dev_uc_del(dev, addr);
2644 else if (is_multicast_ether_addr(addr))
2645 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002646
2647 return err;
2648}
2649EXPORT_SYMBOL(ndo_dflt_fdb_del);
2650
Thomas Graf661d2962013-03-21 07:45:29 +00002651static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002652{
2653 struct net *net = sock_net(skb->sk);
2654 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002655 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002656 struct net_device *dev;
2657 int err = -EINVAL;
2658 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002659 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002660
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002661 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002662 return -EPERM;
2663
2664 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2665 if (err < 0)
2666 return err;
John Fastabend77162022012-04-15 06:43:56 +00002667
2668 ndm = nlmsg_data(nlh);
2669 if (ndm->ndm_ifindex == 0) {
2670 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2671 return -EINVAL;
2672 }
2673
2674 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2675 if (dev == NULL) {
2676 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2677 return -ENODEV;
2678 }
2679
Vlad Yasevich1690be62013-02-13 12:00:18 +00002680 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2681 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002682 return -EINVAL;
2683 }
2684
Vlad Yasevich1690be62013-02-13 12:00:18 +00002685 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002686
Jiri Pirkof6f64242014-11-28 14:34:15 +01002687 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2688 if (err)
2689 return err;
2690
John Fastabend77162022012-04-15 06:43:56 +00002691 err = -EOPNOTSUPP;
2692
2693 /* Support fdb on master device the net/bridge default case */
2694 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2695 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002696 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2697 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002698
Jiri Pirko898e5062013-01-03 22:48:52 +00002699 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002700 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002701
2702 if (err)
2703 goto out;
2704 else
2705 ndm->ndm_flags &= ~NTF_MASTER;
2706 }
2707
2708 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002709 if (ndm->ndm_flags & NTF_SELF) {
2710 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002711 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2712 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002713 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002714 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002715
John Fastabend3ff661c2012-04-15 06:44:14 +00002716 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002717 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002718 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002719 }
John Fastabend77162022012-04-15 06:43:56 +00002720 }
2721out:
2722 return err;
2723}
2724
John Fastabendd83b0602012-04-15 06:44:08 +00002725static int nlmsg_populate_fdb(struct sk_buff *skb,
2726 struct netlink_callback *cb,
2727 struct net_device *dev,
2728 int *idx,
2729 struct netdev_hw_addr_list *list)
2730{
2731 struct netdev_hw_addr *ha;
2732 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002733 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002734
Eric W. Biederman15e47302012-09-07 20:12:54 +00002735 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002736 seq = cb->nlh->nlmsg_seq;
2737
2738 list_for_each_entry(ha, &list->list, list) {
2739 if (*idx < cb->args[0])
2740 goto skip;
2741
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002742 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002743 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002744 RTM_NEWNEIGH, NTF_SELF,
2745 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002746 if (err < 0)
2747 return err;
2748skip:
2749 *idx += 1;
2750 }
2751 return 0;
2752}
2753
2754/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002755 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002756 * @nlh: netlink message header
2757 * @dev: netdevice
2758 *
2759 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002760 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002761 */
2762int ndo_dflt_fdb_dump(struct sk_buff *skb,
2763 struct netlink_callback *cb,
2764 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002765 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002766 int idx)
2767{
2768 int err;
2769
2770 netif_addr_lock_bh(dev);
2771 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2772 if (err)
2773 goto out;
2774 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2775out:
2776 netif_addr_unlock_bh(dev);
2777 return idx;
2778}
2779EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2780
John Fastabend77162022012-04-15 06:43:56 +00002781static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2782{
John Fastabend77162022012-04-15 06:43:56 +00002783 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002784 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002785 struct net_device *br_dev = NULL;
2786 const struct net_device_ops *ops = NULL;
2787 const struct net_device_ops *cops = NULL;
2788 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2789 struct net *net = sock_net(skb->sk);
2790 int brport_idx = 0;
2791 int br_idx = 0;
2792 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002793
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002794 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2795 ifla_policy) == 0) {
2796 if (tb[IFLA_MASTER])
2797 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2798 }
John Fastabend77162022012-04-15 06:43:56 +00002799
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002800 brport_idx = ifm->ifi_index;
2801
2802 if (br_idx) {
2803 br_dev = __dev_get_by_index(net, br_idx);
2804 if (!br_dev)
2805 return -ENODEV;
2806
2807 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002808 }
2809
2810 for_each_netdev(net, dev) {
2811 if (brport_idx && (dev->ifindex != brport_idx))
2812 continue;
2813
2814 if (!br_idx) { /* user did not specify a specific bridge */
2815 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2816 br_dev = netdev_master_upper_dev_get(dev);
2817 cops = br_dev->netdev_ops;
2818 }
2819
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002820 } else {
2821 if (dev != br_dev &&
2822 !(dev->priv_flags & IFF_BRIDGE_PORT))
2823 continue;
2824
2825 if (br_dev != netdev_master_upper_dev_get(dev) &&
2826 !(dev->priv_flags & IFF_EBRIDGE))
2827 continue;
2828
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002829 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002830 }
2831
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002832 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2833 if (cops && cops->ndo_fdb_dump)
2834 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2835 idx);
2836 }
2837
John Fastabend77162022012-04-15 06:43:56 +00002838 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002839 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002840 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002841 else
2842 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002843
2844 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002845 }
John Fastabend77162022012-04-15 06:43:56 +00002846
2847 cb->args[0] = idx;
2848 return skb->len;
2849}
2850
Scott Feldman2c3c0312014-11-28 14:34:25 +01002851static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2852 unsigned int attrnum, unsigned int flag)
2853{
2854 if (mask & flag)
2855 return nla_put_u8(skb, attrnum, !!(flags & flag));
2856 return 0;
2857}
2858
John Fastabend815cccb2012-10-24 08:13:09 +00002859int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002860 struct net_device *dev, u16 mode,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002861 u32 flags, u32 mask, int nlflags)
John Fastabend815cccb2012-10-24 08:13:09 +00002862{
2863 struct nlmsghdr *nlh;
2864 struct ifinfomsg *ifm;
2865 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002866 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002867 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002868 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002869
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002870 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00002871 if (nlh == NULL)
2872 return -EMSGSIZE;
2873
2874 ifm = nlmsg_data(nlh);
2875 ifm->ifi_family = AF_BRIDGE;
2876 ifm->__ifi_pad = 0;
2877 ifm->ifi_type = dev->type;
2878 ifm->ifi_index = dev->ifindex;
2879 ifm->ifi_flags = dev_get_flags(dev);
2880 ifm->ifi_change = 0;
2881
2882
2883 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2884 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2885 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002886 (br_dev &&
2887 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002888 (dev->addr_len &&
2889 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02002890 (dev->ifindex != dev_get_iflink(dev) &&
2891 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00002892 goto nla_put_failure;
2893
2894 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2895 if (!br_afspec)
2896 goto nla_put_failure;
2897
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002898 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002899 nla_nest_cancel(skb, br_afspec);
2900 goto nla_put_failure;
2901 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002902
2903 if (mode != BRIDGE_MODE_UNDEF) {
2904 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2905 nla_nest_cancel(skb, br_afspec);
2906 goto nla_put_failure;
2907 }
2908 }
John Fastabend815cccb2012-10-24 08:13:09 +00002909 nla_nest_end(skb, br_afspec);
2910
Scott Feldman2c3c0312014-11-28 14:34:25 +01002911 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2912 if (!protinfo)
2913 goto nla_put_failure;
2914
2915 if (brport_nla_put_flag(skb, flags, mask,
2916 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2917 brport_nla_put_flag(skb, flags, mask,
2918 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2919 brport_nla_put_flag(skb, flags, mask,
2920 IFLA_BRPORT_FAST_LEAVE,
2921 BR_MULTICAST_FAST_LEAVE) ||
2922 brport_nla_put_flag(skb, flags, mask,
2923 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2924 brport_nla_put_flag(skb, flags, mask,
2925 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2926 brport_nla_put_flag(skb, flags, mask,
2927 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2928 brport_nla_put_flag(skb, flags, mask,
2929 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2930 brport_nla_put_flag(skb, flags, mask,
2931 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2932 nla_nest_cancel(skb, protinfo);
2933 goto nla_put_failure;
2934 }
2935
2936 nla_nest_end(skb, protinfo);
2937
Johannes Berg053c0952015-01-16 22:09:00 +01002938 nlmsg_end(skb, nlh);
2939 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00002940nla_put_failure:
2941 nlmsg_cancel(skb, nlh);
2942 return -EMSGSIZE;
2943}
2944EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2945
John Fastabende5a55a82012-10-24 08:12:57 +00002946static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2947{
2948 struct net *net = sock_net(skb->sk);
2949 struct net_device *dev;
2950 int idx = 0;
2951 u32 portid = NETLINK_CB(cb->skb).portid;
2952 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002953 u32 filter_mask = 0;
2954
Thomas Grafaa68c202014-11-26 13:42:20 +01002955 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2956 struct nlattr *extfilt;
2957
2958 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2959 IFLA_EXT_MASK);
2960 if (extfilt) {
2961 if (nla_len(extfilt) < sizeof(filter_mask))
2962 return -EINVAL;
2963
2964 filter_mask = nla_get_u32(extfilt);
2965 }
2966 }
John Fastabende5a55a82012-10-24 08:12:57 +00002967
2968 rcu_read_lock();
2969 for_each_netdev_rcu(net, dev) {
2970 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002971 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002972
Jiri Pirko898e5062013-01-03 22:48:52 +00002973 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002974 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002975 br_dev->netdev_ops->ndo_bridge_getlink(
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002976 skb, portid, seq, dev, filter_mask,
2977 NLM_F_MULTI) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002978 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002979 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002980 }
2981
2982 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002983 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002984 ops->ndo_bridge_getlink(skb, portid, seq, dev,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002985 filter_mask,
2986 NLM_F_MULTI) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002987 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002988 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002989 }
2990 }
2991 rcu_read_unlock();
2992 cb->args[0] = idx;
2993
2994 return skb->len;
2995}
2996
John Fastabend2469ffd2012-10-24 08:13:03 +00002997static inline size_t bridge_nlmsg_size(void)
2998{
2999 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3000 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3001 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3002 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3003 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3004 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3005 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3006 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3007 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3008 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3009 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3010}
3011
Roopa Prabhu02dba432015-01-14 20:02:25 -08003012static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003013{
3014 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003015 struct sk_buff *skb;
3016 int err = -EOPNOTSUPP;
3017
Roopa Prabhu02dba432015-01-14 20:02:25 -08003018 if (!dev->netdev_ops->ndo_bridge_getlink)
3019 return 0;
3020
John Fastabend2469ffd2012-10-24 08:13:03 +00003021 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3022 if (!skb) {
3023 err = -ENOMEM;
3024 goto errout;
3025 }
3026
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003027 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003028 if (err < 0)
3029 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003030
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003031 if (!skb->len)
3032 goto errout;
3033
John Fastabend2469ffd2012-10-24 08:13:03 +00003034 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3035 return 0;
3036errout:
3037 WARN_ON(err == -EMSGSIZE);
3038 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003039 if (err)
3040 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003041 return err;
3042}
3043
Thomas Graf661d2962013-03-21 07:45:29 +00003044static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003045{
3046 struct net *net = sock_net(skb->sk);
3047 struct ifinfomsg *ifm;
3048 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003049 struct nlattr *br_spec, *attr = NULL;
3050 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003051 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003052 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003053
3054 if (nlmsg_len(nlh) < sizeof(*ifm))
3055 return -EINVAL;
3056
3057 ifm = nlmsg_data(nlh);
3058 if (ifm->ifi_family != AF_BRIDGE)
3059 return -EPFNOSUPPORT;
3060
3061 dev = __dev_get_by_index(net, ifm->ifi_index);
3062 if (!dev) {
3063 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3064 return -ENODEV;
3065 }
3066
John Fastabend2469ffd2012-10-24 08:13:03 +00003067 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3068 if (br_spec) {
3069 nla_for_each_nested(attr, br_spec, rem) {
3070 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003071 if (nla_len(attr) < sizeof(flags))
3072 return -EINVAL;
3073
John Fastabendc38e01b2012-11-02 16:32:36 +00003074 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003075 flags = nla_get_u16(attr);
3076 break;
3077 }
3078 }
3079 }
3080
3081 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003082 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3083
3084 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003085 err = -EOPNOTSUPP;
3086 goto out;
3087 }
3088
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003089 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003090 if (err)
3091 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003092
3093 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003094 }
3095
John Fastabend2469ffd2012-10-24 08:13:03 +00003096 if ((flags & BRIDGE_FLAGS_SELF)) {
3097 if (!dev->netdev_ops->ndo_bridge_setlink)
3098 err = -EOPNOTSUPP;
3099 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003100 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3101 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003102 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003103 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003104
3105 /* Generate event to notify upper layer of bridge
3106 * change
3107 */
3108 err = rtnl_bridge_notify(dev);
3109 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003110 }
3111
John Fastabendc38e01b2012-11-02 16:32:36 +00003112 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003113 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003114out:
3115 return err;
3116}
3117
Thomas Graf661d2962013-03-21 07:45:29 +00003118static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003119{
3120 struct net *net = sock_net(skb->sk);
3121 struct ifinfomsg *ifm;
3122 struct net_device *dev;
3123 struct nlattr *br_spec, *attr = NULL;
3124 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003125 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003126 bool have_flags = false;
3127
3128 if (nlmsg_len(nlh) < sizeof(*ifm))
3129 return -EINVAL;
3130
3131 ifm = nlmsg_data(nlh);
3132 if (ifm->ifi_family != AF_BRIDGE)
3133 return -EPFNOSUPPORT;
3134
3135 dev = __dev_get_by_index(net, ifm->ifi_index);
3136 if (!dev) {
3137 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3138 return -ENODEV;
3139 }
3140
3141 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3142 if (br_spec) {
3143 nla_for_each_nested(attr, br_spec, rem) {
3144 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003145 if (nla_len(attr) < sizeof(flags))
3146 return -EINVAL;
3147
Vlad Yasevich407af322013-02-13 12:00:12 +00003148 have_flags = true;
3149 flags = nla_get_u16(attr);
3150 break;
3151 }
3152 }
3153 }
3154
Vlad Yasevich407af322013-02-13 12:00:12 +00003155 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3156 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3157
3158 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3159 err = -EOPNOTSUPP;
3160 goto out;
3161 }
3162
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003163 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003164 if (err)
3165 goto out;
3166
3167 flags &= ~BRIDGE_FLAGS_MASTER;
3168 }
3169
3170 if ((flags & BRIDGE_FLAGS_SELF)) {
3171 if (!dev->netdev_ops->ndo_bridge_dellink)
3172 err = -EOPNOTSUPP;
3173 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003174 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3175 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003176
Roopa Prabhu02dba432015-01-14 20:02:25 -08003177 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003178 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003179
3180 /* Generate event to notify upper layer of bridge
3181 * change
3182 */
3183 err = rtnl_bridge_notify(dev);
3184 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003185 }
3186
3187 if (have_flags)
3188 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003189out:
3190 return err;
3191}
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193/* Process one rtnetlink message. */
3194
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003195static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003197 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003198 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 int family;
3201 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003202 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003206 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 type -= RTM_BASE;
3209
3210 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003211 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return 0;
3213
Hong zhi guo573ce262013-03-27 06:47:04 +00003214 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 sz_idx = type>>2;
3216 kind = type&3;
3217
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003218 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003219 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
David S. Millerb8f3ab42011-01-18 12:40:38 -08003221 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003222 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003223 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003224 rtnl_calcit_func calcit;
3225 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Thomas Grafe2849862007-03-22 11:48:11 -07003227 dumpit = rtnl_get_dumpit(family, type);
3228 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003229 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003230 calcit = rtnl_get_calcit(family, type);
3231 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003232 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Eric Dumazet2907c352011-05-25 07:34:04 +00003234 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003235 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003236 {
3237 struct netlink_dump_control c = {
3238 .dump = dumpit,
3239 .min_dump_alloc = min_dump_alloc,
3240 };
3241 err = netlink_dump_start(rtnl, skb, nlh, &c);
3242 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003243 rtnl_lock();
3244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246
Thomas Grafe2849862007-03-22 11:48:11 -07003247 doit = rtnl_get_doit(family, type);
3248 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003249 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Thomas Graf661d2962013-03-21 07:45:29 +00003251 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003254static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003256 rtnl_lock();
3257 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3258 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3262{
Jiri Pirko351638e2013-05-28 01:30:21 +00003263 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 case NETDEV_UP:
3267 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003268 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003269 case NETDEV_POST_INIT:
3270 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003272 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003274 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003275 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003276 case NETDEV_RELEASE:
3277 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003278 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 break;
3280 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003281 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 break;
3283 }
3284 return NOTIFY_DONE;
3285}
3286
3287static struct notifier_block rtnetlink_dev_notifier = {
3288 .notifier_call = rtnetlink_event,
3289};
3290
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003291
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003292static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003293{
3294 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003295 struct netlink_kernel_cfg cfg = {
3296 .groups = RTNLGRP_MAX,
3297 .input = rtnetlink_rcv,
3298 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003299 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003300 };
3301
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003302 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003303 if (!sk)
3304 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003305 net->rtnl = sk;
3306 return 0;
3307}
3308
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003309static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003310{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003311 netlink_kernel_release(net->rtnl);
3312 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003313}
3314
3315static struct pernet_operations rtnetlink_net_ops = {
3316 .init = rtnetlink_net_init,
3317 .exit = rtnetlink_net_exit,
3318};
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320void __init rtnetlink_init(void)
3321{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003322 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003326
Greg Rosec7ac8672011-06-10 01:27:09 +00003327 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3328 rtnl_dump_ifinfo, rtnl_calcit);
3329 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3330 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3331 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003332
Greg Rosec7ac8672011-06-10 01:27:09 +00003333 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3334 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003335
3336 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3337 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3338 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003339
3340 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003341 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003342 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344