blob: fb2d61df1e2fc92e16f047bf1f3487ea108f5a7b [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
Jakub Kicinskiee5d0322017-06-21 18:25:04 -070019#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/timer.h>
26#include <linux/string.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/capability.h>
34#include <linux/skbuff.h>
35#include <linux/init.h>
36#include <linux/security.h>
Stephen Hemminger6756ae42006-03-20 22:23:58 -080037#include <linux/mutex.h>
Thomas Graf18237302006-08-04 23:04:54 -070038#include <linux/if_addr.h>
John Fastabend77162022012-04-15 06:43:56 +000039#include <linux/if_bridge.h>
Jiri Pirkof6f64242014-11-28 14:34:15 +010040#include <linux/if_vlan.h>
Williams, Mitch Aebc08a62010-02-10 01:44:05 +000041#include <linux/pci.h>
John Fastabend77162022012-04-15 06:43:56 +000042#include <linux/etherdevice.h>
Martin KaFai Lau58038692017-06-15 17:29:09 -070043#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <linux/inet.h>
48#include <linux/netdevice.h>
Jiri Pirko82f28412014-11-28 14:34:18 +010049#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/ip.h>
51#include <net/protocol.h>
52#include <net/arp.h>
53#include <net/route.h>
54#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010055#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070058#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070059#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric Dumazete0d087a2009-11-07 01:26:17 -080062struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070063 rtnl_doit_func doit;
64 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010065 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020066 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010067 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070068};
69
Stephen Hemminger6756ae42006-03-20 22:23:58 -080070static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72void rtnl_lock(void)
73{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080074 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Eric Dumazete0d087a2009-11-07 01:26:17 -080076EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Eric Dumazet1b5c5492016-06-13 20:21:50 -070078static struct sk_buff *defer_kfree_skb_list;
79void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
80{
81 if (head && tail) {
82 tail->next = defer_kfree_skb_list;
83 defer_kfree_skb_list = head;
84 }
85}
86EXPORT_SYMBOL(rtnl_kfree_skbs);
87
Stephen Hemminger6756ae42006-03-20 22:23:58 -080088void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070090 struct sk_buff *head = defer_kfree_skb_list;
91
92 defer_kfree_skb_list = NULL;
93
Stephen Hemminger6756ae42006-03-20 22:23:58 -080094 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -070095
96 while (head) {
97 struct sk_buff *next = head->next;
98
99 kfree_skb(head);
100 cond_resched();
101 head = next;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void rtnl_unlock(void)
106{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700107 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 netdev_run_todo();
109}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800110EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800112int rtnl_trylock(void)
113{
114 return mutex_trylock(&rtnl_mutex);
115}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800116EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800117
Patrick McHardyc9c10142008-04-23 22:10:48 -0700118int rtnl_is_locked(void)
119{
120 return mutex_is_locked(&rtnl_mutex);
121}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800122EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700123
Paul E. McKenneya898def2010-02-22 17:04:49 -0800124#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800125bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800126{
127 return lockdep_is_held(&rtnl_mutex);
128}
129EXPORT_SYMBOL(lockdep_rtnl_is_held);
130#endif /* #ifdef CONFIG_PROVE_LOCKING */
131
Florian Westphaladdf9b92017-12-02 21:44:05 +0100132static struct rtnl_link __rcu **rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700133
134static inline int rtm_msgindex(int msgtype)
135{
136 int msgindex = msgtype - RTM_BASE;
137
138 /*
139 * msgindex < 0 implies someone tried to register a netlink
140 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
141 * the message type has not been added to linux/rtnetlink.h
142 */
143 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
144
145 return msgindex;
146}
147
Florian Westphaladdf9b92017-12-02 21:44:05 +0100148static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
149{
150 struct rtnl_link **tab;
151
152 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
153 protocol = PF_UNSPEC;
154
155 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
156 if (!tab)
157 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
158
159 return tab[msgtype];
160}
161
Florian Westphale4202512017-12-02 21:44:06 +0100162static int rtnl_register_internal(struct module *owner,
163 int protocol, int msgtype,
164 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
165 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700166{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100167 struct rtnl_link **tab, *link, *old;
Thomas Grafe2849862007-03-22 11:48:11 -0700168 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100169 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700170
Patrick McHardy25239ce2010-04-26 16:02:05 +0200171 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700172 msgindex = rtm_msgindex(msgtype);
173
Florian Westphaladdf9b92017-12-02 21:44:05 +0100174 rtnl_lock();
175 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700176 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100177 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
178 if (!tab)
179 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700180
Florian Westphaladdf9b92017-12-02 21:44:05 +0100181 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200182 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700183 }
184
Florian Westphaladdf9b92017-12-02 21:44:05 +0100185 old = rtnl_dereference(tab[msgindex]);
186 if (old) {
187 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
188 if (!link)
189 goto unlock;
190 } else {
191 link = kzalloc(sizeof(*link), GFP_KERNEL);
192 if (!link)
193 goto unlock;
194 }
Thomas Grafe2849862007-03-22 11:48:11 -0700195
Florian Westphale4202512017-12-02 21:44:06 +0100196 WARN_ON(link->owner && link->owner != owner);
197 link->owner = owner;
198
Florian Westphaladdf9b92017-12-02 21:44:05 +0100199 WARN_ON(doit && link->doit && link->doit != doit);
200 if (doit)
201 link->doit = doit;
202 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
203 if (dumpit)
204 link->dumpit = dumpit;
205
206 link->flags |= flags;
207
208 /* publish protocol:msgtype */
209 rcu_assign_pointer(tab[msgindex], link);
210 ret = 0;
211 if (old)
212 kfree_rcu(old, rcu);
213unlock:
214 rtnl_unlock();
215 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700216}
Florian Westphale4202512017-12-02 21:44:06 +0100217
218/**
219 * rtnl_register_module - Register a rtnetlink message type
220 *
221 * @owner: module registering the hook (THIS_MODULE)
222 * @protocol: Protocol family or PF_UNSPEC
223 * @msgtype: rtnetlink message type
224 * @doit: Function pointer called for each request message
225 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
226 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
227 *
228 * Like rtnl_register, but for use by removable modules.
229 */
230int rtnl_register_module(struct module *owner,
231 int protocol, int msgtype,
232 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
233 unsigned int flags)
234{
235 return rtnl_register_internal(owner, protocol, msgtype,
236 doit, dumpit, flags);
237}
238EXPORT_SYMBOL_GPL(rtnl_register_module);
239
240/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100241 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100242 * @protocol: Protocol family or PF_UNSPEC
243 * @msgtype: rtnetlink message type
244 * @doit: Function pointer called for each request message
245 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
246 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
247 *
248 * Registers the specified function pointers (at least one of them has
249 * to be non-NULL) to be called whenever a request message for the
250 * specified protocol family and message type is received.
251 *
252 * The special protocol family PF_UNSPEC may be used to define fallback
253 * function pointers for the case when no entry for the specific protocol
254 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700255 */
256void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000257 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200258 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700259{
Florian Westphal16feebc2017-12-02 21:44:08 +0100260 int err;
261
262 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
263 flags);
264 if (err)
265 pr_err("Unable to register rtnetlink message handler, "
266 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700267}
Thomas Grafe2849862007-03-22 11:48:11 -0700268EXPORT_SYMBOL_GPL(rtnl_register);
269
270/**
271 * rtnl_unregister - Unregister a rtnetlink message type
272 * @protocol: Protocol family or PF_UNSPEC
273 * @msgtype: rtnetlink message type
274 *
275 * Returns 0 on success or a negative error code.
276 */
277int rtnl_unregister(int protocol, int msgtype)
278{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100279 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700280 int msgindex;
281
Patrick McHardy25239ce2010-04-26 16:02:05 +0200282 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700283 msgindex = rtm_msgindex(msgtype);
284
Florian Westphal6853dd42017-08-09 20:41:51 +0200285 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100286 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
287 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200288 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700289 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200290 }
Thomas Grafe2849862007-03-22 11:48:11 -0700291
Florian Westphaladdf9b92017-12-02 21:44:05 +0100292 link = tab[msgindex];
293 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200294 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700295
Florian Westphaladdf9b92017-12-02 21:44:05 +0100296 kfree_rcu(link, rcu);
297
Thomas Grafe2849862007-03-22 11:48:11 -0700298 return 0;
299}
Thomas Grafe2849862007-03-22 11:48:11 -0700300EXPORT_SYMBOL_GPL(rtnl_unregister);
301
302/**
303 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
304 * @protocol : Protocol family or PF_UNSPEC
305 *
306 * Identical to calling rtnl_unregster() for all registered message types
307 * of a certain protocol family.
308 */
309void rtnl_unregister_all(int protocol)
310{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100311 struct rtnl_link **tab, *link;
312 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200313
Patrick McHardy25239ce2010-04-26 16:02:05 +0200314 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700315
Florian Westphal019a3162017-08-09 20:41:49 +0200316 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100317 tab = rtnl_msg_handlers[protocol];
Florian Westphal6853dd42017-08-09 20:41:51 +0200318 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100319 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
320 link = tab[msgindex];
321 if (!link)
322 continue;
323
324 rcu_assign_pointer(tab[msgindex], NULL);
325 kfree_rcu(link, rcu);
326 }
Florian Westphal019a3162017-08-09 20:41:49 +0200327 rtnl_unlock();
328
Florian Westphal6853dd42017-08-09 20:41:51 +0200329 synchronize_net();
330
Florian Westphaladdf9b92017-12-02 21:44:05 +0100331 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700332}
Thomas Grafe2849862007-03-22 11:48:11 -0700333EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Patrick McHardy38f7b872007-06-13 12:03:51 -0700335static LIST_HEAD(link_ops);
336
Eric Dumazetc63044f2011-12-13 11:38:00 +0000337static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
338{
339 const struct rtnl_link_ops *ops;
340
341 list_for_each_entry(ops, &link_ops, list) {
342 if (!strcmp(ops->kind, kind))
343 return ops;
344 }
345 return NULL;
346}
347
Patrick McHardy38f7b872007-06-13 12:03:51 -0700348/**
349 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
350 * @ops: struct rtnl_link_ops * to register
351 *
352 * The caller must hold the rtnl_mutex. This function should be used
353 * by drivers that create devices during module initialization. It
354 * must be called before registering the devices.
355 *
356 * Returns 0 on success or a negative error code.
357 */
358int __rtnl_link_register(struct rtnl_link_ops *ops)
359{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000360 if (rtnl_link_ops_get(ops->kind))
361 return -EEXIST;
362
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200363 /* The check for setup is here because if ops
364 * does not have that filled up, it is not possible
365 * to use the ops for creating device. So do not
366 * fill up dellink as well. That disables rtnl_dellink.
367 */
368 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000369 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700370
Patrick McHardy38f7b872007-06-13 12:03:51 -0700371 list_add_tail(&ops->list, &link_ops);
372 return 0;
373}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700374EXPORT_SYMBOL_GPL(__rtnl_link_register);
375
376/**
377 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
378 * @ops: struct rtnl_link_ops * to register
379 *
380 * Returns 0 on success or a negative error code.
381 */
382int rtnl_link_register(struct rtnl_link_ops *ops)
383{
384 int err;
385
386 rtnl_lock();
387 err = __rtnl_link_register(ops);
388 rtnl_unlock();
389 return err;
390}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700391EXPORT_SYMBOL_GPL(rtnl_link_register);
392
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700393static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
394{
395 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000396 LIST_HEAD(list_kill);
397
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700398 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000399 if (dev->rtnl_link_ops == ops)
400 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700401 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000402 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700403}
404
Patrick McHardy38f7b872007-06-13 12:03:51 -0700405/**
406 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
407 * @ops: struct rtnl_link_ops * to unregister
408 *
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700409 * The caller must hold the rtnl_mutex.
Patrick McHardy38f7b872007-06-13 12:03:51 -0700410 */
411void __rtnl_link_unregister(struct rtnl_link_ops *ops)
412{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700413 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700414
Eric W. Biederman881d9662007-09-17 11:56:21 -0700415 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700416 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700417 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700418 list_del(&ops->list);
419}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700420EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
421
Cong Wang200b9162014-05-12 15:11:20 -0700422/* Return with the rtnl_lock held when there are no network
423 * devices unregistering in any network namespace.
424 */
425static void rtnl_lock_unregistering_all(void)
426{
427 struct net *net;
428 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100429 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700430
Peter Zijlstraff960a72014-10-29 17:04:56 +0100431 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700432 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700433 unregistering = false;
434 rtnl_lock();
435 for_each_net(net) {
436 if (net->dev_unreg_count > 0) {
437 unregistering = true;
438 break;
439 }
440 }
441 if (!unregistering)
442 break;
443 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100444
445 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700446 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100447 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700448}
449
Patrick McHardy38f7b872007-06-13 12:03:51 -0700450/**
451 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
452 * @ops: struct rtnl_link_ops * to unregister
453 */
454void rtnl_link_unregister(struct rtnl_link_ops *ops)
455{
Cong Wang200b9162014-05-12 15:11:20 -0700456 /* Close the race with cleanup_net() */
457 mutex_lock(&net_mutex);
458 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700459 __rtnl_link_unregister(ops);
460 rtnl_unlock();
Cong Wang200b9162014-05-12 15:11:20 -0700461 mutex_unlock(&net_mutex);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700462}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700463EXPORT_SYMBOL_GPL(rtnl_link_unregister);
464
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100465static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
466{
467 struct net_device *master_dev;
468 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200469 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100470
Florian Westphal8515ae32017-08-10 16:52:59 +0200471 rcu_read_lock();
472
473 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100474 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200475 goto out;
476
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100477 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900478 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200479 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100480 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200481 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100482 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200483
484out:
485 rcu_read_unlock();
486 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100487}
488
Patrick McHardy38f7b872007-06-13 12:03:51 -0700489static size_t rtnl_link_get_size(const struct net_device *dev)
490{
491 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
492 size_t size;
493
494 if (!ops)
495 return 0;
496
Thomas Graf369cf772010-11-11 15:47:59 +0000497 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
498 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700499
500 if (ops->get_size)
501 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000502 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700503 ops->get_size(dev);
504
505 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000506 /* IFLA_INFO_XSTATS */
507 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700508
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100509 size += rtnl_link_get_slave_info_data_size(dev);
510
Patrick McHardy38f7b872007-06-13 12:03:51 -0700511 return size;
512}
513
Thomas Graff8ff1822010-11-16 04:30:14 +0000514static LIST_HEAD(rtnl_af_ops);
515
516static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
517{
518 const struct rtnl_af_ops *ops;
519
Florian Westphal5fa85a02017-10-16 15:44:36 +0200520 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000521 if (ops->family == family)
522 return ops;
523 }
524
525 return NULL;
526}
527
528/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000529 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
530 * @ops: struct rtnl_af_ops * to register
531 *
532 * Returns 0 on success or a negative error code.
533 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800534void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000535{
Thomas Graff8ff1822010-11-16 04:30:14 +0000536 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200537 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000538 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000539}
540EXPORT_SYMBOL_GPL(rtnl_af_register);
541
542/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000543 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
544 * @ops: struct rtnl_af_ops * to unregister
545 */
546void rtnl_af_unregister(struct rtnl_af_ops *ops)
547{
548 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200549 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000550 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200551
552 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000553}
554EXPORT_SYMBOL_GPL(rtnl_af_unregister);
555
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700556static size_t rtnl_link_get_af_size(const struct net_device *dev,
557 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000558{
559 struct rtnl_af_ops *af_ops;
560 size_t size;
561
562 /* IFLA_AF_SPEC */
563 size = nla_total_size(sizeof(struct nlattr));
564
Florian Westphal5fa85a02017-10-16 15:44:36 +0200565 rcu_read_lock();
566 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000567 if (af_ops->get_link_af_size) {
568 /* AF_* + nested data */
569 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700570 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000571 }
572 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200573 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000574
575 return size;
576}
577
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100578static bool rtnl_have_link_slave_info(const struct net_device *dev)
579{
580 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200581 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100582
Florian Westphal4c82a952017-09-26 13:58:43 +0200583 rcu_read_lock();
584
585 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100586 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200587 ret = true;
588 rcu_read_unlock();
589 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100590}
591
592static int rtnl_link_slave_info_fill(struct sk_buff *skb,
593 const struct net_device *dev)
594{
595 struct net_device *master_dev;
596 const struct rtnl_link_ops *ops;
597 struct nlattr *slave_data;
598 int err;
599
600 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
601 if (!master_dev)
602 return 0;
603 ops = master_dev->rtnl_link_ops;
604 if (!ops)
605 return 0;
606 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
607 return -EMSGSIZE;
608 if (ops->fill_slave_info) {
609 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
610 if (!slave_data)
611 return -EMSGSIZE;
612 err = ops->fill_slave_info(skb, master_dev, dev);
613 if (err < 0)
614 goto err_cancel_slave_data;
615 nla_nest_end(skb, slave_data);
616 }
617 return 0;
618
619err_cancel_slave_data:
620 nla_nest_cancel(skb, slave_data);
621 return err;
622}
623
624static int rtnl_link_info_fill(struct sk_buff *skb,
625 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700626{
627 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100628 struct nlattr *data;
629 int err;
630
631 if (!ops)
632 return 0;
633 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
634 return -EMSGSIZE;
635 if (ops->fill_xstats) {
636 err = ops->fill_xstats(skb, dev);
637 if (err < 0)
638 return err;
639 }
640 if (ops->fill_info) {
641 data = nla_nest_start(skb, IFLA_INFO_DATA);
642 if (data == NULL)
643 return -EMSGSIZE;
644 err = ops->fill_info(skb, dev);
645 if (err < 0)
646 goto err_cancel_data;
647 nla_nest_end(skb, data);
648 }
649 return 0;
650
651err_cancel_data:
652 nla_nest_cancel(skb, data);
653 return err;
654}
655
656static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
657{
658 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700659 int err = -EMSGSIZE;
660
661 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
662 if (linkinfo == NULL)
663 goto out;
664
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100665 err = rtnl_link_info_fill(skb, dev);
666 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700667 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100668
669 err = rtnl_link_slave_info_fill(skb, dev);
670 if (err < 0)
671 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700672
673 nla_nest_end(skb, linkinfo);
674 return 0;
675
Patrick McHardy38f7b872007-06-13 12:03:51 -0700676err_cancel_link:
677 nla_nest_cancel(skb, linkinfo);
678out:
679 return err;
680}
681
Eric Dumazet95c96172012-04-15 05:58:06 +0000682int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800684 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int err = 0;
686
Patrick McHardyac6d4392005-08-14 19:29:52 -0700687 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300689 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
691 if (echo)
692 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
693 return err;
694}
695
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800696int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700697{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800698 struct sock *rtnl = net->rtnl;
699
Thomas Graf2942e902006-08-15 00:30:25 -0700700 return nlmsg_unicast(rtnl, skb, pid);
701}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800702EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700703
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800704void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
705 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700706{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800707 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700708 int report = 0;
709
710 if (nlh)
711 report = nlmsg_report(nlh);
712
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800713 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700714}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800715EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700716
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800717void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700718{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800719 struct sock *rtnl = net->rtnl;
720
Thomas Graf97676b62006-08-15 00:31:41 -0700721 netlink_set_err(rtnl, 0, group, error);
722}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800723EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
726{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700727 struct nlattr *mx;
728 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Thomas Graf2d7202b2006-08-22 00:01:27 -0700730 mx = nla_nest_start(skb, RTA_METRICS);
731 if (mx == NULL)
732 return -ENOBUFS;
733
734 for (i = 0; i < RTAX_MAX; i++) {
735 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100736 if (i == RTAX_CC_ALGO - 1) {
737 char tmp[TCP_CA_NAME_MAX], *name;
738
739 name = tcp_ca_get_name_by_key(metrics[i], tmp);
740 if (!name)
741 continue;
742 if (nla_put_string(skb, i + 1, name))
743 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200744 } else if (i == RTAX_FEATURES - 1) {
745 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
746
Phil Sutterf8edcd12016-08-23 13:14:31 +0200747 if (!user_features)
748 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200749 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
750 if (nla_put_u32(skb, i + 1, user_features))
751 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100752 } else {
753 if (nla_put_u32(skb, i + 1, metrics[i]))
754 goto nla_put_failure;
755 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700756 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David S. Millera57d27f2006-08-22 22:20:14 -0700760 if (!valid) {
761 nla_nest_cancel(skb, mx);
762 return 0;
763 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700764
765 return nla_nest_end(skb, mx);
766
767nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700768 nla_nest_cancel(skb, mx);
769 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800771EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Thomas Grafe3703b32006-11-27 09:27:07 -0800773int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700774 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800775{
776 struct rta_cacheinfo ci = {
Eric Dumazeta399a802012-08-08 21:13:53 +0000777 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
Thomas Grafe3703b32006-11-27 09:27:07 -0800778 .rta_used = dst->__use,
779 .rta_clntref = atomic_read(&(dst->__refcnt)),
780 .rta_error = error,
781 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800782 };
783
Li Wei82539472012-07-29 16:01:30 +0000784 if (expires) {
785 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800786
Li Wei82539472012-07-29 16:01:30 +0000787 clock = jiffies_to_clock_t(abs(expires));
788 clock = min_t(unsigned long, clock, INT_MAX);
789 ci.rta_expires = (expires > 0) ? clock : -clock;
790 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800791 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
792}
Thomas Grafe3703b32006-11-27 09:27:07 -0800793EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David S. Miller93b2d4a2008-02-17 18:35:07 -0800795static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800796{
797 unsigned char operstate = dev->operstate;
798
Eric Dumazete0d087a2009-11-07 01:26:17 -0800799 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800800 case IF_OPER_UP:
801 if ((operstate == IF_OPER_DORMANT ||
802 operstate == IF_OPER_UNKNOWN) &&
803 !netif_dormant(dev))
804 operstate = IF_OPER_UP;
805 break;
806
807 case IF_OPER_DORMANT:
808 if (operstate == IF_OPER_UP ||
809 operstate == IF_OPER_UNKNOWN)
810 operstate = IF_OPER_DORMANT;
811 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700812 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800813
814 if (dev->operstate != operstate) {
815 write_lock_bh(&dev_base_lock);
816 dev->operstate = operstate;
817 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800818 netdev_state_change(dev);
819 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800820}
821
Jiri Bencb1beb682012-07-27 02:58:22 +0000822static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
823{
824 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
825 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
826}
827
Patrick McHardy3729d502010-02-26 06:34:54 +0000828static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
829 const struct ifinfomsg *ifm)
830{
831 unsigned int flags = ifm->ifi_flags;
832
833 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
834 if (ifm->ifi_change)
835 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000836 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000837
838 return flags;
839}
840
Thomas Grafb60c5112006-08-04 23:05:34 -0700841static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000842 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Thomas Grafb60c5112006-08-04 23:05:34 -0700844 a->rx_packets = b->rx_packets;
845 a->tx_packets = b->tx_packets;
846 a->rx_bytes = b->rx_bytes;
847 a->tx_bytes = b->tx_bytes;
848 a->rx_errors = b->rx_errors;
849 a->tx_errors = b->tx_errors;
850 a->rx_dropped = b->rx_dropped;
851 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Grafb60c5112006-08-04 23:05:34 -0700853 a->multicast = b->multicast;
854 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Thomas Grafb60c5112006-08-04 23:05:34 -0700856 a->rx_length_errors = b->rx_length_errors;
857 a->rx_over_errors = b->rx_over_errors;
858 a->rx_crc_errors = b->rx_crc_errors;
859 a->rx_frame_errors = b->rx_frame_errors;
860 a->rx_fifo_errors = b->rx_fifo_errors;
861 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Thomas Grafb60c5112006-08-04 23:05:34 -0700863 a->tx_aborted_errors = b->tx_aborted_errors;
864 a->tx_carrier_errors = b->tx_carrier_errors;
865 a->tx_fifo_errors = b->tx_fifo_errors;
866 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
867 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Thomas Grafb60c5112006-08-04 23:05:34 -0700869 a->rx_compressed = b->rx_compressed;
870 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500871
872 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000873}
874
Chris Wrightc02db8c2010-05-16 01:05:45 -0700875/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500876static inline int rtnl_vfinfo_size(const struct net_device *dev,
877 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000878{
Phil Sutter9af15c32017-01-18 14:04:39 +0100879 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700880 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100881 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700882 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100883 (nla_total_size(0) +
884 nla_total_size(sizeof(struct ifla_vf_mac)) +
885 nla_total_size(sizeof(struct ifla_vf_vlan)) +
886 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300887 nla_total_size(MAX_VLAN_LIST_LEN *
888 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400889 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100890 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200891 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300892 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300893 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100894 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300895 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200896 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300897 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200898 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300899 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200900 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300901 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200902 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300903 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200904 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300905 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200906 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000907 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700908 return size;
909 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000910 return 0;
911}
912
David Gibsonc53864f2014-04-24 10:22:36 +1000913static size_t rtnl_port_size(const struct net_device *dev,
914 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700915{
916 size_t port_size = nla_total_size(4) /* PORT_VF */
917 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700918 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
919 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
920 + nla_total_size(1) /* PROT_VDP_REQUEST */
921 + nla_total_size(2); /* PORT_VDP_RESPONSE */
922 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
923 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
924 + port_size;
925 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
926 + port_size;
927
David Gibsonc53864f2014-04-24 10:22:36 +1000928 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
929 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700930 return 0;
931 if (dev_num_vf(dev->dev.parent))
932 return port_self_size + vf_ports_size +
933 vf_port_size * dev_num_vf(dev->dev.parent);
934 else
935 return port_self_size;
936}
937
David S. Millerb5cdae32017-04-18 15:36:58 -0400938static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700939{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100940 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700941 nla_total_size(1) + /* XDP_ATTACHED */
942 nla_total_size(4); /* XDP_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700943
David S. Millerb5cdae32017-04-18 15:36:58 -0400944 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700945}
946
Greg Rose115c9b82012-02-21 16:54:48 -0500947static noinline size_t if_nlmsg_size(const struct net_device *dev,
948 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800949{
950 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
951 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700952 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800953 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200954 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800955 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400956 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800957 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
958 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
959 + nla_total_size(4) /* IFLA_TXQLEN */
960 + nla_total_size(4) /* IFLA_WEIGHT */
961 + nla_total_size(4) /* IFLA_MTU */
962 + nla_total_size(4) /* IFLA_LINK */
963 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000964 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +0000965 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +0000966 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
967 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +0100968 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
969 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -0800970 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700971 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -0700972 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +0100973 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +0300974 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -0500975 + nla_total_size(ext_filter_mask
976 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
977 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +1000978 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +0000979 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700980 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +0100981 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -0700982 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +0200983 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -0400984 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -0400985 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +0200986 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Colin Ian King03ac7382017-11-06 15:04:54 +0000987 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Jiri Benc79e1ad12017-11-02 17:04:38 -0200988 + nla_total_size(4) /* IFLA_IF_NETNSID */
989 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800990}
991
Scott Feldman57b61082010-05-17 22:49:55 -0700992static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
993{
994 struct nlattr *vf_ports;
995 struct nlattr *vf_port;
996 int vf;
997 int err;
998
999 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1000 if (!vf_ports)
1001 return -EMSGSIZE;
1002
1003 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1004 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001005 if (!vf_port)
1006 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001007 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1008 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001009 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001010 if (err == -EMSGSIZE)
1011 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001012 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001013 nla_nest_cancel(skb, vf_port);
1014 continue;
1015 }
1016 nla_nest_end(skb, vf_port);
1017 }
1018
1019 nla_nest_end(skb, vf_ports);
1020
1021 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001022
1023nla_put_failure:
1024 nla_nest_cancel(skb, vf_ports);
1025 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001026}
1027
1028static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1029{
1030 struct nlattr *port_self;
1031 int err;
1032
1033 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1034 if (!port_self)
1035 return -EMSGSIZE;
1036
1037 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1038 if (err) {
1039 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001040 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001041 }
1042
1043 nla_nest_end(skb, port_self);
1044
1045 return 0;
1046}
1047
David Gibsonc53864f2014-04-24 10:22:36 +10001048static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1049 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001050{
1051 int err;
1052
David Gibsonc53864f2014-04-24 10:22:36 +10001053 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1054 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001055 return 0;
1056
1057 err = rtnl_port_self_fill(skb, dev);
1058 if (err)
1059 return err;
1060
1061 if (dev_num_vf(dev->dev.parent)) {
1062 err = rtnl_vf_ports_fill(skb, dev);
1063 if (err)
1064 return err;
1065 }
1066
1067 return 0;
1068}
1069
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001070static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1071{
1072 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001073 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001074
1075 err = dev_get_phys_port_id(dev, &ppid);
1076 if (err) {
1077 if (err == -EOPNOTSUPP)
1078 return 0;
1079 return err;
1080 }
1081
1082 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1083 return -EMSGSIZE;
1084
1085 return 0;
1086}
1087
David Aherndb24a902015-03-17 20:23:15 -06001088static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1089{
1090 char name[IFNAMSIZ];
1091 int err;
1092
1093 err = dev_get_phys_port_name(dev, name, sizeof(name));
1094 if (err) {
1095 if (err == -EOPNOTSUPP)
1096 return 0;
1097 return err;
1098 }
1099
Michal Schmidt77ef0332017-05-04 16:48:58 +02001100 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001101 return -EMSGSIZE;
1102
1103 return 0;
1104}
1105
Jiri Pirko82f28412014-11-28 14:34:18 +01001106static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1107{
1108 int err;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001109 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +01001110 .orig_dev = dev,
Jiri Pirko1f868392015-10-01 11:03:42 +02001111 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001112 .flags = SWITCHDEV_F_NO_RECURSE,
1113 };
Jiri Pirko82f28412014-11-28 14:34:18 +01001114
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001115 err = switchdev_port_attr_get(dev, &attr);
Jiri Pirko82f28412014-11-28 14:34:18 +01001116 if (err) {
1117 if (err == -EOPNOTSUPP)
1118 return 0;
1119 return err;
1120 }
1121
Scott Feldman42275bd2015-05-13 11:16:50 -07001122 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1123 attr.u.ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001124 return -EMSGSIZE;
1125
1126 return 0;
1127}
1128
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001129static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1130 struct net_device *dev)
1131{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001132 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001133 struct nlattr *attr;
1134
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001135 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1136 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001137 if (!attr)
1138 return -EMSGSIZE;
1139
Roopa Prabhu550bce52016-04-15 20:36:25 -07001140 sp = nla_data(attr);
1141 dev_get_stats(dev, sp);
1142
1143 attr = nla_reserve(skb, IFLA_STATS,
1144 sizeof(struct rtnl_link_stats));
1145 if (!attr)
1146 return -EMSGSIZE;
1147
1148 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001149
1150 return 0;
1151}
1152
1153static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1154 struct net_device *dev,
1155 int vfs_num,
1156 struct nlattr *vfinfo)
1157{
1158 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001159 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001160 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001161 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001162 struct ifla_vf_spoofchk vf_spoofchk;
1163 struct ifla_vf_tx_rate vf_tx_rate;
1164 struct ifla_vf_stats vf_stats;
1165 struct ifla_vf_trust vf_trust;
1166 struct ifla_vf_vlan vf_vlan;
1167 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 struct ifla_vf_mac vf_mac;
1169 struct ifla_vf_info ivi;
1170
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001171 memset(&ivi, 0, sizeof(ivi));
1172
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001173 /* Not all SR-IOV capable drivers support the
1174 * spoofcheck and "RSS query enable" query. Preset to
1175 * -1 so the user space tool can detect that the driver
1176 * didn't report anything.
1177 */
1178 ivi.spoofchk = -1;
1179 ivi.rss_query_en = -1;
1180 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001181 /* The default value for VF link state is "auto"
1182 * IFLA_VF_LINK_STATE_AUTO which equals zero
1183 */
1184 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001185 /* VLAN Protocol by default is 802.1Q */
1186 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001187 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1188 return 0;
1189
Dan Carpenter775f4f02016-10-13 11:45:28 +03001190 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1191
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001192 vf_mac.vf =
1193 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001194 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001195 vf_rate.vf =
1196 vf_tx_rate.vf =
1197 vf_spoofchk.vf =
1198 vf_linkstate.vf =
1199 vf_rss_query_en.vf =
1200 vf_trust.vf = ivi.vf;
1201
1202 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1203 vf_vlan.vlan = ivi.vlan;
1204 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001205 vf_vlan_info.vlan = ivi.vlan;
1206 vf_vlan_info.qos = ivi.qos;
1207 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001208 vf_tx_rate.rate = ivi.max_tx_rate;
1209 vf_rate.min_tx_rate = ivi.min_tx_rate;
1210 vf_rate.max_tx_rate = ivi.max_tx_rate;
1211 vf_spoofchk.setting = ivi.spoofchk;
1212 vf_linkstate.link_state = ivi.linkstate;
1213 vf_rss_query_en.setting = ivi.rss_query_en;
1214 vf_trust.setting = ivi.trusted;
1215 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001216 if (!vf)
1217 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001218 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1219 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1220 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1221 &vf_rate) ||
1222 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1223 &vf_tx_rate) ||
1224 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1225 &vf_spoofchk) ||
1226 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1227 &vf_linkstate) ||
1228 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1229 sizeof(vf_rss_query_en),
1230 &vf_rss_query_en) ||
1231 nla_put(skb, IFLA_VF_TRUST,
1232 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001233 goto nla_put_vf_failure;
1234 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1235 if (!vfvlanlist)
1236 goto nla_put_vf_failure;
1237 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1238 &vf_vlan_info)) {
1239 nla_nest_cancel(skb, vfvlanlist);
1240 goto nla_put_vf_failure;
1241 }
1242 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001243 memset(&vf_stats, 0, sizeof(vf_stats));
1244 if (dev->netdev_ops->ndo_get_vf_stats)
1245 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1246 &vf_stats);
1247 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001248 if (!vfstats)
1249 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001250 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1251 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1252 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1253 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1254 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1255 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1256 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1257 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1258 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1259 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1260 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001261 vf_stats.multicast, IFLA_VF_STATS_PAD)) {
1262 nla_nest_cancel(skb, vfstats);
1263 goto nla_put_vf_failure;
1264 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001265 nla_nest_end(skb, vfstats);
1266 nla_nest_end(skb, vf);
1267 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001268
1269nla_put_vf_failure:
1270 nla_nest_cancel(skb, vf);
1271nla_put_vfinfo_failure:
1272 nla_nest_cancel(skb, vfinfo);
1273 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001274}
1275
Florian Westphal250fc3d2017-09-26 13:58:41 +02001276static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1277 struct net_device *dev,
1278 u32 ext_filter_mask)
1279{
1280 struct nlattr *vfinfo;
1281 int i, num_vfs;
1282
1283 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1284 return 0;
1285
1286 num_vfs = dev_num_vf(dev->dev.parent);
1287 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1288 return -EMSGSIZE;
1289
1290 if (!dev->netdev_ops->ndo_get_vf_config)
1291 return 0;
1292
1293 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1294 if (!vfinfo)
1295 return -EMSGSIZE;
1296
1297 for (i = 0; i < num_vfs; i++) {
1298 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1299 return -EMSGSIZE;
1300 }
1301
1302 nla_nest_end(skb, vfinfo);
1303 return 0;
1304}
1305
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001306static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1307{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001308 struct rtnl_link_ifmap map;
1309
1310 memset(&map, 0, sizeof(map));
1311 map.mem_start = dev->mem_start;
1312 map.mem_end = dev->mem_end;
1313 map.base_addr = dev->base_addr;
1314 map.irq = dev->irq;
1315 map.dma = dev->dma;
1316 map.port = dev->if_port;
1317
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001318 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001319 return -EMSGSIZE;
1320
1321 return 0;
1322}
1323
Martin KaFai Lau58038692017-06-15 17:29:09 -07001324static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001325{
1326 const struct net_device_ops *ops = dev->netdev_ops;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001327 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001328
1329 ASSERT_RTNL();
1330
Martin KaFai Lau58038692017-06-15 17:29:09 -07001331 *prog_id = 0;
1332 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1333 if (generic_xdp_prog) {
1334 *prog_id = generic_xdp_prog->aux->id;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001335 return XDP_ATTACHED_SKB;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001336 }
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001337 if (!ops->ndo_bpf)
Jakub Kicinskice158e52017-06-21 18:25:09 -07001338 return XDP_ATTACHED_NONE;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001339
Jakub Kicinskif4e63522017-11-03 13:56:16 -07001340 return __dev_xdp_attached(dev, ops->ndo_bpf, prog_id);
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001341}
1342
Brenden Blancod1fdd912016-07-19 12:16:49 -07001343static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1344{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001345 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001346 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001347 int err;
1348
Brenden Blancod1fdd912016-07-19 12:16:49 -07001349 xdp = nla_nest_start(skb, IFLA_XDP);
1350 if (!xdp)
1351 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001352
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001353 err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
Martin KaFai Lau58038692017-06-15 17:29:09 -07001354 rtnl_xdp_attached_mode(dev, &prog_id));
Brenden Blancod1fdd912016-07-19 12:16:49 -07001355 if (err)
1356 goto err_cancel;
1357
Martin KaFai Lau58038692017-06-15 17:29:09 -07001358 if (prog_id) {
1359 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1360 if (err)
1361 goto err_cancel;
1362 }
1363
Brenden Blancod1fdd912016-07-19 12:16:49 -07001364 nla_nest_end(skb, xdp);
1365 return 0;
1366
1367err_cancel:
1368 nla_nest_cancel(skb, xdp);
1369 return err;
1370}
1371
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001372static u32 rtnl_get_event(unsigned long event)
1373{
1374 u32 rtnl_event_type = IFLA_EVENT_NONE;
1375
1376 switch (event) {
1377 case NETDEV_REBOOT:
1378 rtnl_event_type = IFLA_EVENT_REBOOT;
1379 break;
1380 case NETDEV_FEAT_CHANGE:
1381 rtnl_event_type = IFLA_EVENT_FEATURES;
1382 break;
1383 case NETDEV_BONDING_FAILOVER:
1384 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1385 break;
1386 case NETDEV_NOTIFY_PEERS:
1387 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1388 break;
1389 case NETDEV_RESEND_IGMP:
1390 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1391 break;
1392 case NETDEV_CHANGEINFODATA:
1393 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1394 break;
1395 default:
1396 break;
1397 }
1398
1399 return rtnl_event_type;
1400}
1401
Florian Westphal79110a02017-09-26 13:58:40 +02001402static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1403{
1404 const struct net_device *upper_dev;
1405 int ret = 0;
1406
1407 rcu_read_lock();
1408
1409 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1410 if (upper_dev)
1411 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1412
1413 rcu_read_unlock();
1414 return ret;
1415}
1416
1417static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1418{
1419 int ifindex = dev_get_iflink(dev);
1420
1421 if (dev->ifindex == ifindex)
1422 return 0;
1423
1424 return nla_put_u32(skb, IFLA_LINK, ifindex);
1425}
1426
Florian Westphal6c557002017-10-02 23:50:05 +02001427static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1428 struct net_device *dev)
1429{
1430 char buf[IFALIASZ];
1431 int ret;
1432
1433 ret = dev_get_alias(dev, buf, sizeof(buf));
1434 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1435}
1436
Florian Westphalb1e66b92017-09-26 13:58:42 +02001437static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001438 const struct net_device *dev,
1439 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001440{
1441 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1442 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1443
1444 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001445 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001446
1447 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1448 return -EMSGSIZE;
1449 }
1450 }
1451
1452 return 0;
1453}
1454
Florian Westphal070cbf52017-10-16 15:44:35 +02001455static int rtnl_fill_link_af(struct sk_buff *skb,
1456 const struct net_device *dev,
1457 u32 ext_filter_mask)
1458{
1459 const struct rtnl_af_ops *af_ops;
1460 struct nlattr *af_spec;
1461
1462 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1463 if (!af_spec)
1464 return -EMSGSIZE;
1465
Florian Westphal5fa85a02017-10-16 15:44:36 +02001466 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001467 struct nlattr *af;
1468 int err;
1469
1470 if (!af_ops->fill_link_af)
1471 continue;
1472
1473 af = nla_nest_start(skb, af_ops->family);
1474 if (!af)
1475 return -EMSGSIZE;
1476
1477 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1478 /*
1479 * Caller may return ENODATA to indicate that there
1480 * was no data to be dumped. This is not an error, it
1481 * means we should trim the attribute header and
1482 * continue.
1483 */
1484 if (err == -ENODATA)
1485 nla_nest_cancel(skb, af);
1486 else if (err < 0)
1487 return -EMSGSIZE;
1488
1489 nla_nest_end(skb, af);
1490 }
1491
1492 nla_nest_end(skb, af_spec);
1493 return 0;
1494}
1495
Jiri Benc79e1ad12017-11-02 17:04:38 -02001496static int rtnl_fill_ifinfo(struct sk_buff *skb,
1497 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001498 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001499 unsigned int flags, u32 ext_filter_mask,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001500 u32 event, int *new_nsid, int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001501{
1502 struct ifinfomsg *ifm;
1503 struct nlmsghdr *nlh;
1504
Eric Dumazet2907c352011-05-25 07:34:04 +00001505 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001506 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1507 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001508 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001509
1510 ifm = nlmsg_data(nlh);
1511 ifm->ifi_family = AF_UNSPEC;
1512 ifm->__ifi_pad = 0;
1513 ifm->ifi_type = dev->type;
1514 ifm->ifi_index = dev->ifindex;
1515 ifm->ifi_flags = dev_get_flags(dev);
1516 ifm->ifi_change = change;
1517
Jiri Benc79e1ad12017-11-02 17:04:38 -02001518 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1519 goto nla_put_failure;
1520
David S. Millera6574342012-04-01 20:12:00 -04001521 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1522 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1523 nla_put_u8(skb, IFLA_OPERSTATE,
1524 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1525 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1526 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1527 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001528 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001529 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001530 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1531 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001532#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001533 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001534#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001535 nla_put_iflink(skb, dev) ||
1536 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001537 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001538 (dev->qdisc &&
1539 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001540 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001541 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001542 atomic_read(&dev->carrier_changes)) ||
1543 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
David S. Millera6574342012-04-01 20:12:00 -04001544 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001545
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001546 if (event != IFLA_EVENT_NONE) {
1547 if (nla_put_u32(skb, IFLA_EVENT, event))
1548 goto nla_put_failure;
1549 }
1550
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001551 if (rtnl_fill_link_ifmap(skb, dev))
1552 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001555 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1556 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1557 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001560 if (rtnl_phys_port_id_fill(skb, dev))
1561 goto nla_put_failure;
1562
David Aherndb24a902015-03-17 20:23:15 -06001563 if (rtnl_phys_port_name_fill(skb, dev))
1564 goto nla_put_failure;
1565
Jiri Pirko82f28412014-11-28 14:34:18 +01001566 if (rtnl_phys_switch_id_fill(skb, dev))
1567 goto nla_put_failure;
1568
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001569 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001570 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Florian Westphal250fc3d2017-09-26 13:58:41 +02001572 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001573 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001574
David Gibsonc53864f2014-04-24 10:22:36 +10001575 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001576 goto nla_put_failure;
1577
Brenden Blancod1fdd912016-07-19 12:16:49 -07001578 if (rtnl_xdp_fill(skb, dev))
1579 goto nla_put_failure;
1580
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001581 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001582 if (rtnl_link_fill(skb, dev) < 0)
1583 goto nla_put_failure;
1584 }
1585
Jiri Benc79e1ad12017-11-02 17:04:38 -02001586 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001587 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001588
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001589 if (new_nsid &&
1590 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1591 goto nla_put_failure;
1592
Florian Westphal5fa85a02017-10-16 15:44:36 +02001593 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001594 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001595 goto nla_put_failure_rcu;
1596 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001597
Johannes Berg053c0952015-01-16 22:09:00 +01001598 nlmsg_end(skb, nlh);
1599 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001600
Florian Westphal5fa85a02017-10-16 15:44:36 +02001601nla_put_failure_rcu:
1602 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001603nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001604 nlmsg_cancel(skb, nlh);
1605 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Jiri Pirkof7b12602014-02-18 20:53:18 +01001608static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1609 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1610 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1611 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1612 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1613 [IFLA_MTU] = { .type = NLA_U32 },
1614 [IFLA_LINK] = { .type = NLA_U32 },
1615 [IFLA_MASTER] = { .type = NLA_U32 },
1616 [IFLA_CARRIER] = { .type = NLA_U8 },
1617 [IFLA_TXQLEN] = { .type = NLA_U32 },
1618 [IFLA_WEIGHT] = { .type = NLA_U32 },
1619 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1620 [IFLA_LINKMODE] = { .type = NLA_U8 },
1621 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1622 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1623 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001624 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1625 * allow 0-length string (needed to remove an alias).
1626 */
1627 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001628 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1629 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1630 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1631 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1632 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1633 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1634 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1635 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001636 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001637 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001638 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001639 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001640 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001641 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001642 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001643 [IFLA_GROUP] = { .type = NLA_U32 },
Jiri Benc79e1ad12017-11-02 17:04:38 -02001644 [IFLA_IF_NETNSID] = { .type = NLA_S32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001645};
1646
1647static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1648 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1649 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1650 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1651 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1652};
1653
Jiri Pirkof7b12602014-02-18 20:53:18 +01001654static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001655 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1656 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001657 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001658 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1659 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1660 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1661 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001662 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001663 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001664 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001665 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1666 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001667};
1668
Jiri Pirkof7b12602014-02-18 20:53:18 +01001669static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1670 [IFLA_PORT_VF] = { .type = NLA_U32 },
1671 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1672 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001673 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1674 .len = PORT_UUID_MAX },
1675 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1676 .len = PORT_UUID_MAX },
1677 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1678 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001679
1680 /* Unused, but we need to keep it here since user space could
1681 * fill it. It's also broken with regard to NLA_BINARY use in
1682 * combination with structs.
1683 */
1684 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1685 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001686};
1687
Brenden Blancod1fdd912016-07-19 12:16:49 -07001688static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1689 [IFLA_XDP_FD] = { .type = NLA_S32 },
1690 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001691 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001692 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001693};
1694
David Aherndc599f72016-02-02 08:17:07 -08001695static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1696{
1697 const struct rtnl_link_ops *ops = NULL;
1698 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1699
Johannes Bergfceb6432017-04-12 14:34:07 +02001700 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1701 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001702 return NULL;
1703
1704 if (linfo[IFLA_INFO_KIND]) {
1705 char kind[MODULE_NAME_LEN];
1706
1707 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1708 ops = rtnl_link_ops_get(kind);
1709 }
1710
1711 return ops;
1712}
1713
1714static bool link_master_filtered(struct net_device *dev, int master_idx)
1715{
1716 struct net_device *master;
1717
1718 if (!master_idx)
1719 return false;
1720
1721 master = netdev_master_upper_dev_get(dev);
1722 if (!master || master->ifindex != master_idx)
1723 return true;
1724
1725 return false;
1726}
1727
1728static bool link_kind_filtered(const struct net_device *dev,
1729 const struct rtnl_link_ops *kind_ops)
1730{
1731 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1732 return true;
1733
1734 return false;
1735}
1736
1737static bool link_dump_filtered(struct net_device *dev,
1738 int master_idx,
1739 const struct rtnl_link_ops *kind_ops)
1740{
1741 if (link_master_filtered(dev, master_idx) ||
1742 link_kind_filtered(dev, kind_ops))
1743 return true;
1744
1745 return false;
1746}
1747
Jiri Benc79e1ad12017-11-02 17:04:38 -02001748static struct net *get_target_net(struct sk_buff *skb, int netnsid)
1749{
1750 struct net *net;
1751
1752 net = get_net_ns_by_id(sock_net(skb->sk), netnsid);
1753 if (!net)
1754 return ERR_PTR(-EINVAL);
1755
1756 /* For now, the caller is required to have CAP_NET_ADMIN in
1757 * the user namespace owning the target net ns.
1758 */
1759 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1760 put_net(net);
1761 return ERR_PTR(-EACCES);
1762 }
1763 return net;
1764}
1765
Thomas Grafb60c5112006-08-04 23:05:34 -07001766static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001768 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001769 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001770 int h, s_h;
1771 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001773 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001774 struct nlattr *tb[IFLA_MAX+1];
1775 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001776 const struct rtnl_link_ops *kind_ops = NULL;
1777 unsigned int flags = NLM_F_MULTI;
1778 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001779 int netnsid = -1;
David Gibson973462b2014-04-24 10:22:35 +10001780 int err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001781 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001783 s_h = cb->args[0];
1784 s_idx = cb->args[1];
1785
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001786 /* A hack to preserve kernel<->userspace interface.
1787 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1788 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1789 * what iproute2 < v3.9.0 used.
1790 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1791 * attribute, its netlink message is shorter than struct ifinfomsg.
1792 */
1793 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1794 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1795
Johannes Bergfceb6432017-04-12 14:34:07 +02001796 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
1797 ifla_policy, NULL) >= 0) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001798 if (tb[IFLA_IF_NETNSID]) {
1799 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1800 tgt_net = get_target_net(skb, netnsid);
1801 if (IS_ERR(tgt_net)) {
1802 tgt_net = net;
1803 netnsid = -1;
1804 }
1805 }
1806
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001807 if (tb[IFLA_EXT_MASK])
1808 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
David Aherndc599f72016-02-02 08:17:07 -08001809
1810 if (tb[IFLA_MASTER])
1811 master_idx = nla_get_u32(tb[IFLA_MASTER]);
1812
1813 if (tb[IFLA_LINKINFO])
1814 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1815
1816 if (master_idx || kind_ops)
1817 flags |= NLM_F_DUMP_FILTERED;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001818 }
Greg Rose115c9b82012-02-21 16:54:48 -05001819
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001820 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1821 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001822 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001823 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001824 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001825 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001826 if (idx < s_idx)
1827 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001828 err = rtnl_fill_ifinfo(skb, dev, net,
1829 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001830 NETLINK_CB(cb->skb).portid,
1831 cb->nlh->nlmsg_seq, 0,
David Aherndc599f72016-02-02 08:17:07 -08001832 flags,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001833 ext_filter_mask, 0, NULL,
1834 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001835
David Ahernf6c57752017-05-15 23:19:17 -07001836 if (err < 0) {
1837 if (likely(skb->len))
1838 goto out;
1839
1840 goto out_err;
1841 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001842cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001843 idx++;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001846out:
David Ahernf6c57752017-05-15 23:19:17 -07001847 err = skb->len;
1848out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001849 cb->args[1] = idx;
1850 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02001851 cb->seq = net->dev_base_seq;
1852 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02001853 if (netnsid >= 0)
1854 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
David Ahernf6c57752017-05-15 23:19:17 -07001856 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Johannes Bergfceb6432017-04-12 14:34:07 +02001859int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
1860 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01001861{
Johannes Bergfceb6432017-04-12 14:34:07 +02001862 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001863}
1864EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07001865
Eric W. Biederman81adee42009-11-08 00:53:51 -08001866struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1867{
1868 struct net *net;
1869 /* Examine the link attributes and figure out which
1870 * network namespace we are talking about.
1871 */
1872 if (tb[IFLA_NET_NS_PID])
1873 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001874 else if (tb[IFLA_NET_NS_FD])
1875 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08001876 else
1877 net = get_net(src_net);
1878 return net;
1879}
1880EXPORT_SYMBOL(rtnl_link_get_net);
1881
Thomas Graf1840bb12008-02-23 19:54:36 -08001882static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1883{
1884 if (dev) {
1885 if (tb[IFLA_ADDRESS] &&
1886 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1887 return -EINVAL;
1888
1889 if (tb[IFLA_BROADCAST] &&
1890 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1891 return -EINVAL;
1892 }
1893
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001894 if (tb[IFLA_AF_SPEC]) {
1895 struct nlattr *af;
1896 int rem, err;
1897
1898 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1899 const struct rtnl_af_ops *af_ops;
1900
Florian Westphal5fa85a02017-10-16 15:44:36 +02001901 rcu_read_lock();
1902 af_ops = rtnl_af_lookup(nla_type(af));
1903 if (!af_ops) {
1904 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001905 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001906 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001907
Florian Westphal5fa85a02017-10-16 15:44:36 +02001908 if (!af_ops->set_link_af) {
1909 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001910 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001911 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001912
1913 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00001914 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02001915 if (err < 0) {
1916 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001917 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02001918 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001919 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02001920
1921 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00001922 }
1923 }
1924
Thomas Graf1840bb12008-02-23 19:54:36 -08001925 return 0;
1926}
1927
Eli Cohencc8e27c2016-03-11 22:58:34 +02001928static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1929 int guid_type)
1930{
1931 const struct net_device_ops *ops = dev->netdev_ops;
1932
1933 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1934}
1935
1936static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1937{
1938 if (dev->type != ARPHRD_INFINIBAND)
1939 return -EOPNOTSUPP;
1940
1941 return handle_infiniband_guid(dev, ivt, guid_type);
1942}
1943
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001944static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07001945{
Chris Wrightc02db8c2010-05-16 01:05:45 -07001946 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001947 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001948
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001949 if (tb[IFLA_VF_MAC]) {
1950 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001951
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001952 err = -EOPNOTSUPP;
1953 if (ops->ndo_set_vf_mac)
1954 err = ops->ndo_set_vf_mac(dev, ivm->vf,
1955 ivm->mac);
1956 if (err < 0)
1957 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07001958 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001959
1960 if (tb[IFLA_VF_VLAN]) {
1961 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1962
1963 err = -EOPNOTSUPP;
1964 if (ops->ndo_set_vf_vlan)
1965 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03001966 ivv->qos,
1967 htons(ETH_P_8021Q));
1968 if (err < 0)
1969 return err;
1970 }
1971
1972 if (tb[IFLA_VF_VLAN_LIST]) {
1973 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
1974 struct nlattr *attr;
1975 int rem, len = 0;
1976
1977 err = -EOPNOTSUPP;
1978 if (!ops->ndo_set_vf_vlan)
1979 return err;
1980
1981 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
1982 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
1983 nla_len(attr) < NLA_HDRLEN) {
1984 return -EINVAL;
1985 }
1986 if (len >= MAX_VLAN_LIST_LEN)
1987 return -EOPNOTSUPP;
1988 ivvl[len] = nla_data(attr);
1989
1990 len++;
1991 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02001992 if (len == 0)
1993 return -EINVAL;
1994
Moshe Shemesh79aab092016-09-22 12:11:15 +03001995 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
1996 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02001997 if (err < 0)
1998 return err;
1999 }
2000
2001 if (tb[IFLA_VF_TX_RATE]) {
2002 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2003 struct ifla_vf_info ivf;
2004
2005 err = -EOPNOTSUPP;
2006 if (ops->ndo_get_vf_config)
2007 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2008 if (err < 0)
2009 return err;
2010
2011 err = -EOPNOTSUPP;
2012 if (ops->ndo_set_vf_rate)
2013 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2014 ivf.min_tx_rate,
2015 ivt->rate);
2016 if (err < 0)
2017 return err;
2018 }
2019
2020 if (tb[IFLA_VF_RATE]) {
2021 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2022
2023 err = -EOPNOTSUPP;
2024 if (ops->ndo_set_vf_rate)
2025 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2026 ivt->min_tx_rate,
2027 ivt->max_tx_rate);
2028 if (err < 0)
2029 return err;
2030 }
2031
2032 if (tb[IFLA_VF_SPOOFCHK]) {
2033 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2034
2035 err = -EOPNOTSUPP;
2036 if (ops->ndo_set_vf_spoofchk)
2037 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2038 ivs->setting);
2039 if (err < 0)
2040 return err;
2041 }
2042
2043 if (tb[IFLA_VF_LINK_STATE]) {
2044 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2045
2046 err = -EOPNOTSUPP;
2047 if (ops->ndo_set_vf_link_state)
2048 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2049 ivl->link_state);
2050 if (err < 0)
2051 return err;
2052 }
2053
2054 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2055 struct ifla_vf_rss_query_en *ivrssq_en;
2056
2057 err = -EOPNOTSUPP;
2058 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2059 if (ops->ndo_set_vf_rss_query_en)
2060 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2061 ivrssq_en->setting);
2062 if (err < 0)
2063 return err;
2064 }
2065
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002066 if (tb[IFLA_VF_TRUST]) {
2067 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2068
2069 err = -EOPNOTSUPP;
2070 if (ops->ndo_set_vf_trust)
2071 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2072 if (err < 0)
2073 return err;
2074 }
2075
Eli Cohencc8e27c2016-03-11 22:58:34 +02002076 if (tb[IFLA_VF_IB_NODE_GUID]) {
2077 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2078
2079 if (!ops->ndo_set_vf_guid)
2080 return -EOPNOTSUPP;
2081
2082 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2083 }
2084
2085 if (tb[IFLA_VF_IB_PORT_GUID]) {
2086 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2087
2088 if (!ops->ndo_set_vf_guid)
2089 return -EOPNOTSUPP;
2090
2091 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2092 }
2093
Chris Wrightc02db8c2010-05-16 01:05:45 -07002094 return err;
2095}
2096
David Ahern33eaf2a2017-10-04 17:48:46 -07002097static int do_set_master(struct net_device *dev, int ifindex,
2098 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002099{
Jiri Pirko898e5062013-01-03 22:48:52 +00002100 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002101 const struct net_device_ops *ops;
2102 int err;
2103
Jiri Pirko898e5062013-01-03 22:48:52 +00002104 if (upper_dev) {
2105 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002106 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002107 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002108 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002109 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002110 if (err)
2111 return err;
2112 } else {
2113 return -EOPNOTSUPP;
2114 }
2115 }
2116
2117 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002118 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2119 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002120 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002121 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002122 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002123 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002124 if (err)
2125 return err;
2126 } else {
2127 return -EOPNOTSUPP;
2128 }
2129 }
2130 return 0;
2131}
2132
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002133#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002134/* notify flag means notify + modified. */
2135#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002136static int do_setlink(const struct sk_buff *skb,
2137 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002138 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002139 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002140{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002141 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002142 int err;
2143
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002144 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
Eric W. Biederman81adee42009-11-08 00:53:51 -08002145 struct net *net = rtnl_link_get_net(dev_net(dev), tb);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002146 if (IS_ERR(net)) {
2147 err = PTR_ERR(net);
2148 goto errout;
2149 }
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002150 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
Nicolas Dichtele0ebde02014-11-27 10:16:15 +01002151 put_net(net);
Eric W. Biedermanb51642f2012-11-16 03:03:11 +00002152 err = -EPERM;
2153 goto errout;
2154 }
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002155 err = dev_change_net_namespace(dev, net, ifname);
2156 put_net(net);
2157 if (err)
2158 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002159 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002160 }
2161
Patrick McHardy0157f602007-06-13 12:03:36 -07002162 if (tb[IFLA_MAP]) {
2163 struct rtnl_link_ifmap *u_map;
2164 struct ifmap k_map;
2165
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002166 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002167 err = -EOPNOTSUPP;
2168 goto errout;
2169 }
2170
2171 if (!netif_device_present(dev)) {
2172 err = -ENODEV;
2173 goto errout;
2174 }
2175
2176 u_map = nla_data(tb[IFLA_MAP]);
2177 k_map.mem_start = (unsigned long) u_map->mem_start;
2178 k_map.mem_end = (unsigned long) u_map->mem_end;
2179 k_map.base_addr = (unsigned short) u_map->base_addr;
2180 k_map.irq = (unsigned char) u_map->irq;
2181 k_map.dma = (unsigned char) u_map->dma;
2182 k_map.port = (unsigned char) u_map->port;
2183
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002184 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002185 if (err < 0)
2186 goto errout;
2187
Nicolas Dichtelba998902014-09-01 16:07:29 +02002188 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002189 }
2190
2191 if (tb[IFLA_ADDRESS]) {
2192 struct sockaddr *sa;
2193 int len;
2194
WANG Cong153711f2017-07-20 11:27:57 -07002195 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2196 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002197 sa = kmalloc(len, GFP_KERNEL);
2198 if (!sa) {
2199 err = -ENOMEM;
2200 goto errout;
2201 }
2202 sa->sa_family = dev->type;
2203 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2204 dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002205 err = dev_set_mac_address(dev, sa);
Patrick McHardy0157f602007-06-13 12:03:36 -07002206 kfree(sa);
2207 if (err)
2208 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002209 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002210 }
2211
2212 if (tb[IFLA_MTU]) {
2213 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
2214 if (err < 0)
2215 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002216 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002217 }
2218
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002219 if (tb[IFLA_GROUP]) {
2220 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002221 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002222 }
2223
Patrick McHardy0157f602007-06-13 12:03:36 -07002224 /*
2225 * Interface selected by interface index but interface
2226 * name provided implies that a name change has been
2227 * requested.
2228 */
2229 if (ifm->ifi_index > 0 && ifname[0]) {
2230 err = dev_change_name(dev, ifname);
2231 if (err < 0)
2232 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002233 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002234 }
2235
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002236 if (tb[IFLA_IFALIAS]) {
2237 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2238 nla_len(tb[IFLA_IFALIAS]));
2239 if (err < 0)
2240 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002241 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002242 }
2243
Patrick McHardy0157f602007-06-13 12:03:36 -07002244 if (tb[IFLA_BROADCAST]) {
2245 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002246 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002247 }
2248
2249 if (ifm->ifi_flags || ifm->ifi_change) {
Patrick McHardy3729d502010-02-26 06:34:54 +00002250 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
Johannes Berg5f9021c2008-11-16 23:20:31 -08002251 if (err < 0)
2252 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002253 }
2254
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002255 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002256 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002257 if (err)
2258 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002259 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002260 }
2261
Jiri Pirko9a572472012-12-27 23:49:39 +00002262 if (tb[IFLA_CARRIER]) {
2263 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2264 if (err)
2265 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002266 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002267 }
2268
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002269 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002270 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2271 unsigned int orig_len = dev->tx_queue_len;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002272
Jason Wang08294a22016-06-30 14:45:35 +08002273 if (dev->tx_queue_len ^ value) {
2274 dev->tx_queue_len = value;
2275 err = call_netdevice_notifiers(
2276 NETDEV_CHANGE_TX_QUEUE_LEN, dev);
2277 err = notifier_to_errno(err);
2278 if (err) {
2279 dev->tx_queue_len = orig_len;
2280 goto errout;
2281 }
Xin Long2d7f6692017-10-15 18:13:46 +08002282 status |= DO_SETLINK_MODIFIED;
Jason Wang08294a22016-06-30 14:45:35 +08002283 }
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002284 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002285
Patrick McHardy0157f602007-06-13 12:03:36 -07002286 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002287 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002288
2289 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002290 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2291
David S. Miller93b2d4a2008-02-17 18:35:07 -08002292 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002293 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002294 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002295 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002296 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002297 }
2298
Chris Wrightc02db8c2010-05-16 01:05:45 -07002299 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002300 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002301 struct nlattr *attr;
2302 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002303
Chris Wrightc02db8c2010-05-16 01:05:45 -07002304 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002305 if (nla_type(attr) != IFLA_VF_INFO ||
2306 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002307 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002308 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002309 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002310 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002311 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002312 if (err < 0)
2313 goto errout;
2314 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002315 if (err < 0)
2316 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002317 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002318 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002319 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002320 err = 0;
2321
Scott Feldman57b61082010-05-17 22:49:55 -07002322 if (tb[IFLA_VF_PORTS]) {
2323 struct nlattr *port[IFLA_PORT_MAX+1];
2324 struct nlattr *attr;
2325 int vf;
2326 int rem;
2327
2328 err = -EOPNOTSUPP;
2329 if (!ops->ndo_set_vf_port)
2330 goto errout;
2331
2332 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002333 if (nla_type(attr) != IFLA_VF_PORT ||
2334 nla_len(attr) < NLA_HDRLEN) {
2335 err = -EINVAL;
2336 goto errout;
2337 }
2338 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002339 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002340 if (err < 0)
2341 goto errout;
2342 if (!port[IFLA_PORT_VF]) {
2343 err = -EOPNOTSUPP;
2344 goto errout;
2345 }
2346 vf = nla_get_u32(port[IFLA_PORT_VF]);
2347 err = ops->ndo_set_vf_port(dev, vf, port);
2348 if (err < 0)
2349 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002350 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002351 }
2352 }
2353 err = 0;
2354
2355 if (tb[IFLA_PORT_SELF]) {
2356 struct nlattr *port[IFLA_PORT_MAX+1];
2357
2358 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002359 tb[IFLA_PORT_SELF], ifla_port_policy,
2360 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002361 if (err < 0)
2362 goto errout;
2363
2364 err = -EOPNOTSUPP;
2365 if (ops->ndo_set_vf_port)
2366 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2367 if (err < 0)
2368 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002369 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002370 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002371
2372 if (tb[IFLA_AF_SPEC]) {
2373 struct nlattr *af;
2374 int rem;
2375
2376 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2377 const struct rtnl_af_ops *af_ops;
2378
Florian Westphal5fa85a02017-10-16 15:44:36 +02002379 rcu_read_lock();
2380
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002381 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002382
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002383 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002384 if (err < 0) {
2385 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002386 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002387 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002388
Florian Westphal5fa85a02017-10-16 15:44:36 +02002389 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002390 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002391 }
2392 }
Scott Feldman57b61082010-05-17 22:49:55 -07002393 err = 0;
2394
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002395 if (tb[IFLA_PROTO_DOWN]) {
2396 err = dev_change_proto_down(dev,
2397 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2398 if (err)
2399 goto errout;
2400 status |= DO_SETLINK_NOTIFY;
2401 }
2402
Brenden Blancod1fdd912016-07-19 12:16:49 -07002403 if (tb[IFLA_XDP]) {
2404 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002405 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002406
2407 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002408 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002409 if (err < 0)
2410 goto errout;
2411
Martin KaFai Lau58038692017-06-15 17:29:09 -07002412 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002413 err = -EINVAL;
2414 goto errout;
2415 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002416
2417 if (xdp[IFLA_XDP_FLAGS]) {
2418 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2419 if (xdp_flags & ~XDP_FLAGS_MASK) {
2420 err = -EINVAL;
2421 goto errout;
2422 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002423 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002424 err = -EINVAL;
2425 goto errout;
2426 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002427 }
2428
Brenden Blancod1fdd912016-07-19 12:16:49 -07002429 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002430 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002431 nla_get_s32(xdp[IFLA_XDP_FD]),
2432 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002433 if (err)
2434 goto errout;
2435 status |= DO_SETLINK_NOTIFY;
2436 }
2437 }
2438
Patrick McHardy0157f602007-06-13 12:03:36 -07002439errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002440 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002441 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002442 netdev_state_change(dev);
2443
2444 if (err < 0)
2445 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",
2446 dev->name);
2447 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002448
Patrick McHardy0157f602007-06-13 12:03:36 -07002449 return err;
2450}
2451
David Ahernc21ef3e2017-04-16 09:48:24 -07002452static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2453 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002455 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002456 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002458 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002459 struct nlattr *tb[IFLA_MAX+1];
2460 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
David Ahernc21ef3e2017-04-16 09:48:24 -07002462 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2463 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002464 if (err < 0)
2465 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Jiri Benc79e1ad12017-11-02 17:04:38 -02002467 if (tb[IFLA_IF_NETNSID])
2468 return -EOPNOTSUPP;
2469
Thomas Graf5176f912006-08-26 20:13:18 -07002470 if (tb[IFLA_IFNAME])
2471 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002472 else
2473 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002476 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002477 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002478 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002479 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002480 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002481 else
2482 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Thomas Grafda5e0492006-08-10 21:17:37 -07002484 if (dev == NULL) {
2485 err = -ENODEV;
2486 goto errout;
2487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Eric Dumazete0d087a2009-11-07 01:26:17 -08002489 err = validate_linkmsg(dev, tb);
2490 if (err < 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002491 goto errout;
Thomas Grafda5e0492006-08-10 21:17:37 -07002492
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002493 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002494errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return err;
2496}
2497
WANG Cong66400d52015-03-24 11:53:31 -07002498static int rtnl_group_dellink(const struct net *net, int group)
2499{
2500 struct net_device *dev, *aux;
2501 LIST_HEAD(list_kill);
2502 bool found = false;
2503
2504 if (!group)
2505 return -EPERM;
2506
2507 for_each_netdev(net, dev) {
2508 if (dev->group == group) {
2509 const struct rtnl_link_ops *ops;
2510
2511 found = true;
2512 ops = dev->rtnl_link_ops;
2513 if (!ops || !ops->dellink)
2514 return -EOPNOTSUPP;
2515 }
2516 }
2517
2518 if (!found)
2519 return -ENODEV;
2520
2521 for_each_netdev_safe(net, dev, aux) {
2522 if (dev->group == group) {
2523 const struct rtnl_link_ops *ops;
2524
2525 ops = dev->rtnl_link_ops;
2526 ops->dellink(dev, &list_kill);
2527 }
2528 }
2529 unregister_netdevice_many(&list_kill);
2530
2531 return 0;
2532}
2533
Thomas Graf614732e2015-07-21 10:44:06 +02002534int rtnl_delete_link(struct net_device *dev)
2535{
2536 const struct rtnl_link_ops *ops;
2537 LIST_HEAD(list_kill);
2538
2539 ops = dev->rtnl_link_ops;
2540 if (!ops || !ops->dellink)
2541 return -EOPNOTSUPP;
2542
2543 ops->dellink(dev, &list_kill);
2544 unregister_netdevice_many(&list_kill);
2545
2546 return 0;
2547}
2548EXPORT_SYMBOL_GPL(rtnl_delete_link);
2549
David Ahernc21ef3e2017-04-16 09:48:24 -07002550static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2551 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002552{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002553 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002554 struct net_device *dev;
2555 struct ifinfomsg *ifm;
2556 char ifname[IFNAMSIZ];
2557 struct nlattr *tb[IFLA_MAX+1];
2558 int err;
2559
David Ahernc21ef3e2017-04-16 09:48:24 -07002560 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002561 if (err < 0)
2562 return err;
2563
Jiri Benc79e1ad12017-11-02 17:04:38 -02002564 if (tb[IFLA_IF_NETNSID])
2565 return -EOPNOTSUPP;
2566
Patrick McHardy38f7b872007-06-13 12:03:51 -07002567 if (tb[IFLA_IFNAME])
2568 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2569
2570 ifm = nlmsg_data(nlh);
2571 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002572 dev = __dev_get_by_index(net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002573 else if (tb[IFLA_IFNAME])
Eric W. Biederman881d9662007-09-17 11:56:21 -07002574 dev = __dev_get_by_name(net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002575 else if (tb[IFLA_GROUP])
2576 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002577 else
2578 return -EINVAL;
2579
2580 if (!dev)
2581 return -ENODEV;
2582
Thomas Graf614732e2015-07-21 10:44:06 +02002583 return rtnl_delete_link(dev);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002584}
2585
Patrick McHardy3729d502010-02-26 06:34:54 +00002586int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2587{
2588 unsigned int old_flags;
2589 int err;
2590
2591 old_flags = dev->flags;
2592 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2593 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2594 if (err < 0)
2595 return err;
2596 }
2597
2598 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
Patrick McHardy3729d502010-02-26 06:34:54 +00002599
Nicolas Dichtela528c212013-09-25 12:02:44 +02002600 __dev_notify_flags(dev, old_flags, ~0U);
Patrick McHardy3729d502010-02-26 06:34:54 +00002601 return 0;
2602}
2603EXPORT_SYMBOL(rtnl_configure_link);
2604
Rami Rosenc0713562012-11-30 01:08:47 +00002605struct net_device *rtnl_create_link(struct net *net,
Thomas Graf78ebb0d2015-04-10 01:45:53 +02002606 const char *ifname, unsigned char name_assign_type,
Tom Gundersen55177502014-07-14 16:37:25 +02002607 const struct rtnl_link_ops *ops, struct nlattr *tb[])
Pavel Emelianove7199282007-08-08 22:16:38 -07002608{
Pavel Emelianove7199282007-08-08 22:16:38 -07002609 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002610 unsigned int num_tx_queues = 1;
2611 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002612
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002613 if (tb[IFLA_NUM_TX_QUEUES])
2614 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2615 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002616 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002617
2618 if (tb[IFLA_NUM_RX_QUEUES])
2619 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2620 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002621 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002622
Tom Gundersen55177502014-07-14 16:37:25 +02002623 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002624 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002625 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002626 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002627
Eric W. Biederman81adee42009-11-08 00:53:51 -08002628 dev_net_set(dev, net);
2629 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002630 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002631
Pavel Emelianove7199282007-08-08 22:16:38 -07002632 if (tb[IFLA_MTU])
2633 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002634 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002635 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2636 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002637 dev->addr_assign_type = NET_ADDR_SET;
2638 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002639 if (tb[IFLA_BROADCAST])
2640 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2641 nla_len(tb[IFLA_BROADCAST]));
2642 if (tb[IFLA_TXQLEN])
2643 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2644 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002645 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002646 if (tb[IFLA_LINKMODE])
2647 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002648 if (tb[IFLA_GROUP])
2649 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002650
2651 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002652}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002653EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002654
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002655static int rtnl_group_changelink(const struct sk_buff *skb,
2656 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002657 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002658 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002659 struct nlattr **tb)
2660{
WANG Congd0795352015-03-23 16:31:09 -07002661 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002662 int err;
2663
WANG Congd0795352015-03-23 16:31:09 -07002664 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002665 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002666 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002667 if (err < 0)
2668 return err;
2669 }
2670 }
2671
2672 return 0;
2673}
2674
David Ahernc21ef3e2017-04-16 09:48:24 -07002675static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2676 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002677{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002678 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002679 const struct rtnl_link_ops *ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002680 const struct rtnl_link_ops *m_ops = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002681 struct net_device *dev;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002682 struct net_device *master_dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002683 struct ifinfomsg *ifm;
2684 char kind[MODULE_NAME_LEN];
2685 char ifname[IFNAMSIZ];
2686 struct nlattr *tb[IFLA_MAX+1];
2687 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
Tom Gundersen55177502014-07-14 16:37:25 +02002688 unsigned char name_assign_type = NET_NAME_USER;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002689 int err;
2690
Johannes Berg95a5afc2008-10-16 15:24:51 -07002691#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002692replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002693#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002694 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002695 if (err < 0)
2696 return err;
2697
Jiri Benc79e1ad12017-11-02 17:04:38 -02002698 if (tb[IFLA_IF_NETNSID])
2699 return -EOPNOTSUPP;
2700
Patrick McHardy38f7b872007-06-13 12:03:51 -07002701 if (tb[IFLA_IFNAME])
2702 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2703 else
2704 ifname[0] = '\0';
2705
2706 ifm = nlmsg_data(nlh);
2707 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07002708 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002709 else {
2710 if (ifname[0])
2711 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002712 else
2713 dev = NULL;
2714 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002715
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002716 if (dev) {
2717 master_dev = netdev_master_upper_dev_get(dev);
2718 if (master_dev)
2719 m_ops = master_dev->rtnl_link_ops;
2720 }
2721
Eric Dumazete0d087a2009-11-07 01:26:17 -08002722 err = validate_linkmsg(dev, tb);
2723 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08002724 return err;
2725
Patrick McHardy38f7b872007-06-13 12:03:51 -07002726 if (tb[IFLA_LINKINFO]) {
2727 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002728 tb[IFLA_LINKINFO], ifla_info_policy,
2729 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002730 if (err < 0)
2731 return err;
2732 } else
2733 memset(linkinfo, 0, sizeof(linkinfo));
2734
2735 if (linkinfo[IFLA_INFO_KIND]) {
2736 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2737 ops = rtnl_link_ops_get(kind);
2738 } else {
2739 kind[0] = '\0';
2740 ops = NULL;
2741 }
2742
2743 if (1) {
Sasha Levin4e10fd52015-02-24 14:14:35 -05002744 struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2745 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002746 struct nlattr **data = NULL;
2747 struct nlattr **slave_data = NULL;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002748 struct net *dest_net, *link_net = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002749
2750 if (ops) {
2751 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2752 err = nla_parse_nested(attr, ops->maxtype,
2753 linkinfo[IFLA_INFO_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002754 ops->policy, NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002755 if (err < 0)
2756 return err;
2757 data = attr;
2758 }
2759 if (ops->validate) {
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02002760 err = ops->validate(tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002761 if (err < 0)
2762 return err;
2763 }
2764 }
2765
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002766 if (m_ops) {
2767 if (m_ops->slave_maxtype &&
2768 linkinfo[IFLA_INFO_SLAVE_DATA]) {
2769 err = nla_parse_nested(slave_attr,
2770 m_ops->slave_maxtype,
2771 linkinfo[IFLA_INFO_SLAVE_DATA],
Johannes Bergfceb6432017-04-12 14:34:07 +02002772 m_ops->slave_policy,
2773 NULL);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002774 if (err < 0)
2775 return err;
2776 slave_data = slave_attr;
2777 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002778 }
2779
Patrick McHardy38f7b872007-06-13 12:03:51 -07002780 if (dev) {
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002781 int status = 0;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002782
2783 if (nlh->nlmsg_flags & NLM_F_EXCL)
2784 return -EEXIST;
2785 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2786 return -EOPNOTSUPP;
2787
2788 if (linkinfo[IFLA_INFO_DATA]) {
2789 if (!ops || ops != dev->rtnl_link_ops ||
2790 !ops->changelink)
2791 return -EOPNOTSUPP;
2792
Matthias Schifferad744b22017-06-25 23:56:00 +02002793 err = ops->changelink(dev, tb, data, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002794 if (err < 0)
2795 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002796 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002797 }
2798
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002799 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2800 if (!m_ops || !m_ops->slave_changelink)
2801 return -EOPNOTSUPP;
2802
2803 err = m_ops->slave_changelink(master_dev, dev,
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02002804 tb, slave_data,
2805 extack);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002806 if (err < 0)
2807 return err;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002808 status |= DO_SETLINK_NOTIFY;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01002809 }
2810
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002811 return do_setlink(skb, dev, ifm, extack, tb, ifname,
2812 status);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002813 }
2814
Patrick McHardyffa934f2011-01-20 03:00:42 +00002815 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2816 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002817 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00002818 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002819 ifm, extack, tb);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002820 return -ENODEV;
Patrick McHardyffa934f2011-01-20 03:00:42 +00002821 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002822
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002823 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
Patrick McHardy38f7b872007-06-13 12:03:51 -07002824 return -EOPNOTSUPP;
2825
2826 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07002827#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002828 if (kind[0]) {
2829 __rtnl_unlock();
2830 request_module("rtnl-link-%s", kind);
2831 rtnl_lock();
2832 ops = rtnl_link_ops_get(kind);
2833 if (ops)
2834 goto replay;
2835 }
2836#endif
2837 return -EOPNOTSUPP;
2838 }
2839
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02002840 if (!ops->setup)
2841 return -EOPNOTSUPP;
2842
Tom Gundersen55177502014-07-14 16:37:25 +02002843 if (!ifname[0]) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07002844 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
Tom Gundersen55177502014-07-14 16:37:25 +02002845 name_assign_type = NET_NAME_ENUM;
2846 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07002847
Eric W. Biederman81adee42009-11-08 00:53:51 -08002848 dest_net = rtnl_link_get_net(net, tb);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00002849 if (IS_ERR(dest_net))
2850 return PTR_ERR(dest_net);
2851
Eric W. Biederman505ce412015-02-26 16:19:00 -06002852 err = -EPERM;
2853 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2854 goto out;
2855
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002856 if (tb[IFLA_LINK_NETNSID]) {
2857 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2858
2859 link_net = get_net_ns_by_id(dest_net, id);
2860 if (!link_net) {
2861 err = -EINVAL;
2862 goto out;
2863 }
Eric W. Biederman06615be2015-02-26 16:20:07 -06002864 err = -EPERM;
2865 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2866 goto out;
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002867 }
2868
2869 dev = rtnl_create_link(link_net ? : dest_net, ifname,
2870 name_assign_type, ops, tb);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002871 if (IS_ERR(dev)) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002872 err = PTR_ERR(dev);
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00002873 goto out;
2874 }
2875
2876 dev->ifindex = ifm->ifi_index;
2877
Cong Wang0e0eee22014-02-11 15:51:30 -08002878 if (ops->newlink) {
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02002879 err = ops->newlink(link_net ? : net, dev, tb, data,
2880 extack);
Cong Wang0e0eee22014-02-11 15:51:30 -08002881 /* Drivers should call free_netdev() in ->destructor
Cong Wange51fb152014-06-03 16:40:47 -07002882 * and unregister it on failure after registration
2883 * so that device could be finally freed in rtnl_unlock.
Cong Wang0e0eee22014-02-11 15:51:30 -08002884 */
Cong Wange51fb152014-06-03 16:40:47 -07002885 if (err < 0) {
2886 /* If device is not registered at all, free it now */
2887 if (dev->reg_state == NETREG_UNINITIALIZED)
2888 free_netdev(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002889 goto out;
Cong Wange51fb152014-06-03 16:40:47 -07002890 }
Cong Wang0e0eee22014-02-11 15:51:30 -08002891 } else {
Patrick McHardy2d85cba2007-07-11 19:42:13 -07002892 err = register_netdevice(dev);
Cong Wang0e0eee22014-02-11 15:51:30 -08002893 if (err < 0) {
2894 free_netdev(dev);
2895 goto out;
2896 }
Dan Carpenterfce9b9b2013-08-14 12:35:42 +03002897 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002898 err = rtnl_configure_link(dev, ifm);
David S. Miller43638902015-03-10 21:58:32 -04002899 if (err < 0)
2900 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002901 if (link_net) {
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002902 err = dev_change_net_namespace(dev, dest_net, ifname);
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002903 if (err < 0)
David S. Miller43638902015-03-10 21:58:32 -04002904 goto out_unregister;
Nicolas Dichtelbdef2792015-01-20 15:15:42 +01002905 }
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002906 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002907 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]),
2908 extack);
Theuns Verwoerd160ca012017-01-31 12:23:46 +13002909 if (err)
2910 goto out_unregister;
2911 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002912out:
Nicolas Dichtel317f4812015-01-15 15:11:18 +01002913 if (link_net)
2914 put_net(link_net);
Eric W. Biederman81adee42009-11-08 00:53:51 -08002915 put_net(dest_net);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002916 return err;
David S. Miller43638902015-03-10 21:58:32 -04002917out_unregister:
2918 if (ops->newlink) {
2919 LIST_HEAD(list_kill);
2920
2921 ops->dellink(dev, &list_kill);
2922 unregister_netdevice_many(&list_kill);
2923 } else {
2924 unregister_netdevice(dev);
2925 }
2926 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002927 }
2928}
2929
David Ahernc21ef3e2017-04-16 09:48:24 -07002930static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2931 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002932{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002933 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002934 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07002935 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002936 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07002937 struct nlattr *tb[IFLA_MAX+1];
2938 struct net_device *dev = NULL;
2939 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02002940 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08002941 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05002942 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002943
David Ahernc21ef3e2017-04-16 09:48:24 -07002944 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07002945 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07002946 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07002947
Jiri Benc79e1ad12017-11-02 17:04:38 -02002948 if (tb[IFLA_IF_NETNSID]) {
2949 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2950 tgt_net = get_target_net(skb, netnsid);
2951 if (IS_ERR(tgt_net))
2952 return PTR_ERR(tgt_net);
2953 }
2954
Eric Dumazeta3d12892009-10-21 10:59:31 +00002955 if (tb[IFLA_IFNAME])
2956 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2957
Greg Rose115c9b82012-02-21 16:54:48 -05002958 if (tb[IFLA_EXT_MASK])
2959 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2960
Jiri Benc79e1ad12017-11-02 17:04:38 -02002961 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07002962 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002963 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002964 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002965 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02002966 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002967 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02002968 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07002969
Jiri Benc79e1ad12017-11-02 17:04:38 -02002970 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002971 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002972 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00002973
Jiri Benc79e1ad12017-11-02 17:04:38 -02002974 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05002975 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002976 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02002977 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002978
Jiri Benc79e1ad12017-11-02 17:04:38 -02002979 err = rtnl_fill_ifinfo(nskb, dev, net,
2980 RTM_NEWLINK, NETLINK_CB(skb).portid,
2981 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
2982 0, NULL, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08002983 if (err < 0) {
2984 /* -EMSGSIZE implies BUG in if_nlmsg_size */
2985 WARN_ON(err == -EMSGSIZE);
2986 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00002987 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00002988 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02002989out:
2990 if (netnsid >= 0)
2991 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07002992
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002993 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002994}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002995
Greg Rose115c9b82012-02-21 16:54:48 -05002996static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00002997{
Greg Rose115c9b82012-02-21 16:54:48 -05002998 struct net *net = sock_net(skb->sk);
2999 struct net_device *dev;
3000 struct nlattr *tb[IFLA_MAX+1];
3001 u32 ext_filter_mask = 0;
3002 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003003 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003004
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003005 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3006 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3007 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3008
Johannes Bergfceb6432017-04-12 14:34:07 +02003009 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003010 if (tb[IFLA_EXT_MASK])
3011 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3012 }
Greg Rose115c9b82012-02-21 16:54:48 -05003013
3014 if (!ext_filter_mask)
3015 return NLMSG_GOODSIZE;
3016 /*
3017 * traverse the list of net devices and compute the minimum
3018 * buffer size based upon the filter mask.
3019 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003020 rcu_read_lock();
3021 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003022 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3023 if_nlmsg_size(dev,
3024 ext_filter_mask));
3025 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003026 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003027
Zhang Shengju93af2052016-11-22 14:14:28 +08003028 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003029}
3030
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003031static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 int idx;
3034 int s_idx = cb->family;
3035
3036 if (s_idx == 0)
3037 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003038
Patrick McHardy25239ce2010-04-26 16:02:05 +02003039 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003040 struct rtnl_link **tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 int type = cb->nlh->nlmsg_type-RTM_BASE;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003042 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003043 rtnl_dumpit_func dumpit;
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (idx < s_idx || idx == PF_PACKET)
3046 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003047
Florian Westphaladdf9b92017-12-02 21:44:05 +01003048 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003050
Florian Westphaladdf9b92017-12-02 21:44:05 +01003051 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3052 if (!tab)
3053 continue;
3054
3055 link = tab[type];
3056 if (!link)
3057 continue;
3058
3059 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003060 if (!dumpit)
3061 continue;
3062
Nicolas Dichtel04652772013-03-22 06:28:42 +00003063 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003065 cb->prev_seq = 0;
3066 cb->seq = 0;
3067 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003068 if (dumpit(skb, cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 break;
3070 }
3071 cb->family = idx;
3072
3073 return skb->len;
3074}
3075
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003076struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003077 unsigned int change,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003078 u32 event, gfp_t flags, int *new_nsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003080 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003082 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003083 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003085 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003086 if (skb == NULL)
3087 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Jiri Benc79e1ad12017-11-02 17:04:38 -02003089 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3090 type, 0, 0, change, 0, 0, event,
3091 new_nsid, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003092 if (err < 0) {
3093 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3094 WARN_ON(err == -EMSGSIZE);
3095 kfree_skb(skb);
3096 goto errout;
3097 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003098 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003099errout:
3100 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003101 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003102 return NULL;
3103}
3104
3105void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3106{
3107 struct net *net = dev_net(dev);
3108
3109 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3110}
3111
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003112static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3113 unsigned int change, u32 event,
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003114 gfp_t flags, int *new_nsid)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003115{
3116 struct sk_buff *skb;
3117
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003118 if (dev->reg_state != NETREG_REGISTERED)
3119 return;
3120
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003121 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003122 if (skb)
3123 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003125
3126void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3127 gfp_t flags)
3128{
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003129 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, NULL);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003132void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3133 gfp_t flags, int *new_nsid)
3134{
3135 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3136 new_nsid);
3137}
3138
John Fastabendd83b0602012-04-15 06:44:08 +00003139static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3140 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003141 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003142 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003143 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003144{
3145 struct nlmsghdr *nlh;
3146 struct ndmsg *ndm;
3147
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003148 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003149 if (!nlh)
3150 return -EMSGSIZE;
3151
3152 ndm = nlmsg_data(nlh);
3153 ndm->ndm_family = AF_BRIDGE;
3154 ndm->ndm_pad1 = 0;
3155 ndm->ndm_pad2 = 0;
3156 ndm->ndm_flags = flags;
3157 ndm->ndm_type = 0;
3158 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003159 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003160
3161 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3162 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003163 if (vid)
3164 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3165 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003166
Johannes Berg053c0952015-01-16 22:09:00 +01003167 nlmsg_end(skb, nlh);
3168 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003169
3170nla_put_failure:
3171 nlmsg_cancel(skb, nlh);
3172 return -EMSGSIZE;
3173}
3174
John Fastabend3ff661c2012-04-15 06:44:14 +00003175static inline size_t rtnl_fdb_nlmsg_size(void)
3176{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003177 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3178 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3179 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3180 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003181}
3182
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003183static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3184 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003185{
3186 struct net *net = dev_net(dev);
3187 struct sk_buff *skb;
3188 int err = -ENOBUFS;
3189
3190 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3191 if (!skb)
3192 goto errout;
3193
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003194 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003195 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003196 if (err < 0) {
3197 kfree_skb(skb);
3198 goto errout;
3199 }
3200
3201 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3202 return;
3203errout:
3204 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3205}
3206
Vlad Yasevich090096b2013-03-06 15:39:42 +00003207/**
3208 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3209 */
3210int ndo_dflt_fdb_add(struct ndmsg *ndm,
3211 struct nlattr *tb[],
3212 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003213 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003214 u16 flags)
3215{
3216 int err = -EINVAL;
3217
3218 /* If aging addresses are supported device will need to
3219 * implement its own handler for this.
3220 */
3221 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3222 pr_info("%s: FDB only supports static addresses\n", dev->name);
3223 return err;
3224 }
3225
Or Gerlitz65891fe2014-12-14 18:19:05 +02003226 if (vid) {
3227 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3228 return err;
3229 }
3230
Vlad Yasevich090096b2013-03-06 15:39:42 +00003231 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3232 err = dev_uc_add_excl(dev, addr);
3233 else if (is_multicast_ether_addr(addr))
3234 err = dev_mc_add_excl(dev, addr);
3235
3236 /* Only return duplicate errors if NLM_F_EXCL is set */
3237 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3238 err = 0;
3239
3240 return err;
3241}
3242EXPORT_SYMBOL(ndo_dflt_fdb_add);
3243
Florian Westphalb88d12e2017-10-10 17:10:04 +02003244static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3245 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003246{
3247 u16 vid = 0;
3248
3249 if (vlan_attr) {
3250 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003251 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003252 return -EINVAL;
3253 }
3254
3255 vid = nla_get_u16(vlan_attr);
3256
3257 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003258 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003259 return -EINVAL;
3260 }
3261 }
3262 *p_vid = vid;
3263 return 0;
3264}
3265
David Ahernc21ef3e2017-04-16 09:48:24 -07003266static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3267 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003268{
3269 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003270 struct ndmsg *ndm;
3271 struct nlattr *tb[NDA_MAX+1];
3272 struct net_device *dev;
3273 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003274 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003275 int err;
3276
David Ahernc21ef3e2017-04-16 09:48:24 -07003277 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003278 if (err < 0)
3279 return err;
3280
3281 ndm = nlmsg_data(nlh);
3282 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003283 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003284 return -EINVAL;
3285 }
3286
3287 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3288 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003289 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003290 return -ENODEV;
3291 }
3292
3293 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003294 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003295 return -EINVAL;
3296 }
3297
3298 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003299
Florian Westphalb88d12e2017-10-10 17:10:04 +02003300 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003301 if (err)
3302 return err;
3303
John Fastabend77162022012-04-15 06:43:56 +00003304 err = -EOPNOTSUPP;
3305
3306 /* Support fdb on master device the net/bridge default case */
3307 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3308 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003309 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3310 const struct net_device_ops *ops = br_dev->netdev_ops;
3311
Jiri Pirkof6f64242014-11-28 14:34:15 +01003312 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
3313 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003314 if (err)
3315 goto out;
3316 else
3317 ndm->ndm_flags &= ~NTF_MASTER;
3318 }
3319
3320 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003321 if ((ndm->ndm_flags & NTF_SELF)) {
3322 if (dev->netdev_ops->ndo_fdb_add)
3323 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003324 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003325 nlh->nlmsg_flags);
3326 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003327 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003328 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003329
John Fastabend3ff661c2012-04-15 06:44:14 +00003330 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003331 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3332 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003333 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003334 }
John Fastabend77162022012-04-15 06:43:56 +00003335 }
3336out:
3337 return err;
3338}
3339
Vlad Yasevich090096b2013-03-06 15:39:42 +00003340/**
3341 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3342 */
3343int ndo_dflt_fdb_del(struct ndmsg *ndm,
3344 struct nlattr *tb[],
3345 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003346 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003347{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003348 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003349
3350 /* If aging addresses are supported device will need to
3351 * implement its own handler for this.
3352 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003353 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003354 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003355 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003356 }
3357
3358 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3359 err = dev_uc_del(dev, addr);
3360 else if (is_multicast_ether_addr(addr))
3361 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003362
3363 return err;
3364}
3365EXPORT_SYMBOL(ndo_dflt_fdb_del);
3366
David Ahernc21ef3e2017-04-16 09:48:24 -07003367static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3368 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003369{
3370 struct net *net = sock_net(skb->sk);
3371 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003372 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003373 struct net_device *dev;
3374 int err = -EINVAL;
3375 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003376 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003377
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003378 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003379 return -EPERM;
3380
David Ahernc21ef3e2017-04-16 09:48:24 -07003381 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003382 if (err < 0)
3383 return err;
John Fastabend77162022012-04-15 06:43:56 +00003384
3385 ndm = nlmsg_data(nlh);
3386 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003387 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003388 return -EINVAL;
3389 }
3390
3391 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3392 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003393 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003394 return -ENODEV;
3395 }
3396
Vlad Yasevich1690be62013-02-13 12:00:18 +00003397 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003398 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003399 return -EINVAL;
3400 }
3401
Vlad Yasevich1690be62013-02-13 12:00:18 +00003402 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003403
Florian Westphalb88d12e2017-10-10 17:10:04 +02003404 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003405 if (err)
3406 return err;
3407
John Fastabend77162022012-04-15 06:43:56 +00003408 err = -EOPNOTSUPP;
3409
3410 /* Support fdb on master device the net/bridge default case */
3411 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3412 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003413 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3414 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003415
Jiri Pirko898e5062013-01-03 22:48:52 +00003416 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003417 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003418
3419 if (err)
3420 goto out;
3421 else
3422 ndm->ndm_flags &= ~NTF_MASTER;
3423 }
3424
3425 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003426 if (ndm->ndm_flags & NTF_SELF) {
3427 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003428 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3429 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003430 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003431 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003432
John Fastabend3ff661c2012-04-15 06:44:14 +00003433 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003434 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3435 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003436 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003437 }
John Fastabend77162022012-04-15 06:43:56 +00003438 }
3439out:
3440 return err;
3441}
3442
John Fastabendd83b0602012-04-15 06:44:08 +00003443static int nlmsg_populate_fdb(struct sk_buff *skb,
3444 struct netlink_callback *cb,
3445 struct net_device *dev,
3446 int *idx,
3447 struct netdev_hw_addr_list *list)
3448{
3449 struct netdev_hw_addr *ha;
3450 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003451 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003452
Eric W. Biederman15e47302012-09-07 20:12:54 +00003453 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003454 seq = cb->nlh->nlmsg_seq;
3455
3456 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003457 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003458 goto skip;
3459
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003460 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003461 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003462 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003463 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003464 if (err < 0)
3465 return err;
3466skip:
3467 *idx += 1;
3468 }
3469 return 0;
3470}
3471
3472/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003473 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003474 * @nlh: netlink message header
3475 * @dev: netdevice
3476 *
3477 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003478 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003479 */
3480int ndo_dflt_fdb_dump(struct sk_buff *skb,
3481 struct netlink_callback *cb,
3482 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003483 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003484 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003485{
3486 int err;
3487
3488 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003489 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003490 if (err)
3491 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003492 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003493out:
3494 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003495 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003496}
3497EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3498
John Fastabend77162022012-04-15 06:43:56 +00003499static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3500{
John Fastabend77162022012-04-15 06:43:56 +00003501 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003502 struct nlattr *tb[IFLA_MAX+1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003503 struct net_device *br_dev = NULL;
3504 const struct net_device_ops *ops = NULL;
3505 const struct net_device_ops *cops = NULL;
3506 struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3507 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003508 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003509 int brport_idx = 0;
3510 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003511 int h, s_h;
3512 int idx = 0, s_idx;
3513 int err = 0;
3514 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003515
Alexander Potapenko0ff50e82017-05-23 13:20:28 +02003516 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb,
3517 IFLA_MAX, ifla_policy, NULL);
3518 if (err < 0) {
3519 return -EINVAL;
3520 } else if (err == 0) {
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003521 if (tb[IFLA_MASTER])
3522 br_idx = nla_get_u32(tb[IFLA_MASTER]);
3523 }
John Fastabend77162022012-04-15 06:43:56 +00003524
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003525 brport_idx = ifm->ifi_index;
3526
3527 if (br_idx) {
3528 br_dev = __dev_get_by_index(net, br_idx);
3529 if (!br_dev)
3530 return -ENODEV;
3531
3532 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003533 }
3534
Roopa Prabhud2976532016-08-30 21:56:45 -07003535 s_h = cb->args[0];
3536 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003537
Roopa Prabhud2976532016-08-30 21:56:45 -07003538 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3539 idx = 0;
3540 head = &net->dev_index_head[h];
3541 hlist_for_each_entry(dev, head, index_hlist) {
3542
3543 if (brport_idx && (dev->ifindex != brport_idx))
3544 continue;
3545
3546 if (!br_idx) { /* user did not specify a specific bridge */
3547 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3548 br_dev = netdev_master_upper_dev_get(dev);
3549 cops = br_dev->netdev_ops;
3550 }
3551 } else {
3552 if (dev != br_dev &&
3553 !(dev->priv_flags & IFF_BRIDGE_PORT))
3554 continue;
3555
3556 if (br_dev != netdev_master_upper_dev_get(dev) &&
3557 !(dev->priv_flags & IFF_EBRIDGE))
3558 continue;
3559 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003560 }
3561
Roopa Prabhud2976532016-08-30 21:56:45 -07003562 if (idx < s_idx)
3563 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003564
Roopa Prabhud2976532016-08-30 21:56:45 -07003565 if (dev->priv_flags & IFF_BRIDGE_PORT) {
3566 if (cops && cops->ndo_fdb_dump) {
3567 err = cops->ndo_fdb_dump(skb, cb,
3568 br_dev, dev,
3569 &fidx);
3570 if (err == -EMSGSIZE)
3571 goto out;
3572 }
3573 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003574
Roopa Prabhud2976532016-08-30 21:56:45 -07003575 if (dev->netdev_ops->ndo_fdb_dump)
3576 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
3577 dev, NULL,
3578 &fidx);
3579 else
3580 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
3581 &fidx);
3582 if (err == -EMSGSIZE)
3583 goto out;
3584
3585 cops = NULL;
3586
3587 /* reset fdb offset to 0 for rest of the interfaces */
3588 cb->args[2] = 0;
3589 fidx = 0;
3590cont:
3591 idx++;
John Fastabend77162022012-04-15 06:43:56 +00003592 }
John Fastabend77162022012-04-15 06:43:56 +00003593 }
John Fastabend77162022012-04-15 06:43:56 +00003594
Roopa Prabhud2976532016-08-30 21:56:45 -07003595out:
3596 cb->args[0] = h;
3597 cb->args[1] = idx;
3598 cb->args[2] = fidx;
3599
John Fastabend77162022012-04-15 06:43:56 +00003600 return skb->len;
3601}
3602
Scott Feldman2c3c0312014-11-28 14:34:25 +01003603static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3604 unsigned int attrnum, unsigned int flag)
3605{
3606 if (mask & flag)
3607 return nla_put_u8(skb, attrnum, !!(flags & flag));
3608 return 0;
3609}
3610
John Fastabend815cccb2012-10-24 08:13:09 +00003611int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01003612 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003613 u32 flags, u32 mask, int nlflags,
3614 u32 filter_mask,
3615 int (*vlan_fill)(struct sk_buff *skb,
3616 struct net_device *dev,
3617 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00003618{
3619 struct nlmsghdr *nlh;
3620 struct ifinfomsg *ifm;
3621 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01003622 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00003623 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00003624 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003625 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003626
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003627 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00003628 if (nlh == NULL)
3629 return -EMSGSIZE;
3630
3631 ifm = nlmsg_data(nlh);
3632 ifm->ifi_family = AF_BRIDGE;
3633 ifm->__ifi_pad = 0;
3634 ifm->ifi_type = dev->type;
3635 ifm->ifi_index = dev->ifindex;
3636 ifm->ifi_flags = dev_get_flags(dev);
3637 ifm->ifi_change = 0;
3638
3639
3640 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3641 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3642 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00003643 (br_dev &&
3644 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00003645 (dev->addr_len &&
3646 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02003647 (dev->ifindex != dev_get_iflink(dev) &&
3648 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00003649 goto nla_put_failure;
3650
3651 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3652 if (!br_afspec)
3653 goto nla_put_failure;
3654
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003655 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00003656 nla_nest_cancel(skb, br_afspec);
3657 goto nla_put_failure;
3658 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08003659
3660 if (mode != BRIDGE_MODE_UNDEF) {
3661 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3662 nla_nest_cancel(skb, br_afspec);
3663 goto nla_put_failure;
3664 }
3665 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003666 if (vlan_fill) {
3667 err = vlan_fill(skb, dev, filter_mask);
3668 if (err) {
3669 nla_nest_cancel(skb, br_afspec);
3670 goto nla_put_failure;
3671 }
3672 }
John Fastabend815cccb2012-10-24 08:13:09 +00003673 nla_nest_end(skb, br_afspec);
3674
Scott Feldman2c3c0312014-11-28 14:34:25 +01003675 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3676 if (!protinfo)
3677 goto nla_put_failure;
3678
3679 if (brport_nla_put_flag(skb, flags, mask,
3680 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3681 brport_nla_put_flag(skb, flags, mask,
3682 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3683 brport_nla_put_flag(skb, flags, mask,
3684 IFLA_BRPORT_FAST_LEAVE,
3685 BR_MULTICAST_FAST_LEAVE) ||
3686 brport_nla_put_flag(skb, flags, mask,
3687 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3688 brport_nla_put_flag(skb, flags, mask,
3689 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3690 brport_nla_put_flag(skb, flags, mask,
3691 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3692 brport_nla_put_flag(skb, flags, mask,
3693 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3694 brport_nla_put_flag(skb, flags, mask,
3695 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3696 nla_nest_cancel(skb, protinfo);
3697 goto nla_put_failure;
3698 }
3699
3700 nla_nest_end(skb, protinfo);
3701
Johannes Berg053c0952015-01-16 22:09:00 +01003702 nlmsg_end(skb, nlh);
3703 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00003704nla_put_failure:
3705 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003706 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00003707}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07003708EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00003709
John Fastabende5a55a82012-10-24 08:12:57 +00003710static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3711{
3712 struct net *net = sock_net(skb->sk);
3713 struct net_device *dev;
3714 int idx = 0;
3715 u32 portid = NETLINK_CB(cb->skb).portid;
3716 u32 seq = cb->nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003717 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003718 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00003719
Thomas Grafaa68c202014-11-26 13:42:20 +01003720 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3721 struct nlattr *extfilt;
3722
3723 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3724 IFLA_EXT_MASK);
3725 if (extfilt) {
3726 if (nla_len(extfilt) < sizeof(filter_mask))
3727 return -EINVAL;
3728
3729 filter_mask = nla_get_u32(extfilt);
3730 }
3731 }
John Fastabende5a55a82012-10-24 08:12:57 +00003732
3733 rcu_read_lock();
3734 for_each_netdev_rcu(net, dev) {
3735 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00003736 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00003737
Jiri Pirko898e5062013-01-03 22:48:52 +00003738 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003739 if (idx >= cb->args[0]) {
3740 err = br_dev->netdev_ops->ndo_bridge_getlink(
3741 skb, portid, seq, dev,
3742 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003743 if (err < 0 && err != -EOPNOTSUPP) {
3744 if (likely(skb->len))
3745 break;
3746
3747 goto out_err;
3748 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003749 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003750 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003751 }
3752
3753 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003754 if (idx >= cb->args[0]) {
3755 err = ops->ndo_bridge_getlink(skb, portid,
3756 seq, dev,
3757 filter_mask,
3758 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07003759 if (err < 0 && err != -EOPNOTSUPP) {
3760 if (likely(skb->len))
3761 break;
3762
3763 goto out_err;
3764 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07003765 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00003766 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00003767 }
3768 }
David Ahernf6c57752017-05-15 23:19:17 -07003769 err = skb->len;
3770out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00003771 rcu_read_unlock();
3772 cb->args[0] = idx;
3773
David Ahernf6c57752017-05-15 23:19:17 -07003774 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00003775}
3776
John Fastabend2469ffd2012-10-24 08:13:03 +00003777static inline size_t bridge_nlmsg_size(void)
3778{
3779 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3780 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3781 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3782 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
3783 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
3784 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
3785 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
3786 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
3787 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
3788 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
3789 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
3790}
3791
Roopa Prabhu02dba432015-01-14 20:02:25 -08003792static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00003793{
3794 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00003795 struct sk_buff *skb;
3796 int err = -EOPNOTSUPP;
3797
Roopa Prabhu02dba432015-01-14 20:02:25 -08003798 if (!dev->netdev_ops->ndo_bridge_getlink)
3799 return 0;
3800
John Fastabend2469ffd2012-10-24 08:13:03 +00003801 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3802 if (!skb) {
3803 err = -ENOMEM;
3804 goto errout;
3805 }
3806
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02003807 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003808 if (err < 0)
3809 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00003810
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003811 if (!skb->len)
3812 goto errout;
3813
John Fastabend2469ffd2012-10-24 08:13:03 +00003814 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3815 return 0;
3816errout:
3817 WARN_ON(err == -EMSGSIZE);
3818 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08003819 if (err)
3820 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00003821 return err;
3822}
3823
David Ahernc21ef3e2017-04-16 09:48:24 -07003824static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3825 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00003826{
3827 struct net *net = sock_net(skb->sk);
3828 struct ifinfomsg *ifm;
3829 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00003830 struct nlattr *br_spec, *attr = NULL;
3831 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003832 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00003833 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00003834
3835 if (nlmsg_len(nlh) < sizeof(*ifm))
3836 return -EINVAL;
3837
3838 ifm = nlmsg_data(nlh);
3839 if (ifm->ifi_family != AF_BRIDGE)
3840 return -EPFNOSUPPORT;
3841
3842 dev = __dev_get_by_index(net, ifm->ifi_index);
3843 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003844 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00003845 return -ENODEV;
3846 }
3847
John Fastabend2469ffd2012-10-24 08:13:03 +00003848 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3849 if (br_spec) {
3850 nla_for_each_nested(attr, br_spec, rem) {
3851 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003852 if (nla_len(attr) < sizeof(flags))
3853 return -EINVAL;
3854
John Fastabendc38e01b2012-11-02 16:32:36 +00003855 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00003856 flags = nla_get_u16(attr);
3857 break;
3858 }
3859 }
3860 }
3861
3862 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003863 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3864
3865 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003866 err = -EOPNOTSUPP;
3867 goto out;
3868 }
3869
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003870 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
John Fastabende5a55a82012-10-24 08:12:57 +00003871 if (err)
3872 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00003873
3874 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00003875 }
3876
John Fastabend2469ffd2012-10-24 08:13:03 +00003877 if ((flags & BRIDGE_FLAGS_SELF)) {
3878 if (!dev->netdev_ops->ndo_bridge_setlink)
3879 err = -EOPNOTSUPP;
3880 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003881 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3882 flags);
Roopa Prabhu02dba432015-01-14 20:02:25 -08003883 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00003884 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003885
3886 /* Generate event to notify upper layer of bridge
3887 * change
3888 */
3889 err = rtnl_bridge_notify(dev);
3890 }
John Fastabend2469ffd2012-10-24 08:13:03 +00003891 }
3892
John Fastabendc38e01b2012-11-02 16:32:36 +00003893 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00003894 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00003895out:
3896 return err;
3897}
3898
David Ahernc21ef3e2017-04-16 09:48:24 -07003899static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3900 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00003901{
3902 struct net *net = sock_net(skb->sk);
3903 struct ifinfomsg *ifm;
3904 struct net_device *dev;
3905 struct nlattr *br_spec, *attr = NULL;
3906 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02003907 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00003908 bool have_flags = false;
3909
3910 if (nlmsg_len(nlh) < sizeof(*ifm))
3911 return -EINVAL;
3912
3913 ifm = nlmsg_data(nlh);
3914 if (ifm->ifi_family != AF_BRIDGE)
3915 return -EPFNOSUPPORT;
3916
3917 dev = __dev_get_by_index(net, ifm->ifi_index);
3918 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003919 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00003920 return -ENODEV;
3921 }
3922
3923 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3924 if (br_spec) {
3925 nla_for_each_nested(attr, br_spec, rem) {
3926 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01003927 if (nla_len(attr) < sizeof(flags))
3928 return -EINVAL;
3929
Vlad Yasevich407af322013-02-13 12:00:12 +00003930 have_flags = true;
3931 flags = nla_get_u16(attr);
3932 break;
3933 }
3934 }
3935 }
3936
Vlad Yasevich407af322013-02-13 12:00:12 +00003937 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3938 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3939
3940 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3941 err = -EOPNOTSUPP;
3942 goto out;
3943 }
3944
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003945 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003946 if (err)
3947 goto out;
3948
3949 flags &= ~BRIDGE_FLAGS_MASTER;
3950 }
3951
3952 if ((flags & BRIDGE_FLAGS_SELF)) {
3953 if (!dev->netdev_ops->ndo_bridge_dellink)
3954 err = -EOPNOTSUPP;
3955 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08003956 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3957 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00003958
Roopa Prabhu02dba432015-01-14 20:02:25 -08003959 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00003960 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08003961
3962 /* Generate event to notify upper layer of bridge
3963 * change
3964 */
3965 err = rtnl_bridge_notify(dev);
3966 }
Vlad Yasevich407af322013-02-13 12:00:12 +00003967 }
3968
3969 if (have_flags)
3970 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00003971out:
3972 return err;
3973}
3974
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02003975static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3976{
3977 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3978 (!idxattr || idxattr == attrid);
3979}
3980
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02003981#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
3982static int rtnl_get_offload_stats_attr_size(int attr_id)
3983{
3984 switch (attr_id) {
3985 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
3986 return sizeof(struct rtnl_link_stats64);
3987 }
3988
3989 return 0;
3990}
3991
3992static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
3993 int *prividx)
3994{
3995 struct nlattr *attr = NULL;
3996 int attr_id, size;
3997 void *attr_data;
3998 int err;
3999
4000 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4001 dev->netdev_ops->ndo_get_offload_stats))
4002 return -ENODATA;
4003
4004 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4005 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4006 if (attr_id < *prividx)
4007 continue;
4008
4009 size = rtnl_get_offload_stats_attr_size(attr_id);
4010 if (!size)
4011 continue;
4012
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004013 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004014 continue;
4015
4016 attr = nla_reserve_64bit(skb, attr_id, size,
4017 IFLA_OFFLOAD_XSTATS_UNSPEC);
4018 if (!attr)
4019 goto nla_put_failure;
4020
4021 attr_data = nla_data(attr);
4022 memset(attr_data, 0, size);
4023 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4024 attr_data);
4025 if (err)
4026 goto get_offload_stats_failure;
4027 }
4028
4029 if (!attr)
4030 return -ENODATA;
4031
4032 *prividx = 0;
4033 return 0;
4034
4035nla_put_failure:
4036 err = -EMSGSIZE;
4037get_offload_stats_failure:
4038 *prividx = attr_id;
4039 return err;
4040}
4041
4042static int rtnl_get_offload_stats_size(const struct net_device *dev)
4043{
4044 int nla_size = 0;
4045 int attr_id;
4046 int size;
4047
4048 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4049 dev->netdev_ops->ndo_get_offload_stats))
4050 return 0;
4051
4052 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4053 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004054 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004055 continue;
4056 size = rtnl_get_offload_stats_attr_size(attr_id);
4057 nla_size += nla_total_size_64bit(size);
4058 }
4059
4060 if (nla_size != 0)
4061 nla_size += nla_total_size(0);
4062
4063 return nla_size;
4064}
4065
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004066static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4067 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004068 unsigned int flags, unsigned int filter_mask,
4069 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004070{
4071 struct if_stats_msg *ifsm;
4072 struct nlmsghdr *nlh;
4073 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004074 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004075 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004076
4077 ASSERT_RTNL();
4078
4079 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4080 if (!nlh)
4081 return -EMSGSIZE;
4082
4083 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004084 ifsm->family = PF_UNSPEC;
4085 ifsm->pad1 = 0;
4086 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004087 ifsm->ifindex = dev->ifindex;
4088 ifsm->filter_mask = filter_mask;
4089
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004090 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004091 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004092
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004093 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4094 sizeof(struct rtnl_link_stats64),
4095 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004096 if (!attr)
4097 goto nla_put_failure;
4098
4099 sp = nla_data(attr);
4100 dev_get_stats(dev, sp);
4101 }
4102
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004103 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4104 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4105
4106 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004107 *idxattr = IFLA_STATS_LINK_XSTATS;
4108 attr = nla_nest_start(skb,
4109 IFLA_STATS_LINK_XSTATS);
4110 if (!attr)
4111 goto nla_put_failure;
4112
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004113 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4114 nla_nest_end(skb, attr);
4115 if (err)
4116 goto nla_put_failure;
4117 *idxattr = 0;
4118 }
4119 }
4120
4121 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4122 *idxattr)) {
4123 const struct rtnl_link_ops *ops = NULL;
4124 const struct net_device *master;
4125
4126 master = netdev_master_upper_dev_get(dev);
4127 if (master)
4128 ops = master->rtnl_link_ops;
4129 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004130 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4131 attr = nla_nest_start(skb,
4132 IFLA_STATS_LINK_XSTATS_SLAVE);
4133 if (!attr)
4134 goto nla_put_failure;
4135
4136 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004137 nla_nest_end(skb, attr);
4138 if (err)
4139 goto nla_put_failure;
4140 *idxattr = 0;
4141 }
4142 }
4143
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004144 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4145 *idxattr)) {
4146 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4147 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4148 if (!attr)
4149 goto nla_put_failure;
4150
4151 err = rtnl_get_offload_stats(skb, dev, prividx);
4152 if (err == -ENODATA)
4153 nla_nest_cancel(skb, attr);
4154 else
4155 nla_nest_end(skb, attr);
4156
4157 if (err && err != -ENODATA)
4158 goto nla_put_failure;
4159 *idxattr = 0;
4160 }
4161
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004162 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4163 struct rtnl_af_ops *af_ops;
4164
4165 *idxattr = IFLA_STATS_AF_SPEC;
4166 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4167 if (!attr)
4168 goto nla_put_failure;
4169
Florian Westphal5fa85a02017-10-16 15:44:36 +02004170 rcu_read_lock();
4171 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004172 if (af_ops->fill_stats_af) {
4173 struct nlattr *af;
4174 int err;
4175
4176 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004177 if (!af) {
4178 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004179 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004180 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004181 err = af_ops->fill_stats_af(skb, dev);
4182
Florian Westphal5fa85a02017-10-16 15:44:36 +02004183 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004184 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004185 } else if (err < 0) {
4186 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004187 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004188 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004189
4190 nla_nest_end(skb, af);
4191 }
4192 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004193 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004194
4195 nla_nest_end(skb, attr);
4196
4197 *idxattr = 0;
4198 }
4199
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004200 nlmsg_end(skb, nlh);
4201
4202 return 0;
4203
4204nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004205 /* not a multi message or no progress mean a real error */
4206 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4207 nlmsg_cancel(skb, nlh);
4208 else
4209 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004210
4211 return -EMSGSIZE;
4212}
4213
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004214static size_t if_nlmsg_stats_size(const struct net_device *dev,
4215 u32 filter_mask)
4216{
4217 size_t size = 0;
4218
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004219 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004220 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4221
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004222 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4223 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004224 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004225
4226 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004227 size += nla_total_size(ops->get_linkxstats_size(dev,
4228 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004229 /* for IFLA_STATS_LINK_XSTATS */
4230 size += nla_total_size(0);
4231 }
4232 }
4233
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004234 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4235 struct net_device *_dev = (struct net_device *)dev;
4236 const struct rtnl_link_ops *ops = NULL;
4237 const struct net_device *master;
4238
4239 /* netdev_master_upper_dev_get can't take const */
4240 master = netdev_master_upper_dev_get(_dev);
4241 if (master)
4242 ops = master->rtnl_link_ops;
4243 if (ops && ops->get_linkxstats_size) {
4244 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4245
4246 size += nla_total_size(ops->get_linkxstats_size(dev,
4247 attr));
4248 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4249 size += nla_total_size(0);
4250 }
4251 }
4252
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004253 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4254 size += rtnl_get_offload_stats_size(dev);
4255
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004256 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4257 struct rtnl_af_ops *af_ops;
4258
4259 /* for IFLA_STATS_AF_SPEC */
4260 size += nla_total_size(0);
4261
Florian Westphal5fa85a02017-10-16 15:44:36 +02004262 rcu_read_lock();
4263 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004264 if (af_ops->get_stats_af_size) {
4265 size += nla_total_size(
4266 af_ops->get_stats_af_size(dev));
4267
4268 /* for AF_* */
4269 size += nla_total_size(0);
4270 }
4271 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004272 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004273 }
4274
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004275 return size;
4276}
4277
David Ahernc21ef3e2017-04-16 09:48:24 -07004278static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4279 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004280{
4281 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004282 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004283 int idxattr = 0, prividx = 0;
4284 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004285 struct sk_buff *nskb;
4286 u32 filter_mask;
4287 int err;
4288
Mathias Krause4775cc12016-12-28 17:52:15 +01004289 if (nlmsg_len(nlh) < sizeof(*ifsm))
4290 return -EINVAL;
4291
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004292 ifsm = nlmsg_data(nlh);
4293 if (ifsm->ifindex > 0)
4294 dev = __dev_get_by_index(net, ifsm->ifindex);
4295 else
4296 return -EINVAL;
4297
4298 if (!dev)
4299 return -ENODEV;
4300
4301 filter_mask = ifsm->filter_mask;
4302 if (!filter_mask)
4303 return -EINVAL;
4304
4305 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
4306 if (!nskb)
4307 return -ENOBUFS;
4308
4309 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
4310 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004311 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004312 if (err < 0) {
4313 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
4314 WARN_ON(err == -EMSGSIZE);
4315 kfree_skb(nskb);
4316 } else {
4317 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
4318 }
4319
4320 return err;
4321}
4322
4323static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
4324{
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004325 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004326 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004327 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004328 struct if_stats_msg *ifsm;
4329 struct hlist_head *head;
4330 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004331 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004332 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004333
4334 s_h = cb->args[0];
4335 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004336 s_idxattr = cb->args[2];
4337 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004338
4339 cb->seq = net->dev_base_seq;
4340
Mathias Krause4775cc12016-12-28 17:52:15 +01004341 if (nlmsg_len(cb->nlh) < sizeof(*ifsm))
4342 return -EINVAL;
4343
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004344 ifsm = nlmsg_data(cb->nlh);
4345 filter_mask = ifsm->filter_mask;
4346 if (!filter_mask)
4347 return -EINVAL;
4348
4349 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4350 idx = 0;
4351 head = &net->dev_index_head[h];
4352 hlist_for_each_entry(dev, head, index_hlist) {
4353 if (idx < s_idx)
4354 goto cont;
4355 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
4356 NETLINK_CB(cb->skb).portid,
4357 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004358 flags, filter_mask,
4359 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004360 /* If we ran out of room on the first message,
4361 * we're in trouble
4362 */
4363 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
4364
4365 if (err < 0)
4366 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004367 s_prividx = 0;
4368 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004369 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4370cont:
4371 idx++;
4372 }
4373 }
4374out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004375 cb->args[3] = s_prividx;
4376 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004377 cb->args[1] = idx;
4378 cb->args[0] = h;
4379
4380 return skb->len;
4381}
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383/* Process one rtnetlink message. */
4384
Johannes Berg2d4bc932017-04-12 14:34:04 +02004385static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
4386 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004388 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01004389 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01004390 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02004391 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07004392 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004393 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06004394 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 int family;
4396 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07004400 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 type -= RTM_BASE;
4403
4404 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00004405 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 return 0;
4407
Hong zhi guo573ce262013-03-27 06:47:04 +00004408 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 kind = type&3;
4410
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07004411 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07004412 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Florian Westphal6853dd42017-08-09 20:41:51 +02004414 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08004415 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004416 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07004417 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00004418 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Florian Westphaladdf9b92017-12-02 21:44:05 +01004420 link = rtnl_get_link(family, type);
4421 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02004422 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004423 link = rtnl_get_link(family, type);
4424 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02004425 goto err_unlock;
4426 }
Florian Westphale4202512017-12-02 21:44:06 +01004427 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004428 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02004429
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02004430 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02004431 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Florian Westphale4202512017-12-02 21:44:06 +01004433 err = 0;
4434 /* need to do this before rcu_read_unlock() */
4435 if (!try_module_get(owner))
4436 err = -EPROTONOSUPPORT;
4437
Florian Westphal6853dd42017-08-09 20:41:51 +02004438 rcu_read_unlock();
4439
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004440 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01004441 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004442 struct netlink_dump_control c = {
4443 .dump = dumpit,
4444 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01004445 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004446 };
4447 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01004448 /* netlink_dump_start() will keep a reference on
4449 * module if dump is still in progress.
4450 */
4451 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00004452 }
Eric Dumazet2907c352011-05-25 07:34:04 +00004453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
Florian Westphaladdf9b92017-12-02 21:44:05 +01004456 link = rtnl_get_link(family, type);
4457 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02004458 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01004459 link = rtnl_get_link(PF_UNSPEC, type);
4460 if (!link || !link->doit)
4461 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02004462 }
4463
Florian Westphale4202512017-12-02 21:44:06 +01004464 owner = link->owner;
4465 if (!try_module_get(owner)) {
4466 err = -EPROTONOSUPPORT;
4467 goto out_unlock;
4468 }
4469
Florian Westphaladdf9b92017-12-02 21:44:05 +01004470 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02004471 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01004472 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02004473 rcu_read_unlock();
4474 if (doit)
4475 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01004476 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02004477 return err;
4478 }
Florian Westphal6853dd42017-08-09 20:41:51 +02004479 rcu_read_unlock();
4480
Florian Westphal0cc09022017-08-09 20:41:50 +02004481 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004482 link = rtnl_get_link(family, type);
4483 if (link && link->doit)
4484 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02004485 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01004486
Florian Westphale4202512017-12-02 21:44:06 +01004487 module_put(owner);
4488
Florian Westphaladdf9b92017-12-02 21:44:05 +01004489 return err;
4490
4491out_unlock:
4492 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004493 return err;
4494
4495err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02004496 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02004497 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004500static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07004502 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
Julien Gomes5f729ea2017-06-20 13:54:16 -07004505static int rtnetlink_bind(struct net *net, int group)
4506{
4507 switch (group) {
4508 case RTNLGRP_IPV4_MROUTE_R:
4509 case RTNLGRP_IPV6_MROUTE_R:
4510 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4511 return -EPERM;
4512 break;
4513 }
4514 return 0;
4515}
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
4518{
Jiri Pirko351638e2013-05-28 01:30:21 +00004519 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04004522 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08004523 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07004524 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004525 case NETDEV_CHANGENAME:
4526 case NETDEV_FEAT_CHANGE:
4527 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08004528 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004529 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08004530 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004531 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04004532 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08004533 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08004534 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04004535 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02004536 GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 break;
4538 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 break;
4540 }
4541 return NOTIFY_DONE;
4542}
4543
4544static struct notifier_block rtnetlink_dev_notifier = {
4545 .notifier_call = rtnetlink_event,
4546};
4547
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004548
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004549static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004550{
4551 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004552 struct netlink_kernel_cfg cfg = {
4553 .groups = RTNLGRP_MAX,
4554 .input = rtnetlink_rcv,
4555 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00004556 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07004557 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00004558 };
4559
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00004560 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004561 if (!sk)
4562 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004563 net->rtnl = sk;
4564 return 0;
4565}
4566
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004567static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004568{
Denis V. Lunev775516b2008-01-18 23:55:19 -08004569 netlink_kernel_release(net->rtnl);
4570 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004571}
4572
4573static struct pernet_operations rtnetlink_net_ops = {
4574 .init = rtnetlink_net_init,
4575 .exit = rtnetlink_net_exit,
4576};
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578void __init rtnetlink_init(void)
4579{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004580 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08004582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07004584
Greg Rosec7ac8672011-06-10 01:27:09 +00004585 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02004586 rtnl_dump_ifinfo, 0);
4587 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
4588 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
4589 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07004590
Florian Westphalb97bac62017-08-09 20:41:48 +02004591 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
4592 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
4593 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00004594
Florian Westphalb97bac62017-08-09 20:41:48 +02004595 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
4596 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
4597 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00004598
Florian Westphalb97bac62017-08-09 20:41:48 +02004599 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
4600 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
4601 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004602
4603 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02004604 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605}