blob: 8de36824018de4da2369fb02234692c4e0260b27 [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;
1007 struct netdev_phys_item_id psid;
1008
1009 err = netdev_switch_parent_id_get(dev, &psid);
1010 if (err) {
1011 if (err == -EOPNOTSUPP)
1012 return 0;
1013 return err;
1014 }
1015
1016 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
1017 return -EMSGSIZE;
1018
1019 return 0;
1020}
1021
Thomas Grafb60c5112006-08-04 23:05:34 -07001022static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001023 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -05001024 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -07001025{
1026 struct ifinfomsg *ifm;
1027 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -07001028 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00001029 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +00001030 struct nlattr *attr, *af_spec;
1031 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001032 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001033
Eric Dumazet2907c352011-05-25 07:34:04 +00001034 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001035 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1036 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001037 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001038
1039 ifm = nlmsg_data(nlh);
1040 ifm->ifi_family = AF_UNSPEC;
1041 ifm->__ifi_pad = 0;
1042 ifm->ifi_type = dev->type;
1043 ifm->ifi_index = dev->ifindex;
1044 ifm->ifi_flags = dev_get_flags(dev);
1045 ifm->ifi_change = change;
1046
David S. Millera6574342012-04-01 20:12:00 -04001047 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1048 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1049 nla_put_u8(skb, IFLA_OPERSTATE,
1050 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1051 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1052 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1053 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001054 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001055 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001056#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001057 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001058#endif
Nicolas Dichtela54acb32015-04-02 17:07:00 +02001059 (dev->ifindex != dev_get_iflink(dev) &&
1060 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001061 (upper_dev &&
1062 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001063 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001064 (dev->qdisc &&
1065 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1066 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001067 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1068 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1069 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001070 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001071
Stefan Rompfb00055a2006-03-20 17:09:11 -08001072 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct rtnl_link_ifmap map = {
1074 .mem_start = dev->mem_start,
1075 .mem_end = dev->mem_end,
1076 .base_addr = dev->base_addr,
1077 .irq = dev->irq,
1078 .dma = dev->dma,
1079 .port = dev->if_port,
1080 };
David S. Millera6574342012-04-01 20:12:00 -04001081 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1082 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001086 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1087 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1088 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001091 if (rtnl_phys_port_id_fill(skb, dev))
1092 goto nla_put_failure;
1093
David Aherndb24a902015-03-17 20:23:15 -06001094 if (rtnl_phys_port_name_fill(skb, dev))
1095 goto nla_put_failure;
1096
Jiri Pirko82f28412014-11-28 14:34:18 +01001097 if (rtnl_phys_switch_id_fill(skb, dev))
1098 goto nla_put_failure;
1099
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001100 attr = nla_reserve(skb, IFLA_STATS,
1101 sizeof(struct rtnl_link_stats));
1102 if (attr == NULL)
1103 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Eric Dumazet28172732010-07-07 14:58:56 -07001105 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001106 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jan Engelhardt10708f32010-03-11 09:57:29 +00001108 attr = nla_reserve(skb, IFLA_STATS64,
1109 sizeof(struct rtnl_link_stats64));
1110 if (attr == NULL)
1111 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001112 copy_rtnl_link_stats64(nla_data(attr), stats);
1113
David S. Millera6574342012-04-01 20:12:00 -04001114 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1115 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1116 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001117
Greg Rose115c9b82012-02-21 16:54:48 -05001118 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1119 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001120 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001121
Chris Wrightc02db8c2010-05-16 01:05:45 -07001122 struct nlattr *vfinfo, *vf;
1123 int num_vfs = dev_num_vf(dev->dev.parent);
1124
Chris Wrightc02db8c2010-05-16 01:05:45 -07001125 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1126 if (!vfinfo)
1127 goto nla_put_failure;
1128 for (i = 0; i < num_vfs; i++) {
1129 struct ifla_vf_info ivi;
1130 struct ifla_vf_mac vf_mac;
1131 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001132 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001133 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001134 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001135 struct ifla_vf_link_state vf_linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001136 struct ifla_vf_rss_query_en vf_rss_query_en;
Greg Rose5f8444a2011-10-08 03:05:24 +00001137
1138 /*
1139 * Not all SR-IOV capable drivers support the
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001140 * spoofcheck and "RSS query enable" query. Preset to
1141 * -1 so the user space tool can detect that the driver
1142 * didn't report anything.
Greg Rose5f8444a2011-10-08 03:05:24 +00001143 */
1144 ivi.spoofchk = -1;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001145 ivi.rss_query_en = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001146 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001147 /* The default value for VF link state is "auto"
1148 * IFLA_VF_LINK_STATE_AUTO which equals zero
1149 */
1150 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001151 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1152 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001153 vf_mac.vf =
1154 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001155 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001156 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001157 vf_spoofchk.vf =
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001158 vf_linkstate.vf =
1159 vf_rss_query_en.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001160
Chris Wrightc02db8c2010-05-16 01:05:45 -07001161 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1162 vf_vlan.vlan = ivi.vlan;
1163 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001164 vf_tx_rate.rate = ivi.max_tx_rate;
1165 vf_rate.min_tx_rate = ivi.min_tx_rate;
1166 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001167 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001168 vf_linkstate.link_state = ivi.linkstate;
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001169 vf_rss_query_en.setting = ivi.rss_query_en;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001170 vf = nla_nest_start(skb, IFLA_VF_INFO);
1171 if (!vf) {
1172 nla_nest_cancel(skb, vfinfo);
1173 goto nla_put_failure;
1174 }
David S. Millera6574342012-04-01 20:12:00 -04001175 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1176 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001177 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1178 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001179 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1180 &vf_tx_rate) ||
1181 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001182 &vf_spoofchk) ||
1183 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001184 &vf_linkstate) ||
1185 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1186 sizeof(vf_rss_query_en),
1187 &vf_rss_query_en))
David S. Millera6574342012-04-01 20:12:00 -04001188 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001189 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001190 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001191 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001192 }
Scott Feldman57b61082010-05-17 22:49:55 -07001193
David Gibsonc53864f2014-04-24 10:22:36 +10001194 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001195 goto nla_put_failure;
1196
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001197 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001198 if (rtnl_link_fill(skb, dev) < 0)
1199 goto nla_put_failure;
1200 }
1201
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001202 if (dev->rtnl_link_ops &&
1203 dev->rtnl_link_ops->get_link_net) {
1204 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1205
1206 if (!net_eq(dev_net(dev), link_net)) {
1207 int id = peernet2id(dev_net(dev), link_net);
1208
1209 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1210 goto nla_put_failure;
1211 }
1212 }
1213
Thomas Graff8ff1822010-11-16 04:30:14 +00001214 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1215 goto nla_put_failure;
1216
1217 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1218 if (af_ops->fill_link_af) {
1219 struct nlattr *af;
1220 int err;
1221
1222 if (!(af = nla_nest_start(skb, af_ops->family)))
1223 goto nla_put_failure;
1224
1225 err = af_ops->fill_link_af(skb, dev);
1226
1227 /*
1228 * Caller may return ENODATA to indicate that there
1229 * was no data to be dumped. This is not an error, it
1230 * means we should trim the attribute header and
1231 * continue.
1232 */
1233 if (err == -ENODATA)
1234 nla_nest_cancel(skb, af);
1235 else if (err < 0)
1236 goto nla_put_failure;
1237
1238 nla_nest_end(skb, af);
1239 }
1240 }
1241
1242 nla_nest_end(skb, af_spec);
1243
Johannes Berg053c0952015-01-16 22:09:00 +01001244 nlmsg_end(skb, nlh);
1245 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001246
1247nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001248 nlmsg_cancel(skb, nlh);
1249 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Jiri Pirkof7b12602014-02-18 20:53:18 +01001252static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1253 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1254 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1255 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1256 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1257 [IFLA_MTU] = { .type = NLA_U32 },
1258 [IFLA_LINK] = { .type = NLA_U32 },
1259 [IFLA_MASTER] = { .type = NLA_U32 },
1260 [IFLA_CARRIER] = { .type = NLA_U8 },
1261 [IFLA_TXQLEN] = { .type = NLA_U32 },
1262 [IFLA_WEIGHT] = { .type = NLA_U32 },
1263 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1264 [IFLA_LINKMODE] = { .type = NLA_U8 },
1265 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1266 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1267 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1268 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1269 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1270 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1271 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1272 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1273 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1274 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1275 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1276 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001277 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001278 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001279 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001280 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001281};
1282
1283static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1284 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1285 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1286 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1287 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1288};
1289
1290static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1291 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1292};
1293
1294static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001295 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1296 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
1297 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1298 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1299 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1300 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001301 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001302};
1303
1304static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1305 [IFLA_PORT_VF] = { .type = NLA_U32 },
1306 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1307 .len = PORT_PROFILE_MAX },
1308 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1309 .len = sizeof(struct ifla_port_vsi)},
1310 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1311 .len = PORT_UUID_MAX },
1312 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1313 .len = PORT_UUID_MAX },
1314 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1315 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1316};
1317
Thomas Grafb60c5112006-08-04 23:05:34 -07001318static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001320 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001321 int h, s_h;
1322 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001324 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001325 struct nlattr *tb[IFLA_MAX+1];
1326 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001327 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001328 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001330 s_h = cb->args[0];
1331 s_idx = cb->args[1];
1332
Thomas Graf4e985ad2011-06-21 03:11:20 +00001333 cb->seq = net->dev_base_seq;
1334
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001335 /* A hack to preserve kernel<->userspace interface.
1336 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1337 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1338 * what iproute2 < v3.9.0 used.
1339 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1340 * attribute, its netlink message is shorter than struct ifinfomsg.
1341 */
1342 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1343 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1344
1345 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001346
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001347 if (tb[IFLA_EXT_MASK])
1348 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1349 }
Greg Rose115c9b82012-02-21 16:54:48 -05001350
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001351 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1352 idx = 0;
1353 head = &net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001354 hlist_for_each_entry(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001355 if (idx < s_idx)
1356 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001357 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1358 NETLINK_CB(cb->skb).portid,
1359 cb->nlh->nlmsg_seq, 0,
1360 NLM_F_MULTI,
1361 ext_filter_mask);
1362 /* If we ran out of room on the first message,
1363 * we're in trouble
1364 */
1365 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1366
David S. Miller7b46a642015-01-18 23:36:08 -05001367 if (err < 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001368 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001369
1370 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001371cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001372 idx++;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001375out:
1376 cb->args[1] = idx;
1377 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 return skb->len;
1380}
1381
Jiri Pirkof7b12602014-02-18 20:53:18 +01001382int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1383{
1384 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1385}
1386EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001387
Eric W. Biederman81adee42009-11-08 00:53:51 -08001388struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1389{
1390 struct net *net;
1391 /* Examine the link attributes and figure out which
1392 * network namespace we are talking about.
1393 */
1394 if (tb[IFLA_NET_NS_PID])
1395 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001396 else if (tb[IFLA_NET_NS_FD])
1397 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001398 else
1399 net = get_net(src_net);
1400 return net;
1401}
1402EXPORT_SYMBOL(rtnl_link_get_net);
1403
Thomas Graf1840bb12008-02-23 19:54:36 -08001404static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1405{
1406 if (dev) {
1407 if (tb[IFLA_ADDRESS] &&
1408 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1409 return -EINVAL;
1410
1411 if (tb[IFLA_BROADCAST] &&
1412 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1413 return -EINVAL;
1414 }
1415
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001416 if (tb[IFLA_AF_SPEC]) {
1417 struct nlattr *af;
1418 int rem, err;
1419
1420 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1421 const struct rtnl_af_ops *af_ops;
1422
1423 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1424 return -EAFNOSUPPORT;
1425
1426 if (!af_ops->set_link_af)
1427 return -EOPNOTSUPP;
1428
1429 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001430 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001431 if (err < 0)
1432 return err;
1433 }
1434 }
1435 }
1436
Thomas Graf1840bb12008-02-23 19:54:36 -08001437 return 0;
1438}
1439
Chris Wrightc02db8c2010-05-16 01:05:45 -07001440static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1441{
1442 int rem, err = -EINVAL;
1443 struct nlattr *vf;
1444 const struct net_device_ops *ops = dev->netdev_ops;
1445
1446 nla_for_each_nested(vf, attr, rem) {
1447 switch (nla_type(vf)) {
1448 case IFLA_VF_MAC: {
1449 struct ifla_vf_mac *ivm;
1450 ivm = nla_data(vf);
1451 err = -EOPNOTSUPP;
1452 if (ops->ndo_set_vf_mac)
1453 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1454 ivm->mac);
1455 break;
1456 }
1457 case IFLA_VF_VLAN: {
1458 struct ifla_vf_vlan *ivv;
1459 ivv = nla_data(vf);
1460 err = -EOPNOTSUPP;
1461 if (ops->ndo_set_vf_vlan)
1462 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1463 ivv->vlan,
1464 ivv->qos);
1465 break;
1466 }
1467 case IFLA_VF_TX_RATE: {
1468 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001469 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001470 ivt = nla_data(vf);
1471 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001472 if (ops->ndo_get_vf_config)
1473 err = ops->ndo_get_vf_config(dev, ivt->vf,
1474 &ivf);
1475 if (err)
1476 break;
1477 err = -EOPNOTSUPP;
1478 if (ops->ndo_set_vf_rate)
1479 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1480 ivf.min_tx_rate,
1481 ivt->rate);
1482 break;
1483 }
1484 case IFLA_VF_RATE: {
1485 struct ifla_vf_rate *ivt;
1486 ivt = nla_data(vf);
1487 err = -EOPNOTSUPP;
1488 if (ops->ndo_set_vf_rate)
1489 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1490 ivt->min_tx_rate,
1491 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001492 break;
1493 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001494 case IFLA_VF_SPOOFCHK: {
1495 struct ifla_vf_spoofchk *ivs;
1496 ivs = nla_data(vf);
1497 err = -EOPNOTSUPP;
1498 if (ops->ndo_set_vf_spoofchk)
1499 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1500 ivs->setting);
1501 break;
1502 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001503 case IFLA_VF_LINK_STATE: {
1504 struct ifla_vf_link_state *ivl;
1505 ivl = nla_data(vf);
1506 err = -EOPNOTSUPP;
1507 if (ops->ndo_set_vf_link_state)
1508 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1509 ivl->link_state);
1510 break;
1511 }
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001512 case IFLA_VF_RSS_QUERY_EN: {
1513 struct ifla_vf_rss_query_en *ivrssq_en;
1514
1515 ivrssq_en = nla_data(vf);
1516 err = -EOPNOTSUPP;
1517 if (ops->ndo_set_vf_rss_query_en)
1518 err = ops->ndo_set_vf_rss_query_en(dev,
1519 ivrssq_en->vf,
1520 ivrssq_en->setting);
1521 break;
1522 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001523 default:
1524 err = -EINVAL;
1525 break;
1526 }
1527 if (err)
1528 break;
1529 }
1530 return err;
1531}
1532
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001533static int do_set_master(struct net_device *dev, int ifindex)
1534{
Jiri Pirko898e5062013-01-03 22:48:52 +00001535 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001536 const struct net_device_ops *ops;
1537 int err;
1538
Jiri Pirko898e5062013-01-03 22:48:52 +00001539 if (upper_dev) {
1540 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001541 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001542 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001543 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001544 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001545 if (err)
1546 return err;
1547 } else {
1548 return -EOPNOTSUPP;
1549 }
1550 }
1551
1552 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001553 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1554 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001555 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001556 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001557 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001558 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001559 if (err)
1560 return err;
1561 } else {
1562 return -EOPNOTSUPP;
1563 }
1564 }
1565 return 0;
1566}
1567
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001568#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001569/* notify flag means notify + modified. */
1570#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001571static int do_setlink(const struct sk_buff *skb,
1572 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001573 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001574{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001575 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001576 int err;
1577
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001578 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001579 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001580 if (IS_ERR(net)) {
1581 err = PTR_ERR(net);
1582 goto errout;
1583 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001584 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001585 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001586 err = -EPERM;
1587 goto errout;
1588 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001589 err = dev_change_net_namespace(dev, net, ifname);
1590 put_net(net);
1591 if (err)
1592 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001593 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001594 }
1595
Patrick McHardy0157f602007-06-13 12:03:36 -07001596 if (tb[IFLA_MAP]) {
1597 struct rtnl_link_ifmap *u_map;
1598 struct ifmap k_map;
1599
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001600 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001601 err = -EOPNOTSUPP;
1602 goto errout;
1603 }
1604
1605 if (!netif_device_present(dev)) {
1606 err = -ENODEV;
1607 goto errout;
1608 }
1609
1610 u_map = nla_data(tb[IFLA_MAP]);
1611 k_map.mem_start = (unsigned long) u_map->mem_start;
1612 k_map.mem_end = (unsigned long) u_map->mem_end;
1613 k_map.base_addr = (unsigned short) u_map->base_addr;
1614 k_map.irq = (unsigned char) u_map->irq;
1615 k_map.dma = (unsigned char) u_map->dma;
1616 k_map.port = (unsigned char) u_map->port;
1617
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001618 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001619 if (err < 0)
1620 goto errout;
1621
Nicolas Dichtelba998902014-09-01 16:07:29 +02001622 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001623 }
1624
1625 if (tb[IFLA_ADDRESS]) {
1626 struct sockaddr *sa;
1627 int len;
1628
Patrick McHardy0157f602007-06-13 12:03:36 -07001629 len = sizeof(sa_family_t) + dev->addr_len;
1630 sa = kmalloc(len, GFP_KERNEL);
1631 if (!sa) {
1632 err = -ENOMEM;
1633 goto errout;
1634 }
1635 sa->sa_family = dev->type;
1636 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1637 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001638 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001639 kfree(sa);
1640 if (err)
1641 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001642 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001643 }
1644
1645 if (tb[IFLA_MTU]) {
1646 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1647 if (err < 0)
1648 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001649 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001650 }
1651
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001652 if (tb[IFLA_GROUP]) {
1653 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001654 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001655 }
1656
Patrick McHardy0157f602007-06-13 12:03:36 -07001657 /*
1658 * Interface selected by interface index but interface
1659 * name provided implies that a name change has been
1660 * requested.
1661 */
1662 if (ifm->ifi_index > 0 && ifname[0]) {
1663 err = dev_change_name(dev, ifname);
1664 if (err < 0)
1665 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001666 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001667 }
1668
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001669 if (tb[IFLA_IFALIAS]) {
1670 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1671 nla_len(tb[IFLA_IFALIAS]));
1672 if (err < 0)
1673 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001674 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001675 }
1676
Patrick McHardy0157f602007-06-13 12:03:36 -07001677 if (tb[IFLA_BROADCAST]) {
1678 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001679 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001680 }
1681
1682 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001683 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001684 if (err < 0)
1685 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001686 }
1687
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001688 if (tb[IFLA_MASTER]) {
1689 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1690 if (err)
1691 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001692 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001693 }
1694
Jiri Pirko9a572472012-12-27 23:49:39 +00001695 if (tb[IFLA_CARRIER]) {
1696 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1697 if (err)
1698 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001699 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001700 }
1701
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001702 if (tb[IFLA_TXQLEN]) {
1703 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1704
1705 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001706 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001707
1708 dev->tx_queue_len = value;
1709 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001710
Patrick McHardy0157f602007-06-13 12:03:36 -07001711 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001712 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001713
1714 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001715 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1716
David S. Miller93b2d4a2008-02-17 18:35:07 -08001717 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001718 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001719 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001720 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001721 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001722 }
1723
Chris Wrightc02db8c2010-05-16 01:05:45 -07001724 if (tb[IFLA_VFINFO_LIST]) {
1725 struct nlattr *attr;
1726 int rem;
1727 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001728 if (nla_type(attr) != IFLA_VF_INFO) {
1729 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001730 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001731 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001732 err = do_setvfinfo(dev, attr);
1733 if (err < 0)
1734 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001735 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001736 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001737 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001738 err = 0;
1739
Scott Feldman57b61082010-05-17 22:49:55 -07001740 if (tb[IFLA_VF_PORTS]) {
1741 struct nlattr *port[IFLA_PORT_MAX+1];
1742 struct nlattr *attr;
1743 int vf;
1744 int rem;
1745
1746 err = -EOPNOTSUPP;
1747 if (!ops->ndo_set_vf_port)
1748 goto errout;
1749
1750 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1751 if (nla_type(attr) != IFLA_VF_PORT)
1752 continue;
1753 err = nla_parse_nested(port, IFLA_PORT_MAX,
1754 attr, ifla_port_policy);
1755 if (err < 0)
1756 goto errout;
1757 if (!port[IFLA_PORT_VF]) {
1758 err = -EOPNOTSUPP;
1759 goto errout;
1760 }
1761 vf = nla_get_u32(port[IFLA_PORT_VF]);
1762 err = ops->ndo_set_vf_port(dev, vf, port);
1763 if (err < 0)
1764 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001765 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001766 }
1767 }
1768 err = 0;
1769
1770 if (tb[IFLA_PORT_SELF]) {
1771 struct nlattr *port[IFLA_PORT_MAX+1];
1772
1773 err = nla_parse_nested(port, IFLA_PORT_MAX,
1774 tb[IFLA_PORT_SELF], ifla_port_policy);
1775 if (err < 0)
1776 goto errout;
1777
1778 err = -EOPNOTSUPP;
1779 if (ops->ndo_set_vf_port)
1780 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1781 if (err < 0)
1782 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001783 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001784 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001785
1786 if (tb[IFLA_AF_SPEC]) {
1787 struct nlattr *af;
1788 int rem;
1789
1790 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1791 const struct rtnl_af_ops *af_ops;
1792
1793 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001794 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001795
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001796 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001797 if (err < 0)
1798 goto errout;
1799
Nicolas Dichtelba998902014-09-01 16:07:29 +02001800 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001801 }
1802 }
Scott Feldman57b61082010-05-17 22:49:55 -07001803 err = 0;
1804
Patrick McHardy0157f602007-06-13 12:03:36 -07001805errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001806 if (status & DO_SETLINK_MODIFIED) {
1807 if (status & DO_SETLINK_NOTIFY)
1808 netdev_state_change(dev);
1809
1810 if (err < 0)
1811 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",
1812 dev->name);
1813 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001814
Patrick McHardy0157f602007-06-13 12:03:36 -07001815 return err;
1816}
1817
Thomas Graf661d2962013-03-21 07:45:29 +00001818static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001820 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001821 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001823 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001824 struct nlattr *tb[IFLA_MAX+1];
1825 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Thomas Grafda5e0492006-08-10 21:17:37 -07001827 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1828 if (err < 0)
1829 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Thomas Graf5176f912006-08-26 20:13:18 -07001831 if (tb[IFLA_IFNAME])
1832 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001833 else
1834 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001837 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001838 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001839 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001840 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001841 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001842 else
1843 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Thomas Grafda5e0492006-08-10 21:17:37 -07001845 if (dev == NULL) {
1846 err = -ENODEV;
1847 goto errout;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Eric Dumazete0d087a2009-11-07 01:26:17 -08001850 err = validate_linkmsg(dev, tb);
1851 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001852 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001853
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001854 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001855errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return err;
1857}
1858
WANG Cong66400d52015-03-24 11:53:31 -07001859static int rtnl_group_dellink(const struct net *net, int group)
1860{
1861 struct net_device *dev, *aux;
1862 LIST_HEAD(list_kill);
1863 bool found = false;
1864
1865 if (!group)
1866 return -EPERM;
1867
1868 for_each_netdev(net, dev) {
1869 if (dev->group == group) {
1870 const struct rtnl_link_ops *ops;
1871
1872 found = true;
1873 ops = dev->rtnl_link_ops;
1874 if (!ops || !ops->dellink)
1875 return -EOPNOTSUPP;
1876 }
1877 }
1878
1879 if (!found)
1880 return -ENODEV;
1881
1882 for_each_netdev_safe(net, dev, aux) {
1883 if (dev->group == group) {
1884 const struct rtnl_link_ops *ops;
1885
1886 ops = dev->rtnl_link_ops;
1887 ops->dellink(dev, &list_kill);
1888 }
1889 }
1890 unregister_netdevice_many(&list_kill);
1891
1892 return 0;
1893}
1894
Thomas Graf661d2962013-03-21 07:45:29 +00001895static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001896{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001897 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001898 const struct rtnl_link_ops *ops;
1899 struct net_device *dev;
1900 struct ifinfomsg *ifm;
1901 char ifname[IFNAMSIZ];
1902 struct nlattr *tb[IFLA_MAX+1];
1903 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001904 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001905
1906 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1907 if (err < 0)
1908 return err;
1909
1910 if (tb[IFLA_IFNAME])
1911 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1912
1913 ifm = nlmsg_data(nlh);
1914 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001915 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001916 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001917 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07001918 else if (tb[IFLA_GROUP])
1919 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07001920 else
1921 return -EINVAL;
1922
1923 if (!dev)
1924 return -ENODEV;
1925
1926 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001927 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001928 return -EOPNOTSUPP;
1929
Eric Dumazet226bd342011-05-08 23:17:57 +00001930 ops->dellink(dev, &list_kill);
1931 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001932 return 0;
1933}
1934
Patrick McHardy3729d502010-02-26 06:34:54 +00001935int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1936{
1937 unsigned int old_flags;
1938 int err;
1939
1940 old_flags = dev->flags;
1941 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1942 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1943 if (err < 0)
1944 return err;
1945 }
1946
1947 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001948
Nicolas Dichtela528c212013-09-25 12:02:44 +02001949 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001950 return 0;
1951}
1952EXPORT_SYMBOL(rtnl_configure_link);
1953
Rami Rosenc0713562012-11-30 01:08:47 +00001954struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02001955 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02001956 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001957{
1958 int err;
1959 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001960 unsigned int num_tx_queues = 1;
1961 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001962
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001963 if (tb[IFLA_NUM_TX_QUEUES])
1964 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1965 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001966 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001967
1968 if (tb[IFLA_NUM_RX_QUEUES])
1969 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1970 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001971 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001972
Pavel Emelianove7199282007-08-08 22:16:38 -07001973 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001974 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001975 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001976 if (!dev)
1977 goto err;
1978
Eric W. Biederman81adee42009-11-08 00:53:51 -08001979 dev_net_set(dev, net);
1980 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001981 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001982
Pavel Emelianove7199282007-08-08 22:16:38 -07001983 if (tb[IFLA_MTU])
1984 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001985 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001986 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1987 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001988 dev->addr_assign_type = NET_ADDR_SET;
1989 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001990 if (tb[IFLA_BROADCAST])
1991 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1992 nla_len(tb[IFLA_BROADCAST]));
1993 if (tb[IFLA_TXQLEN])
1994 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1995 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001996 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001997 if (tb[IFLA_LINKMODE])
1998 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001999 if (tb[IFLA_GROUP])
2000 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002001
2002 return dev;
2003
Pavel Emelianove7199282007-08-08 22:16:38 -07002004err:
2005 return ERR_PTR(err);
2006}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002007EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002008
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002009static int rtnl_group_changelink(const struct sk_buff *skb,
2010 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002011 struct ifinfomsg *ifm,
2012 struct nlattr **tb)
2013{
WANG Congd0795352015-03-23 16:31:09 -07002014 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002015 int err;
2016
WANG Congd0795352015-03-23 16:31:09 -07002017 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002018 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002019 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002020 if (err < 0)
2021 return err;
2022 }
2023 }
2024
2025 return 0;
2026}
2027
Thomas Graf661d2962013-03-21 07:45:29 +00002028static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002029{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002030 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002031 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002032 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002033 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002034 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002035 struct ifinfomsg *ifm;
2036 char kind[MODULE_NAME_LEN];
2037 char ifname[IFNAMSIZ];
2038 struct nlattr *tb[IFLA_MAX+1];
2039 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002040 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002041 int err;
2042
Johannes Berg95a5afc2008-10-16 15:24:51 -07002043#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002044replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002045#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07002046 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2047 if (err < 0)
2048 return err;
2049
2050 if (tb[IFLA_IFNAME])
2051 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2052 else
2053 ifname[0] = '\0';
2054
2055 ifm = nlmsg_data(nlh);
2056 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002057 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002058 else {
2059 if (ifname[0])
2060 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002061 else
2062 dev = NULL;
2063 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002064
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002065 if (dev) {
2066 master_dev = netdev_master_upper_dev_get(dev);
2067 if (master_dev)
2068 m_ops = master_dev->rtnl_link_ops;
2069 }
2070
Eric Dumazete0d087a2009-11-07 01:26:17 -08002071 err = validate_linkmsg(dev, tb);
2072 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002073 return err;
2074
Patrick McHardy38f7b872007-06-13 12:03:51 -07002075 if (tb[IFLA_LINKINFO]) {
2076 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2077 tb[IFLA_LINKINFO], ifla_info_policy);
2078 if (err < 0)
2079 return err;
2080 } else
2081 memset(linkinfo, 0, sizeof(linkinfo));
2082
2083 if (linkinfo[IFLA_INFO_KIND]) {
2084 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2085 ops = rtnl_link_ops_get(kind);
2086 } else {
2087 kind[0] = '\0';
2088 ops = NULL;
2089 }
2090
2091 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002092 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2093 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002094 struct nlattr **data = NULL;
2095 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002096 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002097
2098 if (ops) {
2099 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2100 err = nla_parse_nested(attr, ops->maxtype,
2101 linkinfo[IFLA_INFO_DATA],
2102 ops->policy);
2103 if (err < 0)
2104 return err;
2105 data = attr;
2106 }
2107 if (ops->validate) {
2108 err = ops->validate(tb, data);
2109 if (err < 0)
2110 return err;
2111 }
2112 }
2113
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002114 if (m_ops) {
2115 if (m_ops->slave_maxtype &&
2116 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2117 err = nla_parse_nested(slave_attr,
2118 m_ops->slave_maxtype,
2119 linkinfo[IFLA_INFO_SLAVE_DATA],
2120 m_ops->slave_policy);
2121 if (err < 0)
2122 return err;
2123 slave_data = slave_attr;
2124 }
2125 if (m_ops->slave_validate) {
2126 err = m_ops->slave_validate(tb, slave_data);
2127 if (err < 0)
2128 return err;
2129 }
2130 }
2131
Patrick McHardy38f7b872007-06-13 12:03:51 -07002132 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002133 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002134
2135 if (nlh->nlmsg_flags & NLM_F_EXCL)
2136 return -EEXIST;
2137 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2138 return -EOPNOTSUPP;
2139
2140 if (linkinfo[IFLA_INFO_DATA]) {
2141 if (!ops || ops != dev->rtnl_link_ops ||
2142 !ops->changelink)
2143 return -EOPNOTSUPP;
2144
2145 err = ops->changelink(dev, tb, data);
2146 if (err < 0)
2147 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002148 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002149 }
2150
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002151 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2152 if (!m_ops || !m_ops->slave_changelink)
2153 return -EOPNOTSUPP;
2154
2155 err = m_ops->slave_changelink(master_dev, dev,
2156 tb, slave_data);
2157 if (err < 0)
2158 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002159 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002160 }
2161
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002162 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002163 }
2164
Patrick McHardyffa934f2011-01-20 03:00:42 +00002165 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2166 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002167 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002168 nla_get_u32(tb[IFLA_GROUP]),
2169 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002170 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002171 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002172
Patrick McHardy0e068772007-07-11 19:42:31 -07002173 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002174 return -EOPNOTSUPP;
2175
2176 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002177#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002178 if (kind[0]) {
2179 __rtnl_unlock();
2180 request_module("rtnl-link-%s", kind);
2181 rtnl_lock();
2182 ops = rtnl_link_ops_get(kind);
2183 if (ops)
2184 goto replay;
2185 }
2186#endif
2187 return -EOPNOTSUPP;
2188 }
2189
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002190 if (!ops->setup)
2191 return -EOPNOTSUPP;
2192
Tom Gundersen55177502014-07-14 16:37:25 +02002193 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002194 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002195 name_assign_type = NET_NAME_ENUM;
2196 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002197
Eric W. Biederman81adee42009-11-08 00:53:51 -08002198 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002199 if (IS_ERR(dest_net))
2200 return PTR_ERR(dest_net);
2201
Eric W. Biederman505ce412015-02-26 16:19:00 -06002202 err = -EPERM;
2203 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2204 goto out;
2205
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002206 if (tb[IFLA_LINK_NETNSID]) {
2207 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2208
2209 link_net = get_net_ns_by_id(dest_net, id);
2210 if (!link_net) {
2211 err = -EINVAL;
2212 goto out;
2213 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002214 err = -EPERM;
2215 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2216 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002217 }
2218
2219 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2220 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002221 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002222 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002223 goto out;
2224 }
2225
2226 dev->ifindex = ifm->ifi_index;
2227
Cong Wang0e0eee22014-02-11 15:51:30 -08002228 if (ops->newlink) {
Nicolas Dichtel7b4ce692015-01-27 11:13:08 +01002229 err = ops->newlink(link_net ? : net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002230 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002231 * and unregister it on failure after registration
2232 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002233 */
Cong Wange51fb152014-06-03 16:40:47 -07002234 if (err < 0) {
2235 /* If device is not registered at all, free it now */
2236 if (dev->reg_state == NETREG_UNINITIALIZED)
2237 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002238 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002239 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002240 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002241 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002242 if (err < 0) {
2243 free_netdev(dev);
2244 goto out;
2245 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002246 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002247 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002248 if (err < 0)
2249 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002250 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002251 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002252 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002253 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002254 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002255out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002256 if (link_net)
2257 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002258 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002259 return err;
David S. Miller43638902015-03-10 21:58:32 -04002260out_unregister:
2261 if (ops->newlink) {
2262 LIST_HEAD(list_kill);
2263
2264 ops->dellink(dev, &list_kill);
2265 unregister_netdevice_many(&list_kill);
2266 } else {
2267 unregister_netdevice(dev);
2268 }
2269 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002270 }
2271}
2272
Thomas Graf661d2962013-03-21 07:45:29 +00002273static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002274{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002275 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002276 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002277 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002278 struct nlattr *tb[IFLA_MAX+1];
2279 struct net_device *dev = NULL;
2280 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002281 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002282 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002283
Thomas Grafb60c5112006-08-04 23:05:34 -07002284 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2285 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002286 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002287
Eric Dumazeta3d12892009-10-21 10:59:31 +00002288 if (tb[IFLA_IFNAME])
2289 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2290
Greg Rose115c9b82012-02-21 16:54:48 -05002291 if (tb[IFLA_EXT_MASK])
2292 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2293
Thomas Grafb60c5112006-08-04 23:05:34 -07002294 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002295 if (ifm->ifi_index > 0)
2296 dev = __dev_get_by_index(net, ifm->ifi_index);
2297 else if (tb[IFLA_IFNAME])
2298 dev = __dev_get_by_name(net, ifname);
2299 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002300 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002301
Eric Dumazeta3d12892009-10-21 10:59:31 +00002302 if (dev == NULL)
2303 return -ENODEV;
2304
Greg Rose115c9b82012-02-21 16:54:48 -05002305 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002306 if (nskb == NULL)
2307 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002308
Eric W. Biederman15e47302012-09-07 20:12:54 +00002309 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002310 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002311 if (err < 0) {
2312 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2313 WARN_ON(err == -EMSGSIZE);
2314 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002315 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002316 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002317
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002318 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002319}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002320
Greg Rose115c9b82012-02-21 16:54:48 -05002321static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002322{
Greg Rose115c9b82012-02-21 16:54:48 -05002323 struct net *net = sock_net(skb->sk);
2324 struct net_device *dev;
2325 struct nlattr *tb[IFLA_MAX+1];
2326 u32 ext_filter_mask = 0;
2327 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002328 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002329
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002330 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2331 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2332 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2333
2334 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002335 if (tb[IFLA_EXT_MASK])
2336 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2337 }
Greg Rose115c9b82012-02-21 16:54:48 -05002338
2339 if (!ext_filter_mask)
2340 return NLMSG_GOODSIZE;
2341 /*
2342 * traverse the list of net devices and compute the minimum
2343 * buffer size based upon the filter mask.
2344 */
2345 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2346 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2347 if_nlmsg_size(dev,
2348 ext_filter_mask));
2349 }
2350
Greg Rosec7ac8672011-06-10 01:27:09 +00002351 return min_ifinfo_dump_size;
2352}
2353
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002354static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 int idx;
2357 int s_idx = cb->family;
2358
2359 if (s_idx == 0)
2360 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002361 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int type = cb->nlh->nlmsg_type-RTM_BASE;
2363 if (idx < s_idx || idx == PF_PACKET)
2364 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002365 if (rtnl_msg_handlers[idx] == NULL ||
2366 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002368 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002370 cb->prev_seq = 0;
2371 cb->seq = 0;
2372 }
Thomas Grafe2849862007-03-22 11:48:11 -07002373 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 break;
2375 }
2376 cb->family = idx;
2377
2378 return skb->len;
2379}
2380
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002381struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2382 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002384 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002386 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002387 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002389 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002390 if (skb == NULL)
2391 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Greg Rose115c9b82012-02-21 16:54:48 -05002393 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002394 if (err < 0) {
2395 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2396 WARN_ON(err == -EMSGSIZE);
2397 kfree_skb(skb);
2398 goto errout;
2399 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002400 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002401errout:
2402 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002403 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002404 return NULL;
2405}
2406
2407void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2408{
2409 struct net *net = dev_net(dev);
2410
2411 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2412}
2413
2414void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2415 gfp_t flags)
2416{
2417 struct sk_buff *skb;
2418
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02002419 if (dev->reg_state != NETREG_REGISTERED)
2420 return;
2421
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002422 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2423 if (skb)
2424 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002426EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
John Fastabendd83b0602012-04-15 06:44:08 +00002428static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2429 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002430 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002431 int type, unsigned int flags,
2432 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002433{
2434 struct nlmsghdr *nlh;
2435 struct ndmsg *ndm;
2436
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002437 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002438 if (!nlh)
2439 return -EMSGSIZE;
2440
2441 ndm = nlmsg_data(nlh);
2442 ndm->ndm_family = AF_BRIDGE;
2443 ndm->ndm_pad1 = 0;
2444 ndm->ndm_pad2 = 0;
2445 ndm->ndm_flags = flags;
2446 ndm->ndm_type = 0;
2447 ndm->ndm_ifindex = dev->ifindex;
2448 ndm->ndm_state = NUD_PERMANENT;
2449
2450 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2451 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002452 if (vid)
2453 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2454 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00002455
Johannes Berg053c0952015-01-16 22:09:00 +01002456 nlmsg_end(skb, nlh);
2457 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00002458
2459nla_put_failure:
2460 nlmsg_cancel(skb, nlh);
2461 return -EMSGSIZE;
2462}
2463
John Fastabend3ff661c2012-04-15 06:44:14 +00002464static inline size_t rtnl_fdb_nlmsg_size(void)
2465{
2466 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2467}
2468
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002469static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
John Fastabend3ff661c2012-04-15 06:44:14 +00002470{
2471 struct net *net = dev_net(dev);
2472 struct sk_buff *skb;
2473 int err = -ENOBUFS;
2474
2475 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2476 if (!skb)
2477 goto errout;
2478
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002479 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2480 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002481 if (err < 0) {
2482 kfree_skb(skb);
2483 goto errout;
2484 }
2485
2486 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2487 return;
2488errout:
2489 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2490}
2491
Vlad Yasevich090096b2013-03-06 15:39:42 +00002492/**
2493 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2494 */
2495int ndo_dflt_fdb_add(struct ndmsg *ndm,
2496 struct nlattr *tb[],
2497 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002498 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002499 u16 flags)
2500{
2501 int err = -EINVAL;
2502
2503 /* If aging addresses are supported device will need to
2504 * implement its own handler for this.
2505 */
2506 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2507 pr_info("%s: FDB only supports static addresses\n", dev->name);
2508 return err;
2509 }
2510
Or Gerlitz65891fe2014-12-14 18:19:05 +02002511 if (vid) {
2512 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2513 return err;
2514 }
2515
Vlad Yasevich090096b2013-03-06 15:39:42 +00002516 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2517 err = dev_uc_add_excl(dev, addr);
2518 else if (is_multicast_ether_addr(addr))
2519 err = dev_mc_add_excl(dev, addr);
2520
2521 /* Only return duplicate errors if NLM_F_EXCL is set */
2522 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2523 err = 0;
2524
2525 return err;
2526}
2527EXPORT_SYMBOL(ndo_dflt_fdb_add);
2528
Jiri Pirkof6f64242014-11-28 14:34:15 +01002529static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2530{
2531 u16 vid = 0;
2532
2533 if (vlan_attr) {
2534 if (nla_len(vlan_attr) != sizeof(u16)) {
2535 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2536 return -EINVAL;
2537 }
2538
2539 vid = nla_get_u16(vlan_attr);
2540
2541 if (!vid || vid >= VLAN_VID_MASK) {
2542 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2543 vid);
2544 return -EINVAL;
2545 }
2546 }
2547 *p_vid = vid;
2548 return 0;
2549}
2550
Thomas Graf661d2962013-03-21 07:45:29 +00002551static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002552{
2553 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002554 struct ndmsg *ndm;
2555 struct nlattr *tb[NDA_MAX+1];
2556 struct net_device *dev;
2557 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002558 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002559 int err;
2560
2561 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2562 if (err < 0)
2563 return err;
2564
2565 ndm = nlmsg_data(nlh);
2566 if (ndm->ndm_ifindex == 0) {
2567 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2568 return -EINVAL;
2569 }
2570
2571 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2572 if (dev == NULL) {
2573 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2574 return -ENODEV;
2575 }
2576
2577 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2578 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2579 return -EINVAL;
2580 }
2581
2582 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002583
Jiri Pirkof6f64242014-11-28 14:34:15 +01002584 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2585 if (err)
2586 return err;
2587
John Fastabend77162022012-04-15 06:43:56 +00002588 err = -EOPNOTSUPP;
2589
2590 /* Support fdb on master device the net/bridge default case */
2591 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2592 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002593 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2594 const struct net_device_ops *ops = br_dev->netdev_ops;
2595
Jiri Pirkof6f64242014-11-28 14:34:15 +01002596 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2597 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002598 if (err)
2599 goto out;
2600 else
2601 ndm->ndm_flags &= ~NTF_MASTER;
2602 }
2603
2604 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002605 if ((ndm->ndm_flags & NTF_SELF)) {
2606 if (dev->netdev_ops->ndo_fdb_add)
2607 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002608 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002609 nlh->nlmsg_flags);
2610 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002611 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002612 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002613
John Fastabend3ff661c2012-04-15 06:44:14 +00002614 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002615 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002616 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002617 }
John Fastabend77162022012-04-15 06:43:56 +00002618 }
2619out:
2620 return err;
2621}
2622
Vlad Yasevich090096b2013-03-06 15:39:42 +00002623/**
2624 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2625 */
2626int ndo_dflt_fdb_del(struct ndmsg *ndm,
2627 struct nlattr *tb[],
2628 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002629 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002630{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002631 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002632
2633 /* If aging addresses are supported device will need to
2634 * implement its own handler for this.
2635 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002636 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002637 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002638 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002639 }
2640
2641 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2642 err = dev_uc_del(dev, addr);
2643 else if (is_multicast_ether_addr(addr))
2644 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002645
2646 return err;
2647}
2648EXPORT_SYMBOL(ndo_dflt_fdb_del);
2649
Thomas Graf661d2962013-03-21 07:45:29 +00002650static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002651{
2652 struct net *net = sock_net(skb->sk);
2653 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002654 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002655 struct net_device *dev;
2656 int err = -EINVAL;
2657 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002658 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002659
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002660 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002661 return -EPERM;
2662
2663 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2664 if (err < 0)
2665 return err;
John Fastabend77162022012-04-15 06:43:56 +00002666
2667 ndm = nlmsg_data(nlh);
2668 if (ndm->ndm_ifindex == 0) {
2669 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2670 return -EINVAL;
2671 }
2672
2673 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2674 if (dev == NULL) {
2675 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2676 return -ENODEV;
2677 }
2678
Vlad Yasevich1690be62013-02-13 12:00:18 +00002679 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2680 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002681 return -EINVAL;
2682 }
2683
Vlad Yasevich1690be62013-02-13 12:00:18 +00002684 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002685
Jiri Pirkof6f64242014-11-28 14:34:15 +01002686 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2687 if (err)
2688 return err;
2689
John Fastabend77162022012-04-15 06:43:56 +00002690 err = -EOPNOTSUPP;
2691
2692 /* Support fdb on master device the net/bridge default case */
2693 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2694 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002695 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2696 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002697
Jiri Pirko898e5062013-01-03 22:48:52 +00002698 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002699 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002700
2701 if (err)
2702 goto out;
2703 else
2704 ndm->ndm_flags &= ~NTF_MASTER;
2705 }
2706
2707 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002708 if (ndm->ndm_flags & NTF_SELF) {
2709 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002710 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2711 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002712 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002713 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002714
John Fastabend3ff661c2012-04-15 06:44:14 +00002715 if (!err) {
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002716 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002717 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002718 }
John Fastabend77162022012-04-15 06:43:56 +00002719 }
2720out:
2721 return err;
2722}
2723
John Fastabendd83b0602012-04-15 06:44:08 +00002724static int nlmsg_populate_fdb(struct sk_buff *skb,
2725 struct netlink_callback *cb,
2726 struct net_device *dev,
2727 int *idx,
2728 struct netdev_hw_addr_list *list)
2729{
2730 struct netdev_hw_addr *ha;
2731 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002732 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002733
Eric W. Biederman15e47302012-09-07 20:12:54 +00002734 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002735 seq = cb->nlh->nlmsg_seq;
2736
2737 list_for_each_entry(ha, &list->list, list) {
2738 if (*idx < cb->args[0])
2739 goto skip;
2740
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00002741 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00002742 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002743 RTM_NEWNEIGH, NTF_SELF,
2744 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002745 if (err < 0)
2746 return err;
2747skip:
2748 *idx += 1;
2749 }
2750 return 0;
2751}
2752
2753/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002754 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002755 * @nlh: netlink message header
2756 * @dev: netdevice
2757 *
2758 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002759 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002760 */
2761int ndo_dflt_fdb_dump(struct sk_buff *skb,
2762 struct netlink_callback *cb,
2763 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002764 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002765 int idx)
2766{
2767 int err;
2768
2769 netif_addr_lock_bh(dev);
2770 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2771 if (err)
2772 goto out;
2773 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2774out:
2775 netif_addr_unlock_bh(dev);
2776 return idx;
2777}
2778EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2779
John Fastabend77162022012-04-15 06:43:56 +00002780static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2781{
John Fastabend77162022012-04-15 06:43:56 +00002782 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002783 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002784 struct net_device *br_dev = NULL;
2785 const struct net_device_ops *ops = NULL;
2786 const struct net_device_ops *cops = NULL;
2787 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2788 struct net *net = sock_net(skb->sk);
2789 int brport_idx = 0;
2790 int br_idx = 0;
2791 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002792
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002793 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2794 ifla_policy) == 0) {
2795 if (tb[IFLA_MASTER])
2796 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2797 }
John Fastabend77162022012-04-15 06:43:56 +00002798
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002799 brport_idx = ifm->ifi_index;
2800
2801 if (br_idx) {
2802 br_dev = __dev_get_by_index(net, br_idx);
2803 if (!br_dev)
2804 return -ENODEV;
2805
2806 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002807 }
2808
2809 for_each_netdev(net, dev) {
2810 if (brport_idx && (dev->ifindex != brport_idx))
2811 continue;
2812
2813 if (!br_idx) { /* user did not specify a specific bridge */
2814 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2815 br_dev = netdev_master_upper_dev_get(dev);
2816 cops = br_dev->netdev_ops;
2817 }
2818
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002819 } else {
2820 if (dev != br_dev &&
2821 !(dev->priv_flags & IFF_BRIDGE_PORT))
2822 continue;
2823
2824 if (br_dev != netdev_master_upper_dev_get(dev) &&
2825 !(dev->priv_flags & IFF_EBRIDGE))
2826 continue;
2827
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002828 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002829 }
2830
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002831 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2832 if (cops && cops->ndo_fdb_dump)
2833 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2834 idx);
2835 }
2836
John Fastabend77162022012-04-15 06:43:56 +00002837 if (dev->netdev_ops->ndo_fdb_dump)
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002838 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002839 idx);
Hubert Sokolowski6cb69742015-01-05 17:29:21 +00002840 else
2841 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002842
2843 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002844 }
John Fastabend77162022012-04-15 06:43:56 +00002845
2846 cb->args[0] = idx;
2847 return skb->len;
2848}
2849
Scott Feldman2c3c0312014-11-28 14:34:25 +01002850static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2851 unsigned int attrnum, unsigned int flag)
2852{
2853 if (mask & flag)
2854 return nla_put_u8(skb, attrnum, !!(flags & flag));
2855 return 0;
2856}
2857
John Fastabend815cccb2012-10-24 08:13:09 +00002858int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002859 struct net_device *dev, u16 mode,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002860 u32 flags, u32 mask, int nlflags)
John Fastabend815cccb2012-10-24 08:13:09 +00002861{
2862 struct nlmsghdr *nlh;
2863 struct ifinfomsg *ifm;
2864 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002865 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002866 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002867 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002868
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002869 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00002870 if (nlh == NULL)
2871 return -EMSGSIZE;
2872
2873 ifm = nlmsg_data(nlh);
2874 ifm->ifi_family = AF_BRIDGE;
2875 ifm->__ifi_pad = 0;
2876 ifm->ifi_type = dev->type;
2877 ifm->ifi_index = dev->ifindex;
2878 ifm->ifi_flags = dev_get_flags(dev);
2879 ifm->ifi_change = 0;
2880
2881
2882 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2883 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2884 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002885 (br_dev &&
2886 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002887 (dev->addr_len &&
2888 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02002889 (dev->ifindex != dev_get_iflink(dev) &&
2890 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00002891 goto nla_put_failure;
2892
2893 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2894 if (!br_afspec)
2895 goto nla_put_failure;
2896
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002897 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002898 nla_nest_cancel(skb, br_afspec);
2899 goto nla_put_failure;
2900 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002901
2902 if (mode != BRIDGE_MODE_UNDEF) {
2903 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2904 nla_nest_cancel(skb, br_afspec);
2905 goto nla_put_failure;
2906 }
2907 }
John Fastabend815cccb2012-10-24 08:13:09 +00002908 nla_nest_end(skb, br_afspec);
2909
Scott Feldman2c3c0312014-11-28 14:34:25 +01002910 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2911 if (!protinfo)
2912 goto nla_put_failure;
2913
2914 if (brport_nla_put_flag(skb, flags, mask,
2915 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2916 brport_nla_put_flag(skb, flags, mask,
2917 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2918 brport_nla_put_flag(skb, flags, mask,
2919 IFLA_BRPORT_FAST_LEAVE,
2920 BR_MULTICAST_FAST_LEAVE) ||
2921 brport_nla_put_flag(skb, flags, mask,
2922 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2923 brport_nla_put_flag(skb, flags, mask,
2924 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2925 brport_nla_put_flag(skb, flags, mask,
2926 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2927 brport_nla_put_flag(skb, flags, mask,
2928 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2929 brport_nla_put_flag(skb, flags, mask,
2930 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2931 nla_nest_cancel(skb, protinfo);
2932 goto nla_put_failure;
2933 }
2934
2935 nla_nest_end(skb, protinfo);
2936
Johannes Berg053c0952015-01-16 22:09:00 +01002937 nlmsg_end(skb, nlh);
2938 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00002939nla_put_failure:
2940 nlmsg_cancel(skb, nlh);
2941 return -EMSGSIZE;
2942}
2943EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2944
John Fastabende5a55a82012-10-24 08:12:57 +00002945static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2946{
2947 struct net *net = sock_net(skb->sk);
2948 struct net_device *dev;
2949 int idx = 0;
2950 u32 portid = NETLINK_CB(cb->skb).portid;
2951 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002952 u32 filter_mask = 0;
2953
Thomas Grafaa68c202014-11-26 13:42:20 +01002954 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2955 struct nlattr *extfilt;
2956
2957 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2958 IFLA_EXT_MASK);
2959 if (extfilt) {
2960 if (nla_len(extfilt) < sizeof(filter_mask))
2961 return -EINVAL;
2962
2963 filter_mask = nla_get_u32(extfilt);
2964 }
2965 }
John Fastabende5a55a82012-10-24 08:12:57 +00002966
2967 rcu_read_lock();
2968 for_each_netdev_rcu(net, dev) {
2969 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002970 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002971
Jiri Pirko898e5062013-01-03 22:48:52 +00002972 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002973 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002974 br_dev->netdev_ops->ndo_bridge_getlink(
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002975 skb, portid, seq, dev, filter_mask,
2976 NLM_F_MULTI) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002977 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002978 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002979 }
2980
2981 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002982 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002983 ops->ndo_bridge_getlink(skb, portid, seq, dev,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02002984 filter_mask,
2985 NLM_F_MULTI) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002986 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002987 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002988 }
2989 }
2990 rcu_read_unlock();
2991 cb->args[0] = idx;
2992
2993 return skb->len;
2994}
2995
John Fastabend2469ffd2012-10-24 08:13:03 +00002996static inline size_t bridge_nlmsg_size(void)
2997{
2998 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2999 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3000 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3001 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3002 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3003 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3004 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3005 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3006 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3007 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3008 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3009}
3010
Roopa Prabhu02dba432015-01-14 20:02:25 -08003011static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003012{
3013 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003014 struct sk_buff *skb;
3015 int err = -EOPNOTSUPP;
3016
Roopa Prabhu02dba432015-01-14 20:02:25 -08003017 if (!dev->netdev_ops->ndo_bridge_getlink)
3018 return 0;
3019
John Fastabend2469ffd2012-10-24 08:13:03 +00003020 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3021 if (!skb) {
3022 err = -ENOMEM;
3023 goto errout;
3024 }
3025
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003026 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003027 if (err < 0)
3028 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003029
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003030 if (!skb->len)
3031 goto errout;
3032
John Fastabend2469ffd2012-10-24 08:13:03 +00003033 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3034 return 0;
3035errout:
3036 WARN_ON(err == -EMSGSIZE);
3037 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003038 if (err)
3039 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003040 return err;
3041}
3042
Thomas Graf661d2962013-03-21 07:45:29 +00003043static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00003044{
3045 struct net *net = sock_net(skb->sk);
3046 struct ifinfomsg *ifm;
3047 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003048 struct nlattr *br_spec, *attr = NULL;
3049 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003050 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003051 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003052
3053 if (nlmsg_len(nlh) < sizeof(*ifm))
3054 return -EINVAL;
3055
3056 ifm = nlmsg_data(nlh);
3057 if (ifm->ifi_family != AF_BRIDGE)
3058 return -EPFNOSUPPORT;
3059
3060 dev = __dev_get_by_index(net, ifm->ifi_index);
3061 if (!dev) {
3062 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3063 return -ENODEV;
3064 }
3065
John Fastabend2469ffd2012-10-24 08:13:03 +00003066 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3067 if (br_spec) {
3068 nla_for_each_nested(attr, br_spec, rem) {
3069 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003070 if (nla_len(attr) < sizeof(flags))
3071 return -EINVAL;
3072
John Fastabendc38e01b2012-11-02 16:32:36 +00003073 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003074 flags = nla_get_u16(attr);
3075 break;
3076 }
3077 }
3078 }
3079
3080 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003081 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3082
3083 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003084 err = -EOPNOTSUPP;
3085 goto out;
3086 }
3087
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003088 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003089 if (err)
3090 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003091
3092 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003093 }
3094
John Fastabend2469ffd2012-10-24 08:13:03 +00003095 if ((flags & BRIDGE_FLAGS_SELF)) {
3096 if (!dev->netdev_ops->ndo_bridge_setlink)
3097 err = -EOPNOTSUPP;
3098 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003099 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3100 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003101 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003102 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003103
3104 /* Generate event to notify upper layer of bridge
3105 * change
3106 */
3107 err = rtnl_bridge_notify(dev);
3108 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003109 }
3110
John Fastabendc38e01b2012-11-02 16:32:36 +00003111 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003112 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003113out:
3114 return err;
3115}
3116
Thomas Graf661d2962013-03-21 07:45:29 +00003117static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00003118{
3119 struct net *net = sock_net(skb->sk);
3120 struct ifinfomsg *ifm;
3121 struct net_device *dev;
3122 struct nlattr *br_spec, *attr = NULL;
3123 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003124 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003125 bool have_flags = false;
3126
3127 if (nlmsg_len(nlh) < sizeof(*ifm))
3128 return -EINVAL;
3129
3130 ifm = nlmsg_data(nlh);
3131 if (ifm->ifi_family != AF_BRIDGE)
3132 return -EPFNOSUPPORT;
3133
3134 dev = __dev_get_by_index(net, ifm->ifi_index);
3135 if (!dev) {
3136 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3137 return -ENODEV;
3138 }
3139
3140 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3141 if (br_spec) {
3142 nla_for_each_nested(attr, br_spec, rem) {
3143 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003144 if (nla_len(attr) < sizeof(flags))
3145 return -EINVAL;
3146
Vlad Yasevich407af322013-02-13 12:00:12 +00003147 have_flags = true;
3148 flags = nla_get_u16(attr);
3149 break;
3150 }
3151 }
3152 }
3153
Vlad Yasevich407af322013-02-13 12:00:12 +00003154 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3155 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3156
3157 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3158 err = -EOPNOTSUPP;
3159 goto out;
3160 }
3161
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003162 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003163 if (err)
3164 goto out;
3165
3166 flags &= ~BRIDGE_FLAGS_MASTER;
3167 }
3168
3169 if ((flags & BRIDGE_FLAGS_SELF)) {
3170 if (!dev->netdev_ops->ndo_bridge_dellink)
3171 err = -EOPNOTSUPP;
3172 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003173 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3174 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003175
Roopa Prabhu02dba432015-01-14 20:02:25 -08003176 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003177 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003178
3179 /* Generate event to notify upper layer of bridge
3180 * change
3181 */
3182 err = rtnl_bridge_notify(dev);
3183 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003184 }
3185
3186 if (have_flags)
3187 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003188out:
3189 return err;
3190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192/* Process one rtnetlink message. */
3193
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003194static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003196 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003197 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 int family;
3200 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003201 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003205 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 type -= RTM_BASE;
3208
3209 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003210 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return 0;
3212
Hong zhi guo573ce262013-03-27 06:47:04 +00003213 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 sz_idx = type>>2;
3215 kind = type&3;
3216
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003217 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003218 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
David S. Millerb8f3ab42011-01-18 12:40:38 -08003220 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003221 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003222 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003223 rtnl_calcit_func calcit;
3224 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Thomas Grafe2849862007-03-22 11:48:11 -07003226 dumpit = rtnl_get_dumpit(family, type);
3227 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003228 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003229 calcit = rtnl_get_calcit(family, type);
3230 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003231 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Eric Dumazet2907c352011-05-25 07:34:04 +00003233 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003234 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003235 {
3236 struct netlink_dump_control c = {
3237 .dump = dumpit,
3238 .min_dump_alloc = min_dump_alloc,
3239 };
3240 err = netlink_dump_start(rtnl, skb, nlh, &c);
3241 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003242 rtnl_lock();
3243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245
Thomas Grafe2849862007-03-22 11:48:11 -07003246 doit = rtnl_get_doit(family, type);
3247 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003248 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Thomas Graf661d2962013-03-21 07:45:29 +00003250 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003253static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003255 rtnl_lock();
3256 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3257 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3261{
Jiri Pirko351638e2013-05-28 01:30:21 +00003262 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 case NETDEV_UP:
3266 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003267 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003268 case NETDEV_POST_INIT:
3269 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003271 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003273 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003274 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003275 case NETDEV_RELEASE:
3276 case NETDEV_JOIN:
Moni Shoua61bd3852015-02-03 16:48:29 +02003277 case NETDEV_BONDING_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 break;
3279 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003280 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 break;
3282 }
3283 return NOTIFY_DONE;
3284}
3285
3286static struct notifier_block rtnetlink_dev_notifier = {
3287 .notifier_call = rtnetlink_event,
3288};
3289
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003290
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003291static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003292{
3293 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003294 struct netlink_kernel_cfg cfg = {
3295 .groups = RTNLGRP_MAX,
3296 .input = rtnetlink_rcv,
3297 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003298 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003299 };
3300
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003301 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003302 if (!sk)
3303 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003304 net->rtnl = sk;
3305 return 0;
3306}
3307
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003308static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003309{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003310 netlink_kernel_release(net->rtnl);
3311 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003312}
3313
3314static struct pernet_operations rtnetlink_net_ops = {
3315 .init = rtnetlink_net_init,
3316 .exit = rtnetlink_net_exit,
3317};
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319void __init rtnetlink_init(void)
3320{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003321 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003325
Greg Rosec7ac8672011-06-10 01:27:09 +00003326 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3327 rtnl_dump_ifinfo, rtnl_calcit);
3328 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3329 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3330 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003331
Greg Rosec7ac8672011-06-10 01:27:09 +00003332 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3333 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003334
3335 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3336 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3337 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003338
3339 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003340 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003341 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343