blob: eaa057f14bcd4a7b4506709acbb090f0122bf4b4 [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;
370 DEFINE_WAIT(wait);
371
372 for (;;) {
373 prepare_to_wait(&netdev_unregistering_wq, &wait,
374 TASK_UNINTERRUPTIBLE);
375 unregistering = false;
376 rtnl_lock();
377 for_each_net(net) {
378 if (net->dev_unreg_count > 0) {
379 unregistering = true;
380 break;
381 }
382 }
383 if (!unregistering)
384 break;
385 __rtnl_unlock();
386 schedule();
387 }
388 finish_wait(&netdev_unregistering_wq, &wait);
389}
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)) {
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00001526 err = -EPERM;
1527 goto errout;
1528 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001529 err = dev_change_net_namespace(dev, net, ifname);
1530 put_net(net);
1531 if (err)
1532 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001533 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02001534 }
1535
Patrick McHardy0157f602007-06-13 12:03:36 -07001536 if (tb[IFLA_MAP]) {
1537 struct rtnl_link_ifmap *u_map;
1538 struct ifmap k_map;
1539
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001540 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07001541 err = -EOPNOTSUPP;
1542 goto errout;
1543 }
1544
1545 if (!netif_device_present(dev)) {
1546 err = -ENODEV;
1547 goto errout;
1548 }
1549
1550 u_map = nla_data(tb[IFLA_MAP]);
1551 k_map.mem_start = (unsigned long) u_map->mem_start;
1552 k_map.mem_end = (unsigned long) u_map->mem_end;
1553 k_map.base_addr = (unsigned short) u_map->base_addr;
1554 k_map.irq = (unsigned char) u_map->irq;
1555 k_map.dma = (unsigned char) u_map->dma;
1556 k_map.port = (unsigned char) u_map->port;
1557
Stephen Hemmingerd3147742008-11-19 21:32:24 -08001558 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07001559 if (err < 0)
1560 goto errout;
1561
Nicolas Dichtelba998902014-09-01 16:07:29 +02001562 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07001563 }
1564
1565 if (tb[IFLA_ADDRESS]) {
1566 struct sockaddr *sa;
1567 int len;
1568
Patrick McHardy0157f602007-06-13 12:03:36 -07001569 len = sizeof(sa_family_t) + dev->addr_len;
1570 sa = kmalloc(len, GFP_KERNEL);
1571 if (!sa) {
1572 err = -ENOMEM;
1573 goto errout;
1574 }
1575 sa->sa_family = dev->type;
1576 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1577 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001578 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07001579 kfree(sa);
1580 if (err)
1581 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001582 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001583 }
1584
1585 if (tb[IFLA_MTU]) {
1586 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1587 if (err < 0)
1588 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001589 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001590 }
1591
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001592 if (tb[IFLA_GROUP]) {
1593 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02001594 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00001595 }
1596
Patrick McHardy0157f602007-06-13 12:03:36 -07001597 /*
1598 * Interface selected by interface index but interface
1599 * name provided implies that a name change has been
1600 * requested.
1601 */
1602 if (ifm->ifi_index > 0 && ifname[0]) {
1603 err = dev_change_name(dev, ifname);
1604 if (err < 0)
1605 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001606 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07001607 }
1608
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001609 if (tb[IFLA_IFALIAS]) {
1610 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1611 nla_len(tb[IFLA_IFALIAS]));
1612 if (err < 0)
1613 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001614 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001615 }
1616
Patrick McHardy0157f602007-06-13 12:03:36 -07001617 if (tb[IFLA_BROADCAST]) {
1618 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00001619 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07001620 }
1621
1622 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00001623 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08001624 if (err < 0)
1625 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07001626 }
1627
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001628 if (tb[IFLA_MASTER]) {
1629 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1630 if (err)
1631 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001632 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00001633 }
1634
Jiri Pirko9a572472012-12-27 23:49:39 +00001635 if (tb[IFLA_CARRIER]) {
1636 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1637 if (err)
1638 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02001639 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00001640 }
1641
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001642 if (tb[IFLA_TXQLEN]) {
1643 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1644
1645 if (dev->tx_queue_len ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001646 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02001647
1648 dev->tx_queue_len = value;
1649 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001650
Patrick McHardy0157f602007-06-13 12:03:36 -07001651 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001652 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07001653
1654 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001655 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1656
David S. Miller93b2d4a2008-02-17 18:35:07 -08001657 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001658 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02001659 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02001660 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08001661 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07001662 }
1663
Chris Wrightc02db8c2010-05-16 01:05:45 -07001664 if (tb[IFLA_VFINFO_LIST]) {
1665 struct nlattr *attr;
1666 int rem;
1667 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
David Howells253683b2010-05-21 02:25:27 +00001668 if (nla_type(attr) != IFLA_VF_INFO) {
1669 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001670 goto errout;
David Howells253683b2010-05-21 02:25:27 +00001671 }
Chris Wrightc02db8c2010-05-16 01:05:45 -07001672 err = do_setvfinfo(dev, attr);
1673 if (err < 0)
1674 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001675 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001676 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00001677 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001678 err = 0;
1679
Scott Feldman57b61082010-05-17 22:49:55 -07001680 if (tb[IFLA_VF_PORTS]) {
1681 struct nlattr *port[IFLA_PORT_MAX+1];
1682 struct nlattr *attr;
1683 int vf;
1684 int rem;
1685
1686 err = -EOPNOTSUPP;
1687 if (!ops->ndo_set_vf_port)
1688 goto errout;
1689
1690 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1691 if (nla_type(attr) != IFLA_VF_PORT)
1692 continue;
1693 err = nla_parse_nested(port, IFLA_PORT_MAX,
1694 attr, ifla_port_policy);
1695 if (err < 0)
1696 goto errout;
1697 if (!port[IFLA_PORT_VF]) {
1698 err = -EOPNOTSUPP;
1699 goto errout;
1700 }
1701 vf = nla_get_u32(port[IFLA_PORT_VF]);
1702 err = ops->ndo_set_vf_port(dev, vf, port);
1703 if (err < 0)
1704 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001705 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001706 }
1707 }
1708 err = 0;
1709
1710 if (tb[IFLA_PORT_SELF]) {
1711 struct nlattr *port[IFLA_PORT_MAX+1];
1712
1713 err = nla_parse_nested(port, IFLA_PORT_MAX,
1714 tb[IFLA_PORT_SELF], ifla_port_policy);
1715 if (err < 0)
1716 goto errout;
1717
1718 err = -EOPNOTSUPP;
1719 if (ops->ndo_set_vf_port)
1720 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1721 if (err < 0)
1722 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02001723 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07001724 }
Thomas Graff8ff1822010-11-16 04:30:14 +00001725
1726 if (tb[IFLA_AF_SPEC]) {
1727 struct nlattr *af;
1728 int rem;
1729
1730 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1731 const struct rtnl_af_ops *af_ops;
1732
1733 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001734 BUG();
Thomas Graff8ff1822010-11-16 04:30:14 +00001735
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001736 err = af_ops->set_link_af(dev, af);
Thomas Graff8ff1822010-11-16 04:30:14 +00001737 if (err < 0)
1738 goto errout;
1739
Nicolas Dichtelba998902014-09-01 16:07:29 +02001740 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00001741 }
1742 }
Scott Feldman57b61082010-05-17 22:49:55 -07001743 err = 0;
1744
Patrick McHardy0157f602007-06-13 12:03:36 -07001745errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02001746 if (status & DO_SETLINK_MODIFIED) {
1747 if (status & DO_SETLINK_NOTIFY)
1748 netdev_state_change(dev);
1749
1750 if (err < 0)
1751 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",
1752 dev->name);
1753 }
Patrick McHardy0157f602007-06-13 12:03:36 -07001754
Patrick McHardy0157f602007-06-13 12:03:36 -07001755 return err;
1756}
1757
Thomas Graf661d2962013-03-21 07:45:29 +00001758static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001760 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07001761 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07001763 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07001764 struct nlattr *tb[IFLA_MAX+1];
1765 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Thomas Grafda5e0492006-08-10 21:17:37 -07001767 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1768 if (err < 0)
1769 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Thomas Graf5176f912006-08-26 20:13:18 -07001771 if (tb[IFLA_IFNAME])
1772 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07001773 else
1774 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07001777 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07001778 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001779 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07001780 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00001781 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07001782 else
1783 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Thomas Grafda5e0492006-08-10 21:17:37 -07001785 if (dev == NULL) {
1786 err = -ENODEV;
1787 goto errout;
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Eric Dumazete0d087a2009-11-07 01:26:17 -08001790 err = validate_linkmsg(dev, tb);
1791 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00001792 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07001793
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001794 err = do_setlink(skb, dev, ifm, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07001795errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return err;
1797}
1798
Thomas Graf661d2962013-03-21 07:45:29 +00001799static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001800{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001801 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001802 const struct rtnl_link_ops *ops;
1803 struct net_device *dev;
1804 struct ifinfomsg *ifm;
1805 char ifname[IFNAMSIZ];
1806 struct nlattr *tb[IFLA_MAX+1];
1807 int err;
Eric Dumazet226bd342011-05-08 23:17:57 +00001808 LIST_HEAD(list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001809
1810 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1811 if (err < 0)
1812 return err;
1813
1814 if (tb[IFLA_IFNAME])
1815 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1816
1817 ifm = nlmsg_data(nlh);
1818 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001819 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001820 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07001821 dev = __dev_get_by_name(net, ifname);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001822 else
1823 return -EINVAL;
1824
1825 if (!dev)
1826 return -ENODEV;
1827
1828 ops = dev->rtnl_link_ops;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02001829 if (!ops || !ops->dellink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001830 return -EOPNOTSUPP;
1831
Eric Dumazet226bd342011-05-08 23:17:57 +00001832 ops->dellink(dev, &list_kill);
1833 unregister_netdevice_many(&list_kill);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001834 return 0;
1835}
1836
Patrick McHardy3729d502010-02-26 06:34:54 +00001837int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1838{
1839 unsigned int old_flags;
1840 int err;
1841
1842 old_flags = dev->flags;
1843 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1844 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1845 if (err < 0)
1846 return err;
1847 }
1848
1849 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00001850
Nicolas Dichtela528c212013-09-25 12:02:44 +02001851 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00001852 return 0;
1853}
1854EXPORT_SYMBOL(rtnl_configure_link);
1855
Rami Rosenc0713562012-11-30 01:08:47 +00001856struct net_device *rtnl_create_link(struct net *net,
Tom Gundersen55177502014-07-14 16:37:25 +02001857 char *ifname, unsigned char name_assign_type,
1858 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07001859{
1860 int err;
1861 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00001862 unsigned int num_tx_queues = 1;
1863 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07001864
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001865 if (tb[IFLA_NUM_TX_QUEUES])
1866 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1867 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001868 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001869
1870 if (tb[IFLA_NUM_RX_QUEUES])
1871 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1872 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00001873 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00001874
Pavel Emelianove7199282007-08-08 22:16:38 -07001875 err = -ENOMEM;
Tom Gundersen55177502014-07-14 16:37:25 +02001876 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02001877 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07001878 if (!dev)
1879 goto err;
1880
Eric W. Biederman81adee42009-11-08 00:53:51 -08001881 dev_net_set(dev, net);
1882 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00001883 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001884
Pavel Emelianove7199282007-08-08 22:16:38 -07001885 if (tb[IFLA_MTU])
1886 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001887 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07001888 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1889 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00001890 dev->addr_assign_type = NET_ADDR_SET;
1891 }
Pavel Emelianove7199282007-08-08 22:16:38 -07001892 if (tb[IFLA_BROADCAST])
1893 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1894 nla_len(tb[IFLA_BROADCAST]));
1895 if (tb[IFLA_TXQLEN])
1896 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1897 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08001898 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001899 if (tb[IFLA_LINKMODE])
1900 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00001901 if (tb[IFLA_GROUP])
1902 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07001903
1904 return dev;
1905
Pavel Emelianove7199282007-08-08 22:16:38 -07001906err:
1907 return ERR_PTR(err);
1908}
Eric Dumazete0d087a2009-11-07 01:26:17 -08001909EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07001910
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001911static int rtnl_group_changelink(const struct sk_buff *skb,
1912 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001913 struct ifinfomsg *ifm,
1914 struct nlattr **tb)
1915{
1916 struct net_device *dev;
1917 int err;
1918
1919 for_each_netdev(net, dev) {
1920 if (dev->group == group) {
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07001921 err = do_setlink(skb, dev, ifm, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001922 if (err < 0)
1923 return err;
1924 }
1925 }
1926
1927 return 0;
1928}
1929
Thomas Graf661d2962013-03-21 07:45:29 +00001930static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
Patrick McHardy38f7b872007-06-13 12:03:51 -07001931{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001932 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07001933 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001934 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001935 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001936 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001937 struct ifinfomsg *ifm;
1938 char kind[MODULE_NAME_LEN];
1939 char ifname[IFNAMSIZ];
1940 struct nlattr *tb[IFLA_MAX+1];
1941 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02001942 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001943 int err;
1944
Johannes Berg95a5afc2008-10-16 15:24:51 -07001945#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07001946replay:
Thomas Graf8072f082007-07-31 14:13:50 -07001947#endif
Patrick McHardy38f7b872007-06-13 12:03:51 -07001948 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1949 if (err < 0)
1950 return err;
1951
1952 if (tb[IFLA_IFNAME])
1953 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1954 else
1955 ifname[0] = '\0';
1956
1957 ifm = nlmsg_data(nlh);
1958 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001959 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001960 else {
1961 if (ifname[0])
1962 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00001963 else
1964 dev = NULL;
1965 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07001966
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001967 if (dev) {
1968 master_dev = netdev_master_upper_dev_get(dev);
1969 if (master_dev)
1970 m_ops = master_dev->rtnl_link_ops;
1971 }
1972
Eric Dumazete0d087a2009-11-07 01:26:17 -08001973 err = validate_linkmsg(dev, tb);
1974 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08001975 return err;
1976
Patrick McHardy38f7b872007-06-13 12:03:51 -07001977 if (tb[IFLA_LINKINFO]) {
1978 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1979 tb[IFLA_LINKINFO], ifla_info_policy);
1980 if (err < 0)
1981 return err;
1982 } else
1983 memset(linkinfo, 0, sizeof(linkinfo));
1984
1985 if (linkinfo[IFLA_INFO_KIND]) {
1986 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1987 ops = rtnl_link_ops_get(kind);
1988 } else {
1989 kind[0] = '\0';
1990 ops = NULL;
1991 }
1992
1993 if (1) {
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001994 struct nlattr *attr[ops ? ops->maxtype + 1 : 0];
1995 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
1996 struct nlattr **data = NULL;
1997 struct nlattr **slave_data = NULL;
Eric W. Biederman81adee42009-11-08 00:53:51 -08001998 struct net *dest_net;
Patrick McHardy38f7b872007-06-13 12:03:51 -07001999
2000 if (ops) {
2001 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2002 err = nla_parse_nested(attr, ops->maxtype,
2003 linkinfo[IFLA_INFO_DATA],
2004 ops->policy);
2005 if (err < 0)
2006 return err;
2007 data = attr;
2008 }
2009 if (ops->validate) {
2010 err = ops->validate(tb, data);
2011 if (err < 0)
2012 return err;
2013 }
2014 }
2015
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002016 if (m_ops) {
2017 if (m_ops->slave_maxtype &&
2018 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2019 err = nla_parse_nested(slave_attr,
2020 m_ops->slave_maxtype,
2021 linkinfo[IFLA_INFO_SLAVE_DATA],
2022 m_ops->slave_policy);
2023 if (err < 0)
2024 return err;
2025 slave_data = slave_attr;
2026 }
2027 if (m_ops->slave_validate) {
2028 err = m_ops->slave_validate(tb, slave_data);
2029 if (err < 0)
2030 return err;
2031 }
2032 }
2033
Patrick McHardy38f7b872007-06-13 12:03:51 -07002034 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002035 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002036
2037 if (nlh->nlmsg_flags & NLM_F_EXCL)
2038 return -EEXIST;
2039 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2040 return -EOPNOTSUPP;
2041
2042 if (linkinfo[IFLA_INFO_DATA]) {
2043 if (!ops || ops != dev->rtnl_link_ops ||
2044 !ops->changelink)
2045 return -EOPNOTSUPP;
2046
2047 err = ops->changelink(dev, tb, data);
2048 if (err < 0)
2049 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002050 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002051 }
2052
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002053 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2054 if (!m_ops || !m_ops->slave_changelink)
2055 return -EOPNOTSUPP;
2056
2057 err = m_ops->slave_changelink(master_dev, dev,
2058 tb, slave_data);
2059 if (err < 0)
2060 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002061 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002062 }
2063
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002064 return do_setlink(skb, dev, ifm, tb, ifname, status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002065 }
2066
Patrick McHardyffa934f2011-01-20 03:00:42 +00002067 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2068 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002069 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002070 nla_get_u32(tb[IFLA_GROUP]),
2071 ifm, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002072 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002073 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002074
Patrick McHardy0e068772007-07-11 19:42:31 -07002075 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002076 return -EOPNOTSUPP;
2077
2078 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002079#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002080 if (kind[0]) {
2081 __rtnl_unlock();
2082 request_module("rtnl-link-%s", kind);
2083 rtnl_lock();
2084 ops = rtnl_link_ops_get(kind);
2085 if (ops)
2086 goto replay;
2087 }
2088#endif
2089 return -EOPNOTSUPP;
2090 }
2091
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002092 if (!ops->setup)
2093 return -EOPNOTSUPP;
2094
Tom Gundersen55177502014-07-14 16:37:25 +02002095 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002096 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002097 name_assign_type = NET_NAME_ENUM;
2098 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002099
Eric W. Biederman81adee42009-11-08 00:53:51 -08002100 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002101 if (IS_ERR(dest_net))
2102 return PTR_ERR(dest_net);
2103
Tom Gundersen55177502014-07-14 16:37:25 +02002104 dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002105 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002106 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002107 goto out;
2108 }
2109
2110 dev->ifindex = ifm->ifi_index;
2111
Cong Wang0e0eee22014-02-11 15:51:30 -08002112 if (ops->newlink) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002113 err = ops->newlink(net, dev, tb, data);
Cong Wang0e0eee22014-02-11 15:51:30 -08002114 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002115 * and unregister it on failure after registration
2116 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002117 */
Cong Wange51fb152014-06-03 16:40:47 -07002118 if (err < 0) {
2119 /* If device is not registered at all, free it now */
2120 if (dev->reg_state == NETREG_UNINITIALIZED)
2121 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002122 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002123 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002124 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002125 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002126 if (err < 0) {
2127 free_netdev(dev);
2128 goto out;
2129 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002130 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002131 err = rtnl_configure_link(dev, ifm);
2132 if (err < 0)
2133 unregister_netdevice(dev);
2134out:
Eric W. Biederman81adee42009-11-08 00:53:51 -08002135 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002136 return err;
2137 }
2138}
2139
Thomas Graf661d2962013-03-21 07:45:29 +00002140static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002141{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002142 struct net *net = sock_net(skb->sk);
Thomas Grafb60c5112006-08-04 23:05:34 -07002143 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002144 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002145 struct nlattr *tb[IFLA_MAX+1];
2146 struct net_device *dev = NULL;
2147 struct sk_buff *nskb;
Thomas Graf339bf982006-11-10 14:10:15 -08002148 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002149 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002150
Thomas Grafb60c5112006-08-04 23:05:34 -07002151 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2152 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002153 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002154
Eric Dumazeta3d12892009-10-21 10:59:31 +00002155 if (tb[IFLA_IFNAME])
2156 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2157
Greg Rose115c9b82012-02-21 16:54:48 -05002158 if (tb[IFLA_EXT_MASK])
2159 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2160
Thomas Grafb60c5112006-08-04 23:05:34 -07002161 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002162 if (ifm->ifi_index > 0)
2163 dev = __dev_get_by_index(net, ifm->ifi_index);
2164 else if (tb[IFLA_IFNAME])
2165 dev = __dev_get_by_name(net, ifname);
2166 else
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002167 return -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002168
Eric Dumazeta3d12892009-10-21 10:59:31 +00002169 if (dev == NULL)
2170 return -ENODEV;
2171
Greg Rose115c9b82012-02-21 16:54:48 -05002172 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002173 if (nskb == NULL)
2174 return -ENOBUFS;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002175
Eric W. Biederman15e47302012-09-07 20:12:54 +00002176 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
Greg Rose115c9b82012-02-21 16:54:48 -05002177 nlh->nlmsg_seq, 0, 0, ext_filter_mask);
Patrick McHardy26932562007-01-31 23:16:40 -08002178 if (err < 0) {
2179 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2180 WARN_ON(err == -EMSGSIZE);
2181 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002182 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002183 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Thomas Grafb60c5112006-08-04 23:05:34 -07002184
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002185 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002186}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002187
Greg Rose115c9b82012-02-21 16:54:48 -05002188static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002189{
Greg Rose115c9b82012-02-21 16:54:48 -05002190 struct net *net = sock_net(skb->sk);
2191 struct net_device *dev;
2192 struct nlattr *tb[IFLA_MAX+1];
2193 u32 ext_filter_mask = 0;
2194 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002195 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05002196
Michal Schmidte5eca6d2014-05-28 14:15:19 +02002197 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2198 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2199 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2200
2201 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00002202 if (tb[IFLA_EXT_MASK])
2203 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2204 }
Greg Rose115c9b82012-02-21 16:54:48 -05002205
2206 if (!ext_filter_mask)
2207 return NLMSG_GOODSIZE;
2208 /*
2209 * traverse the list of net devices and compute the minimum
2210 * buffer size based upon the filter mask.
2211 */
2212 list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2213 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2214 if_nlmsg_size(dev,
2215 ext_filter_mask));
2216 }
2217
Greg Rosec7ac8672011-06-10 01:27:09 +00002218 return min_ifinfo_dump_size;
2219}
2220
Adrian Bunk42bad1d2007-04-26 00:57:41 -07002221static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 int idx;
2224 int s_idx = cb->family;
2225
2226 if (s_idx == 0)
2227 s_idx = 1;
Patrick McHardy25239ce2010-04-26 16:02:05 +02002228 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 int type = cb->nlh->nlmsg_type-RTM_BASE;
2230 if (idx < s_idx || idx == PF_PACKET)
2231 continue;
Thomas Grafe2849862007-03-22 11:48:11 -07002232 if (rtnl_msg_handlers[idx] == NULL ||
2233 rtnl_msg_handlers[idx][type].dumpit == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 continue;
Nicolas Dichtel04652772013-03-22 06:28:42 +00002235 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00002237 cb->prev_seq = 0;
2238 cb->seq = 0;
2239 }
Thomas Grafe2849862007-03-22 11:48:11 -07002240 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 break;
2242 }
2243 cb->family = idx;
2244
2245 return skb->len;
2246}
2247
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002248struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2249 unsigned int change, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002251 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002253 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002254 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Alexei Starovoitov7f294052013-10-23 16:02:42 -07002256 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002257 if (skb == NULL)
2258 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Greg Rose115c9b82012-02-21 16:54:48 -05002260 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08002261 if (err < 0) {
2262 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
2263 WARN_ON(err == -EMSGSIZE);
2264 kfree_skb(skb);
2265 goto errout;
2266 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002267 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07002268errout:
2269 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08002270 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08002271 return NULL;
2272}
2273
2274void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2275{
2276 struct net *net = dev_net(dev);
2277
2278 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2279}
2280
2281void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2282 gfp_t flags)
2283{
2284 struct sk_buff *skb;
2285
2286 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2287 if (skb)
2288 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002290EXPORT_SYMBOL(rtmsg_ifinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
John Fastabendd83b0602012-04-15 06:44:08 +00002292static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2293 struct net_device *dev,
2294 u8 *addr, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002295 int type, unsigned int flags,
2296 int nlflags)
John Fastabendd83b0602012-04-15 06:44:08 +00002297{
2298 struct nlmsghdr *nlh;
2299 struct ndmsg *ndm;
2300
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002301 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00002302 if (!nlh)
2303 return -EMSGSIZE;
2304
2305 ndm = nlmsg_data(nlh);
2306 ndm->ndm_family = AF_BRIDGE;
2307 ndm->ndm_pad1 = 0;
2308 ndm->ndm_pad2 = 0;
2309 ndm->ndm_flags = flags;
2310 ndm->ndm_type = 0;
2311 ndm->ndm_ifindex = dev->ifindex;
2312 ndm->ndm_state = NUD_PERMANENT;
2313
2314 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2315 goto nla_put_failure;
2316
2317 return nlmsg_end(skb, nlh);
2318
2319nla_put_failure:
2320 nlmsg_cancel(skb, nlh);
2321 return -EMSGSIZE;
2322}
2323
John Fastabend3ff661c2012-04-15 06:44:14 +00002324static inline size_t rtnl_fdb_nlmsg_size(void)
2325{
2326 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2327}
2328
2329static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2330{
2331 struct net *net = dev_net(dev);
2332 struct sk_buff *skb;
2333 int err = -ENOBUFS;
2334
2335 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2336 if (!skb)
2337 goto errout;
2338
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002339 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0);
John Fastabend3ff661c2012-04-15 06:44:14 +00002340 if (err < 0) {
2341 kfree_skb(skb);
2342 goto errout;
2343 }
2344
2345 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2346 return;
2347errout:
2348 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2349}
2350
Vlad Yasevich090096b2013-03-06 15:39:42 +00002351/**
2352 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2353 */
2354int ndo_dflt_fdb_add(struct ndmsg *ndm,
2355 struct nlattr *tb[],
2356 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002357 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002358 u16 flags)
2359{
2360 int err = -EINVAL;
2361
2362 /* If aging addresses are supported device will need to
2363 * implement its own handler for this.
2364 */
2365 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2366 pr_info("%s: FDB only supports static addresses\n", dev->name);
2367 return err;
2368 }
2369
2370 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2371 err = dev_uc_add_excl(dev, addr);
2372 else if (is_multicast_ether_addr(addr))
2373 err = dev_mc_add_excl(dev, addr);
2374
2375 /* Only return duplicate errors if NLM_F_EXCL is set */
2376 if (err == -EEXIST && !(flags & NLM_F_EXCL))
2377 err = 0;
2378
2379 return err;
2380}
2381EXPORT_SYMBOL(ndo_dflt_fdb_add);
2382
Jiri Pirkof6f64242014-11-28 14:34:15 +01002383static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2384{
2385 u16 vid = 0;
2386
2387 if (vlan_attr) {
2388 if (nla_len(vlan_attr) != sizeof(u16)) {
2389 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2390 return -EINVAL;
2391 }
2392
2393 vid = nla_get_u16(vlan_attr);
2394
2395 if (!vid || vid >= VLAN_VID_MASK) {
2396 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2397 vid);
2398 return -EINVAL;
2399 }
2400 }
2401 *p_vid = vid;
2402 return 0;
2403}
2404
Thomas Graf661d2962013-03-21 07:45:29 +00002405static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002406{
2407 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00002408 struct ndmsg *ndm;
2409 struct nlattr *tb[NDA_MAX+1];
2410 struct net_device *dev;
2411 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002412 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002413 int err;
2414
2415 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2416 if (err < 0)
2417 return err;
2418
2419 ndm = nlmsg_data(nlh);
2420 if (ndm->ndm_ifindex == 0) {
2421 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2422 return -EINVAL;
2423 }
2424
2425 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2426 if (dev == NULL) {
2427 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2428 return -ENODEV;
2429 }
2430
2431 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2432 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2433 return -EINVAL;
2434 }
2435
2436 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00002437
Jiri Pirkof6f64242014-11-28 14:34:15 +01002438 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2439 if (err)
2440 return err;
2441
John Fastabend77162022012-04-15 06:43:56 +00002442 err = -EOPNOTSUPP;
2443
2444 /* Support fdb on master device the net/bridge default case */
2445 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2446 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002447 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2448 const struct net_device_ops *ops = br_dev->netdev_ops;
2449
Jiri Pirkof6f64242014-11-28 14:34:15 +01002450 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2451 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002452 if (err)
2453 goto out;
2454 else
2455 ndm->ndm_flags &= ~NTF_MASTER;
2456 }
2457
2458 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002459 if ((ndm->ndm_flags & NTF_SELF)) {
2460 if (dev->netdev_ops->ndo_fdb_add)
2461 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002462 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002463 nlh->nlmsg_flags);
2464 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002465 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00002466 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00002467
John Fastabend3ff661c2012-04-15 06:44:14 +00002468 if (!err) {
2469 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002470 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002471 }
John Fastabend77162022012-04-15 06:43:56 +00002472 }
2473out:
2474 return err;
2475}
2476
Vlad Yasevich090096b2013-03-06 15:39:42 +00002477/**
2478 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2479 */
2480int ndo_dflt_fdb_del(struct ndmsg *ndm,
2481 struct nlattr *tb[],
2482 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01002483 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00002484{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002485 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002486
2487 /* If aging addresses are supported device will need to
2488 * implement its own handler for this.
2489 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07002490 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00002491 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07002492 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00002493 }
2494
2495 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2496 err = dev_uc_del(dev, addr);
2497 else if (is_multicast_ether_addr(addr))
2498 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002499
2500 return err;
2501}
2502EXPORT_SYMBOL(ndo_dflt_fdb_del);
2503
Thomas Graf661d2962013-03-21 07:45:29 +00002504static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabend77162022012-04-15 06:43:56 +00002505{
2506 struct net *net = sock_net(skb->sk);
2507 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00002508 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00002509 struct net_device *dev;
2510 int err = -EINVAL;
2511 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01002512 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00002513
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002514 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00002515 return -EPERM;
2516
2517 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2518 if (err < 0)
2519 return err;
John Fastabend77162022012-04-15 06:43:56 +00002520
2521 ndm = nlmsg_data(nlh);
2522 if (ndm->ndm_ifindex == 0) {
2523 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2524 return -EINVAL;
2525 }
2526
2527 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2528 if (dev == NULL) {
2529 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2530 return -ENODEV;
2531 }
2532
Vlad Yasevich1690be62013-02-13 12:00:18 +00002533 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2534 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
John Fastabend77162022012-04-15 06:43:56 +00002535 return -EINVAL;
2536 }
2537
Vlad Yasevich1690be62013-02-13 12:00:18 +00002538 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00002539
Jiri Pirkof6f64242014-11-28 14:34:15 +01002540 err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2541 if (err)
2542 return err;
2543
John Fastabend77162022012-04-15 06:43:56 +00002544 err = -EOPNOTSUPP;
2545
2546 /* Support fdb on master device the net/bridge default case */
2547 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2548 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002549 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2550 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00002551
Jiri Pirko898e5062013-01-03 22:48:52 +00002552 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002553 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002554
2555 if (err)
2556 goto out;
2557 else
2558 ndm->ndm_flags &= ~NTF_MASTER;
2559 }
2560
2561 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00002562 if (ndm->ndm_flags & NTF_SELF) {
2563 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01002564 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2565 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00002566 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01002567 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00002568
John Fastabend3ff661c2012-04-15 06:44:14 +00002569 if (!err) {
2570 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH);
John Fastabend77162022012-04-15 06:43:56 +00002571 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00002572 }
John Fastabend77162022012-04-15 06:43:56 +00002573 }
2574out:
2575 return err;
2576}
2577
John Fastabendd83b0602012-04-15 06:44:08 +00002578static int nlmsg_populate_fdb(struct sk_buff *skb,
2579 struct netlink_callback *cb,
2580 struct net_device *dev,
2581 int *idx,
2582 struct netdev_hw_addr_list *list)
2583{
2584 struct netdev_hw_addr *ha;
2585 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002586 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00002587
Eric W. Biederman15e47302012-09-07 20:12:54 +00002588 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00002589 seq = cb->nlh->nlmsg_seq;
2590
2591 list_for_each_entry(ha, &list->list, list) {
2592 if (*idx < cb->args[0])
2593 goto skip;
2594
2595 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr,
John Fastabenda7a558f2012-11-01 16:23:10 +00002596 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01002597 RTM_NEWNEIGH, NTF_SELF,
2598 NLM_F_MULTI);
John Fastabendd83b0602012-04-15 06:44:08 +00002599 if (err < 0)
2600 return err;
2601skip:
2602 *idx += 1;
2603 }
2604 return 0;
2605}
2606
2607/**
Ben Hutchings2c530402012-07-10 10:55:09 +00002608 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00002609 * @nlh: netlink message header
2610 * @dev: netdevice
2611 *
2612 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00002613 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00002614 */
2615int ndo_dflt_fdb_dump(struct sk_buff *skb,
2616 struct netlink_callback *cb,
2617 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002618 struct net_device *filter_dev,
John Fastabendd83b0602012-04-15 06:44:08 +00002619 int idx)
2620{
2621 int err;
2622
2623 netif_addr_lock_bh(dev);
2624 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2625 if (err)
2626 goto out;
2627 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2628out:
2629 netif_addr_unlock_bh(dev);
2630 return idx;
2631}
2632EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2633
John Fastabend77162022012-04-15 06:43:56 +00002634static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2635{
John Fastabend77162022012-04-15 06:43:56 +00002636 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002637 struct nlattr *tb[IFLA_MAX+1];
2638 struct net_device *bdev = NULL;
2639 struct net_device *br_dev = NULL;
2640 const struct net_device_ops *ops = NULL;
2641 const struct net_device_ops *cops = NULL;
2642 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2643 struct net *net = sock_net(skb->sk);
2644 int brport_idx = 0;
2645 int br_idx = 0;
2646 int idx = 0;
John Fastabend77162022012-04-15 06:43:56 +00002647
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002648 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2649 ifla_policy) == 0) {
2650 if (tb[IFLA_MASTER])
2651 br_idx = nla_get_u32(tb[IFLA_MASTER]);
2652 }
John Fastabend77162022012-04-15 06:43:56 +00002653
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002654 brport_idx = ifm->ifi_index;
2655
2656 if (br_idx) {
2657 br_dev = __dev_get_by_index(net, br_idx);
2658 if (!br_dev)
2659 return -ENODEV;
2660
2661 ops = br_dev->netdev_ops;
2662 bdev = br_dev;
2663 }
2664
2665 for_each_netdev(net, dev) {
2666 if (brport_idx && (dev->ifindex != brport_idx))
2667 continue;
2668
2669 if (!br_idx) { /* user did not specify a specific bridge */
2670 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2671 br_dev = netdev_master_upper_dev_get(dev);
2672 cops = br_dev->netdev_ops;
2673 }
2674
2675 bdev = dev;
2676 } else {
2677 if (dev != br_dev &&
2678 !(dev->priv_flags & IFF_BRIDGE_PORT))
2679 continue;
2680
2681 if (br_dev != netdev_master_upper_dev_get(dev) &&
2682 !(dev->priv_flags & IFF_EBRIDGE))
2683 continue;
2684
2685 bdev = br_dev;
2686 cops = ops;
John Fastabend77162022012-04-15 06:43:56 +00002687 }
2688
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002689 if (dev->priv_flags & IFF_BRIDGE_PORT) {
2690 if (cops && cops->ndo_fdb_dump)
2691 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2692 idx);
2693 }
2694
2695 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
John Fastabend77162022012-04-15 06:43:56 +00002696 if (dev->netdev_ops->ndo_fdb_dump)
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002697 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, bdev, dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04002698 idx);
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04002699
2700 cops = NULL;
John Fastabend77162022012-04-15 06:43:56 +00002701 }
John Fastabend77162022012-04-15 06:43:56 +00002702
2703 cb->args[0] = idx;
2704 return skb->len;
2705}
2706
Scott Feldman2c3c0312014-11-28 14:34:25 +01002707static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2708 unsigned int attrnum, unsigned int flag)
2709{
2710 if (mask & flag)
2711 return nla_put_u8(skb, attrnum, !!(flags & flag));
2712 return 0;
2713}
2714
John Fastabend815cccb2012-10-24 08:13:09 +00002715int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01002716 struct net_device *dev, u16 mode,
2717 u32 flags, u32 mask)
John Fastabend815cccb2012-10-24 08:13:09 +00002718{
2719 struct nlmsghdr *nlh;
2720 struct ifinfomsg *ifm;
2721 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01002722 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00002723 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00002724 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend815cccb2012-10-24 08:13:09 +00002725
2726 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), NLM_F_MULTI);
2727 if (nlh == NULL)
2728 return -EMSGSIZE;
2729
2730 ifm = nlmsg_data(nlh);
2731 ifm->ifi_family = AF_BRIDGE;
2732 ifm->__ifi_pad = 0;
2733 ifm->ifi_type = dev->type;
2734 ifm->ifi_index = dev->ifindex;
2735 ifm->ifi_flags = dev_get_flags(dev);
2736 ifm->ifi_change = 0;
2737
2738
2739 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
2740 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
2741 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00002742 (br_dev &&
2743 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00002744 (dev->addr_len &&
2745 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2746 (dev->ifindex != dev->iflink &&
2747 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
2748 goto nla_put_failure;
2749
2750 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
2751 if (!br_afspec)
2752 goto nla_put_failure;
2753
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002754 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00002755 nla_nest_cancel(skb, br_afspec);
2756 goto nla_put_failure;
2757 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08002758
2759 if (mode != BRIDGE_MODE_UNDEF) {
2760 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
2761 nla_nest_cancel(skb, br_afspec);
2762 goto nla_put_failure;
2763 }
2764 }
John Fastabend815cccb2012-10-24 08:13:09 +00002765 nla_nest_end(skb, br_afspec);
2766
Scott Feldman2c3c0312014-11-28 14:34:25 +01002767 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
2768 if (!protinfo)
2769 goto nla_put_failure;
2770
2771 if (brport_nla_put_flag(skb, flags, mask,
2772 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
2773 brport_nla_put_flag(skb, flags, mask,
2774 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
2775 brport_nla_put_flag(skb, flags, mask,
2776 IFLA_BRPORT_FAST_LEAVE,
2777 BR_MULTICAST_FAST_LEAVE) ||
2778 brport_nla_put_flag(skb, flags, mask,
2779 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
2780 brport_nla_put_flag(skb, flags, mask,
2781 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
2782 brport_nla_put_flag(skb, flags, mask,
2783 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
2784 brport_nla_put_flag(skb, flags, mask,
2785 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
2786 brport_nla_put_flag(skb, flags, mask,
2787 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
2788 nla_nest_cancel(skb, protinfo);
2789 goto nla_put_failure;
2790 }
2791
2792 nla_nest_end(skb, protinfo);
2793
John Fastabend815cccb2012-10-24 08:13:09 +00002794 return nlmsg_end(skb, nlh);
2795nla_put_failure:
2796 nlmsg_cancel(skb, nlh);
2797 return -EMSGSIZE;
2798}
2799EXPORT_SYMBOL(ndo_dflt_bridge_getlink);
2800
John Fastabende5a55a82012-10-24 08:12:57 +00002801static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
2802{
2803 struct net *net = sock_net(skb->sk);
2804 struct net_device *dev;
2805 int idx = 0;
2806 u32 portid = NETLINK_CB(cb->skb).portid;
2807 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002808 u32 filter_mask = 0;
2809
Thomas Grafaa68c202014-11-26 13:42:20 +01002810 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
2811 struct nlattr *extfilt;
2812
2813 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
2814 IFLA_EXT_MASK);
2815 if (extfilt) {
2816 if (nla_len(extfilt) < sizeof(filter_mask))
2817 return -EINVAL;
2818
2819 filter_mask = nla_get_u32(extfilt);
2820 }
2821 }
John Fastabende5a55a82012-10-24 08:12:57 +00002822
2823 rcu_read_lock();
2824 for_each_netdev_rcu(net, dev) {
2825 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00002826 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00002827
Jiri Pirko898e5062013-01-03 22:48:52 +00002828 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002829 if (idx >= cb->args[0] &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002830 br_dev->netdev_ops->ndo_bridge_getlink(
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002831 skb, portid, seq, dev, filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002832 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002833 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002834 }
2835
2836 if (ops->ndo_bridge_getlink) {
Ben Hutchings25b1e672012-11-02 12:56:52 +00002837 if (idx >= cb->args[0] &&
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002838 ops->ndo_bridge_getlink(skb, portid, seq, dev,
2839 filter_mask) < 0)
John Fastabende5a55a82012-10-24 08:12:57 +00002840 break;
Ben Hutchings25b1e672012-11-02 12:56:52 +00002841 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00002842 }
2843 }
2844 rcu_read_unlock();
2845 cb->args[0] = idx;
2846
2847 return skb->len;
2848}
2849
John Fastabend2469ffd2012-10-24 08:13:03 +00002850static inline size_t bridge_nlmsg_size(void)
2851{
2852 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
2853 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
2854 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
2855 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
2856 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
2857 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
2858 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
2859 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
2860 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
2861 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
2862 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
2863}
2864
2865static int rtnl_bridge_notify(struct net_device *dev, u16 flags)
2866{
2867 struct net *net = dev_net(dev);
Jiri Pirko898e5062013-01-03 22:48:52 +00002868 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00002869 struct sk_buff *skb;
2870 int err = -EOPNOTSUPP;
2871
2872 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
2873 if (!skb) {
2874 err = -ENOMEM;
2875 goto errout;
2876 }
2877
John Fastabendc38e01b2012-11-02 16:32:36 +00002878 if ((!flags || (flags & BRIDGE_FLAGS_MASTER)) &&
Jiri Pirko898e5062013-01-03 22:48:52 +00002879 br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002880 err = br_dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002881 if (err < 0)
2882 goto errout;
2883 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002884
John Fastabendc38e01b2012-11-02 16:32:36 +00002885 if ((flags & BRIDGE_FLAGS_SELF) &&
2886 dev->netdev_ops->ndo_bridge_getlink) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00002887 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0);
John Fastabendc38e01b2012-11-02 16:32:36 +00002888 if (err < 0)
2889 goto errout;
2890 }
John Fastabend2469ffd2012-10-24 08:13:03 +00002891
2892 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
2893 return 0;
2894errout:
2895 WARN_ON(err == -EMSGSIZE);
2896 kfree_skb(skb);
2897 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2898 return err;
2899}
2900
Thomas Graf661d2962013-03-21 07:45:29 +00002901static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
John Fastabende5a55a82012-10-24 08:12:57 +00002902{
2903 struct net *net = sock_net(skb->sk);
2904 struct ifinfomsg *ifm;
2905 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00002906 struct nlattr *br_spec, *attr = NULL;
2907 int rem, err = -EOPNOTSUPP;
John Fastabendc38e01b2012-11-02 16:32:36 +00002908 u16 oflags, flags = 0;
2909 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00002910
2911 if (nlmsg_len(nlh) < sizeof(*ifm))
2912 return -EINVAL;
2913
2914 ifm = nlmsg_data(nlh);
2915 if (ifm->ifi_family != AF_BRIDGE)
2916 return -EPFNOSUPPORT;
2917
2918 dev = __dev_get_by_index(net, ifm->ifi_index);
2919 if (!dev) {
2920 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2921 return -ENODEV;
2922 }
2923
John Fastabend2469ffd2012-10-24 08:13:03 +00002924 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2925 if (br_spec) {
2926 nla_for_each_nested(attr, br_spec, rem) {
2927 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01002928 if (nla_len(attr) < sizeof(flags))
2929 return -EINVAL;
2930
John Fastabendc38e01b2012-11-02 16:32:36 +00002931 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00002932 flags = nla_get_u16(attr);
2933 break;
2934 }
2935 }
2936 }
2937
John Fastabendc38e01b2012-11-02 16:32:36 +00002938 oflags = flags;
2939
John Fastabend2469ffd2012-10-24 08:13:03 +00002940 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002941 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2942
2943 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00002944 err = -EOPNOTSUPP;
2945 goto out;
2946 }
2947
Jiri Pirko898e5062013-01-03 22:48:52 +00002948 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002949 if (err)
2950 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00002951
2952 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00002953 }
2954
John Fastabend2469ffd2012-10-24 08:13:03 +00002955 if ((flags & BRIDGE_FLAGS_SELF)) {
2956 if (!dev->netdev_ops->ndo_bridge_setlink)
2957 err = -EOPNOTSUPP;
2958 else
2959 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh);
John Fastabende5a55a82012-10-24 08:12:57 +00002960
John Fastabend2469ffd2012-10-24 08:13:03 +00002961 if (!err)
2962 flags &= ~BRIDGE_FLAGS_SELF;
2963 }
2964
John Fastabendc38e01b2012-11-02 16:32:36 +00002965 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00002966 memcpy(nla_data(attr), &flags, sizeof(flags));
2967 /* Generate event to notify upper layer of bridge change */
2968 if (!err)
John Fastabendc38e01b2012-11-02 16:32:36 +00002969 err = rtnl_bridge_notify(dev, oflags);
John Fastabende5a55a82012-10-24 08:12:57 +00002970out:
2971 return err;
2972}
2973
Thomas Graf661d2962013-03-21 07:45:29 +00002974static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
Vlad Yasevich407af322013-02-13 12:00:12 +00002975{
2976 struct net *net = sock_net(skb->sk);
2977 struct ifinfomsg *ifm;
2978 struct net_device *dev;
2979 struct nlattr *br_spec, *attr = NULL;
2980 int rem, err = -EOPNOTSUPP;
2981 u16 oflags, flags = 0;
2982 bool have_flags = false;
2983
2984 if (nlmsg_len(nlh) < sizeof(*ifm))
2985 return -EINVAL;
2986
2987 ifm = nlmsg_data(nlh);
2988 if (ifm->ifi_family != AF_BRIDGE)
2989 return -EPFNOSUPPORT;
2990
2991 dev = __dev_get_by_index(net, ifm->ifi_index);
2992 if (!dev) {
2993 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
2994 return -ENODEV;
2995 }
2996
2997 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2998 if (br_spec) {
2999 nla_for_each_nested(attr, br_spec, rem) {
3000 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003001 if (nla_len(attr) < sizeof(flags))
3002 return -EINVAL;
3003
Vlad Yasevich407af322013-02-13 12:00:12 +00003004 have_flags = true;
3005 flags = nla_get_u16(attr);
3006 break;
3007 }
3008 }
3009 }
3010
3011 oflags = flags;
3012
3013 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3014 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3015
3016 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3017 err = -EOPNOTSUPP;
3018 goto out;
3019 }
3020
3021 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3022 if (err)
3023 goto out;
3024
3025 flags &= ~BRIDGE_FLAGS_MASTER;
3026 }
3027
3028 if ((flags & BRIDGE_FLAGS_SELF)) {
3029 if (!dev->netdev_ops->ndo_bridge_dellink)
3030 err = -EOPNOTSUPP;
3031 else
3032 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh);
3033
3034 if (!err)
3035 flags &= ~BRIDGE_FLAGS_SELF;
3036 }
3037
3038 if (have_flags)
3039 memcpy(nla_data(attr), &flags, sizeof(flags));
3040 /* Generate event to notify upper layer of bridge change */
3041 if (!err)
3042 err = rtnl_bridge_notify(dev, oflags);
3043out:
3044 return err;
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047/* Process one rtnetlink message. */
3048
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003049static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003051 struct net *net = sock_net(skb->sk);
Thomas Grafe2849862007-03-22 11:48:11 -07003052 rtnl_doit_func doit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 int sz_idx, kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 int family;
3055 int type;
Eric Dumazet2907c352011-05-25 07:34:04 +00003056 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07003060 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 type -= RTM_BASE;
3063
3064 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00003065 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 0;
3067
Hong zhi guo573ce262013-03-27 06:47:04 +00003068 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 sz_idx = type>>2;
3070 kind = type&3;
3071
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003072 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07003073 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
David S. Millerb8f3ab42011-01-18 12:40:38 -08003075 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003076 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07003077 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00003078 rtnl_calcit_func calcit;
3079 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Thomas Grafe2849862007-03-22 11:48:11 -07003081 dumpit = rtnl_get_dumpit(family, type);
3082 if (dumpit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003083 return -EOPNOTSUPP;
Greg Rosec7ac8672011-06-10 01:27:09 +00003084 calcit = rtnl_get_calcit(family, type);
3085 if (calcit)
Greg Rose115c9b82012-02-21 16:54:48 -05003086 min_dump_alloc = calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Eric Dumazet2907c352011-05-25 07:34:04 +00003088 __rtnl_unlock();
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003089 rtnl = net->rtnl;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00003090 {
3091 struct netlink_dump_control c = {
3092 .dump = dumpit,
3093 .min_dump_alloc = min_dump_alloc,
3094 };
3095 err = netlink_dump_start(rtnl, skb, nlh, &c);
3096 }
Eric Dumazet2907c352011-05-25 07:34:04 +00003097 rtnl_lock();
3098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100
Thomas Grafe2849862007-03-22 11:48:11 -07003101 doit = rtnl_get_doit(family, type);
3102 if (doit == NULL)
Thomas Graf038890f2007-04-05 14:35:52 -07003103 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Thomas Graf661d2962013-03-21 07:45:29 +00003105 return doit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003108static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07003110 rtnl_lock();
3111 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3112 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3116{
Jiri Pirko351638e2013-05-28 01:30:21 +00003117 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 case NETDEV_UP:
3121 case NETDEV_DOWN:
Patrick McHardy10de05a2010-02-26 06:34:50 +00003122 case NETDEV_PRE_UP:
Eric W. Biedermand90a9092009-12-12 22:11:15 +00003123 case NETDEV_POST_INIT:
3124 case NETDEV_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 case NETDEV_CHANGE:
Patrick McHardy755d0e72010-03-19 04:42:24 +00003126 case NETDEV_PRE_TYPE_CHANGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 case NETDEV_GOING_DOWN:
Patrick McHardya2835762010-02-26 06:34:51 +00003128 case NETDEV_UNREGISTER:
Eric Dumazet0115e8e2012-08-22 17:19:46 +00003129 case NETDEV_UNREGISTER_FINAL:
Amerigo Wangac3d3f82011-05-19 23:06:32 +00003130 case NETDEV_RELEASE:
3131 case NETDEV_JOIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 break;
3133 default:
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003134 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 break;
3136 }
3137 return NOTIFY_DONE;
3138}
3139
3140static struct notifier_block rtnetlink_dev_notifier = {
3141 .notifier_call = rtnetlink_event,
3142};
3143
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003144
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003145static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003146{
3147 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003148 struct netlink_kernel_cfg cfg = {
3149 .groups = RTNLGRP_MAX,
3150 .input = rtnetlink_rcv,
3151 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003152 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003153 };
3154
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003155 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003156 if (!sk)
3157 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003158 net->rtnl = sk;
3159 return 0;
3160}
3161
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003162static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003163{
Denis V. Lunev775516b2008-01-18 23:55:19 -08003164 netlink_kernel_release(net->rtnl);
3165 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003166}
3167
3168static struct pernet_operations rtnetlink_net_ops = {
3169 .init = rtnetlink_net_init,
3170 .exit = rtnetlink_net_exit,
3171};
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173void __init rtnetlink_init(void)
3174{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003175 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08003177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07003179
Greg Rosec7ac8672011-06-10 01:27:09 +00003180 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3181 rtnl_dump_ifinfo, rtnl_calcit);
3182 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3183 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3184 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
Thomas Graf687ad8c2007-03-22 11:59:42 -07003185
Greg Rosec7ac8672011-06-10 01:27:09 +00003186 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3187 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
John Fastabend77162022012-04-15 06:43:56 +00003188
3189 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3190 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3191 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003192
3193 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00003194 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
John Fastabende5a55a82012-10-24 08:12:57 +00003195 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197