blob: 9cf6fe9ddc0c99e189916dee672d16e6c4efe19a [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>
53#include <net/sock.h>
54#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070055#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070056#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000057#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Eric Dumazete0d087a2009-11-07 01:26:17 -080059struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070060 rtnl_doit_func doit;
61 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +000062 rtnl_calcit_func calcit;
Thomas Grafe2849862007-03-22 11:48:11 -070063};
64
Stephen Hemminger6756ae42006-03-20 22:23:58 -080065static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67void rtnl_lock(void)
68{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080069 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Eric Dumazete0d087a2009-11-07 01:26:17 -080071EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Stephen Hemminger6756ae42006-03-20 22:23:58 -080073void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080075 mutex_unlock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
Stephen Hemminger6756ae42006-03-20 22:23:58 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void rtnl_unlock(void)
79{
Herbert Xu58ec3b42008-10-07 15:50:03 -070080 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 netdev_run_todo();
82}
Eric Dumazete0d087a2009-11-07 01:26:17 -080083EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Stephen Hemminger6756ae42006-03-20 22:23:58 -080085int rtnl_trylock(void)
86{
87 return mutex_trylock(&rtnl_mutex);
88}
Eric Dumazete0d087a2009-11-07 01:26:17 -080089EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -080090
Patrick McHardyc9c10142008-04-23 22:10:48 -070091int rtnl_is_locked(void)
92{
93 return mutex_is_locked(&rtnl_mutex);
94}
Eric Dumazete0d087a2009-11-07 01:26:17 -080095EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -070096
Paul E. McKenneya898def2010-02-22 17:04:49 -080097#ifdef CONFIG_PROVE_LOCKING
98int lockdep_rtnl_is_held(void)
99{
100 return lockdep_is_held(&rtnl_mutex);
101}
102EXPORT_SYMBOL(lockdep_rtnl_is_held);
103#endif /* #ifdef CONFIG_PROVE_LOCKING */
104
Patrick McHardy25239ce2010-04-26 16:02:05 +0200105static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700106
107static inline int rtm_msgindex(int msgtype)
108{
109 int msgindex = msgtype - RTM_BASE;
110
111 /*
112 * msgindex < 0 implies someone tried to register a netlink
113 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
114 * the message type has not been added to linux/rtnetlink.h
115 */
116 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
117
118 return msgindex;
119}
120
121static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
122{
123 struct rtnl_link *tab;
124
Patrick McHardy25239ce2010-04-26 16:02:05 +0200125 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000126 tab = rtnl_msg_handlers[protocol];
127 else
128 tab = NULL;
129
Thomas Graf51057f22007-03-22 21:41:06 -0700130 if (tab == NULL || tab[msgindex].doit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700131 tab = rtnl_msg_handlers[PF_UNSPEC];
132
Hans Zhangc80bbea2012-10-22 22:21:23 +0000133 return tab[msgindex].doit;
Thomas Grafe2849862007-03-22 11:48:11 -0700134}
135
136static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
137{
138 struct rtnl_link *tab;
139
Patrick McHardy25239ce2010-04-26 16:02:05 +0200140 if (protocol <= RTNL_FAMILY_MAX)
Patrick McHardy0f87b1d2010-04-13 05:03:17 +0000141 tab = rtnl_msg_handlers[protocol];
142 else
143 tab = NULL;
144
Thomas Graf51057f22007-03-22 21:41:06 -0700145 if (tab == NULL || tab[msgindex].dumpit == NULL)
Thomas Grafe2849862007-03-22 11:48:11 -0700146 tab = rtnl_msg_handlers[PF_UNSPEC];
147
Hans Zhangc80bbea2012-10-22 22:21:23 +0000148 return tab[msgindex].dumpit;
Thomas Grafe2849862007-03-22 11:48:11 -0700149}
150
Greg Rosec7ac8672011-06-10 01:27:09 +0000151static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
152{
153 struct rtnl_link *tab;
154
155 if (protocol <= RTNL_FAMILY_MAX)
156 tab = rtnl_msg_handlers[protocol];
157 else
158 tab = NULL;
159
160 if (tab == NULL || tab[msgindex].calcit == NULL)
161 tab = rtnl_msg_handlers[PF_UNSPEC];
162
Hans Zhangc80bbea2012-10-22 22:21:23 +0000163 return tab[msgindex].calcit;
Greg Rosec7ac8672011-06-10 01:27:09 +0000164}
165
Thomas Grafe2849862007-03-22 11:48:11 -0700166/**
167 * __rtnl_register - Register a rtnetlink message type
168 * @protocol: Protocol family or PF_UNSPEC
169 * @msgtype: rtnetlink message type
170 * @doit: Function pointer called for each request message
171 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
Greg Rosec7ac8672011-06-10 01:27:09 +0000172 * @calcit: Function pointer to calc size of dump message
Thomas Grafe2849862007-03-22 11:48:11 -0700173 *
174 * Registers the specified function pointers (at least one of them has
175 * to be non-NULL) to be called whenever a request message for the
176 * specified protocol family and message type is received.
177 *
178 * The special protocol family PF_UNSPEC may be used to define fallback
179 * function pointers for the case when no entry for the specific protocol
180 * family exists.
181 *
182 * Returns 0 on success or a negative error code.
183 */
184int __rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000185 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
186 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700187{
188 struct rtnl_link *tab;
189 int msgindex;
190
Patrick McHardy25239ce2010-04-26 16:02:05 +0200191 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700192 msgindex = rtm_msgindex(msgtype);
193
194 tab = rtnl_msg_handlers[protocol];
195 if (tab == NULL) {
196 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
197 if (tab == NULL)
198 return -ENOBUFS;
199
200 rtnl_msg_handlers[protocol] = tab;
201 }
202
203 if (doit)
204 tab[msgindex].doit = doit;
205
206 if (dumpit)
207 tab[msgindex].dumpit = dumpit;
208
Greg Rosec7ac8672011-06-10 01:27:09 +0000209 if (calcit)
210 tab[msgindex].calcit = calcit;
211
Thomas Grafe2849862007-03-22 11:48:11 -0700212 return 0;
213}
Thomas Grafe2849862007-03-22 11:48:11 -0700214EXPORT_SYMBOL_GPL(__rtnl_register);
215
216/**
217 * rtnl_register - Register a rtnetlink message type
218 *
219 * Identical to __rtnl_register() but panics on failure. This is useful
220 * as failure of this function is very unlikely, it can only happen due
221 * to lack of memory when allocating the chain to store all message
222 * handlers for a protocol. Meant for use in init functions where lack
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300223 * of memory implies no sense in continuing.
Thomas Grafe2849862007-03-22 11:48:11 -0700224 */
225void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000226 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
227 rtnl_calcit_func calcit)
Thomas Grafe2849862007-03-22 11:48:11 -0700228{
Greg Rosec7ac8672011-06-10 01:27:09 +0000229 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
Thomas Grafe2849862007-03-22 11:48:11 -0700230 panic("Unable to register rtnetlink message handler, "
231 "protocol = %d, message type = %d\n",
232 protocol, msgtype);
233}
Thomas Grafe2849862007-03-22 11:48:11 -0700234EXPORT_SYMBOL_GPL(rtnl_register);
235
236/**
237 * rtnl_unregister - Unregister a rtnetlink message type
238 * @protocol: Protocol family or PF_UNSPEC
239 * @msgtype: rtnetlink message type
240 *
241 * Returns 0 on success or a negative error code.
242 */
243int rtnl_unregister(int protocol, int msgtype)
244{
245 int msgindex;
246
Patrick McHardy25239ce2010-04-26 16:02:05 +0200247 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700248 msgindex = rtm_msgindex(msgtype);
249
250 if (rtnl_msg_handlers[protocol] == NULL)
251 return -ENOENT;
252
253 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
254 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
255
256 return 0;
257}
Thomas Grafe2849862007-03-22 11:48:11 -0700258EXPORT_SYMBOL_GPL(rtnl_unregister);
259
260/**
261 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
262 * @protocol : Protocol family or PF_UNSPEC
263 *
264 * Identical to calling rtnl_unregster() for all registered message types
265 * of a certain protocol family.
266 */
267void rtnl_unregister_all(int protocol)
268{
Patrick McHardy25239ce2010-04-26 16:02:05 +0200269 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700270
271 kfree(rtnl_msg_handlers[protocol]);
272 rtnl_msg_handlers[protocol] = NULL;
273}
Thomas Grafe2849862007-03-22 11:48:11 -0700274EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Patrick McHardy38f7b872007-06-13 12:03:51 -0700276static LIST_HEAD(link_ops);
277
Eric Dumazetc63044f2011-12-13 11:38:00 +0000278static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
279{
280 const struct rtnl_link_ops *ops;
281
282 list_for_each_entry(ops, &link_ops, list) {
283 if (!strcmp(ops->kind, kind))
284 return ops;
285 }
286 return NULL;
287}
288
Patrick McHardy38f7b872007-06-13 12:03:51 -0700289/**
290 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
291 * @ops: struct rtnl_link_ops * to register
292 *
293 * The caller must hold the rtnl_mutex. This function should be used
294 * by drivers that create devices during module initialization. It
295 * must be called before registering the devices.
296 *
297 * Returns 0 on success or a negative error code.
298 */
299int __rtnl_link_register(struct rtnl_link_ops *ops)
300{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000301 if (rtnl_link_ops_get(ops->kind))
302 return -EEXIST;
303
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200304 /* The check for setup is here because if ops
305 * does not have that filled up, it is not possible
306 * to use the ops for creating device. So do not
307 * fill up dellink as well. That disables rtnl_dellink.
308 */
309 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000310 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700311
Patrick McHardy38f7b872007-06-13 12:03:51 -0700312 list_add_tail(&ops->list, &link_ops);
313 return 0;
314}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700315EXPORT_SYMBOL_GPL(__rtnl_link_register);
316
317/**
318 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
319 * @ops: struct rtnl_link_ops * to register
320 *
321 * Returns 0 on success or a negative error code.
322 */
323int rtnl_link_register(struct rtnl_link_ops *ops)
324{
325 int err;
326
327 rtnl_lock();
328 err = __rtnl_link_register(ops);
329 rtnl_unlock();
330 return err;
331}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700332EXPORT_SYMBOL_GPL(rtnl_link_register);
333
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700334static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
335{
336 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000337 LIST_HEAD(list_kill);
338
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700339 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000340 if (dev->rtnl_link_ops == ops)
341 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700342 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000343 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700344}
345
Patrick McHardy38f7b872007-06-13 12:03:51 -0700346/**
347 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
348 * @ops: struct rtnl_link_ops * to unregister
349 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700350 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700351 */
352void __rtnl_link_unregister(struct rtnl_link_ops *ops)
353{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700354 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700355
Eric W. Biederman881d9662007-09-17 11:56:21 -0700356 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700357 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700358 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700359 list_del(&ops->list);
360}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700361EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
362
Cong Wang200b9162014-05-12 15:11:20 -0700363/* Return with the rtnl_lock held when there are no network
364 * devices unregistering in any network namespace.
365 */
366static void rtnl_lock_unregistering_all(void)
367{
368 struct net *net;
369 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100370 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700371
Peter Zijlstraff960a72014-10-29 17:04:56 +0100372 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700373 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700374 unregistering = false;
375 rtnl_lock();
376 for_each_net(net) {
377 if (net->dev_unreg_count > 0) {
378 unregistering = true;
379 break;
380 }
381 }
382 if (!unregistering)
383 break;
384 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100385
386 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700387 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100388 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700389}
390
Patrick McHardy38f7b872007-06-13 12:03:51 -0700391/**
392 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
393 * @ops: struct rtnl_link_ops * to unregister
394 */
395void rtnl_link_unregister(struct rtnl_link_ops *ops)
396{
Cong Wang200b9162014-05-12 15:11:20 -0700397 /* Close the race with cleanup_net() */
398 mutex_lock(&net_mutex);
399 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700400 __rtnl_link_unregister(ops);
401 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700402 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700403}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700404EXPORT_SYMBOL_GPL(rtnl_link_unregister);
405
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100406static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
407{
408 struct net_device *master_dev;
409 const struct rtnl_link_ops *ops;
410
411 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
412 if (!master_dev)
413 return 0;
414 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900415 if (!ops || !ops->get_slave_size)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100416 return 0;
417 /* IFLA_INFO_SLAVE_DATA + nested data */
418 return nla_total_size(sizeof(struct nlattr)) +
419 ops->get_slave_size(master_dev, dev);
420}
421
Patrick McHardy38f7b872007-06-13 12:03:51 -0700422static size_t rtnl_link_get_size(const struct net_device *dev)
423{
424 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
425 size_t size;
426
427 if (!ops)
428 return 0;
429
Thomas Graf369cf772010-11-11 15:47:59 +0000430 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
431 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700432
433 if (ops->get_size)
434 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000435 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700436 ops->get_size(dev);
437
438 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000439 /* IFLA_INFO_XSTATS */
440 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700441
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100442 size += rtnl_link_get_slave_info_data_size(dev);
443
Patrick McHardy38f7b872007-06-13 12:03:51 -0700444 return size;
445}
446
Thomas Graff8ff1822010-11-16 04:30:14 +0000447static LIST_HEAD(rtnl_af_ops);
448
449static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
450{
451 const struct rtnl_af_ops *ops;
452
453 list_for_each_entry(ops, &rtnl_af_ops, list) {
454 if (ops->family == family)
455 return ops;
456 }
457
458 return NULL;
459}
460
461/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000462 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
463 * @ops: struct rtnl_af_ops * to register
464 *
465 * Returns 0 on success or a negative error code.
466 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800467void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000468{
Thomas Graff8ff1822010-11-16 04:30:14 +0000469 rtnl_lock();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800470 list_add_tail(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000471 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000472}
473EXPORT_SYMBOL_GPL(rtnl_af_register);
474
475/**
476 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
477 * @ops: struct rtnl_af_ops * to unregister
478 *
479 * The caller must hold the rtnl_mutex.
480 */
481void __rtnl_af_unregister(struct rtnl_af_ops *ops)
482{
483 list_del(&ops->list);
484}
485EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
486
487/**
488 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
489 * @ops: struct rtnl_af_ops * to unregister
490 */
491void rtnl_af_unregister(struct rtnl_af_ops *ops)
492{
493 rtnl_lock();
494 __rtnl_af_unregister(ops);
495 rtnl_unlock();
496}
497EXPORT_SYMBOL_GPL(rtnl_af_unregister);
498
499static size_t rtnl_link_get_af_size(const struct net_device *dev)
500{
501 struct rtnl_af_ops *af_ops;
502 size_t size;
503
504 /* IFLA_AF_SPEC */
505 size = nla_total_size(sizeof(struct nlattr));
506
507 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
508 if (af_ops->get_link_af_size) {
509 /* AF_* + nested data */
510 size += nla_total_size(sizeof(struct nlattr)) +
511 af_ops->get_link_af_size(dev);
512 }
513 }
514
515 return size;
516}
517
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100518static bool rtnl_have_link_slave_info(const struct net_device *dev)
519{
520 struct net_device *master_dev;
521
522 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100523 if (master_dev && master_dev->rtnl_link_ops)
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100524 return true;
525 return false;
526}
527
528static int rtnl_link_slave_info_fill(struct sk_buff *skb,
529 const struct net_device *dev)
530{
531 struct net_device *master_dev;
532 const struct rtnl_link_ops *ops;
533 struct nlattr *slave_data;
534 int err;
535
536 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
537 if (!master_dev)
538 return 0;
539 ops = master_dev->rtnl_link_ops;
540 if (!ops)
541 return 0;
542 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
543 return -EMSGSIZE;
544 if (ops->fill_slave_info) {
545 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
546 if (!slave_data)
547 return -EMSGSIZE;
548 err = ops->fill_slave_info(skb, master_dev, dev);
549 if (err < 0)
550 goto err_cancel_slave_data;
551 nla_nest_end(skb, slave_data);
552 }
553 return 0;
554
555err_cancel_slave_data:
556 nla_nest_cancel(skb, slave_data);
557 return err;
558}
559
560static int rtnl_link_info_fill(struct sk_buff *skb,
561 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700562{
563 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100564 struct nlattr *data;
565 int err;
566
567 if (!ops)
568 return 0;
569 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
570 return -EMSGSIZE;
571 if (ops->fill_xstats) {
572 err = ops->fill_xstats(skb, dev);
573 if (err < 0)
574 return err;
575 }
576 if (ops->fill_info) {
577 data = nla_nest_start(skb, IFLA_INFO_DATA);
578 if (data == NULL)
579 return -EMSGSIZE;
580 err = ops->fill_info(skb, dev);
581 if (err < 0)
582 goto err_cancel_data;
583 nla_nest_end(skb, data);
584 }
585 return 0;
586
587err_cancel_data:
588 nla_nest_cancel(skb, data);
589 return err;
590}
591
592static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
593{
594 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700595 int err = -EMSGSIZE;
596
597 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
598 if (linkinfo == NULL)
599 goto out;
600
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100601 err = rtnl_link_info_fill(skb, dev);
602 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700603 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100604
605 err = rtnl_link_slave_info_fill(skb, dev);
606 if (err < 0)
607 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700608
609 nla_nest_end(skb, linkinfo);
610 return 0;
611
Patrick McHardy38f7b872007-06-13 12:03:51 -0700612err_cancel_link:
613 nla_nest_cancel(skb, linkinfo);
614out:
615 return err;
616}
617
Eric Dumazet95c96172012-04-15 05:58:06 +0000618int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800620 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int err = 0;
622
Patrick McHardyac6d4392005-08-14 19:29:52 -0700623 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (echo)
625 atomic_inc(&skb->users);
626 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
627 if (echo)
628 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
629 return err;
630}
631
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800632int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700633{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800634 struct sock *rtnl = net->rtnl;
635
Thomas Graf2942e902006-08-15 00:30:25 -0700636 return nlmsg_unicast(rtnl, skb, pid);
637}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800638EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700639
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800640void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
641 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700642{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800643 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700644 int report = 0;
645
646 if (nlh)
647 report = nlmsg_report(nlh);
648
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800649 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700650}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800651EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700652
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800653void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700654{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800655 struct sock *rtnl = net->rtnl;
656
Thomas Graf97676b62006-08-15 00:31:41 -0700657 netlink_set_err(rtnl, 0, group, error);
658}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800659EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
662{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700663 struct nlattr *mx;
664 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Thomas Graf2d7202b2006-08-22 00:01:27 -0700666 mx = nla_nest_start(skb, RTA_METRICS);
667 if (mx == NULL)
668 return -ENOBUFS;
669
670 for (i = 0; i < RTAX_MAX; i++) {
671 if (metrics[i]) {
672 valid++;
David S. Millera6574342012-04-01 20:12:00 -0400673 if (nla_put_u32(skb, i+1, metrics[i]))
674 goto nla_put_failure;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David S. Millera57d27f2006-08-22 22:20:14 -0700678 if (!valid) {
679 nla_nest_cancel(skb, mx);
680 return 0;
681 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700682
683 return nla_nest_end(skb, mx);
684
685nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700686 nla_nest_cancel(skb, mx);
687 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800689EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Thomas Grafe3703b32006-11-27 09:27:07 -0800691int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700692 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800693{
694 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000695 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800696 .rta_used = dst->__use,
697 .rta_clntref = atomic_read(&(dst->__refcnt)),
698 .rta_error = error,
699 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800700 };
701
Li Wei82539472012-07-29 16:01:30 +0000702 if (expires) {
703 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800704
Li Wei82539472012-07-29 16:01:30 +0000705 clock = jiffies_to_clock_t(abs(expires));
706 clock = min_t(unsigned long, clock, INT_MAX);
707 ci.rta_expires = (expires > 0) ? clock : -clock;
708 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800709 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
710}
Thomas Grafe3703b32006-11-27 09:27:07 -0800711EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
David S. Miller93b2d4a2008-02-17 18:35:07 -0800713static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800714{
715 unsigned char operstate = dev->operstate;
716
Eric Dumazete0d087a2009-11-07 01:26:17 -0800717 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800718 case IF_OPER_UP:
719 if ((operstate == IF_OPER_DORMANT ||
720 operstate == IF_OPER_UNKNOWN) &&
721 !netif_dormant(dev))
722 operstate = IF_OPER_UP;
723 break;
724
725 case IF_OPER_DORMANT:
726 if (operstate == IF_OPER_UP ||
727 operstate == IF_OPER_UNKNOWN)
728 operstate = IF_OPER_DORMANT;
729 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700730 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800731
732 if (dev->operstate != operstate) {
733 write_lock_bh(&dev_base_lock);
734 dev->operstate = operstate;
735 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800736 netdev_state_change(dev);
737 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800738}
739
Jiri Bencb1beb682012-07-27 02:58:22 +0000740static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
741{
742 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
743 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
744}
745
Patrick McHardy3729d502010-02-26 06:34:54 +0000746static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
747 const struct ifinfomsg *ifm)
748{
749 unsigned int flags = ifm->ifi_flags;
750
751 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
752 if (ifm->ifi_change)
753 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000754 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000755
756 return flags;
757}
758
Thomas Grafb60c5112006-08-04 23:05:34 -0700759static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000760 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Thomas Grafb60c5112006-08-04 23:05:34 -0700762 a->rx_packets = b->rx_packets;
763 a->tx_packets = b->tx_packets;
764 a->rx_bytes = b->rx_bytes;
765 a->tx_bytes = b->tx_bytes;
766 a->rx_errors = b->rx_errors;
767 a->tx_errors = b->tx_errors;
768 a->rx_dropped = b->rx_dropped;
769 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Thomas Grafb60c5112006-08-04 23:05:34 -0700771 a->multicast = b->multicast;
772 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Thomas Grafb60c5112006-08-04 23:05:34 -0700774 a->rx_length_errors = b->rx_length_errors;
775 a->rx_over_errors = b->rx_over_errors;
776 a->rx_crc_errors = b->rx_crc_errors;
777 a->rx_frame_errors = b->rx_frame_errors;
778 a->rx_fifo_errors = b->rx_fifo_errors;
779 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Thomas Grafb60c5112006-08-04 23:05:34 -0700781 a->tx_aborted_errors = b->tx_aborted_errors;
782 a->tx_carrier_errors = b->tx_carrier_errors;
783 a->tx_fifo_errors = b->tx_fifo_errors;
784 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
785 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Thomas Grafb60c5112006-08-04 23:05:34 -0700787 a->rx_compressed = b->rx_compressed;
788 a->tx_compressed = b->tx_compressed;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000789}
790
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000791static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
Jan Engelhardt10708f32010-03-11 09:57:29 +0000792{
Eric Dumazetafdcba32010-08-23 07:14:36 +0000793 memcpy(v, b, sizeof(*b));
Jan Engelhardt10708f32010-03-11 09:57:29 +0000794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Chris Wrightc02db8c2010-05-16 01:05:45 -0700796/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500797static inline int rtnl_vfinfo_size(const struct net_device *dev,
798 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000799{
Greg Rose115c9b82012-02-21 16:54:48 -0500800 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
801 (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700802 int num_vfs = dev_num_vf(dev->dev.parent);
Scott Feldman045de012010-05-28 03:42:43 -0700803 size_t size = nla_total_size(sizeof(struct nlattr));
804 size += nla_total_size(num_vfs * sizeof(struct nlattr));
805 size += num_vfs *
806 (nla_total_size(sizeof(struct ifla_vf_mac)) +
807 nla_total_size(sizeof(struct ifla_vf_vlan)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400808 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200809 nla_total_size(sizeof(struct ifla_vf_rate)) +
810 nla_total_size(sizeof(struct ifla_vf_link_state)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700811 return size;
812 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000813 return 0;
814}
815
David Gibsonc53864f2014-04-24 10:22:36 +1000816static size_t rtnl_port_size(const struct net_device *dev,
817 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700818{
819 size_t port_size = nla_total_size(4) /* PORT_VF */
820 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
821 + nla_total_size(sizeof(struct ifla_port_vsi))
822 /* PORT_VSI_TYPE */
823 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
824 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
825 + nla_total_size(1) /* PROT_VDP_REQUEST */
826 + nla_total_size(2); /* PORT_VDP_RESPONSE */
827 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
828 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
829 + port_size;
830 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
831 + port_size;
832
David Gibsonc53864f2014-04-24 10:22:36 +1000833 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
834 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700835 return 0;
836 if (dev_num_vf(dev->dev.parent))
837 return port_self_size + vf_ports_size +
838 vf_port_size * dev_num_vf(dev->dev.parent);
839 else
840 return port_self_size;
841}
842
Greg Rose115c9b82012-02-21 16:54:48 -0500843static noinline size_t if_nlmsg_size(const struct net_device *dev,
844 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800845{
846 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
847 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700848 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800849 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
850 + nla_total_size(sizeof(struct rtnl_link_ifmap))
851 + nla_total_size(sizeof(struct rtnl_link_stats))
Jan Engelhardtadcfe192010-03-27 17:15:29 -0700852 + nla_total_size(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800853 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
854 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
855 + nla_total_size(4) /* IFLA_TXQLEN */
856 + nla_total_size(4) /* IFLA_WEIGHT */
857 + nla_total_size(4) /* IFLA_MTU */
858 + nla_total_size(4) /* IFLA_LINK */
859 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000860 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000861 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000862 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
863 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Thomas Graf339bf982006-11-10 14:10:15 -0800864 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700865 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700866 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Greg Rose115c9b82012-02-21 16:54:48 -0500867 + nla_total_size(ext_filter_mask
868 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
869 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000870 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000871 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200872 + rtnl_link_get_af_size(dev) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100873 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
874 + nla_total_size(MAX_PHYS_ITEM_ID_LEN); /* IFLA_PHYS_SWITCH_ID */
Thomas Graf339bf982006-11-10 14:10:15 -0800875}
876
Scott Feldman57b61082010-05-17 22:49:55 -0700877static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
878{
879 struct nlattr *vf_ports;
880 struct nlattr *vf_port;
881 int vf;
882 int err;
883
884 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
885 if (!vf_ports)
886 return -EMSGSIZE;
887
888 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
889 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -0700890 if (!vf_port)
891 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -0400892 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
893 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700894 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -0700895 if (err == -EMSGSIZE)
896 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -0700897 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -0700898 nla_nest_cancel(skb, vf_port);
899 continue;
900 }
901 nla_nest_end(skb, vf_port);
902 }
903
904 nla_nest_end(skb, vf_ports);
905
906 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -0700907
908nla_put_failure:
909 nla_nest_cancel(skb, vf_ports);
910 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -0700911}
912
913static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
914{
915 struct nlattr *port_self;
916 int err;
917
918 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
919 if (!port_self)
920 return -EMSGSIZE;
921
922 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
923 if (err) {
924 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -0700925 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -0700926 }
927
928 nla_nest_end(skb, port_self);
929
930 return 0;
931}
932
David Gibsonc53864f2014-04-24 10:22:36 +1000933static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
934 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700935{
936 int err;
937
David Gibsonc53864f2014-04-24 10:22:36 +1000938 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
939 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700940 return 0;
941
942 err = rtnl_port_self_fill(skb, dev);
943 if (err)
944 return err;
945
946 if (dev_num_vf(dev->dev.parent)) {
947 err = rtnl_vf_ports_fill(skb, dev);
948 if (err)
949 return err;
950 }
951
952 return 0;
953}
954
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200955static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
956{
957 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +0100958 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +0200959
960 err = dev_get_phys_port_id(dev, &ppid);
961 if (err) {
962 if (err == -EOPNOTSUPP)
963 return 0;
964 return err;
965 }
966
967 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
968 return -EMSGSIZE;
969
970 return 0;
971}
972
Jiri Pirko82f28412014-11-28 14:34:18 +0100973static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
974{
975 int err;
976 struct netdev_phys_item_id psid;
977
978 err = netdev_switch_parent_id_get(dev, &psid);
979 if (err) {
980 if (err == -EOPNOTSUPP)
981 return 0;
982 return err;
983 }
984
985 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, psid.id_len, psid.id))
986 return -EMSGSIZE;
987
988 return 0;
989}
990
Thomas Grafb60c5112006-08-04 23:05:34 -0700991static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Patrick McHardy575c3e22007-05-22 17:00:49 -0700992 int type, u32 pid, u32 seq, u32 change,
Greg Rose115c9b82012-02-21 16:54:48 -0500993 unsigned int flags, u32 ext_filter_mask)
Thomas Grafb60c5112006-08-04 23:05:34 -0700994{
995 struct ifinfomsg *ifm;
996 struct nlmsghdr *nlh;
Eric Dumazet28172732010-07-07 14:58:56 -0700997 struct rtnl_link_stats64 temp;
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000998 const struct rtnl_link_stats64 *stats;
Thomas Graff8ff1822010-11-16 04:30:14 +0000999 struct nlattr *attr, *af_spec;
1000 struct rtnl_af_ops *af_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00001001 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Thomas Grafb60c5112006-08-04 23:05:34 -07001002
Eric Dumazet2907c352011-05-25 07:34:04 +00001003 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001004 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1005 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001006 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001007
1008 ifm = nlmsg_data(nlh);
1009 ifm->ifi_family = AF_UNSPEC;
1010 ifm->__ifi_pad = 0;
1011 ifm->ifi_type = dev->type;
1012 ifm->ifi_index = dev->ifindex;
1013 ifm->ifi_flags = dev_get_flags(dev);
1014 ifm->ifi_change = change;
1015
David S. Millera6574342012-04-01 20:12:00 -04001016 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1017 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1018 nla_put_u8(skb, IFLA_OPERSTATE,
1019 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1020 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1021 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1022 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001023 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001024 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001025#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001026 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001027#endif
David S. Millera6574342012-04-01 20:12:00 -04001028 (dev->ifindex != dev->iflink &&
1029 nla_put_u32(skb, IFLA_LINK, dev->iflink)) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00001030 (upper_dev &&
1031 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001032 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001033 (dev->qdisc &&
1034 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1035 (dev->ifalias &&
david decotigny2d3b4792014-03-29 09:48:35 -07001036 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1037 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1038 atomic_read(&dev->carrier_changes)))
David S. Millera6574342012-04-01 20:12:00 -04001039 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001040
Stefan Rompfb00055a2006-03-20 17:09:11 -08001041 if (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct rtnl_link_ifmap map = {
1043 .mem_start = dev->mem_start,
1044 .mem_end = dev->mem_end,
1045 .base_addr = dev->base_addr,
1046 .irq = dev->irq,
1047 .dma = dev->dma,
1048 .port = dev->if_port,
1049 };
David S. Millera6574342012-04-01 20:12:00 -04001050 if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1051 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
1054 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001055 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1056 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1057 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001060 if (rtnl_phys_port_id_fill(skb, dev))
1061 goto nla_put_failure;
1062
Jiri Pirko82f28412014-11-28 14:34:18 +01001063 if (rtnl_phys_switch_id_fill(skb, dev))
1064 goto nla_put_failure;
1065
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001066 attr = nla_reserve(skb, IFLA_STATS,
1067 sizeof(struct rtnl_link_stats));
1068 if (attr == NULL)
1069 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Eric Dumazet28172732010-07-07 14:58:56 -07001071 stats = dev_get_stats(dev, &temp);
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001072 copy_rtnl_link_stats(nla_data(attr), stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Jan Engelhardt10708f32010-03-11 09:57:29 +00001074 attr = nla_reserve(skb, IFLA_STATS64,
1075 sizeof(struct rtnl_link_stats64));
1076 if (attr == NULL)
1077 goto nla_put_failure;
Jan Engelhardt10708f32010-03-11 09:57:29 +00001078 copy_rtnl_link_stats64(nla_data(attr), stats);
1079
David S. Millera6574342012-04-01 20:12:00 -04001080 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1081 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1082 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001083
Greg Rose115c9b82012-02-21 16:54:48 -05001084 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
1085 && (ext_filter_mask & RTEXT_FILTER_VF)) {
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001086 int i;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001087
Chris Wrightc02db8c2010-05-16 01:05:45 -07001088 struct nlattr *vfinfo, *vf;
1089 int num_vfs = dev_num_vf(dev->dev.parent);
1090
Chris Wrightc02db8c2010-05-16 01:05:45 -07001091 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1092 if (!vfinfo)
1093 goto nla_put_failure;
1094 for (i = 0; i < num_vfs; i++) {
1095 struct ifla_vf_info ivi;
1096 struct ifla_vf_mac vf_mac;
1097 struct ifla_vf_vlan vf_vlan;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001098 struct ifla_vf_rate vf_rate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001099 struct ifla_vf_tx_rate vf_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001100 struct ifla_vf_spoofchk vf_spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001101 struct ifla_vf_link_state vf_linkstate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001102
1103 /*
1104 * Not all SR-IOV capable drivers support the
1105 * spoofcheck query. Preset to -1 so the user
1106 * space tool can detect that the driver didn't
1107 * report anything.
1108 */
1109 ivi.spoofchk = -1;
Mathias Krause84d73cd2013-03-09 05:52:20 +00001110 memset(ivi.mac, 0, sizeof(ivi.mac));
Rony Efraim1d8faf42013-06-13 13:19:10 +03001111 /* The default value for VF link state is "auto"
1112 * IFLA_VF_LINK_STATE_AUTO which equals zero
1113 */
1114 ivi.linkstate = 0;
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001115 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1116 break;
Greg Rose5f8444a2011-10-08 03:05:24 +00001117 vf_mac.vf =
1118 vf_vlan.vf =
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001119 vf_rate.vf =
Greg Rose5f8444a2011-10-08 03:05:24 +00001120 vf_tx_rate.vf =
Rony Efraim1d8faf42013-06-13 13:19:10 +03001121 vf_spoofchk.vf =
1122 vf_linkstate.vf = ivi.vf;
Greg Rose5f8444a2011-10-08 03:05:24 +00001123
Chris Wrightc02db8c2010-05-16 01:05:45 -07001124 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1125 vf_vlan.vlan = ivi.vlan;
1126 vf_vlan.qos = ivi.qos;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001127 vf_tx_rate.rate = ivi.max_tx_rate;
1128 vf_rate.min_tx_rate = ivi.min_tx_rate;
1129 vf_rate.max_tx_rate = ivi.max_tx_rate;
Greg Rose5f8444a2011-10-08 03:05:24 +00001130 vf_spoofchk.setting = ivi.spoofchk;
Rony Efraim1d8faf42013-06-13 13:19:10 +03001131 vf_linkstate.link_state = ivi.linkstate;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001132 vf = nla_nest_start(skb, IFLA_VF_INFO);
1133 if (!vf) {
1134 nla_nest_cancel(skb, vfinfo);
1135 goto nla_put_failure;
1136 }
David S. Millera6574342012-04-01 20:12:00 -04001137 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1138 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001139 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1140 &vf_rate) ||
David S. Millera6574342012-04-01 20:12:00 -04001141 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1142 &vf_tx_rate) ||
1143 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
Rony Efraim1d8faf42013-06-13 13:19:10 +03001144 &vf_spoofchk) ||
1145 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1146 &vf_linkstate))
David S. Millera6574342012-04-01 20:12:00 -04001147 goto nla_put_failure;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001148 nla_nest_end(skb, vf);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001149 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001150 nla_nest_end(skb, vfinfo);
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001151 }
Scott Feldman57b61082010-05-17 22:49:55 -07001152
David Gibsonc53864f2014-04-24 10:22:36 +10001153 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001154 goto nla_put_failure;
1155
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001156 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001157 if (rtnl_link_fill(skb, dev) < 0)
1158 goto nla_put_failure;
1159 }
1160
Thomas Graff8ff1822010-11-16 04:30:14 +00001161 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1162 goto nla_put_failure;
1163
1164 list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1165 if (af_ops->fill_link_af) {
1166 struct nlattr *af;
1167 int err;
1168
1169 if (!(af = nla_nest_start(skb, af_ops->family)))
1170 goto nla_put_failure;
1171
1172 err = af_ops->fill_link_af(skb, dev);
1173
1174 /*
1175 * Caller may return ENODATA to indicate that there
1176 * was no data to be dumped. This is not an error, it
1177 * means we should trim the attribute header and
1178 * continue.
1179 */
1180 if (err == -ENODATA)
1181 nla_nest_cancel(skb, af);
1182 else if (err < 0)
1183 goto nla_put_failure;
1184
1185 nla_nest_end(skb, af);
1186 }
1187 }
1188
1189 nla_nest_end(skb, af_spec);
1190
Thomas Grafb60c5112006-08-04 23:05:34 -07001191 return nlmsg_end(skb, nlh);
1192
1193nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001194 nlmsg_cancel(skb, nlh);
1195 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Jiri Pirkof7b12602014-02-18 20:53:18 +01001198static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1199 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1200 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1201 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1202 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1203 [IFLA_MTU] = { .type = NLA_U32 },
1204 [IFLA_LINK] = { .type = NLA_U32 },
1205 [IFLA_MASTER] = { .type = NLA_U32 },
1206 [IFLA_CARRIER] = { .type = NLA_U8 },
1207 [IFLA_TXQLEN] = { .type = NLA_U32 },
1208 [IFLA_WEIGHT] = { .type = NLA_U32 },
1209 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1210 [IFLA_LINKMODE] = { .type = NLA_U8 },
1211 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1212 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1213 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
1214 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 },
1215 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1216 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1217 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1218 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1219 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1220 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1221 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1222 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001223 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001224 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001225 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001226};
1227
1228static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1229 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1230 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1231 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1232 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1233};
1234
1235static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1236 [IFLA_VF_INFO] = { .type = NLA_NESTED },
1237};
1238
1239static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1240 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1241 .len = sizeof(struct ifla_vf_mac) },
1242 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1243 .len = sizeof(struct ifla_vf_vlan) },
1244 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1245 .len = sizeof(struct ifla_vf_tx_rate) },
1246 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1247 .len = sizeof(struct ifla_vf_spoofchk) },
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001248 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1249 .len = sizeof(struct ifla_vf_rate) },
Doug Ledfordc5b46162014-06-11 10:38:03 -04001250 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1251 .len = sizeof(struct ifla_vf_link_state) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001252};
1253
1254static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1255 [IFLA_PORT_VF] = { .type = NLA_U32 },
1256 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1257 .len = PORT_PROFILE_MAX },
1258 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1259 .len = sizeof(struct ifla_port_vsi)},
1260 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1261 .len = PORT_UUID_MAX },
1262 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1263 .len = PORT_UUID_MAX },
1264 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1265 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
1266};
1267
Thomas Grafb60c5112006-08-04 23:05:34 -07001268static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001270 struct net *net = sock_net(skb->sk);
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001271 int h, s_h;
1272 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001274 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001275 struct nlattr *tb[IFLA_MAX+1];
1276 u32 ext_filter_mask = 0;
David Gibson973462b2014-04-24 10:22:35 +10001277 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001278 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001280 s_h = cb->args[0];
1281 s_idx = cb->args[1];
1282
Eric Dumazete67f88d2011-04-27 22:56:07 +00001283 rcu_read_lock();
Thomas Graf4e985ad2011-06-21 03:11:20 +00001284 cb->seq = net->dev_base_seq;
1285
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001286 /* A hack to preserve kernel<->userspace interface.
1287 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1288 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1289 * what iproute2 < v3.9.0 used.
1290 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1291 * attribute, its netlink message is shorter than struct ifinfomsg.
1292 */
1293 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1294 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1295
1296 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Greg Rose115c9b82012-02-21 16:54:48 -05001297
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001298 if (tb[IFLA_EXT_MASK])
1299 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1300 }
Greg Rose115c9b82012-02-21 16:54:48 -05001301
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001302 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1303 idx = 0;
1304 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001305 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001306 if (idx < s_idx)
1307 goto cont;
David Gibson973462b2014-04-24 10:22:35 +10001308 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1309 NETLINK_CB(cb->skb).portid,
1310 cb->nlh->nlmsg_seq, 0,
1311 NLM_F_MULTI,
1312 ext_filter_mask);
1313 /* If we ran out of room on the first message,
1314 * we're in trouble
1315 */
1316 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1317
1318 if (err <= 0)
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001319 goto out;
Thomas Graf4e985ad2011-06-21 03:11:20 +00001320
1321 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pavel Emelianov7562f872007-05-03 15:13:45 -07001322cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001323 idx++;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001326out:
Eric Dumazete67f88d2011-04-27 22:56:07 +00001327 rcu_read_unlock();
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001328 cb->args[1] = idx;
1329 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 return skb->len;
1332}
1333
Jiri Pirkof7b12602014-02-18 20:53:18 +01001334int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1335{
1336 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1337}
1338EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001339
Eric W. Biederman81adee42009-11-08 00:53:51 -08001340struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1341{
1342 struct net *net;
1343 /* Examine the link attributes and figure out which
1344 * network namespace we are talking about.
1345 */
1346 if (tb[IFLA_NET_NS_PID])
1347 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001348 else if (tb[IFLA_NET_NS_FD])
1349 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001350 else
1351 net = get_net(src_net);
1352 return net;
1353}
1354EXPORT_SYMBOL(rtnl_link_get_net);
1355
Thomas Graf1840bb12008-02-23 19:54:36 -08001356static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1357{
1358 if (dev) {
1359 if (tb[IFLA_ADDRESS] &&
1360 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1361 return -EINVAL;
1362
1363 if (tb[IFLA_BROADCAST] &&
1364 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1365 return -EINVAL;
1366 }
1367
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001368 if (tb[IFLA_AF_SPEC]) {
1369 struct nlattr *af;
1370 int rem, err;
1371
1372 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1373 const struct rtnl_af_ops *af_ops;
1374
1375 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1376 return -EAFNOSUPPORT;
1377
1378 if (!af_ops->set_link_af)
1379 return -EOPNOTSUPP;
1380
1381 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001382 err = af_ops->validate_link_af(dev, af);
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001383 if (err < 0)
1384 return err;
1385 }
1386 }
1387 }
1388
Thomas Graf1840bb12008-02-23 19:54:36 -08001389 return 0;
1390}
1391
Chris Wrightc02db8c2010-05-16 01:05:45 -07001392static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1393{
1394 int rem, err = -EINVAL;
1395 struct nlattr *vf;
1396 const struct net_device_ops *ops = dev->netdev_ops;
1397
1398 nla_for_each_nested(vf, attr, rem) {
1399 switch (nla_type(vf)) {
1400 case IFLA_VF_MAC: {
1401 struct ifla_vf_mac *ivm;
1402 ivm = nla_data(vf);
1403 err = -EOPNOTSUPP;
1404 if (ops->ndo_set_vf_mac)
1405 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1406 ivm->mac);
1407 break;
1408 }
1409 case IFLA_VF_VLAN: {
1410 struct ifla_vf_vlan *ivv;
1411 ivv = nla_data(vf);
1412 err = -EOPNOTSUPP;
1413 if (ops->ndo_set_vf_vlan)
1414 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1415 ivv->vlan,
1416 ivv->qos);
1417 break;
1418 }
1419 case IFLA_VF_TX_RATE: {
1420 struct ifla_vf_tx_rate *ivt;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001421 struct ifla_vf_info ivf;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001422 ivt = nla_data(vf);
1423 err = -EOPNOTSUPP;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04001424 if (ops->ndo_get_vf_config)
1425 err = ops->ndo_get_vf_config(dev, ivt->vf,
1426 &ivf);
1427 if (err)
1428 break;
1429 err = -EOPNOTSUPP;
1430 if (ops->ndo_set_vf_rate)
1431 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1432 ivf.min_tx_rate,
1433 ivt->rate);
1434 break;
1435 }
1436 case IFLA_VF_RATE: {
1437 struct ifla_vf_rate *ivt;
1438 ivt = nla_data(vf);
1439 err = -EOPNOTSUPP;
1440 if (ops->ndo_set_vf_rate)
1441 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1442 ivt->min_tx_rate,
1443 ivt->max_tx_rate);
Chris Wrightc02db8c2010-05-16 01:05:45 -07001444 break;
1445 }
Greg Rose5f8444a2011-10-08 03:05:24 +00001446 case IFLA_VF_SPOOFCHK: {
1447 struct ifla_vf_spoofchk *ivs;
1448 ivs = nla_data(vf);
1449 err = -EOPNOTSUPP;
1450 if (ops->ndo_set_vf_spoofchk)
1451 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1452 ivs->setting);
1453 break;
1454 }
Rony Efraim1d8faf42013-06-13 13:19:10 +03001455 case IFLA_VF_LINK_STATE: {
1456 struct ifla_vf_link_state *ivl;
1457 ivl = nla_data(vf);
1458 err = -EOPNOTSUPP;
1459 if (ops->ndo_set_vf_link_state)
1460 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1461 ivl->link_state);
1462 break;
1463 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001464 default:
1465 err = -EINVAL;
1466 break;
1467 }
1468 if (err)
1469 break;
1470 }
1471 return err;
1472}
1473
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001474static int do_set_master(struct net_device *dev, int ifindex)
1475{
Jiri Pirko898e5062013-01-03 22:48:52 +00001476 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001477 const struct net_device_ops *ops;
1478 int err;
1479
Jiri Pirko898e5062013-01-03 22:48:52 +00001480 if (upper_dev) {
1481 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001482 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00001483 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001484 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001485 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001486 if (err)
1487 return err;
1488 } else {
1489 return -EOPNOTSUPP;
1490 }
1491 }
1492
1493 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001494 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1495 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001496 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00001497 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001498 if (ops->ndo_add_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00001499 err = ops->ndo_add_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001500 if (err)
1501 return err;
1502 } else {
1503 return -EOPNOTSUPP;
1504 }
1505 }
1506 return 0;
1507}
1508
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001509#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02001510/* notify flag means notify + modified. */
1511#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001512static int do_setlink(const struct sk_buff *skb,
1513 struct net_device *dev, struct ifinfomsg *ifm,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001514 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07001515{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001516 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07001517 int err;
1518
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001519 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08001520 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001521 if (IS_ERR(net)) {
1522 err = PTR_ERR(net);
1523 goto errout;
1524 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001525 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01001526 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001527 err = -EPERM;
1528 goto errout;
1529 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001530 err = dev_change_net_namespace(dev, net, ifname);
1531 put_net(net);
1532 if (err)
1533 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001534 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001535 }
1536
Patrick McHardy0157f602007-06-13 12:03:36 -07001537 if (tb[IFLA_MAP]) {
1538 struct rtnl_link_ifmap *u_map;
1539 struct ifmap k_map;
1540
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001541 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001542 err = -EOPNOTSUPP;
1543 goto errout;
1544 }
1545
1546 if (!netif_device_present(dev)) {
1547 err = -ENODEV;
1548 goto errout;
1549 }
1550
1551 u_map = nla_data(tb[IFLA_MAP]);
1552 k_map.mem_start = (unsigned long) u_map->mem_start;
1553 k_map.mem_end = (unsigned long) u_map->mem_end;
1554 k_map.base_addr = (unsigned short) u_map->base_addr;
1555 k_map.irq = (unsigned char) u_map->irq;
1556 k_map.dma = (unsigned char) u_map->dma;
1557 k_map.port = (unsigned char) u_map->port;
1558
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001559 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001560 if (err < 0)
1561 goto errout;
1562
Nicolas Dichtelba998902014-09-01 16:07:29 +02001563 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001564 }
1565
1566 if (tb[IFLA_ADDRESS]) {
1567 struct sockaddr *sa;
1568 int len;
1569
Patrick McHardy0157f602007-06-13 12:03:36 -07001570 len = sizeof(sa_family_t) + dev->addr_len;
1571 sa = kmalloc(len, GFP_KERNEL);
1572 if (!sa) {
1573 err = -ENOMEM;
1574 goto errout;
1575 }
1576 sa->sa_family = dev->type;
1577 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1578 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001579 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001580 kfree(sa);
1581 if (err)
1582 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001583 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001584 }
1585
1586 if (tb[IFLA_MTU]) {
1587 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1588 if (err < 0)
1589 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001590 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001591 }
1592
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001593 if (tb[IFLA_GROUP]) {
1594 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001595 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001596 }
1597
Patrick McHardy0157f602007-06-13 12:03:36 -07001598 /*
1599 * Interface selected by interface index but interface
1600 * name provided implies that a name change has been
1601 * requested.
1602 */
1603 if (ifm->ifi_index > 0 && ifname[0]) {
1604 err = dev_change_name(dev, ifname);
1605 if (err < 0)
1606 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001607 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001608 }
1609
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001610 if (tb[IFLA_IFALIAS]) {
1611 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1612 nla_len(tb[IFLA_IFALIAS]));
1613 if (err < 0)
1614 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001615 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001616 }
1617
Patrick McHardy0157f602007-06-13 12:03:36 -07001618 if (tb[IFLA_BROADCAST]) {
1619 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001620 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001621 }
1622
1623 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001624 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001625 if (err < 0)
1626 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001627 }
1628
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001629 if (tb[IFLA_MASTER]) {
1630 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1631 if (err)
1632 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001633 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001634 }
1635
Jiri Pirko9a572472012-12-27 23:49:39 +00001636 if (tb[IFLA_CARRIER]) {
1637 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1638 if (err)
1639 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001640 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001641 }
1642
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001643 if (tb[IFLA_TXQLEN]) {
1644 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1645
1646 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001647 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001648
1649 dev->tx_queue_len = value;
1650 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001651
Patrick McHardy0157f602007-06-13 12:03:36 -07001652 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001653 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001654
1655 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001656 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1657
David S. Miller93b2d4a2008-02-17 18:35:07 -08001658 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001659 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001660 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001661 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001662 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001663 }
1664
Chris Wrightc02db8c2010-05-16 01:05:45 -07001665 if (tb[IFLA_VFINFO_LIST]) {
1666 struct nlattr *attr;
1667 int rem;
1668 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001669 if (nla_type(attr) != IFLA_VF_INFO) {
1670 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001671 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001672 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001673 err = do_setvfinfo(dev, attr);
1674 if (err < 0)
1675 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001676 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001677 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001678 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001679 err = 0;
1680
Scott Feldman57b61082010-05-17 22:49:55 -07001681 if (tb[IFLA_VF_PORTS]) {
1682 struct nlattr *port[IFLA_PORT_MAX+1];
1683 struct nlattr *attr;
1684 int vf;
1685 int rem;
1686
1687 err = -EOPNOTSUPP;
1688 if (!ops->ndo_set_vf_port)
1689 goto errout;
1690
1691 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1692 if (nla_type(attr) != IFLA_VF_PORT)
1693 continue;
1694 err = nla_parse_nested(port, IFLA_PORT_MAX,
1695 attr, ifla_port_policy);
1696 if (err < 0)
1697 goto errout;
1698 if (!port[IFLA_PORT_VF]) {
1699 err = -EOPNOTSUPP;
1700 goto errout;
1701 }
1702 vf = nla_get_u32(port[IFLA_PORT_VF]);
1703 err = ops->ndo_set_vf_port(dev, vf, port);
1704 if (err < 0)
1705 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001706 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001707 }
1708 }
1709 err = 0;
1710
1711 if (tb[IFLA_PORT_SELF]) {
1712 struct nlattr *port[IFLA_PORT_MAX+1];
1713
1714 err = nla_parse_nested(port, IFLA_PORT_MAX,
1715 tb[IFLA_PORT_SELF], ifla_port_policy);
1716 if (err < 0)
1717 goto errout;
1718
1719 err = -EOPNOTSUPP;
1720 if (ops->ndo_set_vf_port)
1721 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1722 if (err < 0)
1723 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001724 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001725 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001726
1727 if (tb[IFLA_AF_SPEC]) {
1728 struct nlattr *af;
1729 int rem;
1730
1731 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1732 const struct rtnl_af_ops *af_ops;
1733
1734 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001735 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001736
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001737 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001738 if (err < 0)
1739 goto errout;
1740
Nicolas Dichtelba998902014-09-01 16:07:29 +02001741 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001742 }
1743 }
Scott Feldman57b61082010-05-17 22:49:55 -07001744 err = 0;
1745
Patrick McHardy0157f602007-06-13 12:03:36 -07001746errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001747 if (status & DO_SETLINK_MODIFIED) {
1748 if (status & DO_SETLINK_NOTIFY)
1749 netdev_state_change(dev);
1750
1751 if (err < 0)
1752 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",
1753 dev->name);
1754 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001755
Patrick McHardy0157f602007-06-13 12:03:36 -07001756 return err;
1757}
1758
Thomas Graf661d2962013-03-21 07:45:29 +00001759static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001761 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001762 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001764 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001765 struct nlattr *tb[IFLA_MAX+1];
1766 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Thomas Grafda5e0492006-08-10 21:17:37 -07001768 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1769 if (err < 0)
1770 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Thomas Graf5176f912006-08-26 20:13:18 -07001772 if (tb[IFLA_IFNAME])
1773 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001774 else
1775 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001778 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001779 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001780 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001781 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001782 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001783 else
1784 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Thomas Grafda5e0492006-08-10 21:17:37 -07001786 if (dev == NULL) {
1787 err = -ENODEV;
1788 goto errout;
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Eric Dumazete0d087a2009-11-07 01:26:17 -08001791 err = validate_linkmsg(dev, tb);
1792 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001793 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001794
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001795 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001796errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return err;
1798}
1799
Thomas Graf661d2962013-03-21 07:45:29 +00001800static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001801{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001802 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001803 const struct rtnl_link_ops *ops;
1804 struct net_device *dev;
1805 struct ifinfomsg *ifm;
1806 char ifname[IFNAMSIZ];
1807 struct nlattr *tb[IFLA_MAX+1];
1808 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001809 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001810
1811 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1812 if (err < 0)
1813 return err;
1814
1815 if (tb[IFLA_IFNAME])
1816 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1817
1818 ifm = nlmsg_data(nlh);
1819 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001820 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001821 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001822 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001823 else
1824 return -EINVAL;
1825
1826 if (!dev)
1827 return -ENODEV;
1828
1829 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001830 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001831 return -EOPNOTSUPP;
1832
Eric Dumazet226bd342011-05-08 23:17:57 +00001833 ops->dellink(dev, &list_kill);
1834 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001835 return 0;
1836}
1837
Patrick McHardy3729d502010-02-26 06:34:54 +00001838int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1839{
1840 unsigned int old_flags;
1841 int err;
1842
1843 old_flags = dev->flags;
1844 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1845 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1846 if (err < 0)
1847 return err;
1848 }
1849
1850 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001851
Nicolas Dichtela528c212013-09-25 12:02:44 +02001852 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001853 return 0;
1854}
1855EXPORT_SYMBOL(rtnl_configure_link);
1856
Rami Rosenc0713562012-11-30 01:08:47 +00001857struct net_device *rtnl_create_link(struct net *net,
Tom Gundersen55177502014-07-14 16:37:25 +02001858 char *ifname, unsigned char name_assign_type,
1859 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001860{
1861 int err;
1862 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001863 unsigned int num_tx_queues = 1;
1864 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001865
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001866 if (tb[IFLA_NUM_TX_QUEUES])
1867 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1868 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001869 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001870
1871 if (tb[IFLA_NUM_RX_QUEUES])
1872 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1873 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001874 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001875
Pavel Emelianove7199282007-08-08 22:16:38 -07001876 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001877 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001878 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001879 if (!dev)
1880 goto err;
1881
Eric W. Biederman81adee42009-11-08 00:53:51 -08001882 dev_net_set(dev, net);
1883 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001884 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001885
Pavel Emelianove7199282007-08-08 22:16:38 -07001886 if (tb[IFLA_MTU])
1887 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001888 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001889 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1890 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001891 dev->addr_assign_type = NET_ADDR_SET;
1892 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001893 if (tb[IFLA_BROADCAST])
1894 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1895 nla_len(tb[IFLA_BROADCAST]));
1896 if (tb[IFLA_TXQLEN])
1897 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1898 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001899 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001900 if (tb[IFLA_LINKMODE])
1901 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001902 if (tb[IFLA_GROUP])
1903 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001904
1905 return dev;
1906
Pavel Emelianove7199282007-08-08 22:16:38 -07001907err:
1908 return ERR_PTR(err);
1909}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001910EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001911
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001912static int rtnl_group_changelink(const struct sk_buff *skb,
1913 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001914 struct ifinfomsg *ifm,
1915 struct nlattr **tb)
1916{
1917 struct net_device *dev;
1918 int err;
1919
1920 for_each_netdev(net, dev) {
1921 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001922 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001923 if (err < 0)
1924 return err;
1925 }
1926 }
1927
1928 return 0;
1929}
1930
Thomas Graf661d2962013-03-21 07:45:29 +00001931static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001932{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001933 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001934 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001935 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001936 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001937 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001938 struct ifinfomsg *ifm;
1939 char kind[MODULE_NAME_LEN];
1940 char ifname[IFNAMSIZ];
1941 struct nlattr *tb[IFLA_MAX+1];
1942 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02001943 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001944 int err;
1945
Johannes Berg95a5afc2008-10-16 15:24:51 -07001946#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001947replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001948#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001949 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1950 if (err < 0)
1951 return err;
1952
1953 if (tb[IFLA_IFNAME])
1954 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1955 else
1956 ifname[0] = '\0';
1957
1958 ifm = nlmsg_data(nlh);
1959 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001960 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001961 else {
1962 if (ifname[0])
1963 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001964 else
1965 dev = NULL;
1966 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001967
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001968 if (dev) {
1969 master_dev = netdev_master_upper_dev_get(dev);
1970 if (master_dev)
1971 m_ops = master_dev->rtnl_link_ops;
1972 }
1973
Eric Dumazete0d087a2009-11-07 01:26:17 -08001974 err = validate_linkmsg(dev, tb);
1975 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001976 return err;
1977
Patrick McHardy38f7b872007-06-13 12:03:51 -07001978 if (tb[IFLA_LINKINFO]) {
1979 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1980 tb[IFLA_LINKINFO], ifla_info_policy);
1981 if (err < 0)
1982 return err;
1983 } else
1984 memset(linkinfo, 0, sizeof(linkinfo));
1985
1986 if (linkinfo[IFLA_INFO_KIND]) {
1987 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1988 ops = rtnl_link_ops_get(kind);
1989 } else {
1990 kind[0] = '\0';
1991 ops = NULL;
1992 }
1993
1994 if (1) {
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001995 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1996 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1997 struct nlattr **data = NULL;
1998 struct nlattr **slave_data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001999 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002000
2001 if (ops) {
2002 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2003 err = nla_parse_nested(attr, ops->maxtype,
2004 linkinfo[IFLA_INFO_DATA],
2005 ops->policy);
2006 if (err < 0)
2007 return err;
2008 data = attr;
2009 }
2010 if (ops->validate) {
2011 err = ops->validate(tb, data);
2012 if (err < 0)
2013 return err;
2014 }
2015 }
2016
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002017 if (m_ops) {
2018 if (m_ops->slave_maxtype &&
2019 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2020 err = nla_parse_nested(slave_attr,
2021 m_ops->slave_maxtype,
2022 linkinfo[IFLA_INFO_SLAVE_DATA],
2023 m_ops->slave_policy);
2024 if (err < 0)
2025 return err;
2026 slave_data = slave_attr;
2027 }
2028 if (m_ops->slave_validate) {
2029 err = m_ops->slave_validate(tb, slave_data);
2030 if (err < 0)
2031 return err;
2032 }
2033 }
2034
Patrick McHardy38f7b872007-06-13 12:03:51 -07002035 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002036 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002037
2038 if (nlh->nlmsg_flags & NLM_F_EXCL)
2039 return -EEXIST;
2040 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2041 return -EOPNOTSUPP;
2042
2043 if (linkinfo[IFLA_INFO_DATA]) {
2044 if (!ops || ops != dev->rtnl_link_ops ||
2045 !ops->changelink)
2046 return -EOPNOTSUPP;
2047
2048 err = ops->changelink(dev, tb, data);
2049 if (err < 0)
2050 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002051 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002052 }
2053
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002054 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2055 if (!m_ops || !m_ops->slave_changelink)
2056 return -EOPNOTSUPP;
2057
2058 err = m_ops->slave_changelink(master_dev, dev,
2059 tb, slave_data);
2060 if (err < 0)
2061 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002062 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002063 }
2064
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002065 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002066 }
2067
Patrick McHardyffa934f2011-01-20 03:00:42 +00002068 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2069 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002070 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002071 nla_get_u32(tb[IFLA_GROUP]),
2072 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002073 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002074 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002075
Patrick McHardy0e068772007-07-11 19:42:31 -07002076 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002077 return -EOPNOTSUPP;
2078
2079 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002080#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002081 if (kind[0]) {
2082 __rtnl_unlock();
2083 request_module("rtnl-link-%s", kind);
2084 rtnl_lock();
2085 ops = rtnl_link_ops_get(kind);
2086 if (ops)
2087 goto replay;
2088 }
2089#endif
2090 return -EOPNOTSUPP;
2091 }
2092
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002093 if (!ops->setup)
2094 return -EOPNOTSUPP;
2095
Tom Gundersen55177502014-07-14 16:37:25 +02002096 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002097 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002098 name_assign_type = NET_NAME_ENUM;
2099 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002100
Eric W. Biederman81adee42009-11-08 00:53:51 -08002101 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002102 if (IS_ERR(dest_net))
2103 return PTR_ERR(dest_net);
2104
Tom Gundersen55177502014-07-14 16:37:25 +02002105 dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002106 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002107 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002108 goto out;
2109 }
2110
2111 dev->ifindex = ifm->ifi_index;
2112
Cong Wang0e0eee22014-02-11 15:51:30 -08002113 if (ops->newlink) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002114 err = ops->newlink(net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002115 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002116 * and unregister it on failure after registration
2117 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002118 */
Cong Wange51fb152014-06-03 16:40:47 -07002119 if (err < 0) {
2120 /* If device is not registered at all, free it now */
2121 if (dev->reg_state == NETREG_UNINITIALIZED)
2122 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002123 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002124 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002125 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002126 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002127 if (err < 0) {
2128 free_netdev(dev);
2129 goto out;
2130 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002131 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002132 err = rtnl_configure_link(dev, ifm);
2133 if (err < 0)
2134 unregister_netdevice(dev);
2135out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08002136 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002137 return err;
2138 }
2139}
2140
Thomas Graf661d2962013-03-21 07:45:29 +00002141static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002142{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002143 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002144 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002145 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002146 struct nlattr *tb[IFLA_MAX+1];
2147 struct net_device *dev = NULL;
2148 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002149 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002150 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002151
Thomas Grafb60c5112006-08-04 23:05:34 -07002152 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2153 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002154 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002155
Eric Dumazeta3d12892009-10-21 10:59:31 +00002156 if (tb[IFLA_IFNAME])
2157 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2158
Greg Rose115c9b82012-02-21 16:54:48 -05002159 if (tb[IFLA_EXT_MASK])
2160 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2161
Thomas Grafb60c5112006-08-04 23:05:34 -07002162 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002163 if (ifm->ifi_index > 0)
2164 dev = __dev_get_by_index(net, ifm->ifi_index);
2165 else if (tb[IFLA_IFNAME])
2166 dev = __dev_get_by_name(net, ifname);
2167 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002168 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002169
Eric Dumazeta3d12892009-10-21 10:59:31 +00002170 if (dev == NULL)
2171 return -ENODEV;
2172
Greg Rose115c9b82012-02-21 16:54:48 -05002173 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002174 if (nskb == NULL)
2175 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002176
Eric W. Biederman15e47302012-09-07 20:12:54 +00002177 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002178 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002179 if (err < 0) {
2180 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2181 WARN_ON(err == -EMSGSIZE);
2182 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002183 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002184 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002185
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002186 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002187}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002188
Greg Rose115c9b82012-02-21 16:54:48 -05002189static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002190{
Greg Rose115c9b82012-02-21 16:54:48 -05002191 struct net *net = sock_net(skb->sk);
2192 struct net_device *dev;
2193 struct nlattr *tb[IFLA_MAX+1];
2194 u32 ext_filter_mask = 0;
2195 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002196 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002197
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002198 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2199 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2200 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2201
2202 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002203 if (tb[IFLA_EXT_MASK])
2204 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2205 }
Greg Rose115c9b82012-02-21 16:54:48 -05002206
2207 if (!ext_filter_mask)
2208 return NLMSG_GOODSIZE;
2209 /*
2210 * traverse the list of net devices and compute the minimum
2211 * buffer size based upon the filter mask.
2212 */
2213 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2214 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2215 if_nlmsg_size(dev,
2216 ext_filter_mask));
2217 }
2218
Greg Rosec7ac8672011-06-10 01:27:09 +00002219 return min_ifinfo_dump_size;
2220}
2221
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002222static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int idx;
2225 int s_idx = cb->family;
2226
2227 if (s_idx == 0)
2228 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002229 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 int type = cb->nlh->nlmsg_type-RTM_BASE;
2231 if (idx < s_idx || idx == PF_PACKET)
2232 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002233 if (rtnl_msg_handlers[idx] == NULL ||
2234 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002236 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002238 cb->prev_seq = 0;
2239 cb->seq = 0;
2240 }
Thomas Grafe2849862007-03-22 11:48:11 -07002241 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 }
2244 cb->family = idx;
2245
2246 return skb->len;
2247}
2248
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002249struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2250 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002252 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002254 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002255 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002257 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002258 if (skb == NULL)
2259 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Greg Rose115c9b82012-02-21 16:54:48 -05002261 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002262 if (err < 0) {
2263 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2264 WARN_ON(err == -EMSGSIZE);
2265 kfree_skb(skb);
2266 goto errout;
2267 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002268 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002269errout:
2270 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002271 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002272 return NULL;
2273}
2274
2275void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2276{
2277 struct net *net = dev_net(dev);
2278
2279 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2280}
2281
2282void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2283 gfp_t flags)
2284{
2285 struct sk_buff *skb;
2286
2287 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2288 if (skb)
2289 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002291EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
John Fastabendd83b0602012-04-15 06:44:08 +00002293static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2294 struct net_device *dev,
2295 u8 *addr, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002296 int type, unsigned int flags,
2297 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002298{
2299 struct nlmsghdr *nlh;
2300 struct ndmsg *ndm;
2301
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002302 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002303 if (!nlh)
2304 return -EMSGSIZE;
2305
2306 ndm = nlmsg_data(nlh);
2307 ndm->ndm_family = AF_BRIDGE;
2308 ndm->ndm_pad1 = 0;
2309 ndm->ndm_pad2 = 0;
2310 ndm->ndm_flags = flags;
2311 ndm->ndm_type = 0;
2312 ndm->ndm_ifindex = dev->ifindex;
2313 ndm->ndm_state = NUD_PERMANENT;
2314
2315 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2316 goto nla_put_failure;
2317
2318 return nlmsg_end(skb, nlh);
2319
2320nla_put_failure:
2321 nlmsg_cancel(skb, nlh);
2322 return -EMSGSIZE;
2323}
2324
John Fastabend3ff661c2012-04-15 06:44:14 +00002325static inline size_t rtnl_fdb_nlmsg_size(void)
2326{
2327 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2328}
2329
2330static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2331{
2332 struct net *net = dev_net(dev);
2333 struct sk_buff *skb;
2334 int err = -ENOBUFS;
2335
2336 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2337 if (!skb)
2338 goto errout;
2339
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002340 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002341 if (err < 0) {
2342 kfree_skb(skb);
2343 goto errout;
2344 }
2345
2346 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2347 return;
2348errout:
2349 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2350}
2351
Vlad Yasevich090096b2013-03-06 15:39:42 +00002352/**
2353 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2354 */
2355int ndo_dflt_fdb_add(struct ndmsg *ndm,
2356 struct nlattr *tb[],
2357 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002358 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002359 u16 flags)
2360{
2361 int err = -EINVAL;
2362
2363 /* If aging addresses are supported device will need to
2364 * implement its own handler for this.
2365 */
2366 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2367 pr_info("%s: FDB only supports static addresses\n", dev->name);
2368 return err;
2369 }
2370
Or Gerlitz65891fe2014-12-14 18:19:05 +02002371 if (vid) {
2372 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2373 return err;
2374 }
2375
Vlad Yasevich090096b2013-03-06 15:39:42 +00002376 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2377 err = dev_uc_add_excl(dev, addr);
2378 else if (is_multicast_ether_addr(addr))
2379 err = dev_mc_add_excl(dev, addr);
2380
2381 /* Only return duplicate errors if NLM_F_EXCL is set */
2382 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2383 err = 0;
2384
2385 return err;
2386}
2387EXPORT_SYMBOL(ndo_dflt_fdb_add);
2388
Jiri Pirkof6f64242014-11-28 14:34:15 +01002389static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2390{
2391 u16 vid = 0;
2392
2393 if (vlan_attr) {
2394 if (nla_len(vlan_attr) != sizeof(u16)) {
2395 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2396 return -EINVAL;
2397 }
2398
2399 vid = nla_get_u16(vlan_attr);
2400
2401 if (!vid || vid >= VLAN_VID_MASK) {
2402 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2403 vid);
2404 return -EINVAL;
2405 }
2406 }
2407 *p_vid = vid;
2408 return 0;
2409}
2410
Thomas Graf661d2962013-03-21 07:45:29 +00002411static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002412{
2413 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002414 struct ndmsg *ndm;
2415 struct nlattr *tb[NDA_MAX+1];
2416 struct net_device *dev;
2417 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002418 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002419 int err;
2420
2421 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2422 if (err < 0)
2423 return err;
2424
2425 ndm = nlmsg_data(nlh);
2426 if (ndm->ndm_ifindex == 0) {
2427 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2428 return -EINVAL;
2429 }
2430
2431 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2432 if (dev == NULL) {
2433 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2434 return -ENODEV;
2435 }
2436
2437 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2438 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2439 return -EINVAL;
2440 }
2441
2442 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002443
Jiri Pirkof6f64242014-11-28 14:34:15 +01002444 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2445 if (err)
2446 return err;
2447
John Fastabend77162022012-04-15 06:43:56 +00002448 err = -EOPNOTSUPP;
2449
2450 /* Support fdb on master device the net/bridge default case */
2451 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2452 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002453 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2454 const struct net_device_ops *ops = br_dev->netdev_ops;
2455
Jiri Pirkof6f64242014-11-28 14:34:15 +01002456 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2457 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002458 if (err)
2459 goto out;
2460 else
2461 ndm->ndm_flags &= ~NTF_MASTER;
2462 }
2463
2464 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002465 if ((ndm->ndm_flags & NTF_SELF)) {
2466 if (dev->netdev_ops->ndo_fdb_add)
2467 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002468 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002469 nlh->nlmsg_flags);
2470 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002471 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002472 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002473
John Fastabend3ff661c2012-04-15 06:44:14 +00002474 if (!err) {
2475 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002476 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002477 }
John Fastabend77162022012-04-15 06:43:56 +00002478 }
2479out:
2480 return err;
2481}
2482
Vlad Yasevich090096b2013-03-06 15:39:42 +00002483/**
2484 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2485 */
2486int ndo_dflt_fdb_del(struct ndmsg *ndm,
2487 struct nlattr *tb[],
2488 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002489 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002490{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002491 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002492
2493 /* If aging addresses are supported device will need to
2494 * implement its own handler for this.
2495 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002496 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002497 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002498 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002499 }
2500
2501 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2502 err = dev_uc_del(dev, addr);
2503 else if (is_multicast_ether_addr(addr))
2504 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002505
2506 return err;
2507}
2508EXPORT_SYMBOL(ndo_dflt_fdb_del);
2509
Thomas Graf661d2962013-03-21 07:45:29 +00002510static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002511{
2512 struct net *net = sock_net(skb->sk);
2513 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002514 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002515 struct net_device *dev;
2516 int err = -EINVAL;
2517 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002518 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002519
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002520 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002521 return -EPERM;
2522
2523 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2524 if (err < 0)
2525 return err;
John Fastabend77162022012-04-15 06:43:56 +00002526
2527 ndm = nlmsg_data(nlh);
2528 if (ndm->ndm_ifindex == 0) {
2529 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2530 return -EINVAL;
2531 }
2532
2533 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2534 if (dev == NULL) {
2535 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2536 return -ENODEV;
2537 }
2538
Vlad Yasevich1690be62013-02-13 12:00:18 +00002539 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2540 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002541 return -EINVAL;
2542 }
2543
Vlad Yasevich1690be62013-02-13 12:00:18 +00002544 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002545
Jiri Pirkof6f64242014-11-28 14:34:15 +01002546 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2547 if (err)
2548 return err;
2549
John Fastabend77162022012-04-15 06:43:56 +00002550 err = -EOPNOTSUPP;
2551
2552 /* Support fdb on master device the net/bridge default case */
2553 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2554 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002555 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2556 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002557
Jiri Pirko898e5062013-01-03 22:48:52 +00002558 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002559 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002560
2561 if (err)
2562 goto out;
2563 else
2564 ndm->ndm_flags &= ~NTF_MASTER;
2565 }
2566
2567 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002568 if (ndm->ndm_flags & NTF_SELF) {
2569 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002570 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2571 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002572 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002573 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002574
John Fastabend3ff661c2012-04-15 06:44:14 +00002575 if (!err) {
2576 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002577 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002578 }
John Fastabend77162022012-04-15 06:43:56 +00002579 }
2580out:
2581 return err;
2582}
2583
John Fastabendd83b0602012-04-15 06:44:08 +00002584static int nlmsg_populate_fdb(struct sk_buff *skb,
2585 struct netlink_callback *cb,
2586 struct net_device *dev,
2587 int *idx,
2588 struct netdev_hw_addr_list *list)
2589{
2590 struct netdev_hw_addr *ha;
2591 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002592 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002593
Eric W. Biederman15e47302012-09-07 20:12:54 +00002594 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002595 seq = cb->nlh->nlmsg_seq;
2596
2597 list_for_each_entry(ha, &list->list, list) {
2598 if (*idx < cb->args[0])
2599 goto skip;
2600
2601 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
John Fastabenda7a558f2012-11-01 16:23:10 +00002602 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002603 RTM_NEWNEIGH, NTF_SELF,
2604 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002605 if (err < 0)
2606 return err;
2607skip:
2608 *idx += 1;
2609 }
2610 return 0;
2611}
2612
2613/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002614 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002615 * @nlh: netlink message header
2616 * @dev: netdevice
2617 *
2618 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002619 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002620 */
2621int ndo_dflt_fdb_dump(struct sk_buff *skb,
2622 struct netlink_callback *cb,
2623 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002624 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002625 int idx)
2626{
2627 int err;
2628
2629 netif_addr_lock_bh(dev);
2630 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2631 if (err)
2632 goto out;
2633 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2634out:
2635 netif_addr_unlock_bh(dev);
2636 return idx;
2637}
2638EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2639
John Fastabend77162022012-04-15 06:43:56 +00002640static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2641{
John Fastabend77162022012-04-15 06:43:56 +00002642 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002643 struct nlattr *tb[IFLA_MAX+1];
2644 struct net_device *bdev = NULL;
2645 struct net_device *br_dev = NULL;
2646 const struct net_device_ops *ops = NULL;
2647 const struct net_device_ops *cops = NULL;
2648 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2649 struct net *net = sock_net(skb->sk);
2650 int brport_idx = 0;
2651 int br_idx = 0;
2652 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002653
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002654 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2655 ifla_policy) == 0) {
2656 if (tb[IFLA_MASTER])
2657 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2658 }
John Fastabend77162022012-04-15 06:43:56 +00002659
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002660 brport_idx = ifm->ifi_index;
2661
2662 if (br_idx) {
2663 br_dev = __dev_get_by_index(net, br_idx);
2664 if (!br_dev)
2665 return -ENODEV;
2666
2667 ops = br_dev->netdev_ops;
2668 bdev = br_dev;
2669 }
2670
2671 for_each_netdev(net, dev) {
2672 if (brport_idx && (dev->ifindex != brport_idx))
2673 continue;
2674
2675 if (!br_idx) { /* user did not specify a specific bridge */
2676 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2677 br_dev = netdev_master_upper_dev_get(dev);
2678 cops = br_dev->netdev_ops;
2679 }
2680
2681 bdev = dev;
2682 } else {
2683 if (dev != br_dev &&
2684 !(dev->priv_flags & IFF_BRIDGE_PORT))
2685 continue;
2686
2687 if (br_dev != netdev_master_upper_dev_get(dev) &&
2688 !(dev->priv_flags & IFF_EBRIDGE))
2689 continue;
2690
2691 bdev = br_dev;
2692 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002693 }
2694
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002695 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2696 if (cops && cops->ndo_fdb_dump)
2697 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2698 idx);
2699 }
2700
2701 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
John Fastabend77162022012-04-15 06:43:56 +00002702 if (dev->netdev_ops->ndo_fdb_dump)
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002703 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002704 idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002705
2706 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002707 }
John Fastabend77162022012-04-15 06:43:56 +00002708
2709 cb->args[0] = idx;
2710 return skb->len;
2711}
2712
Scott Feldman2c3c0312014-11-28 14:34:25 +01002713static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2714 unsigned int attrnum, unsigned int flag)
2715{
2716 if (mask & flag)
2717 return nla_put_u8(skb, attrnum, !!(flags & flag));
2718 return 0;
2719}
2720
John Fastabend815cccb2012-10-24 08:13:09 +00002721int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002722 struct net_device *dev, u16 mode,
2723 u32 flags, u32 mask)
John Fastabend815cccb2012-10-24 08:13:09 +00002724{
2725 struct nlmsghdr *nlh;
2726 struct ifinfomsg *ifm;
2727 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002728 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002729 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002730 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002731
2732 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2733 if (nlh == NULL)
2734 return -EMSGSIZE;
2735
2736 ifm = nlmsg_data(nlh);
2737 ifm->ifi_family = AF_BRIDGE;
2738 ifm->__ifi_pad = 0;
2739 ifm->ifi_type = dev->type;
2740 ifm->ifi_index = dev->ifindex;
2741 ifm->ifi_flags = dev_get_flags(dev);
2742 ifm->ifi_change = 0;
2743
2744
2745 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2746 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2747 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002748 (br_dev &&
2749 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002750 (dev->addr_len &&
2751 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2752 (dev->ifindex != dev->iflink &&
2753 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2754 goto nla_put_failure;
2755
2756 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2757 if (!br_afspec)
2758 goto nla_put_failure;
2759
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002760 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002761 nla_nest_cancel(skb, br_afspec);
2762 goto nla_put_failure;
2763 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002764
2765 if (mode != BRIDGE_MODE_UNDEF) {
2766 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2767 nla_nest_cancel(skb, br_afspec);
2768 goto nla_put_failure;
2769 }
2770 }
John Fastabend815cccb2012-10-24 08:13:09 +00002771 nla_nest_end(skb, br_afspec);
2772
Scott Feldman2c3c0312014-11-28 14:34:25 +01002773 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2774 if (!protinfo)
2775 goto nla_put_failure;
2776
2777 if (brport_nla_put_flag(skb, flags, mask,
2778 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2779 brport_nla_put_flag(skb, flags, mask,
2780 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2781 brport_nla_put_flag(skb, flags, mask,
2782 IFLA_BRPORT_FAST_LEAVE,
2783 BR_MULTICAST_FAST_LEAVE) ||
2784 brport_nla_put_flag(skb, flags, mask,
2785 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2786 brport_nla_put_flag(skb, flags, mask,
2787 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2788 brport_nla_put_flag(skb, flags, mask,
2789 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2790 brport_nla_put_flag(skb, flags, mask,
2791 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2792 brport_nla_put_flag(skb, flags, mask,
2793 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2794 nla_nest_cancel(skb, protinfo);
2795 goto nla_put_failure;
2796 }
2797
2798 nla_nest_end(skb, protinfo);
2799
John Fastabend815cccb2012-10-24 08:13:09 +00002800 return nlmsg_end(skb, nlh);
2801nla_put_failure:
2802 nlmsg_cancel(skb, nlh);
2803 return -EMSGSIZE;
2804}
2805EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2806
John Fastabende5a55a82012-10-24 08:12:57 +00002807static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2808{
2809 struct net *net = sock_net(skb->sk);
2810 struct net_device *dev;
2811 int idx = 0;
2812 u32 portid = NETLINK_CB(cb->skb).portid;
2813 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002814 u32 filter_mask = 0;
2815
Thomas Grafaa68c202014-11-26 13:42:20 +01002816 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2817 struct nlattr *extfilt;
2818
2819 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2820 IFLA_EXT_MASK);
2821 if (extfilt) {
2822 if (nla_len(extfilt) < sizeof(filter_mask))
2823 return -EINVAL;
2824
2825 filter_mask = nla_get_u32(extfilt);
2826 }
2827 }
John Fastabende5a55a82012-10-24 08:12:57 +00002828
2829 rcu_read_lock();
2830 for_each_netdev_rcu(net, dev) {
2831 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002832 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002833
Jiri Pirko898e5062013-01-03 22:48:52 +00002834 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002835 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002836 br_dev->netdev_ops->ndo_bridge_getlink(
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002837 skb, portid, seq, dev, filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002838 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002839 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002840 }
2841
2842 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002843 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002844 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2845 filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002846 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002847 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002848 }
2849 }
2850 rcu_read_unlock();
2851 cb->args[0] = idx;
2852
2853 return skb->len;
2854}
2855
John Fastabend2469ffd2012-10-24 08:13:03 +00002856static inline size_t bridge_nlmsg_size(void)
2857{
2858 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2859 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2860 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2861 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2862 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2863 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2864 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2865 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2866 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2867 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2868 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2869}
2870
2871static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2872{
2873 struct net *net = dev_net(dev);
Jiri Pirko898e5062013-01-03 22:48:52 +00002874 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00002875 struct sk_buff *skb;
2876 int err = -EOPNOTSUPP;
2877
2878 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2879 if (!skb) {
2880 err = -ENOMEM;
2881 goto errout;
2882 }
2883
John Fastabendc38e01b2012-11-02 16:32:36 +00002884 if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002885 br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002886 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002887 if (err < 0)
2888 goto errout;
2889 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002890
John Fastabendc38e01b2012-11-02 16:32:36 +00002891 if ((flags & BRIDGE_FLAGS_SELF) &&
2892 dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002893 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002894 if (err < 0)
2895 goto errout;
2896 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002897
2898 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2899 return 0;
2900errout:
2901 WARN_ON(err == -EMSGSIZE);
2902 kfree_skb(skb);
2903 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2904 return err;
2905}
2906
Thomas Graf661d2962013-03-21 07:45:29 +00002907static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00002908{
2909 struct net *net = sock_net(skb->sk);
2910 struct ifinfomsg *ifm;
2911 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00002912 struct nlattr *br_spec, *attr = NULL;
2913 int rem, err = -EOPNOTSUPP;
John Fastabendc38e01b2012-11-02 16:32:36 +00002914 u16 oflags, flags = 0;
2915 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00002916
2917 if (nlmsg_len(nlh) < sizeof(*ifm))
2918 return -EINVAL;
2919
2920 ifm = nlmsg_data(nlh);
2921 if (ifm->ifi_family != AF_BRIDGE)
2922 return -EPFNOSUPPORT;
2923
2924 dev = __dev_get_by_index(net, ifm->ifi_index);
2925 if (!dev) {
2926 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2927 return -ENODEV;
2928 }
2929
John Fastabend2469ffd2012-10-24 08:13:03 +00002930 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2931 if (br_spec) {
2932 nla_for_each_nested(attr, br_spec, rem) {
2933 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002934 if (nla_len(attr) < sizeof(flags))
2935 return -EINVAL;
2936
John Fastabendc38e01b2012-11-02 16:32:36 +00002937 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00002938 flags = nla_get_u16(attr);
2939 break;
2940 }
2941 }
2942 }
2943
John Fastabendc38e01b2012-11-02 16:32:36 +00002944 oflags = flags;
2945
John Fastabend2469ffd2012-10-24 08:13:03 +00002946 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002947 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2948
2949 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002950 err = -EOPNOTSUPP;
2951 goto out;
2952 }
2953
Jiri Pirko898e5062013-01-03 22:48:52 +00002954 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002955 if (err)
2956 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00002957
2958 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00002959 }
2960
John Fastabend2469ffd2012-10-24 08:13:03 +00002961 if ((flags & BRIDGE_FLAGS_SELF)) {
2962 if (!dev->netdev_ops->ndo_bridge_setlink)
2963 err = -EOPNOTSUPP;
2964 else
2965 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002966
John Fastabend2469ffd2012-10-24 08:13:03 +00002967 if (!err)
2968 flags &= ~BRIDGE_FLAGS_SELF;
2969 }
2970
John Fastabendc38e01b2012-11-02 16:32:36 +00002971 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00002972 memcpy(nla_data(attr), &flags, sizeof(flags));
2973 /* Generate event to notify upper layer of bridge change */
2974 if (!err)
John Fastabendc38e01b2012-11-02 16:32:36 +00002975 err = rtnl_bridge_notify(dev, oflags);
John Fastabende5a55a82012-10-24 08:12:57 +00002976out:
2977 return err;
2978}
2979
Thomas Graf661d2962013-03-21 07:45:29 +00002980static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00002981{
2982 struct net *net = sock_net(skb->sk);
2983 struct ifinfomsg *ifm;
2984 struct net_device *dev;
2985 struct nlattr *br_spec, *attr = NULL;
2986 int rem, err = -EOPNOTSUPP;
2987 u16 oflags, flags = 0;
2988 bool have_flags = false;
2989
2990 if (nlmsg_len(nlh) < sizeof(*ifm))
2991 return -EINVAL;
2992
2993 ifm = nlmsg_data(nlh);
2994 if (ifm->ifi_family != AF_BRIDGE)
2995 return -EPFNOSUPPORT;
2996
2997 dev = __dev_get_by_index(net, ifm->ifi_index);
2998 if (!dev) {
2999 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3000 return -ENODEV;
3001 }
3002
3003 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3004 if (br_spec) {
3005 nla_for_each_nested(attr, br_spec, rem) {
3006 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003007 if (nla_len(attr) < sizeof(flags))
3008 return -EINVAL;
3009
Vlad Yasevich407af322013-02-13 12:00:12 +00003010 have_flags = true;
3011 flags = nla_get_u16(attr);
3012 break;
3013 }
3014 }
3015 }
3016
3017 oflags = flags;
3018
3019 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3020 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3021
3022 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3023 err = -EOPNOTSUPP;
3024 goto out;
3025 }
3026
3027 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3028 if (err)
3029 goto out;
3030
3031 flags &= ~BRIDGE_FLAGS_MASTER;
3032 }
3033
3034 if ((flags & BRIDGE_FLAGS_SELF)) {
3035 if (!dev->netdev_ops->ndo_bridge_dellink)
3036 err = -EOPNOTSUPP;
3037 else
3038 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3039
3040 if (!err)
3041 flags &= ~BRIDGE_FLAGS_SELF;
3042 }
3043
3044 if (have_flags)
3045 memcpy(nla_data(attr), &flags, sizeof(flags));
3046 /* Generate event to notify upper layer of bridge change */
3047 if (!err)
3048 err = rtnl_bridge_notify(dev, oflags);
3049out:
3050 return err;
3051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053/* Process one rtnetlink message. */
3054
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003055static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003057 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003058 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int family;
3061 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003062 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003066 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 type -= RTM_BASE;
3069
3070 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003071 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return 0;
3073
Hong zhi guo573ce262013-03-27 06:47:04 +00003074 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 sz_idx = type>>2;
3076 kind = type&3;
3077
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003078 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003079 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
David S. Millerb8f3ab42011-01-18 12:40:38 -08003081 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003082 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003083 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003084 rtnl_calcit_func calcit;
3085 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Thomas Grafe2849862007-03-22 11:48:11 -07003087 dumpit = rtnl_get_dumpit(family, type);
3088 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003089 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003090 calcit = rtnl_get_calcit(family, type);
3091 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003092 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Eric Dumazet2907c352011-05-25 07:34:04 +00003094 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003095 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003096 {
3097 struct netlink_dump_control c = {
3098 .dump = dumpit,
3099 .min_dump_alloc = min_dump_alloc,
3100 };
3101 err = netlink_dump_start(rtnl, skb, nlh, &c);
3102 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003103 rtnl_lock();
3104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106
Thomas Grafe2849862007-03-22 11:48:11 -07003107 doit = rtnl_get_doit(family, type);
3108 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003109 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Thomas Graf661d2962013-03-21 07:45:29 +00003111 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003114static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003116 rtnl_lock();
3117 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3118 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3122{
Jiri Pirko351638e2013-05-28 01:30:21 +00003123 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 case NETDEV_UP:
3127 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003128 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003129 case NETDEV_POST_INIT:
3130 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003132 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003134 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003135 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003136 case NETDEV_RELEASE:
3137 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 break;
3139 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003140 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 break;
3142 }
3143 return NOTIFY_DONE;
3144}
3145
3146static struct notifier_block rtnetlink_dev_notifier = {
3147 .notifier_call = rtnetlink_event,
3148};
3149
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003150
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003151static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003152{
3153 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003154 struct netlink_kernel_cfg cfg = {
3155 .groups = RTNLGRP_MAX,
3156 .input = rtnetlink_rcv,
3157 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003158 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003159 };
3160
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003161 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003162 if (!sk)
3163 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003164 net->rtnl = sk;
3165 return 0;
3166}
3167
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003168static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003169{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003170 netlink_kernel_release(net->rtnl);
3171 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003172}
3173
3174static struct pernet_operations rtnetlink_net_ops = {
3175 .init = rtnetlink_net_init,
3176 .exit = rtnetlink_net_exit,
3177};
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179void __init rtnetlink_init(void)
3180{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003181 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003185
Greg Rosec7ac8672011-06-10 01:27:09 +00003186 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3187 rtnl_dump_ifinfo, rtnl_calcit);
3188 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3189 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3190 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003191
Greg Rosec7ac8672011-06-10 01:27:09 +00003192 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3193 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003194
3195 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3196 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3197 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003198
3199 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003200 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003201 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203