blob: a51cab95ba64c7d76a2ba0940c67e9f6e53f54e1 [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>
49#include <net/ip.h>
50#include <net/protocol.h>
51#include <net/arp.h>
52#include <net/route.h>
53#include <net/udp.h>
Daniel Borkmannea697632015-01-05 23:57:47 +010054#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/pkt_sched.h>
Thomas Graf14c0b972006-08-04 03:38:38 -070057#include <net/fib_rules.h>
Thomas Grafe2849862007-03-22 11:48:11 -070058#include <net/rtnetlink.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000059#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +020061#define RTNL_MAX_TYPE 50
Kees Cookccf8dbc2018-05-30 15:20:52 -070062#define RTNL_SLAVE_MAX_TYPE 36
63
Eric Dumazete0d087a2009-11-07 01:26:17 -080064struct rtnl_link {
Thomas Grafe2849862007-03-22 11:48:11 -070065 rtnl_doit_func doit;
66 rtnl_dumpit_func dumpit;
Florian Westphale4202512017-12-02 21:44:06 +010067 struct module *owner;
Florian Westphal62256f92017-08-09 20:41:52 +020068 unsigned int flags;
Florian Westphaladdf9b92017-12-02 21:44:05 +010069 struct rcu_head rcu;
Thomas Grafe2849862007-03-22 11:48:11 -070070};
71
Stephen Hemminger6756ae42006-03-20 22:23:58 -080072static DEFINE_MUTEX(rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74void rtnl_lock(void)
75{
Stephen Hemminger6756ae42006-03-20 22:23:58 -080076 mutex_lock(&rtnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Eric Dumazete0d087a2009-11-07 01:26:17 -080078EXPORT_SYMBOL(rtnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Kirill Tkhai79ffdfc2018-03-14 22:17:20 +030080int rtnl_lock_killable(void)
81{
82 return mutex_lock_killable(&rtnl_mutex);
83}
84EXPORT_SYMBOL(rtnl_lock_killable);
85
Eric Dumazet1b5c5492016-06-13 20:21:50 -070086static struct sk_buff *defer_kfree_skb_list;
87void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
88{
89 if (head && tail) {
90 tail->next = defer_kfree_skb_list;
91 defer_kfree_skb_list = head;
92 }
93}
94EXPORT_SYMBOL(rtnl_kfree_skbs);
95
Stephen Hemminger6756ae42006-03-20 22:23:58 -080096void __rtnl_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Eric Dumazet1b5c5492016-06-13 20:21:50 -070098 struct sk_buff *head = defer_kfree_skb_list;
99
100 defer_kfree_skb_list = NULL;
101
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800102 mutex_unlock(&rtnl_mutex);
Eric Dumazet1b5c5492016-06-13 20:21:50 -0700103
104 while (head) {
105 struct sk_buff *next = head->next;
106
107 kfree_skb(head);
108 cond_resched();
109 head = next;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113void rtnl_unlock(void)
114{
Herbert Xu58ec3b42008-10-07 15:50:03 -0700115 /* This fellow will unlock it for us. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 netdev_run_todo();
117}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800118EXPORT_SYMBOL(rtnl_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800120int rtnl_trylock(void)
121{
122 return mutex_trylock(&rtnl_mutex);
123}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800124EXPORT_SYMBOL(rtnl_trylock);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800125
Patrick McHardyc9c10142008-04-23 22:10:48 -0700126int rtnl_is_locked(void)
127{
128 return mutex_is_locked(&rtnl_mutex);
129}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800130EXPORT_SYMBOL(rtnl_is_locked);
Patrick McHardyc9c10142008-04-23 22:10:48 -0700131
Vlad Buslov6f995282018-09-24 19:22:49 +0300132bool refcount_dec_and_rtnl_lock(refcount_t *r)
133{
134 return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
135}
136EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
137
Paul E. McKenneya898def2010-02-22 17:04:49 -0800138#ifdef CONFIG_PROVE_LOCKING
Yaowei Bai0cbf3342015-10-08 21:29:02 +0800139bool lockdep_rtnl_is_held(void)
Paul E. McKenneya898def2010-02-22 17:04:49 -0800140{
141 return lockdep_is_held(&rtnl_mutex);
142}
143EXPORT_SYMBOL(lockdep_rtnl_is_held);
144#endif /* #ifdef CONFIG_PROVE_LOCKING */
145
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100146static struct rtnl_link *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
Thomas Grafe2849862007-03-22 11:48:11 -0700147
148static inline int rtm_msgindex(int msgtype)
149{
150 int msgindex = msgtype - RTM_BASE;
151
152 /*
153 * msgindex < 0 implies someone tried to register a netlink
154 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
155 * the message type has not been added to linux/rtnetlink.h
156 */
157 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
158
159 return msgindex;
160}
161
Florian Westphaladdf9b92017-12-02 21:44:05 +0100162static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
163{
164 struct rtnl_link **tab;
165
166 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
167 protocol = PF_UNSPEC;
168
169 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
170 if (!tab)
171 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
172
173 return tab[msgtype];
174}
175
Florian Westphale4202512017-12-02 21:44:06 +0100176static int rtnl_register_internal(struct module *owner,
177 int protocol, int msgtype,
178 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
179 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700180{
Florian Westphalb0e9fe12017-12-04 22:42:30 +0100181 struct rtnl_link *link, *old;
182 struct rtnl_link __rcu **tab;
Thomas Grafe2849862007-03-22 11:48:11 -0700183 int msgindex;
Florian Westphaladdf9b92017-12-02 21:44:05 +0100184 int ret = -ENOBUFS;
Thomas Grafe2849862007-03-22 11:48:11 -0700185
Patrick McHardy25239ce2010-04-26 16:02:05 +0200186 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700187 msgindex = rtm_msgindex(msgtype);
188
Florian Westphaladdf9b92017-12-02 21:44:05 +0100189 rtnl_lock();
190 tab = rtnl_msg_handlers[protocol];
Thomas Grafe2849862007-03-22 11:48:11 -0700191 if (tab == NULL) {
Florian Westphaladdf9b92017-12-02 21:44:05 +0100192 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
193 if (!tab)
194 goto unlock;
Thomas Grafe2849862007-03-22 11:48:11 -0700195
Florian Westphaladdf9b92017-12-02 21:44:05 +0100196 /* ensures we see the 0 stores */
Florian Westphal6853dd42017-08-09 20:41:51 +0200197 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700198 }
199
Florian Westphaladdf9b92017-12-02 21:44:05 +0100200 old = rtnl_dereference(tab[msgindex]);
201 if (old) {
202 link = kmemdup(old, sizeof(*old), GFP_KERNEL);
203 if (!link)
204 goto unlock;
205 } else {
206 link = kzalloc(sizeof(*link), GFP_KERNEL);
207 if (!link)
208 goto unlock;
209 }
Thomas Grafe2849862007-03-22 11:48:11 -0700210
Florian Westphale4202512017-12-02 21:44:06 +0100211 WARN_ON(link->owner && link->owner != owner);
212 link->owner = owner;
213
Florian Westphaladdf9b92017-12-02 21:44:05 +0100214 WARN_ON(doit && link->doit && link->doit != doit);
215 if (doit)
216 link->doit = doit;
217 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
218 if (dumpit)
219 link->dumpit = dumpit;
220
221 link->flags |= flags;
222
223 /* publish protocol:msgtype */
224 rcu_assign_pointer(tab[msgindex], link);
225 ret = 0;
226 if (old)
227 kfree_rcu(old, rcu);
228unlock:
229 rtnl_unlock();
230 return ret;
Thomas Grafe2849862007-03-22 11:48:11 -0700231}
Florian Westphale4202512017-12-02 21:44:06 +0100232
233/**
234 * rtnl_register_module - Register a rtnetlink message type
235 *
236 * @owner: module registering the hook (THIS_MODULE)
237 * @protocol: Protocol family or PF_UNSPEC
238 * @msgtype: rtnetlink message type
239 * @doit: Function pointer called for each request message
240 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
241 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
242 *
243 * Like rtnl_register, but for use by removable modules.
244 */
245int rtnl_register_module(struct module *owner,
246 int protocol, int msgtype,
247 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
248 unsigned int flags)
249{
250 return rtnl_register_internal(owner, protocol, msgtype,
251 doit, dumpit, flags);
252}
253EXPORT_SYMBOL_GPL(rtnl_register_module);
254
255/**
Florian Westphal16feebc2017-12-02 21:44:08 +0100256 * rtnl_register - Register a rtnetlink message type
Florian Westphale4202512017-12-02 21:44:06 +0100257 * @protocol: Protocol family or PF_UNSPEC
258 * @msgtype: rtnetlink message type
259 * @doit: Function pointer called for each request message
260 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
261 * @flags: rtnl_link_flags to modifiy behaviour of doit/dumpit functions
262 *
263 * Registers the specified function pointers (at least one of them has
264 * to be non-NULL) to be called whenever a request message for the
265 * specified protocol family and message type is received.
266 *
267 * The special protocol family PF_UNSPEC may be used to define fallback
268 * function pointers for the case when no entry for the specific protocol
269 * family exists.
Thomas Grafe2849862007-03-22 11:48:11 -0700270 */
271void rtnl_register(int protocol, int msgtype,
Greg Rosec7ac8672011-06-10 01:27:09 +0000272 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
Florian Westphalb97bac62017-08-09 20:41:48 +0200273 unsigned int flags)
Thomas Grafe2849862007-03-22 11:48:11 -0700274{
Florian Westphal16feebc2017-12-02 21:44:08 +0100275 int err;
276
277 err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
278 flags);
279 if (err)
280 pr_err("Unable to register rtnetlink message handler, "
281 "protocol = %d, message type = %d\n", protocol, msgtype);
Thomas Grafe2849862007-03-22 11:48:11 -0700282}
Thomas Grafe2849862007-03-22 11:48:11 -0700283
284/**
285 * rtnl_unregister - Unregister a rtnetlink message type
286 * @protocol: Protocol family or PF_UNSPEC
287 * @msgtype: rtnetlink message type
288 *
289 * Returns 0 on success or a negative error code.
290 */
291int rtnl_unregister(int protocol, int msgtype)
292{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100293 struct rtnl_link **tab, *link;
Thomas Grafe2849862007-03-22 11:48:11 -0700294 int msgindex;
295
Patrick McHardy25239ce2010-04-26 16:02:05 +0200296 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700297 msgindex = rtm_msgindex(msgtype);
298
Florian Westphal6853dd42017-08-09 20:41:51 +0200299 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100300 tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
301 if (!tab) {
Florian Westphal6853dd42017-08-09 20:41:51 +0200302 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700303 return -ENOENT;
Florian Westphal6853dd42017-08-09 20:41:51 +0200304 }
Thomas Grafe2849862007-03-22 11:48:11 -0700305
Florian Westphaladdf9b92017-12-02 21:44:05 +0100306 link = tab[msgindex];
307 rcu_assign_pointer(tab[msgindex], NULL);
Florian Westphal6853dd42017-08-09 20:41:51 +0200308 rtnl_unlock();
Thomas Grafe2849862007-03-22 11:48:11 -0700309
Florian Westphaladdf9b92017-12-02 21:44:05 +0100310 kfree_rcu(link, rcu);
311
Thomas Grafe2849862007-03-22 11:48:11 -0700312 return 0;
313}
Thomas Grafe2849862007-03-22 11:48:11 -0700314EXPORT_SYMBOL_GPL(rtnl_unregister);
315
316/**
317 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
318 * @protocol : Protocol family or PF_UNSPEC
319 *
320 * Identical to calling rtnl_unregster() for all registered message types
321 * of a certain protocol family.
322 */
323void rtnl_unregister_all(int protocol)
324{
Florian Westphaladdf9b92017-12-02 21:44:05 +0100325 struct rtnl_link **tab, *link;
326 int msgindex;
Florian Westphal019a3162017-08-09 20:41:49 +0200327
Patrick McHardy25239ce2010-04-26 16:02:05 +0200328 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
Thomas Grafe2849862007-03-22 11:48:11 -0700329
Florian Westphal019a3162017-08-09 20:41:49 +0200330 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +0100331 tab = rtnl_msg_handlers[protocol];
Sabrina Dubrocaf707ef62018-08-28 13:40:53 +0200332 if (!tab) {
333 rtnl_unlock();
334 return;
335 }
Florian Westphal6853dd42017-08-09 20:41:51 +0200336 RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
Florian Westphaladdf9b92017-12-02 21:44:05 +0100337 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
338 link = tab[msgindex];
339 if (!link)
340 continue;
341
342 rcu_assign_pointer(tab[msgindex], NULL);
343 kfree_rcu(link, rcu);
344 }
Florian Westphal019a3162017-08-09 20:41:49 +0200345 rtnl_unlock();
346
Florian Westphal6853dd42017-08-09 20:41:51 +0200347 synchronize_net();
348
Florian Westphaladdf9b92017-12-02 21:44:05 +0100349 kfree(tab);
Thomas Grafe2849862007-03-22 11:48:11 -0700350}
Thomas Grafe2849862007-03-22 11:48:11 -0700351EXPORT_SYMBOL_GPL(rtnl_unregister_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Patrick McHardy38f7b872007-06-13 12:03:51 -0700353static LIST_HEAD(link_ops);
354
Eric Dumazetc63044f2011-12-13 11:38:00 +0000355static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
356{
357 const struct rtnl_link_ops *ops;
358
359 list_for_each_entry(ops, &link_ops, list) {
360 if (!strcmp(ops->kind, kind))
361 return ops;
362 }
363 return NULL;
364}
365
Patrick McHardy38f7b872007-06-13 12:03:51 -0700366/**
367 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
368 * @ops: struct rtnl_link_ops * to register
369 *
370 * The caller must hold the rtnl_mutex. This function should be used
371 * by drivers that create devices during module initialization. It
372 * must be called before registering the devices.
373 *
374 * Returns 0 on success or a negative error code.
375 */
376int __rtnl_link_register(struct rtnl_link_ops *ops)
377{
Eric Dumazetc63044f2011-12-13 11:38:00 +0000378 if (rtnl_link_ops_get(ops->kind))
379 return -EEXIST;
380
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +0200381 /* The check for setup is here because if ops
382 * does not have that filled up, it is not possible
383 * to use the ops for creating device. So do not
384 * fill up dellink as well. That disables rtnl_dellink.
385 */
386 if (ops->setup && !ops->dellink)
Eric Dumazet23289a32009-10-27 07:06:36 +0000387 ops->dellink = unregister_netdevice_queue;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700388
Patrick McHardy38f7b872007-06-13 12:03:51 -0700389 list_add_tail(&ops->list, &link_ops);
390 return 0;
391}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700392EXPORT_SYMBOL_GPL(__rtnl_link_register);
393
394/**
395 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
396 * @ops: struct rtnl_link_ops * to register
397 *
398 * Returns 0 on success or a negative error code.
399 */
400int rtnl_link_register(struct rtnl_link_ops *ops)
401{
402 int err;
403
Kees Cookccf8dbc2018-05-30 15:20:52 -0700404 /* Sanity-check max sizes to avoid stack buffer overflow. */
405 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
406 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
407 return -EINVAL;
408
Patrick McHardy38f7b872007-06-13 12:03:51 -0700409 rtnl_lock();
410 err = __rtnl_link_register(ops);
411 rtnl_unlock();
412 return err;
413}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700414EXPORT_SYMBOL_GPL(rtnl_link_register);
415
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700416static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
417{
418 struct net_device *dev;
Eric Dumazet23289a32009-10-27 07:06:36 +0000419 LIST_HEAD(list_kill);
420
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700421 for_each_netdev(net, dev) {
Eric Dumazet23289a32009-10-27 07:06:36 +0000422 if (dev->rtnl_link_ops == ops)
423 ops->dellink(dev, &list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700424 }
Eric Dumazet23289a32009-10-27 07:06:36 +0000425 unregister_netdevice_many(&list_kill);
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700426}
427
Patrick McHardy38f7b872007-06-13 12:03:51 -0700428/**
429 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
430 * @ops: struct rtnl_link_ops * to unregister
431 *
Kirill Tkhai554873e2018-03-30 19:38:37 +0300432 * The caller must hold the rtnl_mutex and guarantee net_namespace_list
433 * integrity (hold pernet_ops_rwsem for writing to close the race
434 * with setup_net() and cleanup_net()).
Patrick McHardy38f7b872007-06-13 12:03:51 -0700435 */
436void __rtnl_link_unregister(struct rtnl_link_ops *ops)
437{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700438 struct net *net;
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700439
Eric W. Biederman881d9662007-09-17 11:56:21 -0700440 for_each_net(net) {
Pavel Emelyanov669f87b2008-04-16 00:46:52 -0700441 __rtnl_kill_links(net, ops);
Patrick McHardy2d85cba2007-07-11 19:42:13 -0700442 }
Patrick McHardy38f7b872007-06-13 12:03:51 -0700443 list_del(&ops->list);
444}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700445EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
446
Cong Wang200b9162014-05-12 15:11:20 -0700447/* Return with the rtnl_lock held when there are no network
448 * devices unregistering in any network namespace.
449 */
450static void rtnl_lock_unregistering_all(void)
451{
452 struct net *net;
453 bool unregistering;
Peter Zijlstraff960a72014-10-29 17:04:56 +0100454 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Cong Wang200b9162014-05-12 15:11:20 -0700455
Peter Zijlstraff960a72014-10-29 17:04:56 +0100456 add_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700457 for (;;) {
Cong Wang200b9162014-05-12 15:11:20 -0700458 unregistering = false;
459 rtnl_lock();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300460 /* We held write locked pernet_ops_rwsem, and parallel
461 * setup_net() and cleanup_net() are not possible.
462 */
Cong Wang200b9162014-05-12 15:11:20 -0700463 for_each_net(net) {
464 if (net->dev_unreg_count > 0) {
465 unregistering = true;
466 break;
467 }
468 }
469 if (!unregistering)
470 break;
471 __rtnl_unlock();
Peter Zijlstraff960a72014-10-29 17:04:56 +0100472
473 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Cong Wang200b9162014-05-12 15:11:20 -0700474 }
Peter Zijlstraff960a72014-10-29 17:04:56 +0100475 remove_wait_queue(&netdev_unregistering_wq, &wait);
Cong Wang200b9162014-05-12 15:11:20 -0700476}
477
Patrick McHardy38f7b872007-06-13 12:03:51 -0700478/**
479 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
480 * @ops: struct rtnl_link_ops * to unregister
481 */
482void rtnl_link_unregister(struct rtnl_link_ops *ops)
483{
Kirill Tkhai8518e9b2018-03-27 18:02:32 +0300484 /* Close the race with setup_net() and cleanup_net() */
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300485 down_write(&pernet_ops_rwsem);
Cong Wang200b9162014-05-12 15:11:20 -0700486 rtnl_lock_unregistering_all();
Patrick McHardy38f7b872007-06-13 12:03:51 -0700487 __rtnl_link_unregister(ops);
488 rtnl_unlock();
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300489 up_write(&pernet_ops_rwsem);
Patrick McHardy38f7b872007-06-13 12:03:51 -0700490}
Patrick McHardy38f7b872007-06-13 12:03:51 -0700491EXPORT_SYMBOL_GPL(rtnl_link_unregister);
492
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100493static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
494{
495 struct net_device *master_dev;
496 const struct rtnl_link_ops *ops;
Florian Westphal8515ae32017-08-10 16:52:59 +0200497 size_t size = 0;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100498
Florian Westphal8515ae32017-08-10 16:52:59 +0200499 rcu_read_lock();
500
501 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100502 if (!master_dev)
Florian Westphal8515ae32017-08-10 16:52:59 +0200503 goto out;
504
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100505 ops = master_dev->rtnl_link_ops;
Fernando Luis Vazquez Cao6049f252014-02-04 19:35:02 +0900506 if (!ops || !ops->get_slave_size)
Florian Westphal8515ae32017-08-10 16:52:59 +0200507 goto out;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100508 /* IFLA_INFO_SLAVE_DATA + nested data */
Florian Westphal8515ae32017-08-10 16:52:59 +0200509 size = nla_total_size(sizeof(struct nlattr)) +
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100510 ops->get_slave_size(master_dev, dev);
Florian Westphal8515ae32017-08-10 16:52:59 +0200511
512out:
513 rcu_read_unlock();
514 return size;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100515}
516
Patrick McHardy38f7b872007-06-13 12:03:51 -0700517static size_t rtnl_link_get_size(const struct net_device *dev)
518{
519 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
520 size_t size;
521
522 if (!ops)
523 return 0;
524
Thomas Graf369cf772010-11-11 15:47:59 +0000525 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
526 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
Patrick McHardy38f7b872007-06-13 12:03:51 -0700527
528 if (ops->get_size)
529 /* IFLA_INFO_DATA + nested data */
Thomas Graf369cf772010-11-11 15:47:59 +0000530 size += nla_total_size(sizeof(struct nlattr)) +
Patrick McHardy38f7b872007-06-13 12:03:51 -0700531 ops->get_size(dev);
532
533 if (ops->get_xstats_size)
Thomas Graf369cf772010-11-11 15:47:59 +0000534 /* IFLA_INFO_XSTATS */
535 size += nla_total_size(ops->get_xstats_size(dev));
Patrick McHardy38f7b872007-06-13 12:03:51 -0700536
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100537 size += rtnl_link_get_slave_info_data_size(dev);
538
Patrick McHardy38f7b872007-06-13 12:03:51 -0700539 return size;
540}
541
Thomas Graff8ff1822010-11-16 04:30:14 +0000542static LIST_HEAD(rtnl_af_ops);
543
544static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
545{
546 const struct rtnl_af_ops *ops;
547
Florian Westphal5fa85a02017-10-16 15:44:36 +0200548 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000549 if (ops->family == family)
550 return ops;
551 }
552
553 return NULL;
554}
555
556/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000557 * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
558 * @ops: struct rtnl_af_ops * to register
559 *
560 * Returns 0 on success or a negative error code.
561 */
stephen hemminger3678a9d2013-12-30 10:41:32 -0800562void rtnl_af_register(struct rtnl_af_ops *ops)
Thomas Graff8ff1822010-11-16 04:30:14 +0000563{
Thomas Graff8ff1822010-11-16 04:30:14 +0000564 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200565 list_add_tail_rcu(&ops->list, &rtnl_af_ops);
Thomas Graff8ff1822010-11-16 04:30:14 +0000566 rtnl_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000567}
568EXPORT_SYMBOL_GPL(rtnl_af_register);
569
570/**
Thomas Graff8ff1822010-11-16 04:30:14 +0000571 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
572 * @ops: struct rtnl_af_ops * to unregister
573 */
574void rtnl_af_unregister(struct rtnl_af_ops *ops)
575{
576 rtnl_lock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200577 list_del_rcu(&ops->list);
Thomas Graff8ff1822010-11-16 04:30:14 +0000578 rtnl_unlock();
Florian Westphal5fa85a02017-10-16 15:44:36 +0200579
580 synchronize_rcu();
Thomas Graff8ff1822010-11-16 04:30:14 +0000581}
582EXPORT_SYMBOL_GPL(rtnl_af_unregister);
583
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700584static size_t rtnl_link_get_af_size(const struct net_device *dev,
585 u32 ext_filter_mask)
Thomas Graff8ff1822010-11-16 04:30:14 +0000586{
587 struct rtnl_af_ops *af_ops;
588 size_t size;
589
590 /* IFLA_AF_SPEC */
591 size = nla_total_size(sizeof(struct nlattr));
592
Florian Westphal5fa85a02017-10-16 15:44:36 +0200593 rcu_read_lock();
594 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Thomas Graff8ff1822010-11-16 04:30:14 +0000595 if (af_ops->get_link_af_size) {
596 /* AF_* + nested data */
597 size += nla_total_size(sizeof(struct nlattr)) +
Arad, Ronenb1974ed2015-10-19 09:23:28 -0700598 af_ops->get_link_af_size(dev, ext_filter_mask);
Thomas Graff8ff1822010-11-16 04:30:14 +0000599 }
600 }
Florian Westphal5fa85a02017-10-16 15:44:36 +0200601 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +0000602
603 return size;
604}
605
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100606static bool rtnl_have_link_slave_info(const struct net_device *dev)
607{
608 struct net_device *master_dev;
Florian Westphal4c82a952017-09-26 13:58:43 +0200609 bool ret = false;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100610
Florian Westphal4c82a952017-09-26 13:58:43 +0200611 rcu_read_lock();
612
613 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
Jiri Pirko813f0202014-01-23 19:19:21 +0100614 if (master_dev && master_dev->rtnl_link_ops)
Florian Westphal4c82a952017-09-26 13:58:43 +0200615 ret = true;
616 rcu_read_unlock();
617 return ret;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100618}
619
620static int rtnl_link_slave_info_fill(struct sk_buff *skb,
621 const struct net_device *dev)
622{
623 struct net_device *master_dev;
624 const struct rtnl_link_ops *ops;
625 struct nlattr *slave_data;
626 int err;
627
628 master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
629 if (!master_dev)
630 return 0;
631 ops = master_dev->rtnl_link_ops;
632 if (!ops)
633 return 0;
634 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
635 return -EMSGSIZE;
636 if (ops->fill_slave_info) {
637 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
638 if (!slave_data)
639 return -EMSGSIZE;
640 err = ops->fill_slave_info(skb, master_dev, dev);
641 if (err < 0)
642 goto err_cancel_slave_data;
643 nla_nest_end(skb, slave_data);
644 }
645 return 0;
646
647err_cancel_slave_data:
648 nla_nest_cancel(skb, slave_data);
649 return err;
650}
651
652static int rtnl_link_info_fill(struct sk_buff *skb,
653 const struct net_device *dev)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700654{
655 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100656 struct nlattr *data;
657 int err;
658
659 if (!ops)
660 return 0;
661 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
662 return -EMSGSIZE;
663 if (ops->fill_xstats) {
664 err = ops->fill_xstats(skb, dev);
665 if (err < 0)
666 return err;
667 }
668 if (ops->fill_info) {
669 data = nla_nest_start(skb, IFLA_INFO_DATA);
670 if (data == NULL)
671 return -EMSGSIZE;
672 err = ops->fill_info(skb, dev);
673 if (err < 0)
674 goto err_cancel_data;
675 nla_nest_end(skb, data);
676 }
677 return 0;
678
679err_cancel_data:
680 nla_nest_cancel(skb, data);
681 return err;
682}
683
684static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
685{
686 struct nlattr *linkinfo;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700687 int err = -EMSGSIZE;
688
689 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
690 if (linkinfo == NULL)
691 goto out;
692
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100693 err = rtnl_link_info_fill(skb, dev);
694 if (err < 0)
Patrick McHardy38f7b872007-06-13 12:03:51 -0700695 goto err_cancel_link;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +0100696
697 err = rtnl_link_slave_info_fill(skb, dev);
698 if (err < 0)
699 goto err_cancel_link;
Patrick McHardy38f7b872007-06-13 12:03:51 -0700700
701 nla_nest_end(skb, linkinfo);
702 return 0;
703
Patrick McHardy38f7b872007-06-13 12:03:51 -0700704err_cancel_link:
705 nla_nest_cancel(skb, linkinfo);
706out:
707 return err;
708}
709
Eric Dumazet95c96172012-04-15 05:58:06 +0000710int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800712 struct sock *rtnl = net->rtnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int err = 0;
714
Patrick McHardyac6d4392005-08-14 19:29:52 -0700715 NETLINK_CB(skb).dst_group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (echo)
Reshetova, Elena63354792017-06-30 13:07:58 +0300717 refcount_inc(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
719 if (echo)
720 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
721 return err;
722}
723
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800724int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
Thomas Graf2942e902006-08-15 00:30:25 -0700725{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800726 struct sock *rtnl = net->rtnl;
727
Thomas Graf2942e902006-08-15 00:30:25 -0700728 return nlmsg_unicast(rtnl, skb, pid);
729}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800730EXPORT_SYMBOL(rtnl_unicast);
Thomas Graf2942e902006-08-15 00:30:25 -0700731
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800732void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
733 struct nlmsghdr *nlh, gfp_t flags)
Thomas Graf97676b62006-08-15 00:31:41 -0700734{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800735 struct sock *rtnl = net->rtnl;
Thomas Graf97676b62006-08-15 00:31:41 -0700736 int report = 0;
737
738 if (nlh)
739 report = nlmsg_report(nlh);
740
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800741 nlmsg_notify(rtnl, skb, pid, group, report, flags);
Thomas Graf97676b62006-08-15 00:31:41 -0700742}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800743EXPORT_SYMBOL(rtnl_notify);
Thomas Graf97676b62006-08-15 00:31:41 -0700744
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800745void rtnl_set_sk_err(struct net *net, u32 group, int error)
Thomas Graf97676b62006-08-15 00:31:41 -0700746{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -0800747 struct sock *rtnl = net->rtnl;
748
Thomas Graf97676b62006-08-15 00:31:41 -0700749 netlink_set_err(rtnl, 0, group, error);
750}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800751EXPORT_SYMBOL(rtnl_set_sk_err);
Thomas Graf97676b62006-08-15 00:31:41 -0700752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
754{
Thomas Graf2d7202b2006-08-22 00:01:27 -0700755 struct nlattr *mx;
756 int i, valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Thomas Graf2d7202b2006-08-22 00:01:27 -0700758 mx = nla_nest_start(skb, RTA_METRICS);
759 if (mx == NULL)
760 return -ENOBUFS;
761
762 for (i = 0; i < RTAX_MAX; i++) {
763 if (metrics[i]) {
Daniel Borkmannea697632015-01-05 23:57:47 +0100764 if (i == RTAX_CC_ALGO - 1) {
765 char tmp[TCP_CA_NAME_MAX], *name;
766
767 name = tcp_ca_get_name_by_key(metrics[i], tmp);
768 if (!name)
769 continue;
770 if (nla_put_string(skb, i + 1, name))
771 goto nla_put_failure;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200772 } else if (i == RTAX_FEATURES - 1) {
773 u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
774
Phil Sutterf8edcd12016-08-23 13:14:31 +0200775 if (!user_features)
776 continue;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200777 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
778 if (nla_put_u32(skb, i + 1, user_features))
779 goto nla_put_failure;
Daniel Borkmannea697632015-01-05 23:57:47 +0100780 } else {
781 if (nla_put_u32(skb, i + 1, metrics[i]))
782 goto nla_put_failure;
783 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700784 valid++;
Thomas Graf2d7202b2006-08-22 00:01:27 -0700785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David S. Millera57d27f2006-08-22 22:20:14 -0700788 if (!valid) {
789 nla_nest_cancel(skb, mx);
790 return 0;
791 }
Thomas Graf2d7202b2006-08-22 00:01:27 -0700792
793 return nla_nest_end(skb, mx);
794
795nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700796 nla_nest_cancel(skb, mx);
797 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
Eric Dumazete0d087a2009-11-07 01:26:17 -0800799EXPORT_SYMBOL(rtnetlink_put_metrics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Thomas Grafe3703b32006-11-27 09:27:07 -0800801int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
David S. Miller87a50692012-07-10 05:06:14 -0700802 long expires, u32 error)
Thomas Grafe3703b32006-11-27 09:27:07 -0800803{
804 struct rta_cacheinfo ci = {
Thomas Grafe3703b32006-11-27 09:27:07 -0800805 .rta_error = error,
806 .rta_id = id,
Thomas Grafe3703b32006-11-27 09:27:07 -0800807 };
808
David Ahern39407462018-04-17 17:33:08 -0700809 if (dst) {
810 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
811 ci.rta_used = dst->__use;
812 ci.rta_clntref = atomic_read(&dst->__refcnt);
813 }
Li Wei82539472012-07-29 16:01:30 +0000814 if (expires) {
815 unsigned long clock;
Thomas Grafe3703b32006-11-27 09:27:07 -0800816
Li Wei82539472012-07-29 16:01:30 +0000817 clock = jiffies_to_clock_t(abs(expires));
818 clock = min_t(unsigned long, clock, INT_MAX);
819 ci.rta_expires = (expires > 0) ? clock : -clock;
820 }
Thomas Grafe3703b32006-11-27 09:27:07 -0800821 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
822}
Thomas Grafe3703b32006-11-27 09:27:07 -0800823EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
David S. Miller93b2d4a2008-02-17 18:35:07 -0800825static void set_operstate(struct net_device *dev, unsigned char transition)
Stefan Rompfb00055a2006-03-20 17:09:11 -0800826{
827 unsigned char operstate = dev->operstate;
828
Eric Dumazete0d087a2009-11-07 01:26:17 -0800829 switch (transition) {
Stefan Rompfb00055a2006-03-20 17:09:11 -0800830 case IF_OPER_UP:
831 if ((operstate == IF_OPER_DORMANT ||
832 operstate == IF_OPER_UNKNOWN) &&
833 !netif_dormant(dev))
834 operstate = IF_OPER_UP;
835 break;
836
837 case IF_OPER_DORMANT:
838 if (operstate == IF_OPER_UP ||
839 operstate == IF_OPER_UNKNOWN)
840 operstate = IF_OPER_DORMANT;
841 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700842 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800843
844 if (dev->operstate != operstate) {
845 write_lock_bh(&dev_base_lock);
846 dev->operstate = operstate;
847 write_unlock_bh(&dev_base_lock);
David S. Miller93b2d4a2008-02-17 18:35:07 -0800848 netdev_state_change(dev);
849 }
Stefan Rompfb00055a2006-03-20 17:09:11 -0800850}
851
Jiri Bencb1beb682012-07-27 02:58:22 +0000852static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
853{
854 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
855 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
856}
857
Patrick McHardy3729d502010-02-26 06:34:54 +0000858static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
859 const struct ifinfomsg *ifm)
860{
861 unsigned int flags = ifm->ifi_flags;
862
863 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
864 if (ifm->ifi_change)
865 flags = (flags & ifm->ifi_change) |
Jiri Bencb1beb682012-07-27 02:58:22 +0000866 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
Patrick McHardy3729d502010-02-26 06:34:54 +0000867
868 return flags;
869}
870
Thomas Grafb60c5112006-08-04 23:05:34 -0700871static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +0000872 const struct rtnl_link_stats64 *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Thomas Grafb60c5112006-08-04 23:05:34 -0700874 a->rx_packets = b->rx_packets;
875 a->tx_packets = b->tx_packets;
876 a->rx_bytes = b->rx_bytes;
877 a->tx_bytes = b->tx_bytes;
878 a->rx_errors = b->rx_errors;
879 a->tx_errors = b->tx_errors;
880 a->rx_dropped = b->rx_dropped;
881 a->tx_dropped = b->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Thomas Grafb60c5112006-08-04 23:05:34 -0700883 a->multicast = b->multicast;
884 a->collisions = b->collisions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Thomas Grafb60c5112006-08-04 23:05:34 -0700886 a->rx_length_errors = b->rx_length_errors;
887 a->rx_over_errors = b->rx_over_errors;
888 a->rx_crc_errors = b->rx_crc_errors;
889 a->rx_frame_errors = b->rx_frame_errors;
890 a->rx_fifo_errors = b->rx_fifo_errors;
891 a->rx_missed_errors = b->rx_missed_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Thomas Grafb60c5112006-08-04 23:05:34 -0700893 a->tx_aborted_errors = b->tx_aborted_errors;
894 a->tx_carrier_errors = b->tx_carrier_errors;
895 a->tx_fifo_errors = b->tx_fifo_errors;
896 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
897 a->tx_window_errors = b->tx_window_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Thomas Grafb60c5112006-08-04 23:05:34 -0700899 a->rx_compressed = b->rx_compressed;
900 a->tx_compressed = b->tx_compressed;
Jarod Wilson6e7333d2016-02-01 18:51:05 -0500901
902 a->rx_nohandler = b->rx_nohandler;
Jan Engelhardt10708f32010-03-11 09:57:29 +0000903}
904
Chris Wrightc02db8c2010-05-16 01:05:45 -0700905/* All VF info */
Greg Rose115c9b82012-02-21 16:54:48 -0500906static inline int rtnl_vfinfo_size(const struct net_device *dev,
907 u32 ext_filter_mask)
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000908{
Phil Sutter9af15c32017-01-18 14:04:39 +0100909 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
Chris Wrightc02db8c2010-05-16 01:05:45 -0700910 int num_vfs = dev_num_vf(dev->dev.parent);
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100911 size_t size = nla_total_size(0);
Scott Feldman045de012010-05-28 03:42:43 -0700912 size += num_vfs *
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100913 (nla_total_size(0) +
914 nla_total_size(sizeof(struct ifla_vf_mac)) +
915 nla_total_size(sizeof(struct ifla_vf_vlan)) +
916 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
Moshe Shemesh79aab092016-09-22 12:11:15 +0300917 nla_total_size(MAX_VLAN_LIST_LEN *
918 sizeof(struct ifla_vf_vlan_info)) +
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400919 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100920 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
Jiri Benc945a3672014-08-08 16:44:32 +0200921 nla_total_size(sizeof(struct ifla_vf_rate)) +
Vlad Zolotarov01a3d792015-03-30 21:35:23 +0300922 nla_total_size(sizeof(struct ifla_vf_link_state)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300923 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
Sabrina Dubroca7e75f742016-11-15 10:39:03 +0100924 nla_total_size(0) + /* nest IFLA_VF_STATS */
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300925 /* IFLA_VF_STATS_RX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200926 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300927 /* IFLA_VF_STATS_TX_PACKETS */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200928 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300929 /* IFLA_VF_STATS_RX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200930 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300931 /* IFLA_VF_STATS_TX_BYTES */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200932 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300933 /* IFLA_VF_STATS_BROADCAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200934 nla_total_size_64bit(sizeof(__u64)) +
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +0300935 /* IFLA_VF_STATS_MULTICAST */
Nicolas Dichtel343a6d82016-04-25 10:25:14 +0200936 nla_total_size_64bit(sizeof(__u64)) +
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +0300937 /* IFLA_VF_STATS_RX_DROPPED */
938 nla_total_size_64bit(sizeof(__u64)) +
939 /* IFLA_VF_STATS_TX_DROPPED */
940 nla_total_size_64bit(sizeof(__u64)) +
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +0000941 nla_total_size(sizeof(struct ifla_vf_trust)));
Chris Wrightc02db8c2010-05-16 01:05:45 -0700942 return size;
943 } else
Williams, Mitch Aebc08a62010-02-10 01:44:05 +0000944 return 0;
945}
946
David Gibsonc53864f2014-04-24 10:22:36 +1000947static size_t rtnl_port_size(const struct net_device *dev,
948 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -0700949{
950 size_t port_size = nla_total_size(4) /* PORT_VF */
951 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */
Scott Feldman57b61082010-05-17 22:49:55 -0700952 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */
953 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */
954 + nla_total_size(1) /* PROT_VDP_REQUEST */
955 + nla_total_size(2); /* PORT_VDP_RESPONSE */
956 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
957 size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
958 + port_size;
959 size_t port_self_size = nla_total_size(sizeof(struct nlattr))
960 + port_size;
961
David Gibsonc53864f2014-04-24 10:22:36 +1000962 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
963 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -0700964 return 0;
965 if (dev_num_vf(dev->dev.parent))
966 return port_self_size + vf_ports_size +
967 vf_port_size * dev_num_vf(dev->dev.parent);
968 else
969 return port_self_size;
970}
971
David S. Millerb5cdae32017-04-18 15:36:58 -0400972static size_t rtnl_xdp_size(void)
Brenden Blancod1fdd912016-07-19 12:16:49 -0700973{
Sabrina Dubrocab3cfaa32016-11-15 11:16:35 +0100974 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
Martin KaFai Lau58038692017-06-15 17:29:09 -0700975 nla_total_size(1) + /* XDP_ATTACHED */
Jakub Kicinskia25717d2018-07-11 20:36:41 -0700976 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */
Jakub Kicinski4f91da22018-07-11 20:36:38 -0700977 nla_total_size(4); /* XDP_<mode>_PROG_ID */
Brenden Blancod1fdd912016-07-19 12:16:49 -0700978
David S. Millerb5cdae32017-04-18 15:36:58 -0400979 return xdp_size;
Brenden Blancod1fdd912016-07-19 12:16:49 -0700980}
981
Greg Rose115c9b82012-02-21 16:54:48 -0500982static noinline size_t if_nlmsg_size(const struct net_device *dev,
983 u32 ext_filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800984{
985 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
986 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
Stephen Hemminger0b815a12008-09-22 21:28:11 -0700987 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
Thomas Graf339bf982006-11-10 14:10:15 -0800988 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +0200989 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
Thomas Graf339bf982006-11-10 14:10:15 -0800990 + nla_total_size(sizeof(struct rtnl_link_stats))
David S. Miller35c58452016-04-19 19:49:29 -0400991 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
Thomas Graf339bf982006-11-10 14:10:15 -0800992 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
993 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
994 + nla_total_size(4) /* IFLA_TXQLEN */
995 + nla_total_size(4) /* IFLA_WEIGHT */
996 + nla_total_size(4) /* IFLA_MTU */
997 + nla_total_size(4) /* IFLA_LINK */
998 + nla_total_size(4) /* IFLA_MASTER */
Jiri Pirko9a572472012-12-27 23:49:39 +0000999 + nla_total_size(1) /* IFLA_CARRIER */
Ben Greearedbc0bb2012-03-29 12:51:30 +00001000 + nla_total_size(4) /* IFLA_PROMISCUITY */
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001001 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1002 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
Tobias Klauser69197562016-11-30 14:30:37 +01001003 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1004 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
Thomas Graf339bf982006-11-10 14:10:15 -08001005 + nla_total_size(1) /* IFLA_OPERSTATE */
Patrick McHardy38f7b872007-06-13 12:03:51 -07001006 + nla_total_size(1) /* IFLA_LINKMODE */
david decotigny2d3b4792014-03-29 09:48:35 -07001007 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001008 + nla_total_size(4) /* IFLA_LINK_NETNSID */
Serhey Popovychdb833d42017-06-20 14:35:23 +03001009 + nla_total_size(4) /* IFLA_GROUP */
Greg Rose115c9b82012-02-21 16:54:48 -05001010 + nla_total_size(ext_filter_mask
1011 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1012 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
David Gibsonc53864f2014-04-24 10:22:36 +10001013 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
Thomas Graff8ff1822010-11-16 04:30:14 +00001014 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001015 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
Jiri Pirko82f28412014-11-28 14:34:18 +01001016 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001017 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
Nicolas Dichtelc57c7a92016-03-31 18:10:31 +02001018 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
David S. Millerb5cdae32017-04-18 15:36:58 -04001019 + rtnl_xdp_size() /* IFLA_XDP */
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001020 + nla_total_size(4) /* IFLA_EVENT */
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001021 + nla_total_size(4) /* IFLA_NEW_NETNSID */
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001022 + nla_total_size(4) /* IFLA_NEW_IFINDEX */
Colin Ian King03ac7382017-11-06 15:04:54 +00001023 + nla_total_size(1) /* IFLA_PROTO_DOWN */
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001024 + nla_total_size(4) /* IFLA_TARGET_NETNSID */
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001025 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
1026 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001027 + nla_total_size(4) /* IFLA_MIN_MTU */
1028 + nla_total_size(4) /* IFLA_MAX_MTU */
Jiri Benc79e1ad12017-11-02 17:04:38 -02001029 + 0;
Thomas Graf339bf982006-11-10 14:10:15 -08001030}
1031
Scott Feldman57b61082010-05-17 22:49:55 -07001032static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1033{
1034 struct nlattr *vf_ports;
1035 struct nlattr *vf_port;
1036 int vf;
1037 int err;
1038
1039 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
1040 if (!vf_ports)
1041 return -EMSGSIZE;
1042
1043 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1044 vf_port = nla_nest_start(skb, IFLA_VF_PORT);
Scott Feldman8ca94182010-05-28 03:42:18 -07001045 if (!vf_port)
1046 goto nla_put_failure;
David S. Millera6574342012-04-01 20:12:00 -04001047 if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1048 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001049 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
Scott Feldman8ca94182010-05-28 03:42:18 -07001050 if (err == -EMSGSIZE)
1051 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001052 if (err) {
Scott Feldman57b61082010-05-17 22:49:55 -07001053 nla_nest_cancel(skb, vf_port);
1054 continue;
1055 }
1056 nla_nest_end(skb, vf_port);
1057 }
1058
1059 nla_nest_end(skb, vf_ports);
1060
1061 return 0;
Scott Feldman8ca94182010-05-28 03:42:18 -07001062
1063nla_put_failure:
1064 nla_nest_cancel(skb, vf_ports);
1065 return -EMSGSIZE;
Scott Feldman57b61082010-05-17 22:49:55 -07001066}
1067
1068static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1069{
1070 struct nlattr *port_self;
1071 int err;
1072
1073 port_self = nla_nest_start(skb, IFLA_PORT_SELF);
1074 if (!port_self)
1075 return -EMSGSIZE;
1076
1077 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1078 if (err) {
1079 nla_nest_cancel(skb, port_self);
Scott Feldman8ca94182010-05-28 03:42:18 -07001080 return (err == -EMSGSIZE) ? err : 0;
Scott Feldman57b61082010-05-17 22:49:55 -07001081 }
1082
1083 nla_nest_end(skb, port_self);
1084
1085 return 0;
1086}
1087
David Gibsonc53864f2014-04-24 10:22:36 +10001088static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1089 u32 ext_filter_mask)
Scott Feldman57b61082010-05-17 22:49:55 -07001090{
1091 int err;
1092
David Gibsonc53864f2014-04-24 10:22:36 +10001093 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1094 !(ext_filter_mask & RTEXT_FILTER_VF))
Scott Feldman57b61082010-05-17 22:49:55 -07001095 return 0;
1096
1097 err = rtnl_port_self_fill(skb, dev);
1098 if (err)
1099 return err;
1100
1101 if (dev_num_vf(dev->dev.parent)) {
1102 err = rtnl_vf_ports_fill(skb, dev);
1103 if (err)
1104 return err;
1105 }
1106
1107 return 0;
1108}
1109
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001110static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1111{
1112 int err;
Jiri Pirko02637fc2014-11-28 14:34:16 +01001113 struct netdev_phys_item_id ppid;
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001114
1115 err = dev_get_phys_port_id(dev, &ppid);
1116 if (err) {
1117 if (err == -EOPNOTSUPP)
1118 return 0;
1119 return err;
1120 }
1121
1122 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1123 return -EMSGSIZE;
1124
1125 return 0;
1126}
1127
David Aherndb24a902015-03-17 20:23:15 -06001128static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1129{
1130 char name[IFNAMSIZ];
1131 int err;
1132
1133 err = dev_get_phys_port_name(dev, name, sizeof(name));
1134 if (err) {
1135 if (err == -EOPNOTSUPP)
1136 return 0;
1137 return err;
1138 }
1139
Michal Schmidt77ef0332017-05-04 16:48:58 +02001140 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
David Aherndb24a902015-03-17 20:23:15 -06001141 return -EMSGSIZE;
1142
1143 return 0;
1144}
1145
Jiri Pirko82f28412014-11-28 14:34:18 +01001146static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1147{
Florian Fainellibccb3022019-02-06 09:45:46 -08001148 struct netdev_phys_item_id ppid = { };
Jiri Pirko82f28412014-11-28 14:34:18 +01001149 int err;
Jiri Pirko82f28412014-11-28 14:34:18 +01001150
Florian Fainellibccb3022019-02-06 09:45:46 -08001151 err = dev_get_port_parent_id(dev, &ppid, false);
Jiri Pirko82f28412014-11-28 14:34:18 +01001152 if (err) {
1153 if (err == -EOPNOTSUPP)
1154 return 0;
1155 return err;
1156 }
1157
Florian Fainellibccb3022019-02-06 09:45:46 -08001158 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
Jiri Pirko82f28412014-11-28 14:34:18 +01001159 return -EMSGSIZE;
1160
1161 return 0;
1162}
1163
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001164static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1165 struct net_device *dev)
1166{
Roopa Prabhu550bce52016-04-15 20:36:25 -07001167 struct rtnl_link_stats64 *sp;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001168 struct nlattr *attr;
1169
Nicolas Dichtel58414d32016-04-21 18:58:25 +02001170 attr = nla_reserve_64bit(skb, IFLA_STATS64,
1171 sizeof(struct rtnl_link_stats64), IFLA_PAD);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001172 if (!attr)
1173 return -EMSGSIZE;
1174
Roopa Prabhu550bce52016-04-15 20:36:25 -07001175 sp = nla_data(attr);
1176 dev_get_stats(dev, sp);
1177
1178 attr = nla_reserve(skb, IFLA_STATS,
1179 sizeof(struct rtnl_link_stats));
1180 if (!attr)
1181 return -EMSGSIZE;
1182
1183 copy_rtnl_link_stats(nla_data(attr), sp);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001184
1185 return 0;
1186}
1187
1188static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1189 struct net_device *dev,
1190 int vfs_num,
1191 struct nlattr *vfinfo)
1192{
1193 struct ifla_vf_rss_query_en vf_rss_query_en;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001194 struct nlattr *vf, *vfstats, *vfvlanlist;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001195 struct ifla_vf_link_state vf_linkstate;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001196 struct ifla_vf_vlan_info vf_vlan_info;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001197 struct ifla_vf_spoofchk vf_spoofchk;
1198 struct ifla_vf_tx_rate vf_tx_rate;
1199 struct ifla_vf_stats vf_stats;
1200 struct ifla_vf_trust vf_trust;
1201 struct ifla_vf_vlan vf_vlan;
1202 struct ifla_vf_rate vf_rate;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001203 struct ifla_vf_mac vf_mac;
1204 struct ifla_vf_info ivi;
1205
Mintz, Yuval0eed9cf2017-06-07 21:00:33 +03001206 memset(&ivi, 0, sizeof(ivi));
1207
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001208 /* Not all SR-IOV capable drivers support the
1209 * spoofcheck and "RSS query enable" query. Preset to
1210 * -1 so the user space tool can detect that the driver
1211 * didn't report anything.
1212 */
1213 ivi.spoofchk = -1;
1214 ivi.rss_query_en = -1;
1215 ivi.trusted = -1;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001216 /* The default value for VF link state is "auto"
1217 * IFLA_VF_LINK_STATE_AUTO which equals zero
1218 */
1219 ivi.linkstate = 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001220 /* VLAN Protocol by default is 802.1Q */
1221 ivi.vlan_proto = htons(ETH_P_8021Q);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001222 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1223 return 0;
1224
Dan Carpenter775f4f02016-10-13 11:45:28 +03001225 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1226
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001227 vf_mac.vf =
1228 vf_vlan.vf =
Moshe Shemesh79aab092016-09-22 12:11:15 +03001229 vf_vlan_info.vf =
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001230 vf_rate.vf =
1231 vf_tx_rate.vf =
1232 vf_spoofchk.vf =
1233 vf_linkstate.vf =
1234 vf_rss_query_en.vf =
1235 vf_trust.vf = ivi.vf;
1236
1237 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1238 vf_vlan.vlan = ivi.vlan;
1239 vf_vlan.qos = ivi.qos;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001240 vf_vlan_info.vlan = ivi.vlan;
1241 vf_vlan_info.qos = ivi.qos;
1242 vf_vlan_info.vlan_proto = ivi.vlan_proto;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001243 vf_tx_rate.rate = ivi.max_tx_rate;
1244 vf_rate.min_tx_rate = ivi.min_tx_rate;
1245 vf_rate.max_tx_rate = ivi.max_tx_rate;
1246 vf_spoofchk.setting = ivi.spoofchk;
1247 vf_linkstate.link_state = ivi.linkstate;
1248 vf_rss_query_en.setting = ivi.rss_query_en;
1249 vf_trust.setting = ivi.trusted;
1250 vf = nla_nest_start(skb, IFLA_VF_INFO);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001251 if (!vf)
1252 goto nla_put_vfinfo_failure;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001253 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1254 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1255 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1256 &vf_rate) ||
1257 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1258 &vf_tx_rate) ||
1259 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1260 &vf_spoofchk) ||
1261 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1262 &vf_linkstate) ||
1263 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1264 sizeof(vf_rss_query_en),
1265 &vf_rss_query_en) ||
1266 nla_put(skb, IFLA_VF_TRUST,
1267 sizeof(vf_trust), &vf_trust))
Moshe Shemesh79aab092016-09-22 12:11:15 +03001268 goto nla_put_vf_failure;
1269 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST);
1270 if (!vfvlanlist)
1271 goto nla_put_vf_failure;
1272 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1273 &vf_vlan_info)) {
1274 nla_nest_cancel(skb, vfvlanlist);
1275 goto nla_put_vf_failure;
1276 }
1277 nla_nest_end(skb, vfvlanlist);
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001278 memset(&vf_stats, 0, sizeof(vf_stats));
1279 if (dev->netdev_ops->ndo_get_vf_stats)
1280 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1281 &vf_stats);
1282 vfstats = nla_nest_start(skb, IFLA_VF_STATS);
Moshe Shemesh79aab092016-09-22 12:11:15 +03001283 if (!vfstats)
1284 goto nla_put_vf_failure;
Nicolas Dichtel343a6d82016-04-25 10:25:14 +02001285 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1286 vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1287 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1288 vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1289 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1290 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1291 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1292 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1293 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1294 vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1295 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
Eugenia Emantayevc5a9f6f2017-07-17 13:47:07 +03001296 vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1297 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1298 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1299 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1300 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
Moshe Shemesh79aab092016-09-22 12:11:15 +03001301 nla_nest_cancel(skb, vfstats);
1302 goto nla_put_vf_failure;
1303 }
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001304 nla_nest_end(skb, vfstats);
1305 nla_nest_end(skb, vf);
1306 return 0;
Moshe Shemesh79aab092016-09-22 12:11:15 +03001307
1308nla_put_vf_failure:
1309 nla_nest_cancel(skb, vf);
1310nla_put_vfinfo_failure:
1311 nla_nest_cancel(skb, vfinfo);
1312 return -EMSGSIZE;
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001313}
1314
Florian Westphal250fc3d2017-09-26 13:58:41 +02001315static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1316 struct net_device *dev,
1317 u32 ext_filter_mask)
1318{
1319 struct nlattr *vfinfo;
1320 int i, num_vfs;
1321
1322 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1323 return 0;
1324
1325 num_vfs = dev_num_vf(dev->dev.parent);
1326 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1327 return -EMSGSIZE;
1328
1329 if (!dev->netdev_ops->ndo_get_vf_config)
1330 return 0;
1331
1332 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1333 if (!vfinfo)
1334 return -EMSGSIZE;
1335
1336 for (i = 0; i < num_vfs; i++) {
1337 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1338 return -EMSGSIZE;
1339 }
1340
1341 nla_nest_end(skb, vfinfo);
1342 return 0;
1343}
1344
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001345static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1346{
Kangjie Lu5f8e4472016-05-03 16:46:24 -04001347 struct rtnl_link_ifmap map;
1348
1349 memset(&map, 0, sizeof(map));
1350 map.mem_start = dev->mem_start;
1351 map.mem_end = dev->mem_end;
1352 map.base_addr = dev->base_addr;
1353 map.irq = dev->irq;
1354 map.dma = dev->dma;
1355 map.port = dev->if_port;
1356
Nicolas Dichtel270cb4d2016-04-26 10:06:16 +02001357 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001358 return -EMSGSIZE;
1359
1360 return 0;
1361}
1362
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001363static u32 rtnl_xdp_prog_skb(struct net_device *dev)
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001364{
Martin KaFai Lau58038692017-06-15 17:29:09 -07001365 const struct bpf_prog *generic_xdp_prog;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001366
1367 ASSERT_RTNL();
1368
Martin KaFai Lau58038692017-06-15 17:29:09 -07001369 generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001370 if (!generic_xdp_prog)
1371 return 0;
1372 return generic_xdp_prog->aux->id;
1373}
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001374
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001375static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1376{
1377 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf, XDP_QUERY_PROG);
1378}
Jakub Kicinski118b4aa2017-12-01 15:08:55 -08001379
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001380static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1381{
1382 return __dev_xdp_query(dev, dev->netdev_ops->ndo_bpf,
1383 XDP_QUERY_PROG_HW);
1384}
1385
1386static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1387 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1388 u32 (*get_prog_id)(struct net_device *dev))
1389{
1390 u32 curr_id;
1391 int err;
1392
1393 curr_id = get_prog_id(dev);
1394 if (!curr_id)
1395 return 0;
1396
1397 *prog_id = curr_id;
1398 err = nla_put_u32(skb, attr, curr_id);
1399 if (err)
1400 return err;
1401
1402 if (*mode != XDP_ATTACHED_NONE)
1403 *mode = XDP_ATTACHED_MULTI;
1404 else
1405 *mode = tgt_mode;
1406
1407 return 0;
Daniel Borkmannd67b9cd2017-05-12 01:04:46 +02001408}
1409
Brenden Blancod1fdd912016-07-19 12:16:49 -07001410static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1411{
Brenden Blancod1fdd912016-07-19 12:16:49 -07001412 struct nlattr *xdp;
Martin KaFai Lau58038692017-06-15 17:29:09 -07001413 u32 prog_id;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001414 int err;
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001415 u8 mode;
Brenden Blancod1fdd912016-07-19 12:16:49 -07001416
Brenden Blancod1fdd912016-07-19 12:16:49 -07001417 xdp = nla_nest_start(skb, IFLA_XDP);
1418 if (!xdp)
1419 return -EMSGSIZE;
David S. Millerb5cdae32017-04-18 15:36:58 -04001420
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001421 prog_id = 0;
1422 mode = XDP_ATTACHED_NONE;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001423 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1424 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1425 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001426 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001427 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1428 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1429 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001430 goto err_cancel;
Jakub Kicinski202aabe2018-07-16 19:08:50 -07001431 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1432 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1433 if (err)
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001434 goto err_cancel;
1435
Jakub Kicinski4f91da22018-07-11 20:36:38 -07001436 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
Brenden Blancod1fdd912016-07-19 12:16:49 -07001437 if (err)
1438 goto err_cancel;
1439
Jakub Kicinskia25717d2018-07-11 20:36:41 -07001440 if (prog_id && mode != XDP_ATTACHED_MULTI) {
Martin KaFai Lau58038692017-06-15 17:29:09 -07001441 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1442 if (err)
1443 goto err_cancel;
1444 }
1445
Brenden Blancod1fdd912016-07-19 12:16:49 -07001446 nla_nest_end(skb, xdp);
1447 return 0;
1448
1449err_cancel:
1450 nla_nest_cancel(skb, xdp);
1451 return err;
1452}
1453
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001454static u32 rtnl_get_event(unsigned long event)
1455{
1456 u32 rtnl_event_type = IFLA_EVENT_NONE;
1457
1458 switch (event) {
1459 case NETDEV_REBOOT:
1460 rtnl_event_type = IFLA_EVENT_REBOOT;
1461 break;
1462 case NETDEV_FEAT_CHANGE:
1463 rtnl_event_type = IFLA_EVENT_FEATURES;
1464 break;
1465 case NETDEV_BONDING_FAILOVER:
1466 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1467 break;
1468 case NETDEV_NOTIFY_PEERS:
1469 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1470 break;
1471 case NETDEV_RESEND_IGMP:
1472 rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1473 break;
1474 case NETDEV_CHANGEINFODATA:
1475 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1476 break;
1477 default:
1478 break;
1479 }
1480
1481 return rtnl_event_type;
1482}
1483
Florian Westphal79110a02017-09-26 13:58:40 +02001484static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1485{
1486 const struct net_device *upper_dev;
1487 int ret = 0;
1488
1489 rcu_read_lock();
1490
1491 upper_dev = netdev_master_upper_dev_get_rcu(dev);
1492 if (upper_dev)
1493 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1494
1495 rcu_read_unlock();
1496 return ret;
1497}
1498
1499static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev)
1500{
1501 int ifindex = dev_get_iflink(dev);
1502
1503 if (dev->ifindex == ifindex)
1504 return 0;
1505
1506 return nla_put_u32(skb, IFLA_LINK, ifindex);
1507}
1508
Florian Westphal6c557002017-10-02 23:50:05 +02001509static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1510 struct net_device *dev)
1511{
1512 char buf[IFALIASZ];
1513 int ret;
1514
1515 ret = dev_get_alias(dev, buf, sizeof(buf));
1516 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1517}
1518
Florian Westphalb1e66b92017-09-26 13:58:42 +02001519static int rtnl_fill_link_netnsid(struct sk_buff *skb,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001520 const struct net_device *dev,
1521 struct net *src_net)
Florian Westphalb1e66b92017-09-26 13:58:42 +02001522{
1523 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1524 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1525
1526 if (!net_eq(dev_net(dev), link_net)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001527 int id = peernet2id_alloc(src_net, link_net);
Florian Westphalb1e66b92017-09-26 13:58:42 +02001528
1529 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1530 return -EMSGSIZE;
1531 }
1532 }
1533
1534 return 0;
1535}
1536
Florian Westphal070cbf52017-10-16 15:44:35 +02001537static int rtnl_fill_link_af(struct sk_buff *skb,
1538 const struct net_device *dev,
1539 u32 ext_filter_mask)
1540{
1541 const struct rtnl_af_ops *af_ops;
1542 struct nlattr *af_spec;
1543
1544 af_spec = nla_nest_start(skb, IFLA_AF_SPEC);
1545 if (!af_spec)
1546 return -EMSGSIZE;
1547
Florian Westphal5fa85a02017-10-16 15:44:36 +02001548 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Florian Westphal070cbf52017-10-16 15:44:35 +02001549 struct nlattr *af;
1550 int err;
1551
1552 if (!af_ops->fill_link_af)
1553 continue;
1554
1555 af = nla_nest_start(skb, af_ops->family);
1556 if (!af)
1557 return -EMSGSIZE;
1558
1559 err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1560 /*
1561 * Caller may return ENODATA to indicate that there
1562 * was no data to be dumped. This is not an error, it
1563 * means we should trim the attribute header and
1564 * continue.
1565 */
1566 if (err == -ENODATA)
1567 nla_nest_cancel(skb, af);
1568 else if (err < 0)
1569 return -EMSGSIZE;
1570
1571 nla_nest_end(skb, af);
1572 }
1573
1574 nla_nest_end(skb, af_spec);
1575 return 0;
1576}
1577
Jiri Benc79e1ad12017-11-02 17:04:38 -02001578static int rtnl_fill_ifinfo(struct sk_buff *skb,
1579 struct net_device *dev, struct net *src_net,
Patrick McHardy575c3e22007-05-22 17:00:49 -07001580 int type, u32 pid, u32 seq, u32 change,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001581 unsigned int flags, u32 ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001582 u32 event, int *new_nsid, int new_ifindex,
1583 int tgt_netnsid)
Thomas Grafb60c5112006-08-04 23:05:34 -07001584{
1585 struct ifinfomsg *ifm;
1586 struct nlmsghdr *nlh;
1587
Eric Dumazet2907c352011-05-25 07:34:04 +00001588 ASSERT_RTNL();
Thomas Grafb60c5112006-08-04 23:05:34 -07001589 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1590 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08001591 return -EMSGSIZE;
Thomas Grafb60c5112006-08-04 23:05:34 -07001592
1593 ifm = nlmsg_data(nlh);
1594 ifm->ifi_family = AF_UNSPEC;
1595 ifm->__ifi_pad = 0;
1596 ifm->ifi_type = dev->type;
1597 ifm->ifi_index = dev->ifindex;
1598 ifm->ifi_flags = dev_get_flags(dev);
1599 ifm->ifi_change = change;
1600
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001601 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
Jiri Benc79e1ad12017-11-02 17:04:38 -02001602 goto nla_put_failure;
1603
David S. Millera6574342012-04-01 20:12:00 -04001604 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1605 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1606 nla_put_u8(skb, IFLA_OPERSTATE,
1607 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1608 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1609 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001610 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1611 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
David S. Millera6574342012-04-01 20:12:00 -04001612 nla_put_u32(skb, IFLA_GROUP, dev->group) ||
Ben Greearedbc0bb2012-03-29 12:51:30 +00001613 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001614 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
Eric Dumazetc70ce022016-03-21 09:55:10 -07001615 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1616 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001617#ifdef CONFIG_RPS
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00001618 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
Mark A. Greer1d69c2b2012-07-20 13:35:13 +00001619#endif
Florian Westphal79110a02017-09-26 13:58:40 +02001620 nla_put_iflink(skb, dev) ||
1621 put_master_ifindex(skb, dev) ||
Jiri Pirko9a572472012-12-27 23:49:39 +00001622 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
David S. Millera6574342012-04-01 20:12:00 -04001623 (dev->qdisc &&
1624 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
Florian Westphal6c557002017-10-02 23:50:05 +02001625 nla_put_ifalias(skb, dev) ||
david decotigny2d3b4792014-03-29 09:48:35 -07001626 nla_put_u32(skb, IFLA_CARRIER_CHANGES,
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001627 atomic_read(&dev->carrier_up_count) +
1628 atomic_read(&dev->carrier_down_count)) ||
1629 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
1630 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1631 atomic_read(&dev->carrier_up_count)) ||
1632 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1633 atomic_read(&dev->carrier_down_count)))
David S. Millera6574342012-04-01 20:12:00 -04001634 goto nla_put_failure;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07001635
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001636 if (event != IFLA_EVENT_NONE) {
1637 if (nla_put_u32(skb, IFLA_EVENT, event))
1638 goto nla_put_failure;
1639 }
1640
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001641 if (rtnl_fill_link_ifmap(skb, dev))
1642 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (dev->addr_len) {
David S. Millera6574342012-04-01 20:12:00 -04001645 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1646 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1647 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Jiri Pirko66cae9e2013-07-29 18:16:50 +02001650 if (rtnl_phys_port_id_fill(skb, dev))
1651 goto nla_put_failure;
1652
David Aherndb24a902015-03-17 20:23:15 -06001653 if (rtnl_phys_port_name_fill(skb, dev))
1654 goto nla_put_failure;
1655
Jiri Pirko82f28412014-11-28 14:34:18 +01001656 if (rtnl_phys_switch_id_fill(skb, dev))
1657 goto nla_put_failure;
1658
Hannes Frederic Sowab22b9412015-11-17 14:16:52 +01001659 if (rtnl_fill_stats(skb, dev))
Pavel Emelyanov96e74082008-05-21 14:12:46 -07001660 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Florian Westphal250fc3d2017-09-26 13:58:41 +02001662 if (rtnl_fill_vf(skb, dev, ext_filter_mask))
David S. Millera6574342012-04-01 20:12:00 -04001663 goto nla_put_failure;
Scott Feldman57b61082010-05-17 22:49:55 -07001664
David Gibsonc53864f2014-04-24 10:22:36 +10001665 if (rtnl_port_fill(skb, dev, ext_filter_mask))
Scott Feldman57b61082010-05-17 22:49:55 -07001666 goto nla_put_failure;
1667
Brenden Blancod1fdd912016-07-19 12:16:49 -07001668 if (rtnl_xdp_fill(skb, dev))
1669 goto nla_put_failure;
1670
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01001671 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
Patrick McHardy38f7b872007-06-13 12:03:51 -07001672 if (rtnl_link_fill(skb, dev) < 0)
1673 goto nla_put_failure;
1674 }
1675
Jiri Benc79e1ad12017-11-02 17:04:38 -02001676 if (rtnl_fill_link_netnsid(skb, dev, src_net))
Florian Westphalb1e66b92017-09-26 13:58:42 +02001677 goto nla_put_failure;
Nicolas Dichteld37512a2015-01-15 15:11:16 +01001678
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001679 if (new_nsid &&
1680 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1681 goto nla_put_failure;
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001682 if (new_ifindex &&
1683 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1684 goto nla_put_failure;
1685
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02001686
Florian Westphal5fa85a02017-10-16 15:44:36 +02001687 rcu_read_lock();
Florian Westphal070cbf52017-10-16 15:44:35 +02001688 if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
Florian Westphal5fa85a02017-10-16 15:44:36 +02001689 goto nla_put_failure_rcu;
1690 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00001691
Johannes Berg053c0952015-01-16 22:09:00 +01001692 nlmsg_end(skb, nlh);
1693 return 0;
Thomas Grafb60c5112006-08-04 23:05:34 -07001694
Florian Westphal5fa85a02017-10-16 15:44:36 +02001695nla_put_failure_rcu:
1696 rcu_read_unlock();
Thomas Grafb60c5112006-08-04 23:05:34 -07001697nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08001698 nlmsg_cancel(skb, nlh);
1699 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Jiri Pirkof7b12602014-02-18 20:53:18 +01001702static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1703 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1704 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1705 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1706 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
1707 [IFLA_MTU] = { .type = NLA_U32 },
1708 [IFLA_LINK] = { .type = NLA_U32 },
1709 [IFLA_MASTER] = { .type = NLA_U32 },
1710 [IFLA_CARRIER] = { .type = NLA_U8 },
1711 [IFLA_TXQLEN] = { .type = NLA_U32 },
1712 [IFLA_WEIGHT] = { .type = NLA_U32 },
1713 [IFLA_OPERSTATE] = { .type = NLA_U8 },
1714 [IFLA_LINKMODE] = { .type = NLA_U8 },
1715 [IFLA_LINKINFO] = { .type = NLA_NESTED },
1716 [IFLA_NET_NS_PID] = { .type = NLA_U32 },
1717 [IFLA_NET_NS_FD] = { .type = NLA_U32 },
Nicolas Dichtel2459b4c2017-10-11 16:24:48 +02001718 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1719 * allow 0-length string (needed to remove an alias).
1720 */
1721 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001722 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED },
1723 [IFLA_VF_PORTS] = { .type = NLA_NESTED },
1724 [IFLA_PORT_SELF] = { .type = NLA_NESTED },
1725 [IFLA_AF_SPEC] = { .type = NLA_NESTED },
1726 [IFLA_EXT_MASK] = { .type = NLA_U32 },
1727 [IFLA_PROMISCUITY] = { .type = NLA_U32 },
1728 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 },
1729 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 },
Stephen Hemminger46e6b992017-12-07 15:40:19 -08001730 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 },
1731 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 },
Jiri Pirko02637fc2014-11-28 14:34:16 +01001732 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
david decotigny2d3b4792014-03-29 09:48:35 -07001733 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
Jiri Pirko82f28412014-11-28 14:34:18 +01001734 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
Nicolas Dichtel317f4812015-01-15 15:11:18 +01001735 [IFLA_LINK_NETNSID] = { .type = NLA_S32 },
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07001736 [IFLA_PROTO_DOWN] = { .type = NLA_U8 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001737 [IFLA_XDP] = { .type = NLA_NESTED },
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04001738 [IFLA_EVENT] = { .type = NLA_U32 },
Serhey Popovychdb833d42017-06-20 14:35:23 +03001739 [IFLA_GROUP] = { .type = NLA_U32 },
Christian Brauner7e4a8d52018-09-04 21:53:53 +02001740 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
David Decotignyb2d3bcf2018-01-18 09:59:13 -08001741 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
1742 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
Stephen Hemminger3e7a50c2018-07-27 13:43:22 -07001743 [IFLA_MIN_MTU] = { .type = NLA_U32 },
1744 [IFLA_MAX_MTU] = { .type = NLA_U32 },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001745};
1746
1747static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1748 [IFLA_INFO_KIND] = { .type = NLA_STRING },
1749 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
1750 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING },
1751 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED },
1752};
1753
Jiri Pirkof7b12602014-02-18 20:53:18 +01001754static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
Daniel Borkmann364d5712015-02-05 18:44:04 +01001755 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) },
1756 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) },
Moshe Shemesh79aab092016-09-22 12:11:15 +03001757 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED },
Daniel Borkmann364d5712015-02-05 18:44:04 +01001758 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) },
1759 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1760 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1761 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03001762 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001763 [IFLA_VF_STATS] = { .type = NLA_NESTED },
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00001764 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) },
Eli Cohencc8e27c2016-03-11 22:58:34 +02001765 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) },
1766 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) },
Eran Ben Elisha3b766cd2015-06-15 17:59:07 +03001767};
1768
Jiri Pirkof7b12602014-02-18 20:53:18 +01001769static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1770 [IFLA_PORT_VF] = { .type = NLA_U32 },
1771 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1772 .len = PORT_PROFILE_MAX },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001773 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1774 .len = PORT_UUID_MAX },
1775 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING,
1776 .len = PORT_UUID_MAX },
1777 [IFLA_PORT_REQUEST] = { .type = NLA_U8, },
1778 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, },
Daniel Borkmann025331d2017-02-17 01:56:11 +01001779
1780 /* Unused, but we need to keep it here since user space could
1781 * fill it. It's also broken with regard to NLA_BINARY use in
1782 * combination with structs.
1783 */
1784 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1785 .len = sizeof(struct ifla_port_vsi) },
Jiri Pirkof7b12602014-02-18 20:53:18 +01001786};
1787
Brenden Blancod1fdd912016-07-19 12:16:49 -07001788static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1789 [IFLA_XDP_FD] = { .type = NLA_S32 },
1790 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 },
Daniel Borkmann85de8572016-11-28 23:16:54 +01001791 [IFLA_XDP_FLAGS] = { .type = NLA_U32 },
Martin KaFai Lau58038692017-06-15 17:29:09 -07001792 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 },
Brenden Blancod1fdd912016-07-19 12:16:49 -07001793};
1794
David Aherndc599f72016-02-02 08:17:07 -08001795static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1796{
1797 const struct rtnl_link_ops *ops = NULL;
1798 struct nlattr *linfo[IFLA_INFO_MAX + 1];
1799
Johannes Bergfceb6432017-04-12 14:34:07 +02001800 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla,
1801 ifla_info_policy, NULL) < 0)
David Aherndc599f72016-02-02 08:17:07 -08001802 return NULL;
1803
1804 if (linfo[IFLA_INFO_KIND]) {
1805 char kind[MODULE_NAME_LEN];
1806
1807 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1808 ops = rtnl_link_ops_get(kind);
1809 }
1810
1811 return ops;
1812}
1813
1814static bool link_master_filtered(struct net_device *dev, int master_idx)
1815{
1816 struct net_device *master;
1817
1818 if (!master_idx)
1819 return false;
1820
1821 master = netdev_master_upper_dev_get(dev);
1822 if (!master || master->ifindex != master_idx)
1823 return true;
1824
1825 return false;
1826}
1827
1828static bool link_kind_filtered(const struct net_device *dev,
1829 const struct rtnl_link_ops *kind_ops)
1830{
1831 if (kind_ops && dev->rtnl_link_ops != kind_ops)
1832 return true;
1833
1834 return false;
1835}
1836
1837static bool link_dump_filtered(struct net_device *dev,
1838 int master_idx,
1839 const struct rtnl_link_ops *kind_ops)
1840{
1841 if (link_master_filtered(dev, master_idx) ||
1842 link_kind_filtered(dev, kind_ops))
1843 return true;
1844
1845 return false;
1846}
1847
Christian Braunerc383edc2018-09-04 21:53:47 +02001848/**
1849 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1850 * @sk: netlink socket
1851 * @netnsid: network namespace identifier
1852 *
1853 * Returns the network namespace identified by netnsid on success or an error
1854 * pointer on failure.
1855 */
1856struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
Jiri Benc79e1ad12017-11-02 17:04:38 -02001857{
1858 struct net *net;
1859
Andrei Vaginf428fe42018-01-02 23:27:33 -08001860 net = get_net_ns_by_id(sock_net(sk), netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001861 if (!net)
1862 return ERR_PTR(-EINVAL);
1863
1864 /* For now, the caller is required to have CAP_NET_ADMIN in
1865 * the user namespace owning the target net ns.
1866 */
Andrei Vaginf428fe42018-01-02 23:27:33 -08001867 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
Jiri Benc79e1ad12017-11-02 17:04:38 -02001868 put_net(net);
1869 return ERR_PTR(-EACCES);
1870 }
1871 return net;
1872}
Christian Braunerc383edc2018-09-04 21:53:47 +02001873EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001874
David Ahern905cf0a2018-10-07 20:16:30 -07001875static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
1876 bool strict_check, struct nlattr **tb,
1877 struct netlink_ext_ack *extack)
1878{
1879 int hdrlen;
1880
1881 if (strict_check) {
1882 struct ifinfomsg *ifm;
1883
1884 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
1885 NL_SET_ERR_MSG(extack, "Invalid header for link dump");
1886 return -EINVAL;
1887 }
1888
1889 ifm = nlmsg_data(nlh);
1890 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
1891 ifm->ifi_change) {
1892 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
1893 return -EINVAL;
1894 }
1895 if (ifm->ifi_index) {
1896 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
1897 return -EINVAL;
1898 }
1899
1900 return nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
1901 ifla_policy, extack);
1902 }
1903
1904 /* A hack to preserve kernel<->userspace interface.
1905 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1906 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1907 * what iproute2 < v3.9.0 used.
1908 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1909 * attribute, its netlink message is shorter than struct ifinfomsg.
1910 */
1911 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
1912 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1913
1914 return nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, extack);
1915}
1916
Thomas Grafb60c5112006-08-04 23:05:34 -07001917static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
David Ahern905cf0a2018-10-07 20:16:30 -07001919 struct netlink_ext_ack *extack = cb->extack;
1920 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001921 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02001922 struct net *tgt_net = net;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001923 int h, s_h;
1924 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 struct net_device *dev;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001926 struct hlist_head *head;
Greg Rose115c9b82012-02-21 16:54:48 -05001927 struct nlattr *tb[IFLA_MAX+1];
1928 u32 ext_filter_mask = 0;
David Aherndc599f72016-02-02 08:17:07 -08001929 const struct rtnl_link_ops *kind_ops = NULL;
1930 unsigned int flags = NLM_F_MULTI;
1931 int master_idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001932 int netnsid = -1;
David Ahern905cf0a2018-10-07 20:16:30 -07001933 int err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001935 s_h = cb->args[0];
1936 s_idx = cb->args[1];
1937
David Ahern905cf0a2018-10-07 20:16:30 -07001938 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
1939 if (err < 0) {
1940 if (cb->strict_check)
1941 return err;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02001942
David Ahern905cf0a2018-10-07 20:16:30 -07001943 goto walk_entries;
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00001944 }
Greg Rose115c9b82012-02-21 16:54:48 -05001945
David Ahern905cf0a2018-10-07 20:16:30 -07001946 for (i = 0; i <= IFLA_MAX; ++i) {
1947 if (!tb[i])
1948 continue;
1949
1950 /* new attributes should only be added with strict checking */
1951 switch (i) {
1952 case IFLA_TARGET_NETNSID:
1953 netnsid = nla_get_s32(tb[i]);
1954 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
1955 if (IS_ERR(tgt_net)) {
1956 NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
1957 return PTR_ERR(tgt_net);
1958 }
1959 break;
1960 case IFLA_EXT_MASK:
1961 ext_filter_mask = nla_get_u32(tb[i]);
1962 break;
1963 case IFLA_MASTER:
1964 master_idx = nla_get_u32(tb[i]);
1965 break;
1966 case IFLA_LINKINFO:
1967 kind_ops = linkinfo_to_kind_ops(tb[i]);
1968 break;
1969 default:
1970 if (cb->strict_check) {
1971 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
1972 return -EINVAL;
1973 }
1974 }
1975 }
1976
1977 if (master_idx || kind_ops)
1978 flags |= NLM_F_DUMP_FILTERED;
1979
1980walk_entries:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001981 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1982 idx = 0;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001983 head = &tgt_net->dev_index_head[h];
Eric Dumazetcac5e652015-02-27 09:42:50 -08001984 hlist_for_each_entry(dev, head, index_hlist) {
David Aherndc599f72016-02-02 08:17:07 -08001985 if (link_dump_filtered(dev, master_idx, kind_ops))
Zhang Shengju3f0ae052016-11-19 23:28:32 +08001986 goto cont;
Eric Dumazet7c28bd02009-10-24 06:13:17 -07001987 if (idx < s_idx)
1988 goto cont;
Jiri Benc79e1ad12017-11-02 17:04:38 -02001989 err = rtnl_fill_ifinfo(skb, dev, net,
1990 RTM_NEWLINK,
David Gibson973462b2014-04-24 10:22:35 +10001991 NETLINK_CB(cb->skb).portid,
David Ahern905cf0a2018-10-07 20:16:30 -07001992 nlh->nlmsg_seq, 0, flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01001993 ext_filter_mask, 0, NULL, 0,
Jiri Benc79e1ad12017-11-02 17:04:38 -02001994 netnsid);
David Gibson973462b2014-04-24 10:22:35 +10001995
David Ahernf6c57752017-05-15 23:19:17 -07001996 if (err < 0) {
1997 if (likely(skb->len))
1998 goto out;
1999
2000 goto out_err;
2001 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002002cont:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002003 idx++;
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002006out:
David Ahernf6c57752017-05-15 23:19:17 -07002007 err = skb->len;
2008out_err:
Eric Dumazet7c28bd02009-10-24 06:13:17 -07002009 cb->args[1] = idx;
2010 cb->args[0] = h;
Jakub Sitnickid0225782017-08-09 17:39:12 +02002011 cb->seq = net->dev_base_seq;
2012 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Jiri Benc79e1ad12017-11-02 17:04:38 -02002013 if (netnsid >= 0)
2014 put_net(tgt_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
David Ahernf6c57752017-05-15 23:19:17 -07002016 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Johannes Bergfceb6432017-04-12 14:34:07 +02002019int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
2020 struct netlink_ext_ack *exterr)
Jiri Pirkof7b12602014-02-18 20:53:18 +01002021{
Johannes Bergfceb6432017-04-12 14:34:07 +02002022 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr);
Jiri Pirkof7b12602014-02-18 20:53:18 +01002023}
2024EXPORT_SYMBOL(rtnl_nla_parse_ifla);
Scott Feldman57b61082010-05-17 22:49:55 -07002025
Eric W. Biederman81adee42009-11-08 00:53:51 -08002026struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2027{
2028 struct net *net;
2029 /* Examine the link attributes and figure out which
2030 * network namespace we are talking about.
2031 */
2032 if (tb[IFLA_NET_NS_PID])
2033 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
Eric W. Biedermanf0630522011-05-04 17:51:50 -07002034 else if (tb[IFLA_NET_NS_FD])
2035 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
Eric W. Biederman81adee42009-11-08 00:53:51 -08002036 else
2037 net = get_net(src_net);
2038 return net;
2039}
2040EXPORT_SYMBOL(rtnl_link_get_net);
2041
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002042/* Figure out which network namespace we are talking about by
2043 * examining the link attributes in the following order:
2044 *
2045 * 1. IFLA_NET_NS_PID
2046 * 2. IFLA_NET_NS_FD
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002047 * 3. IFLA_TARGET_NETNSID
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002048 */
2049static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2050 struct nlattr *tb[])
2051{
2052 struct net *net;
2053
2054 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2055 return rtnl_link_get_net(src_net, tb);
2056
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002057 if (!tb[IFLA_TARGET_NETNSID])
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002058 return get_net(src_net);
2059
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002060 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002061 if (!net)
2062 return ERR_PTR(-EINVAL);
2063
2064 return net;
2065}
2066
2067static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2068 struct net *src_net,
2069 struct nlattr *tb[], int cap)
2070{
2071 struct net *net;
2072
2073 net = rtnl_link_get_net_by_nlattr(src_net, tb);
2074 if (IS_ERR(net))
2075 return net;
2076
2077 if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2078 put_net(net);
2079 return ERR_PTR(-EPERM);
2080 }
2081
2082 return net;
2083}
2084
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002085/* Verify that rtnetlink requests do not pass additional properties
2086 * potentially referring to different network namespaces.
2087 */
2088static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2089 struct netlink_ext_ack *extack,
2090 bool netns_id_only)
2091{
2092
2093 if (netns_id_only) {
2094 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2095 return 0;
2096
2097 NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2098 return -EOPNOTSUPP;
2099 }
2100
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002101 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002102 goto invalid_attr;
2103
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002104 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002105 goto invalid_attr;
2106
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002107 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002108 goto invalid_attr;
2109
2110 return 0;
2111
2112invalid_attr:
2113 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2114 return -EINVAL;
2115}
2116
Thomas Graf1840bb12008-02-23 19:54:36 -08002117static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
2118{
2119 if (dev) {
2120 if (tb[IFLA_ADDRESS] &&
2121 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2122 return -EINVAL;
2123
2124 if (tb[IFLA_BROADCAST] &&
2125 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2126 return -EINVAL;
2127 }
2128
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002129 if (tb[IFLA_AF_SPEC]) {
2130 struct nlattr *af;
2131 int rem, err;
2132
2133 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2134 const struct rtnl_af_ops *af_ops;
2135
Florian Westphal5fa85a02017-10-16 15:44:36 +02002136 rcu_read_lock();
2137 af_ops = rtnl_af_lookup(nla_type(af));
2138 if (!af_ops) {
2139 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002140 return -EAFNOSUPPORT;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002141 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002142
Florian Westphal5fa85a02017-10-16 15:44:36 +02002143 if (!af_ops->set_link_af) {
2144 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002145 return -EOPNOTSUPP;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002146 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002147
2148 if (af_ops->validate_link_af) {
Kurt Van Dijck6d3a9a62011-01-26 04:55:24 +00002149 err = af_ops->validate_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002150 if (err < 0) {
2151 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002152 return err;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002153 }
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002154 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02002155
2156 rcu_read_unlock();
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002157 }
2158 }
2159
Thomas Graf1840bb12008-02-23 19:54:36 -08002160 return 0;
2161}
2162
Eli Cohencc8e27c2016-03-11 22:58:34 +02002163static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2164 int guid_type)
2165{
2166 const struct net_device_ops *ops = dev->netdev_ops;
2167
2168 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2169}
2170
2171static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2172{
2173 if (dev->type != ARPHRD_INFINIBAND)
2174 return -EOPNOTSUPP;
2175
2176 return handle_infiniband_guid(dev, ivt, guid_type);
2177}
2178
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002179static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
Chris Wrightc02db8c2010-05-16 01:05:45 -07002180{
Chris Wrightc02db8c2010-05-16 01:05:45 -07002181 const struct net_device_ops *ops = dev->netdev_ops;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002182 int err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002183
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002184 if (tb[IFLA_VF_MAC]) {
2185 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
Vlad Zolotarov01a3d792015-03-30 21:35:23 +03002186
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002187 err = -EOPNOTSUPP;
2188 if (ops->ndo_set_vf_mac)
2189 err = ops->ndo_set_vf_mac(dev, ivm->vf,
2190 ivm->mac);
2191 if (err < 0)
2192 return err;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002193 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002194
2195 if (tb[IFLA_VF_VLAN]) {
2196 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2197
2198 err = -EOPNOTSUPP;
2199 if (ops->ndo_set_vf_vlan)
2200 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
Moshe Shemesh79aab092016-09-22 12:11:15 +03002201 ivv->qos,
2202 htons(ETH_P_8021Q));
2203 if (err < 0)
2204 return err;
2205 }
2206
2207 if (tb[IFLA_VF_VLAN_LIST]) {
2208 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2209 struct nlattr *attr;
2210 int rem, len = 0;
2211
2212 err = -EOPNOTSUPP;
2213 if (!ops->ndo_set_vf_vlan)
2214 return err;
2215
2216 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2217 if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2218 nla_len(attr) < NLA_HDRLEN) {
2219 return -EINVAL;
2220 }
2221 if (len >= MAX_VLAN_LIST_LEN)
2222 return -EOPNOTSUPP;
2223 ivvl[len] = nla_data(attr);
2224
2225 len++;
2226 }
Arnd Bergmannfa34cd92016-09-30 18:13:49 +02002227 if (len == 0)
2228 return -EINVAL;
2229
Moshe Shemesh79aab092016-09-22 12:11:15 +03002230 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2231 ivvl[0]->qos, ivvl[0]->vlan_proto);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002232 if (err < 0)
2233 return err;
2234 }
2235
2236 if (tb[IFLA_VF_TX_RATE]) {
2237 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2238 struct ifla_vf_info ivf;
2239
2240 err = -EOPNOTSUPP;
2241 if (ops->ndo_get_vf_config)
2242 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2243 if (err < 0)
2244 return err;
2245
2246 err = -EOPNOTSUPP;
2247 if (ops->ndo_set_vf_rate)
2248 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2249 ivf.min_tx_rate,
2250 ivt->rate);
2251 if (err < 0)
2252 return err;
2253 }
2254
2255 if (tb[IFLA_VF_RATE]) {
2256 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2257
2258 err = -EOPNOTSUPP;
2259 if (ops->ndo_set_vf_rate)
2260 err = ops->ndo_set_vf_rate(dev, ivt->vf,
2261 ivt->min_tx_rate,
2262 ivt->max_tx_rate);
2263 if (err < 0)
2264 return err;
2265 }
2266
2267 if (tb[IFLA_VF_SPOOFCHK]) {
2268 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2269
2270 err = -EOPNOTSUPP;
2271 if (ops->ndo_set_vf_spoofchk)
2272 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2273 ivs->setting);
2274 if (err < 0)
2275 return err;
2276 }
2277
2278 if (tb[IFLA_VF_LINK_STATE]) {
2279 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2280
2281 err = -EOPNOTSUPP;
2282 if (ops->ndo_set_vf_link_state)
2283 err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2284 ivl->link_state);
2285 if (err < 0)
2286 return err;
2287 }
2288
2289 if (tb[IFLA_VF_RSS_QUERY_EN]) {
2290 struct ifla_vf_rss_query_en *ivrssq_en;
2291
2292 err = -EOPNOTSUPP;
2293 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2294 if (ops->ndo_set_vf_rss_query_en)
2295 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2296 ivrssq_en->setting);
2297 if (err < 0)
2298 return err;
2299 }
2300
Hiroshi Shimamotodd461d62015-08-28 06:57:55 +00002301 if (tb[IFLA_VF_TRUST]) {
2302 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2303
2304 err = -EOPNOTSUPP;
2305 if (ops->ndo_set_vf_trust)
2306 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2307 if (err < 0)
2308 return err;
2309 }
2310
Eli Cohencc8e27c2016-03-11 22:58:34 +02002311 if (tb[IFLA_VF_IB_NODE_GUID]) {
2312 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2313
2314 if (!ops->ndo_set_vf_guid)
2315 return -EOPNOTSUPP;
2316
2317 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2318 }
2319
2320 if (tb[IFLA_VF_IB_PORT_GUID]) {
2321 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2322
2323 if (!ops->ndo_set_vf_guid)
2324 return -EOPNOTSUPP;
2325
2326 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2327 }
2328
Chris Wrightc02db8c2010-05-16 01:05:45 -07002329 return err;
2330}
2331
David Ahern33eaf2a2017-10-04 17:48:46 -07002332static int do_set_master(struct net_device *dev, int ifindex,
2333 struct netlink_ext_ack *extack)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002334{
Jiri Pirko898e5062013-01-03 22:48:52 +00002335 struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002336 const struct net_device_ops *ops;
2337 int err;
2338
Jiri Pirko898e5062013-01-03 22:48:52 +00002339 if (upper_dev) {
2340 if (upper_dev->ifindex == ifindex)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002341 return 0;
Jiri Pirko898e5062013-01-03 22:48:52 +00002342 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002343 if (ops->ndo_del_slave) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002344 err = ops->ndo_del_slave(upper_dev, dev);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002345 if (err)
2346 return err;
2347 } else {
2348 return -EOPNOTSUPP;
2349 }
2350 }
2351
2352 if (ifindex) {
Jiri Pirko898e5062013-01-03 22:48:52 +00002353 upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2354 if (!upper_dev)
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002355 return -EINVAL;
Jiri Pirko898e5062013-01-03 22:48:52 +00002356 ops = upper_dev->netdev_ops;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002357 if (ops->ndo_add_slave) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002358 err = ops->ndo_add_slave(upper_dev, dev, extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002359 if (err)
2360 return err;
2361 } else {
2362 return -EOPNOTSUPP;
2363 }
2364 }
2365 return 0;
2366}
2367
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002368#define DO_SETLINK_MODIFIED 0x01
Nicolas Dichtelba998902014-09-01 16:07:29 +02002369/* notify flag means notify + modified. */
2370#define DO_SETLINK_NOTIFY 0x03
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002371static int do_setlink(const struct sk_buff *skb,
2372 struct net_device *dev, struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002373 struct netlink_ext_ack *extack,
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002374 struct nlattr **tb, char *ifname, int status)
Patrick McHardy0157f602007-06-13 12:03:36 -07002375{
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002376 const struct net_device_ops *ops = dev->netdev_ops;
Patrick McHardy0157f602007-06-13 12:03:36 -07002377 int err;
2378
Eric Dumazet644c7ee2018-06-05 09:25:19 -07002379 err = validate_linkmsg(dev, tb);
2380 if (err < 0)
2381 return err;
2382
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002383 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002384 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
2385 tb, CAP_NET_ADMIN);
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002386 if (IS_ERR(net)) {
2387 err = PTR_ERR(net);
2388 goto errout;
2389 }
Christian Brauner7c4f63b2018-01-24 15:26:32 +01002390
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002391 err = dev_change_net_namespace(dev, net, ifname);
2392 put_net(net);
2393 if (err)
2394 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002395 status |= DO_SETLINK_MODIFIED;
Eric W. Biedermand8a5ec62007-09-12 13:57:04 +02002396 }
2397
Patrick McHardy0157f602007-06-13 12:03:36 -07002398 if (tb[IFLA_MAP]) {
2399 struct rtnl_link_ifmap *u_map;
2400 struct ifmap k_map;
2401
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002402 if (!ops->ndo_set_config) {
Patrick McHardy0157f602007-06-13 12:03:36 -07002403 err = -EOPNOTSUPP;
2404 goto errout;
2405 }
2406
2407 if (!netif_device_present(dev)) {
2408 err = -ENODEV;
2409 goto errout;
2410 }
2411
2412 u_map = nla_data(tb[IFLA_MAP]);
2413 k_map.mem_start = (unsigned long) u_map->mem_start;
2414 k_map.mem_end = (unsigned long) u_map->mem_end;
2415 k_map.base_addr = (unsigned short) u_map->base_addr;
2416 k_map.irq = (unsigned char) u_map->irq;
2417 k_map.dma = (unsigned char) u_map->dma;
2418 k_map.port = (unsigned char) u_map->port;
2419
Stephen Hemmingerd3147742008-11-19 21:32:24 -08002420 err = ops->ndo_set_config(dev, &k_map);
Patrick McHardy0157f602007-06-13 12:03:36 -07002421 if (err < 0)
2422 goto errout;
2423
Nicolas Dichtelba998902014-09-01 16:07:29 +02002424 status |= DO_SETLINK_NOTIFY;
Patrick McHardy0157f602007-06-13 12:03:36 -07002425 }
2426
2427 if (tb[IFLA_ADDRESS]) {
2428 struct sockaddr *sa;
2429 int len;
2430
WANG Cong153711f2017-07-20 11:27:57 -07002431 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2432 sizeof(*sa));
Patrick McHardy0157f602007-06-13 12:03:36 -07002433 sa = kmalloc(len, GFP_KERNEL);
2434 if (!sa) {
2435 err = -ENOMEM;
2436 goto errout;
2437 }
2438 sa->sa_family = dev->type;
2439 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2440 dev->addr_len);
Petr Machata3a37a962018-12-13 11:54:30 +00002441 err = dev_set_mac_address(dev, sa, extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002442 kfree(sa);
2443 if (err)
2444 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002445 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002446 }
2447
2448 if (tb[IFLA_MTU]) {
Stephen Hemminger7a4c53b2018-07-27 13:43:23 -07002449 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
Patrick McHardy0157f602007-06-13 12:03:36 -07002450 if (err < 0)
2451 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002452 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002453 }
2454
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002455 if (tb[IFLA_GROUP]) {
2456 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Nicolas Dichtelba998902014-09-01 16:07:29 +02002457 status |= DO_SETLINK_NOTIFY;
Vlad Dogarucbda10f2011-01-13 23:38:30 +00002458 }
2459
Patrick McHardy0157f602007-06-13 12:03:36 -07002460 /*
2461 * Interface selected by interface index but interface
2462 * name provided implies that a name change has been
2463 * requested.
2464 */
2465 if (ifm->ifi_index > 0 && ifname[0]) {
2466 err = dev_change_name(dev, ifname);
2467 if (err < 0)
2468 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002469 status |= DO_SETLINK_MODIFIED;
Patrick McHardy0157f602007-06-13 12:03:36 -07002470 }
2471
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002472 if (tb[IFLA_IFALIAS]) {
2473 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2474 nla_len(tb[IFLA_IFALIAS]));
2475 if (err < 0)
2476 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002477 status |= DO_SETLINK_NOTIFY;
Stephen Hemminger0b815a12008-09-22 21:28:11 -07002478 }
2479
Patrick McHardy0157f602007-06-13 12:03:36 -07002480 if (tb[IFLA_BROADCAST]) {
2481 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
Jiri Pirkoe7c32732013-01-01 03:30:13 +00002482 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
Patrick McHardy0157f602007-06-13 12:03:36 -07002483 }
2484
2485 if (ifm->ifi_flags || ifm->ifi_change) {
Petr Machata567c5e12018-12-06 17:05:42 +00002486 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2487 extack);
Johannes Berg5f9021c2008-11-16 23:20:31 -08002488 if (err < 0)
2489 goto errout;
Patrick McHardy0157f602007-06-13 12:03:36 -07002490 }
2491
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002492 if (tb[IFLA_MASTER]) {
David Ahern33eaf2a2017-10-04 17:48:46 -07002493 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002494 if (err)
2495 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002496 status |= DO_SETLINK_MODIFIED;
Jiri Pirkofbaec0e2011-02-13 10:15:37 +00002497 }
2498
Jiri Pirko9a572472012-12-27 23:49:39 +00002499 if (tb[IFLA_CARRIER]) {
2500 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2501 if (err)
2502 goto errout;
Nicolas Dichtel90c325e2014-09-01 16:07:28 +02002503 status |= DO_SETLINK_MODIFIED;
Jiri Pirko9a572472012-12-27 23:49:39 +00002504 }
2505
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002506 if (tb[IFLA_TXQLEN]) {
Alexey Dobriyan0cd29502017-05-17 13:30:44 +03002507 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002508
Cong Wang6a643dd2018-01-25 18:26:22 -08002509 err = dev_change_tx_queue_len(dev, value);
2510 if (err)
2511 goto errout;
2512 status |= DO_SETLINK_MODIFIED;
Nicolas Dichtel5d1180f2014-09-01 16:07:26 +02002513 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002514
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002515 if (tb[IFLA_GSO_MAX_SIZE]) {
2516 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2517
2518 if (max_size > GSO_MAX_SIZE) {
2519 err = -EINVAL;
2520 goto errout;
2521 }
2522
2523 if (dev->gso_max_size ^ max_size) {
2524 netif_set_gso_max_size(dev, max_size);
2525 status |= DO_SETLINK_MODIFIED;
2526 }
2527 }
2528
2529 if (tb[IFLA_GSO_MAX_SEGS]) {
2530 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2531
2532 if (max_segs > GSO_MAX_SEGS) {
2533 err = -EINVAL;
2534 goto errout;
2535 }
2536
2537 if (dev->gso_max_segs ^ max_segs) {
2538 dev->gso_max_segs = max_segs;
2539 status |= DO_SETLINK_MODIFIED;
2540 }
2541 }
2542
Patrick McHardy0157f602007-06-13 12:03:36 -07002543 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002544 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Patrick McHardy0157f602007-06-13 12:03:36 -07002545
2546 if (tb[IFLA_LINKMODE]) {
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002547 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2548
David S. Miller93b2d4a2008-02-17 18:35:07 -08002549 write_lock_bh(&dev_base_lock);
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002550 if (dev->link_mode ^ value)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002551 status |= DO_SETLINK_NOTIFY;
Nicolas Dichtel1889b0e2014-09-01 16:07:27 +02002552 dev->link_mode = value;
David S. Miller93b2d4a2008-02-17 18:35:07 -08002553 write_unlock_bh(&dev_base_lock);
Patrick McHardy0157f602007-06-13 12:03:36 -07002554 }
2555
Chris Wrightc02db8c2010-05-16 01:05:45 -07002556 if (tb[IFLA_VFINFO_LIST]) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002557 struct nlattr *vfinfo[IFLA_VF_MAX + 1];
Chris Wrightc02db8c2010-05-16 01:05:45 -07002558 struct nlattr *attr;
2559 int rem;
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002560
Chris Wrightc02db8c2010-05-16 01:05:45 -07002561 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002562 if (nla_type(attr) != IFLA_VF_INFO ||
2563 nla_len(attr) < NLA_HDRLEN) {
David Howells253683b2010-05-21 02:25:27 +00002564 err = -EINVAL;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002565 goto errout;
David Howells253683b2010-05-21 02:25:27 +00002566 }
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002567 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002568 ifla_vf_policy, NULL);
Daniel Borkmann4f7d2cd2015-07-07 00:07:52 +02002569 if (err < 0)
2570 goto errout;
2571 err = do_setvfinfo(dev, vfinfo);
Chris Wrightc02db8c2010-05-16 01:05:45 -07002572 if (err < 0)
2573 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002574 status |= DO_SETLINK_NOTIFY;
Chris Wrightc02db8c2010-05-16 01:05:45 -07002575 }
Williams, Mitch Aebc08a62010-02-10 01:44:05 +00002576 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002577 err = 0;
2578
Scott Feldman57b61082010-05-17 22:49:55 -07002579 if (tb[IFLA_VF_PORTS]) {
2580 struct nlattr *port[IFLA_PORT_MAX+1];
2581 struct nlattr *attr;
2582 int vf;
2583 int rem;
2584
2585 err = -EOPNOTSUPP;
2586 if (!ops->ndo_set_vf_port)
2587 goto errout;
2588
2589 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
Daniel Borkmann035d2102015-07-13 00:06:02 +02002590 if (nla_type(attr) != IFLA_VF_PORT ||
2591 nla_len(attr) < NLA_HDRLEN) {
2592 err = -EINVAL;
2593 goto errout;
2594 }
2595 err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
Johannes Bergfceb6432017-04-12 14:34:07 +02002596 ifla_port_policy, NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002597 if (err < 0)
2598 goto errout;
2599 if (!port[IFLA_PORT_VF]) {
2600 err = -EOPNOTSUPP;
2601 goto errout;
2602 }
2603 vf = nla_get_u32(port[IFLA_PORT_VF]);
2604 err = ops->ndo_set_vf_port(dev, vf, port);
2605 if (err < 0)
2606 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002607 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002608 }
2609 }
2610 err = 0;
2611
2612 if (tb[IFLA_PORT_SELF]) {
2613 struct nlattr *port[IFLA_PORT_MAX+1];
2614
2615 err = nla_parse_nested(port, IFLA_PORT_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02002616 tb[IFLA_PORT_SELF], ifla_port_policy,
2617 NULL);
Scott Feldman57b61082010-05-17 22:49:55 -07002618 if (err < 0)
2619 goto errout;
2620
2621 err = -EOPNOTSUPP;
2622 if (ops->ndo_set_vf_port)
2623 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2624 if (err < 0)
2625 goto errout;
Nicolas Dichtelba998902014-09-01 16:07:29 +02002626 status |= DO_SETLINK_NOTIFY;
Scott Feldman57b61082010-05-17 22:49:55 -07002627 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002628
2629 if (tb[IFLA_AF_SPEC]) {
2630 struct nlattr *af;
2631 int rem;
2632
2633 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2634 const struct rtnl_af_ops *af_ops;
2635
Florian Westphal5fa85a02017-10-16 15:44:36 +02002636 rcu_read_lock();
2637
Gustavo A. R. Silva058c8d52017-10-20 19:43:11 -05002638 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
Thomas Graff8ff1822010-11-16 04:30:14 +00002639
Thomas Grafcf7afbf2010-11-22 01:31:54 +00002640 err = af_ops->set_link_af(dev, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02002641 if (err < 0) {
2642 rcu_read_unlock();
Thomas Graff8ff1822010-11-16 04:30:14 +00002643 goto errout;
Florian Westphal5fa85a02017-10-16 15:44:36 +02002644 }
Thomas Graff8ff1822010-11-16 04:30:14 +00002645
Florian Westphal5fa85a02017-10-16 15:44:36 +02002646 rcu_read_unlock();
Nicolas Dichtelba998902014-09-01 16:07:29 +02002647 status |= DO_SETLINK_NOTIFY;
Thomas Graff8ff1822010-11-16 04:30:14 +00002648 }
2649 }
Scott Feldman57b61082010-05-17 22:49:55 -07002650 err = 0;
2651
Anuradha Karuppiah88d63782015-07-14 13:43:20 -07002652 if (tb[IFLA_PROTO_DOWN]) {
2653 err = dev_change_proto_down(dev,
2654 nla_get_u8(tb[IFLA_PROTO_DOWN]));
2655 if (err)
2656 goto errout;
2657 status |= DO_SETLINK_NOTIFY;
2658 }
2659
Brenden Blancod1fdd912016-07-19 12:16:49 -07002660 if (tb[IFLA_XDP]) {
2661 struct nlattr *xdp[IFLA_XDP_MAX + 1];
Daniel Borkmann85de8572016-11-28 23:16:54 +01002662 u32 xdp_flags = 0;
Brenden Blancod1fdd912016-07-19 12:16:49 -07002663
2664 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP],
Johannes Bergfceb6432017-04-12 14:34:07 +02002665 ifla_xdp_policy, NULL);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002666 if (err < 0)
2667 goto errout;
2668
Martin KaFai Lau58038692017-06-15 17:29:09 -07002669 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
Brenden Blanco262d8622016-07-20 17:22:34 -07002670 err = -EINVAL;
2671 goto errout;
2672 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002673
2674 if (xdp[IFLA_XDP_FLAGS]) {
2675 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
2676 if (xdp_flags & ~XDP_FLAGS_MASK) {
2677 err = -EINVAL;
2678 goto errout;
2679 }
Jakub Kicinskiee5d0322017-06-21 18:25:04 -07002680 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
Daniel Borkmann0489df92017-05-12 01:04:45 +02002681 err = -EINVAL;
2682 goto errout;
2683 }
Daniel Borkmann85de8572016-11-28 23:16:54 +01002684 }
2685
Brenden Blancod1fdd912016-07-19 12:16:49 -07002686 if (xdp[IFLA_XDP_FD]) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002687 err = dev_change_xdp_fd(dev, extack,
Daniel Borkmann85de8572016-11-28 23:16:54 +01002688 nla_get_s32(xdp[IFLA_XDP_FD]),
2689 xdp_flags);
Brenden Blancod1fdd912016-07-19 12:16:49 -07002690 if (err)
2691 goto errout;
2692 status |= DO_SETLINK_NOTIFY;
2693 }
2694 }
2695
Patrick McHardy0157f602007-06-13 12:03:36 -07002696errout:
Nicolas Dichtelba998902014-09-01 16:07:29 +02002697 if (status & DO_SETLINK_MODIFIED) {
Xin Long64ff90c2017-10-15 18:13:45 +08002698 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
Nicolas Dichtelba998902014-09-01 16:07:29 +02002699 netdev_state_change(dev);
2700
2701 if (err < 0)
2702 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",
2703 dev->name);
2704 }
Patrick McHardy0157f602007-06-13 12:03:36 -07002705
Patrick McHardy0157f602007-06-13 12:03:36 -07002706 return err;
2707}
2708
David Ahernc21ef3e2017-04-16 09:48:24 -07002709static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2710 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002712 struct net *net = sock_net(skb->sk);
Thomas Grafda5e0492006-08-10 21:17:37 -07002713 struct ifinfomsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct net_device *dev;
Patrick McHardy0157f602007-06-13 12:03:36 -07002715 int err;
Thomas Grafda5e0492006-08-10 21:17:37 -07002716 struct nlattr *tb[IFLA_MAX+1];
2717 char ifname[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
David Ahernc21ef3e2017-04-16 09:48:24 -07002719 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
2720 extack);
Thomas Grafda5e0492006-08-10 21:17:37 -07002721 if (err < 0)
2722 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002724 err = rtnl_ensure_unique_netns(tb, extack, false);
2725 if (err < 0)
2726 goto errout;
2727
Thomas Graf5176f912006-08-26 20:13:18 -07002728 if (tb[IFLA_IFNAME])
2729 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
Patrick McHardy78e5b8912006-09-13 20:35:36 -07002730 else
2731 ifname[0] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 err = -EINVAL;
Thomas Grafda5e0492006-08-10 21:17:37 -07002734 ifm = nlmsg_data(nlh);
Patrick McHardy51055be2007-06-05 12:40:01 -07002735 if (ifm->ifi_index > 0)
Eric Dumazeta3d12892009-10-21 10:59:31 +00002736 dev = __dev_get_by_index(net, ifm->ifi_index);
Thomas Grafda5e0492006-08-10 21:17:37 -07002737 else if (tb[IFLA_IFNAME])
Eric Dumazeta3d12892009-10-21 10:59:31 +00002738 dev = __dev_get_by_name(net, ifname);
Thomas Grafda5e0492006-08-10 21:17:37 -07002739 else
2740 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Thomas Grafda5e0492006-08-10 21:17:37 -07002742 if (dev == NULL) {
2743 err = -ENODEV;
2744 goto errout;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002747 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0);
Thomas Grafda5e0492006-08-10 21:17:37 -07002748errout:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return err;
2750}
2751
WANG Cong66400d52015-03-24 11:53:31 -07002752static int rtnl_group_dellink(const struct net *net, int group)
2753{
2754 struct net_device *dev, *aux;
2755 LIST_HEAD(list_kill);
2756 bool found = false;
2757
2758 if (!group)
2759 return -EPERM;
2760
2761 for_each_netdev(net, dev) {
2762 if (dev->group == group) {
2763 const struct rtnl_link_ops *ops;
2764
2765 found = true;
2766 ops = dev->rtnl_link_ops;
2767 if (!ops || !ops->dellink)
2768 return -EOPNOTSUPP;
2769 }
2770 }
2771
2772 if (!found)
2773 return -ENODEV;
2774
2775 for_each_netdev_safe(net, dev, aux) {
2776 if (dev->group == group) {
2777 const struct rtnl_link_ops *ops;
2778
2779 ops = dev->rtnl_link_ops;
2780 ops->dellink(dev, &list_kill);
2781 }
2782 }
2783 unregister_netdevice_many(&list_kill);
2784
2785 return 0;
2786}
2787
Thomas Graf614732e2015-07-21 10:44:06 +02002788int rtnl_delete_link(struct net_device *dev)
2789{
2790 const struct rtnl_link_ops *ops;
2791 LIST_HEAD(list_kill);
2792
2793 ops = dev->rtnl_link_ops;
2794 if (!ops || !ops->dellink)
2795 return -EOPNOTSUPP;
2796
2797 ops->dellink(dev, &list_kill);
2798 unregister_netdevice_many(&list_kill);
2799
2800 return 0;
2801}
2802EXPORT_SYMBOL_GPL(rtnl_delete_link);
2803
David Ahernc21ef3e2017-04-16 09:48:24 -07002804static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
2805 struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002806{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002807 struct net *net = sock_net(skb->sk);
Christian Braunerb61ad682018-01-24 15:26:34 +01002808 struct net *tgt_net = net;
2809 struct net_device *dev = NULL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002810 struct ifinfomsg *ifm;
2811 char ifname[IFNAMSIZ];
2812 struct nlattr *tb[IFLA_MAX+1];
2813 int err;
Christian Braunerb61ad682018-01-24 15:26:34 +01002814 int netnsid = -1;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002815
David Ahernc21ef3e2017-04-16 09:48:24 -07002816 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002817 if (err < 0)
2818 return err;
2819
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002820 err = rtnl_ensure_unique_netns(tb, extack, true);
2821 if (err < 0)
2822 return err;
2823
Patrick McHardy38f7b872007-06-13 12:03:51 -07002824 if (tb[IFLA_IFNAME])
2825 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2826
Christian Brauner7e4a8d52018-09-04 21:53:53 +02002827 if (tb[IFLA_TARGET_NETNSID]) {
2828 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02002829 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Christian Braunerb61ad682018-01-24 15:26:34 +01002830 if (IS_ERR(tgt_net))
2831 return PTR_ERR(tgt_net);
2832 }
2833
2834 err = -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002835 ifm = nlmsg_data(nlh);
2836 if (ifm->ifi_index > 0)
Christian Braunerb61ad682018-01-24 15:26:34 +01002837 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002838 else if (tb[IFLA_IFNAME])
Christian Braunerb61ad682018-01-24 15:26:34 +01002839 dev = __dev_get_by_name(tgt_net, ifname);
WANG Cong66400d52015-03-24 11:53:31 -07002840 else if (tb[IFLA_GROUP])
Christian Braunerb61ad682018-01-24 15:26:34 +01002841 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
Patrick McHardy38f7b872007-06-13 12:03:51 -07002842 else
Christian Braunerb61ad682018-01-24 15:26:34 +01002843 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002844
Christian Braunerb61ad682018-01-24 15:26:34 +01002845 if (!dev) {
2846 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0)
2847 err = -ENODEV;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002848
Christian Braunerb61ad682018-01-24 15:26:34 +01002849 goto out;
2850 }
2851
2852 err = rtnl_delete_link(dev);
2853
2854out:
2855 if (netnsid >= 0)
2856 put_net(tgt_net);
2857
2858 return err;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002859}
2860
Patrick McHardy3729d502010-02-26 06:34:54 +00002861int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2862{
2863 unsigned int old_flags;
2864 int err;
2865
2866 old_flags = dev->flags;
2867 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
Petr Machata6d040322018-12-06 17:05:43 +00002868 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2869 NULL);
Patrick McHardy3729d502010-02-26 06:34:54 +00002870 if (err < 0)
2871 return err;
2872 }
2873
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002874 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
Roopa Prabhu56a49d72018-09-12 13:21:48 -07002875 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
Roopa Prabhu8d356b82018-07-04 16:46:29 -07002876 } else {
2877 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2878 __dev_notify_flags(dev, old_flags, ~0U);
2879 }
Patrick McHardy3729d502010-02-26 06:34:54 +00002880 return 0;
2881}
2882EXPORT_SYMBOL(rtnl_configure_link);
2883
David Ahernd0522f12018-11-06 12:51:14 -08002884struct net_device *rtnl_create_link(struct net *net, const char *ifname,
2885 unsigned char name_assign_type,
2886 const struct rtnl_link_ops *ops,
2887 struct nlattr *tb[],
2888 struct netlink_ext_ack *extack)
Pavel Emelianove7199282007-08-08 22:16:38 -07002889{
Pavel Emelianove7199282007-08-08 22:16:38 -07002890 struct net_device *dev;
Jiri Pirkod40156a2012-07-20 02:28:47 +00002891 unsigned int num_tx_queues = 1;
2892 unsigned int num_rx_queues = 1;
Pavel Emelianove7199282007-08-08 22:16:38 -07002893
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002894 if (tb[IFLA_NUM_TX_QUEUES])
2895 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2896 else if (ops->get_num_tx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002897 num_tx_queues = ops->get_num_tx_queues();
Jiri Pirko76ff5cc2012-07-20 02:28:48 +00002898
2899 if (tb[IFLA_NUM_RX_QUEUES])
2900 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2901 else if (ops->get_num_rx_queues)
Jiri Pirkod40156a2012-07-20 02:28:47 +00002902 num_rx_queues = ops->get_num_rx_queues();
stephen hemmingerefacb302012-04-10 18:34:43 +00002903
David Ahernd0522f12018-11-06 12:51:14 -08002904 if (num_tx_queues < 1 || num_tx_queues > 4096) {
2905 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002906 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002907 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002908
David Ahernd0522f12018-11-06 12:51:14 -08002909 if (num_rx_queues < 1 || num_rx_queues > 4096) {
2910 NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002911 return ERR_PTR(-EINVAL);
David Ahernd0522f12018-11-06 12:51:14 -08002912 }
Eric Dumazet0e1d6ec2018-10-02 15:47:35 -07002913
Tom Gundersen55177502014-07-14 16:37:25 +02002914 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
Tom Gundersenc835a672014-07-14 16:37:24 +02002915 ops->setup, num_tx_queues, num_rx_queues);
Pavel Emelianove7199282007-08-08 22:16:38 -07002916 if (!dev)
Tobias Klauserd1892e42017-02-20 16:32:06 +01002917 return ERR_PTR(-ENOMEM);
Pavel Emelianove7199282007-08-08 22:16:38 -07002918
Eric W. Biederman81adee42009-11-08 00:53:51 -08002919 dev_net_set(dev, net);
2920 dev->rtnl_link_ops = ops;
Patrick McHardy3729d502010-02-26 06:34:54 +00002921 dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
Eric W. Biederman81adee42009-11-08 00:53:51 -08002922
Pavel Emelianove7199282007-08-08 22:16:38 -07002923 if (tb[IFLA_MTU])
2924 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002925 if (tb[IFLA_ADDRESS]) {
Pavel Emelianove7199282007-08-08 22:16:38 -07002926 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2927 nla_len(tb[IFLA_ADDRESS]));
Jiri Pirko2afb9b52013-01-06 12:41:57 +00002928 dev->addr_assign_type = NET_ADDR_SET;
2929 }
Pavel Emelianove7199282007-08-08 22:16:38 -07002930 if (tb[IFLA_BROADCAST])
2931 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2932 nla_len(tb[IFLA_BROADCAST]));
2933 if (tb[IFLA_TXQLEN])
2934 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2935 if (tb[IFLA_OPERSTATE])
David S. Miller93b2d4a2008-02-17 18:35:07 -08002936 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
Pavel Emelianove7199282007-08-08 22:16:38 -07002937 if (tb[IFLA_LINKMODE])
2938 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
Patrick McHardyffa934f2011-01-20 03:00:42 +00002939 if (tb[IFLA_GROUP])
2940 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
Stephen Hemminger46e6b992017-12-07 15:40:19 -08002941 if (tb[IFLA_GSO_MAX_SIZE])
2942 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
2943 if (tb[IFLA_GSO_MAX_SEGS])
Stephen Hemmingera0b586f2017-12-08 15:34:13 -08002944 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
Pavel Emelianove7199282007-08-08 22:16:38 -07002945
2946 return dev;
Pavel Emelianove7199282007-08-08 22:16:38 -07002947}
Eric Dumazete0d087a2009-11-07 01:26:17 -08002948EXPORT_SYMBOL(rtnl_create_link);
Pavel Emelianove7199282007-08-08 22:16:38 -07002949
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002950static int rtnl_group_changelink(const struct sk_buff *skb,
2951 struct net *net, int group,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002952 struct ifinfomsg *ifm,
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002953 struct netlink_ext_ack *extack,
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002954 struct nlattr **tb)
2955{
WANG Congd0795352015-03-23 16:31:09 -07002956 struct net_device *dev, *aux;
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002957 int err;
2958
WANG Congd0795352015-03-23 16:31:09 -07002959 for_each_netdev_safe(net, dev, aux) {
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002960 if (dev->group == group) {
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07002961 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00002962 if (err < 0)
2963 return err;
2964 }
2965 }
2966
2967 return 0;
2968}
2969
Jakub Kicinskia2939742018-11-27 22:32:31 -08002970static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
2971 struct nlattr **attr, struct netlink_ext_ack *extack)
Patrick McHardy38f7b872007-06-13 12:03:51 -07002972{
Jakub Kicinski420d0312018-11-27 22:32:30 -08002973 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
2974 unsigned char name_assign_type = NET_NAME_USER;
2975 struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
2976 const struct rtnl_link_ops *m_ops = NULL;
Jakub Kicinski420d0312018-11-27 22:32:30 -08002977 struct net_device *master_dev = NULL;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002978 struct net *net = sock_net(skb->sk);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002979 const struct rtnl_link_ops *ops;
Jakub Kicinski420d0312018-11-27 22:32:30 -08002980 struct nlattr *tb[IFLA_MAX + 1];
2981 struct net *dest_net, *link_net;
2982 struct nlattr **slave_data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002983 char kind[MODULE_NAME_LEN];
Jakub Kicinski420d0312018-11-27 22:32:30 -08002984 struct net_device *dev;
2985 struct ifinfomsg *ifm;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002986 char ifname[IFNAMSIZ];
Jakub Kicinski420d0312018-11-27 22:32:30 -08002987 struct nlattr **data;
Patrick McHardy38f7b872007-06-13 12:03:51 -07002988 int err;
2989
Johannes Berg95a5afc2008-10-16 15:24:51 -07002990#ifdef CONFIG_MODULES
Patrick McHardy38f7b872007-06-13 12:03:51 -07002991replay:
Thomas Graf8072f082007-07-31 14:13:50 -07002992#endif
David Ahernc21ef3e2017-04-16 09:48:24 -07002993 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
Patrick McHardy38f7b872007-06-13 12:03:51 -07002994 if (err < 0)
2995 return err;
2996
Christian Brauner4ff66ca2018-02-07 13:53:20 +01002997 err = rtnl_ensure_unique_netns(tb, extack, false);
2998 if (err < 0)
2999 return err;
3000
Patrick McHardy38f7b872007-06-13 12:03:51 -07003001 if (tb[IFLA_IFNAME])
3002 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3003 else
3004 ifname[0] = '\0';
3005
3006 ifm = nlmsg_data(nlh);
3007 if (ifm->ifi_index > 0)
Eric W. Biederman881d9662007-09-17 11:56:21 -07003008 dev = __dev_get_by_index(net, ifm->ifi_index);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003009 else {
3010 if (ifname[0])
3011 dev = __dev_get_by_name(net, ifname);
Vlad Dogarue7ed8282011-01-13 23:38:31 +00003012 else
3013 dev = NULL;
3014 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003015
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003016 if (dev) {
3017 master_dev = netdev_master_upper_dev_get(dev);
3018 if (master_dev)
3019 m_ops = master_dev->rtnl_link_ops;
3020 }
3021
Eric Dumazete0d087a2009-11-07 01:26:17 -08003022 err = validate_linkmsg(dev, tb);
3023 if (err < 0)
Thomas Graf1840bb12008-02-23 19:54:36 -08003024 return err;
3025
Patrick McHardy38f7b872007-06-13 12:03:51 -07003026 if (tb[IFLA_LINKINFO]) {
3027 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
Johannes Bergfceb6432017-04-12 14:34:07 +02003028 tb[IFLA_LINKINFO], ifla_info_policy,
3029 NULL);
Patrick McHardy38f7b872007-06-13 12:03:51 -07003030 if (err < 0)
3031 return err;
3032 } else
3033 memset(linkinfo, 0, sizeof(linkinfo));
3034
3035 if (linkinfo[IFLA_INFO_KIND]) {
3036 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3037 ops = rtnl_link_ops_get(kind);
3038 } else {
3039 kind[0] = '\0';
3040 ops = NULL;
3041 }
3042
Jakub Kicinski420d0312018-11-27 22:32:30 -08003043 data = NULL;
3044 if (ops) {
3045 if (ops->maxtype > RTNL_MAX_TYPE)
3046 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003047
Jakub Kicinski420d0312018-11-27 22:32:30 -08003048 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3049 err = nla_parse_nested(attr, ops->maxtype,
3050 linkinfo[IFLA_INFO_DATA],
3051 ops->policy, extack);
3052 if (err < 0)
3053 return err;
3054 data = attr;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003055 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003056 if (ops->validate) {
3057 err = ops->validate(tb, data, extack);
3058 if (err < 0)
3059 return err;
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003060 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003061 }
Jiri Pirkoba7d49b2014-01-22 09:05:55 +01003062
Jakub Kicinski420d0312018-11-27 22:32:30 -08003063 slave_data = NULL;
3064 if (m_ops) {
3065 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3066 return -EINVAL;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003067
Jakub Kicinski420d0312018-11-27 22:32:30 -08003068 if (m_ops->slave_maxtype &&
3069 linkinfo[IFLA_INFO_SLAVE_DATA]) {
3070 err = nla_parse_nested(slave_attr, m_ops->slave_maxtype,
3071 linkinfo[IFLA_INFO_SLAVE_DATA],
3072 m_ops->slave_policy, extack);
3073 if (err < 0)
3074 return err;
3075 slave_data = slave_attr;
3076 }
3077 }
3078
3079 if (dev) {
3080 int status = 0;
3081
3082 if (nlh->nlmsg_flags & NLM_F_EXCL)
3083 return -EEXIST;
3084 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3085 return -EOPNOTSUPP;
3086
3087 if (linkinfo[IFLA_INFO_DATA]) {
3088 if (!ops || ops != dev->rtnl_link_ops ||
3089 !ops->changelink)
Patrick McHardy38f7b872007-06-13 12:03:51 -07003090 return -EOPNOTSUPP;
3091
Jakub Kicinski420d0312018-11-27 22:32:30 -08003092 err = ops->changelink(dev, tb, data, extack);
3093 if (err < 0)
3094 return err;
3095 status |= DO_SETLINK_NOTIFY;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003096 }
3097
Jakub Kicinski420d0312018-11-27 22:32:30 -08003098 if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3099 if (!m_ops || !m_ops->slave_changelink)
3100 return -EOPNOTSUPP;
3101
3102 err = m_ops->slave_changelink(master_dev, dev, tb,
3103 slave_data, extack);
3104 if (err < 0)
3105 return err;
3106 status |= DO_SETLINK_NOTIFY;
3107 }
3108
3109 return do_setlink(skb, dev, ifm, extack, tb, ifname, status);
3110 }
3111
3112 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3113 if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
3114 return rtnl_group_changelink(skb, net,
Patrick McHardyffa934f2011-01-20 03:00:42 +00003115 nla_get_u32(tb[IFLA_GROUP]),
Jakub Kicinskiddf9f972017-04-30 21:46:46 -07003116 ifm, extack, tb);
Jakub Kicinski420d0312018-11-27 22:32:30 -08003117 return -ENODEV;
3118 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003119
Jakub Kicinski420d0312018-11-27 22:32:30 -08003120 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3121 return -EOPNOTSUPP;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003122
Jakub Kicinski420d0312018-11-27 22:32:30 -08003123 if (!ops) {
Johannes Berg95a5afc2008-10-16 15:24:51 -07003124#ifdef CONFIG_MODULES
Jakub Kicinski420d0312018-11-27 22:32:30 -08003125 if (kind[0]) {
3126 __rtnl_unlock();
3127 request_module("rtnl-link-%s", kind);
3128 rtnl_lock();
3129 ops = rtnl_link_ops_get(kind);
3130 if (ops)
3131 goto replay;
3132 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003133#endif
Jakub Kicinski420d0312018-11-27 22:32:30 -08003134 NL_SET_ERR_MSG(extack, "Unknown device type");
3135 return -EOPNOTSUPP;
3136 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003137
Jakub Kicinski420d0312018-11-27 22:32:30 -08003138 if (!ops->setup)
3139 return -EOPNOTSUPP;
Jiri Pirkob0ab2fa2014-06-26 09:58:25 +02003140
Jakub Kicinski420d0312018-11-27 22:32:30 -08003141 if (!ifname[0]) {
3142 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3143 name_assign_type = NET_NAME_ENUM;
3144 }
Patrick McHardy38f7b872007-06-13 12:03:51 -07003145
Jakub Kicinski420d0312018-11-27 22:32:30 -08003146 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3147 if (IS_ERR(dest_net))
3148 return PTR_ERR(dest_net);
Eric W. Biederman13ad1772011-01-29 14:57:22 +00003149
Jakub Kicinski420d0312018-11-27 22:32:30 -08003150 if (tb[IFLA_LINK_NETNSID]) {
3151 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
Nicolas Dichtel317f4812015-01-15 15:11:18 +01003152
Jakub Kicinski420d0312018-11-27 22:32:30 -08003153 link_net = get_net_ns_by_id(dest_net, id);
3154 if (!link_net) {
3155 NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3156 err = -EINVAL;
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003157 goto out;
3158 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003159 err = -EPERM;
3160 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3161 goto out;
3162 } else {
3163 link_net = NULL;
3164 }
Pavel Emelyanov9c7dafb2012-08-08 21:52:46 +00003165
Jakub Kicinski420d0312018-11-27 22:32:30 -08003166 dev = rtnl_create_link(link_net ? : dest_net, ifname,
3167 name_assign_type, ops, tb, extack);
3168 if (IS_ERR(dev)) {
3169 err = PTR_ERR(dev);
David S. Miller43638902015-03-10 21:58:32 -04003170 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003171 }
Jakub Kicinski420d0312018-11-27 22:32:30 -08003172
3173 dev->ifindex = ifm->ifi_index;
3174
3175 if (ops->newlink) {
3176 err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3177 /* Drivers should call free_netdev() in ->destructor
3178 * and unregister it on failure after registration
3179 * so that device could be finally freed in rtnl_unlock.
3180 */
3181 if (err < 0) {
3182 /* If device is not registered at all, free it now */
3183 if (dev->reg_state == NETREG_UNINITIALIZED)
3184 free_netdev(dev);
3185 goto out;
3186 }
3187 } else {
3188 err = register_netdevice(dev);
3189 if (err < 0) {
3190 free_netdev(dev);
3191 goto out;
3192 }
3193 }
3194 err = rtnl_configure_link(dev, ifm);
3195 if (err < 0)
3196 goto out_unregister;
3197 if (link_net) {
3198 err = dev_change_net_namespace(dev, dest_net, ifname);
3199 if (err < 0)
3200 goto out_unregister;
3201 }
3202 if (tb[IFLA_MASTER]) {
3203 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3204 if (err)
3205 goto out_unregister;
3206 }
3207out:
3208 if (link_net)
3209 put_net(link_net);
3210 put_net(dest_net);
3211 return err;
3212out_unregister:
3213 if (ops->newlink) {
3214 LIST_HEAD(list_kill);
3215
3216 ops->dellink(dev, &list_kill);
3217 unregister_netdevice_many(&list_kill);
3218 } else {
3219 unregister_netdevice(dev);
3220 }
3221 goto out;
Patrick McHardy38f7b872007-06-13 12:03:51 -07003222}
3223
Jakub Kicinskia2939742018-11-27 22:32:31 -08003224static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3225 struct netlink_ext_ack *extack)
3226{
3227 struct nlattr **attr;
3228 int ret;
3229
3230 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL);
3231 if (!attr)
3232 return -ENOMEM;
3233
3234 ret = __rtnl_newlink(skb, nlh, attr, extack);
3235 kfree(attr);
3236 return ret;
3237}
3238
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003239static int rtnl_valid_getlink_req(struct sk_buff *skb,
3240 const struct nlmsghdr *nlh,
3241 struct nlattr **tb,
3242 struct netlink_ext_ack *extack)
3243{
3244 struct ifinfomsg *ifm;
3245 int i, err;
3246
3247 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3248 NL_SET_ERR_MSG(extack, "Invalid header for get link");
3249 return -EINVAL;
3250 }
3251
3252 if (!netlink_strict_get_check(skb))
3253 return nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
3254 extack);
3255
3256 ifm = nlmsg_data(nlh);
3257 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3258 ifm->ifi_change) {
3259 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3260 return -EINVAL;
3261 }
3262
3263 err = nlmsg_parse_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy,
3264 extack);
3265 if (err)
3266 return err;
3267
3268 for (i = 0; i <= IFLA_MAX; i++) {
3269 if (!tb[i])
3270 continue;
3271
3272 switch (i) {
3273 case IFLA_IFNAME:
3274 case IFLA_EXT_MASK:
3275 case IFLA_TARGET_NETNSID:
3276 break;
3277 default:
3278 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3279 return -EINVAL;
3280 }
3281 }
3282
3283 return 0;
3284}
3285
David Ahernc21ef3e2017-04-16 09:48:24 -07003286static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3287 struct netlink_ext_ack *extack)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003288{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003289 struct net *net = sock_net(skb->sk);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003290 struct net *tgt_net = net;
Thomas Grafb60c5112006-08-04 23:05:34 -07003291 struct ifinfomsg *ifm;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003292 char ifname[IFNAMSIZ];
Thomas Grafb60c5112006-08-04 23:05:34 -07003293 struct nlattr *tb[IFLA_MAX+1];
3294 struct net_device *dev = NULL;
3295 struct sk_buff *nskb;
Jiri Benc79e1ad12017-11-02 17:04:38 -02003296 int netnsid = -1;
Thomas Graf339bf982006-11-10 14:10:15 -08003297 int err;
Greg Rose115c9b82012-02-21 16:54:48 -05003298 u32 ext_filter_mask = 0;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003299
Jakub Kicinski9b3757b2019-01-18 10:46:16 -08003300 err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
Thomas Grafb60c5112006-08-04 23:05:34 -07003301 if (err < 0)
Eric Sesterhenn9918f232006-09-26 23:26:38 -07003302 return err;
Thomas Grafb60c5112006-08-04 23:05:34 -07003303
Christian Brauner4ff66ca2018-02-07 13:53:20 +01003304 err = rtnl_ensure_unique_netns(tb, extack, true);
3305 if (err < 0)
3306 return err;
3307
Christian Brauner7e4a8d52018-09-04 21:53:53 +02003308 if (tb[IFLA_TARGET_NETNSID]) {
3309 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
Christian Braunerc383edc2018-09-04 21:53:47 +02003310 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003311 if (IS_ERR(tgt_net))
3312 return PTR_ERR(tgt_net);
3313 }
3314
Eric Dumazeta3d12892009-10-21 10:59:31 +00003315 if (tb[IFLA_IFNAME])
3316 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3317
Greg Rose115c9b82012-02-21 16:54:48 -05003318 if (tb[IFLA_EXT_MASK])
3319 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3320
Jiri Benc79e1ad12017-11-02 17:04:38 -02003321 err = -EINVAL;
Thomas Grafb60c5112006-08-04 23:05:34 -07003322 ifm = nlmsg_data(nlh);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003323 if (ifm->ifi_index > 0)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003324 dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003325 else if (tb[IFLA_IFNAME])
Jiri Benc79e1ad12017-11-02 17:04:38 -02003326 dev = __dev_get_by_name(tgt_net, ifname);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003327 else
Jiri Benc79e1ad12017-11-02 17:04:38 -02003328 goto out;
Thomas Grafb60c5112006-08-04 23:05:34 -07003329
Jiri Benc79e1ad12017-11-02 17:04:38 -02003330 err = -ENODEV;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003331 if (dev == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003332 goto out;
Eric Dumazeta3d12892009-10-21 10:59:31 +00003333
Jiri Benc79e1ad12017-11-02 17:04:38 -02003334 err = -ENOBUFS;
Greg Rose115c9b82012-02-21 16:54:48 -05003335 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003336 if (nskb == NULL)
Jiri Benc79e1ad12017-11-02 17:04:38 -02003337 goto out;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003338
Jiri Benc79e1ad12017-11-02 17:04:38 -02003339 err = rtnl_fill_ifinfo(nskb, dev, net,
3340 RTM_NEWLINK, NETLINK_CB(skb).portid,
3341 nlh->nlmsg_seq, 0, 0, ext_filter_mask,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003342 0, NULL, 0, netnsid);
Patrick McHardy26932562007-01-31 23:16:40 -08003343 if (err < 0) {
3344 /* -EMSGSIZE implies BUG in if_nlmsg_size */
3345 WARN_ON(err == -EMSGSIZE);
3346 kfree_skb(nskb);
Eric Dumazeta3d12892009-10-21 10:59:31 +00003347 } else
Eric W. Biederman15e47302012-09-07 20:12:54 +00003348 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
Jiri Benc79e1ad12017-11-02 17:04:38 -02003349out:
3350 if (netnsid >= 0)
3351 put_net(tgt_net);
Thomas Grafb60c5112006-08-04 23:05:34 -07003352
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003353 return err;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003354}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08003355
Greg Rose115c9b82012-02-21 16:54:48 -05003356static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
Greg Rosec7ac8672011-06-10 01:27:09 +00003357{
Greg Rose115c9b82012-02-21 16:54:48 -05003358 struct net *net = sock_net(skb->sk);
3359 struct net_device *dev;
3360 struct nlattr *tb[IFLA_MAX+1];
3361 u32 ext_filter_mask = 0;
3362 u16 min_ifinfo_dump_size = 0;
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003363 int hdrlen;
Greg Rose115c9b82012-02-21 16:54:48 -05003364
Michal Schmidte5eca6d2014-05-28 14:15:19 +02003365 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3366 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3367 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3368
Johannes Bergfceb6432017-04-12 14:34:07 +02003369 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
Eric Dumazeta4b64fb2012-03-04 12:32:10 +00003370 if (tb[IFLA_EXT_MASK])
3371 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3372 }
Greg Rose115c9b82012-02-21 16:54:48 -05003373
3374 if (!ext_filter_mask)
3375 return NLMSG_GOODSIZE;
3376 /*
3377 * traverse the list of net devices and compute the minimum
3378 * buffer size based upon the filter mask.
3379 */
Florian Westphal6853dd42017-08-09 20:41:51 +02003380 rcu_read_lock();
3381 for_each_netdev_rcu(net, dev) {
Greg Rose115c9b82012-02-21 16:54:48 -05003382 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
3383 if_nlmsg_size(dev,
3384 ext_filter_mask));
3385 }
Florian Westphal6853dd42017-08-09 20:41:51 +02003386 rcu_read_unlock();
Greg Rose115c9b82012-02-21 16:54:48 -05003387
Zhang Shengju93af2052016-11-22 14:14:28 +08003388 return nlmsg_total_size(min_ifinfo_dump_size);
Greg Rosec7ac8672011-06-10 01:27:09 +00003389}
3390
Adrian Bunk42bad1d2007-04-26 00:57:41 -07003391static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 int idx;
3394 int s_idx = cb->family;
Christian Brauner87ccbb12018-09-04 21:53:51 +02003395 int type = cb->nlh->nlmsg_type - RTM_BASE;
David Ahernc63586d2018-10-24 12:59:02 -07003396 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 if (s_idx == 0)
3399 s_idx = 1;
Florian Westphal6853dd42017-08-09 20:41:51 +02003400
Patrick McHardy25239ce2010-04-26 16:02:05 +02003401 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01003402 struct rtnl_link **tab;
Florian Westphaladdf9b92017-12-02 21:44:05 +01003403 struct rtnl_link *link;
Florian Westphal6853dd42017-08-09 20:41:51 +02003404 rtnl_dumpit_func dumpit;
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if (idx < s_idx || idx == PF_PACKET)
3407 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003408
Florian Westphaladdf9b92017-12-02 21:44:05 +01003409 if (type < 0 || type >= RTM_NR_MSGTYPES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 continue;
Florian Westphal6853dd42017-08-09 20:41:51 +02003411
Florian Westphaladdf9b92017-12-02 21:44:05 +01003412 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3413 if (!tab)
3414 continue;
3415
3416 link = tab[type];
3417 if (!link)
3418 continue;
3419
3420 dumpit = link->dumpit;
Florian Westphal6853dd42017-08-09 20:41:51 +02003421 if (!dumpit)
3422 continue;
3423
Nicolas Dichtel04652772013-03-22 06:28:42 +00003424 if (idx > s_idx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 memset(&cb->args[0], 0, sizeof(cb->args));
Nicolas Dichtel04652772013-03-22 06:28:42 +00003426 cb->prev_seq = 0;
3427 cb->seq = 0;
3428 }
David Ahernc63586d2018-10-24 12:59:02 -07003429 ret = dumpit(skb, cb);
Alexey Kodanev5e1acb42018-11-02 19:11:04 +03003430 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 break;
3432 }
3433 cb->family = idx;
3434
David Ahernc63586d2018-10-24 12:59:02 -07003435 return skb->len ? : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003438struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003439 unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003440 u32 event, gfp_t flags, int *new_nsid,
3441 int new_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003443 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct sk_buff *skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003445 int err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00003446 size_t if_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Alexei Starovoitov7f294052013-10-23 16:02:42 -07003448 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003449 if (skb == NULL)
3450 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Jiri Benc79e1ad12017-11-02 17:04:38 -02003452 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3453 type, 0, 0, change, 0, 0, event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003454 new_nsid, new_ifindex, -1);
Patrick McHardy26932562007-01-31 23:16:40 -08003455 if (err < 0) {
3456 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
3457 WARN_ON(err == -EMSGSIZE);
3458 kfree_skb(skb);
3459 goto errout;
3460 }
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003461 return skb;
Thomas Graf0ec6d3f2006-08-15 00:37:09 -07003462errout:
3463 if (err < 0)
Eric W. Biederman4b3da702007-11-19 22:27:40 -08003464 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003465 return NULL;
3466}
3467
3468void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3469{
3470 struct net *net = dev_net(dev);
3471
3472 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3473}
3474
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003475static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3476 unsigned int change, u32 event,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003477 gfp_t flags, int *new_nsid, int new_ifindex)
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003478{
3479 struct sk_buff *skb;
3480
Nicolas Dichteled2a80a2015-05-13 14:19:42 +02003481 if (dev->reg_state != NETREG_REGISTERED)
3482 return;
3483
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003484 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3485 new_ifindex);
Mahesh Bandewar395eea62014-12-03 13:46:24 -08003486 if (skb)
3487 rtmsg_ifinfo_send(skb, dev, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003489
3490void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3491 gfp_t flags)
3492{
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003493 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3494 NULL, 0);
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04003495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003497void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003498 gfp_t flags, int *new_nsid, int new_ifindex)
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003499{
3500 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01003501 new_nsid, new_ifindex);
Nicolas Dichtel6621dd22017-10-03 13:53:23 +02003502}
3503
John Fastabendd83b0602012-04-15 06:44:08 +00003504static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3505 struct net_device *dev,
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003506 u8 *addr, u16 vid, u32 pid, u32 seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003507 int type, unsigned int flags,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003508 int nlflags, u16 ndm_state)
John Fastabendd83b0602012-04-15 06:44:08 +00003509{
3510 struct nlmsghdr *nlh;
3511 struct ndmsg *ndm;
3512
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003513 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
John Fastabendd83b0602012-04-15 06:44:08 +00003514 if (!nlh)
3515 return -EMSGSIZE;
3516
3517 ndm = nlmsg_data(nlh);
3518 ndm->ndm_family = AF_BRIDGE;
3519 ndm->ndm_pad1 = 0;
3520 ndm->ndm_pad2 = 0;
3521 ndm->ndm_flags = flags;
3522 ndm->ndm_type = 0;
3523 ndm->ndm_ifindex = dev->ifindex;
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003524 ndm->ndm_state = ndm_state;
John Fastabendd83b0602012-04-15 06:44:08 +00003525
3526 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
3527 goto nla_put_failure;
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003528 if (vid)
3529 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
3530 goto nla_put_failure;
John Fastabendd83b0602012-04-15 06:44:08 +00003531
Johannes Berg053c0952015-01-16 22:09:00 +01003532 nlmsg_end(skb, nlh);
3533 return 0;
John Fastabendd83b0602012-04-15 06:44:08 +00003534
3535nla_put_failure:
3536 nlmsg_cancel(skb, nlh);
3537 return -EMSGSIZE;
3538}
3539
John Fastabend3ff661c2012-04-15 06:44:14 +00003540static inline size_t rtnl_fdb_nlmsg_size(void)
3541{
Sabrina Dubrocaf82ef3e2016-11-18 15:50:39 +01003542 return NLMSG_ALIGN(sizeof(struct ndmsg)) +
3543 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */
3544 nla_total_size(sizeof(u16)) + /* NDA_VLAN */
3545 0;
John Fastabend3ff661c2012-04-15 06:44:14 +00003546}
3547
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003548static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
3549 u16 ndm_state)
John Fastabend3ff661c2012-04-15 06:44:14 +00003550{
3551 struct net *net = dev_net(dev);
3552 struct sk_buff *skb;
3553 int err = -ENOBUFS;
3554
3555 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
3556 if (!skb)
3557 goto errout;
3558
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003559 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003560 0, 0, type, NTF_SELF, 0, ndm_state);
John Fastabend3ff661c2012-04-15 06:44:14 +00003561 if (err < 0) {
3562 kfree_skb(skb);
3563 goto errout;
3564 }
3565
3566 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
3567 return;
3568errout:
3569 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
3570}
3571
Vlad Yasevich090096b2013-03-06 15:39:42 +00003572/**
3573 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
3574 */
3575int ndo_dflt_fdb_add(struct ndmsg *ndm,
3576 struct nlattr *tb[],
3577 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003578 const unsigned char *addr, u16 vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003579 u16 flags)
3580{
3581 int err = -EINVAL;
3582
3583 /* If aging addresses are supported device will need to
3584 * implement its own handler for this.
3585 */
3586 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3587 pr_info("%s: FDB only supports static addresses\n", dev->name);
3588 return err;
3589 }
3590
Or Gerlitz65891fe2014-12-14 18:19:05 +02003591 if (vid) {
3592 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
3593 return err;
3594 }
3595
Vlad Yasevich090096b2013-03-06 15:39:42 +00003596 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3597 err = dev_uc_add_excl(dev, addr);
3598 else if (is_multicast_ether_addr(addr))
3599 err = dev_mc_add_excl(dev, addr);
3600
3601 /* Only return duplicate errors if NLM_F_EXCL is set */
3602 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3603 err = 0;
3604
3605 return err;
3606}
3607EXPORT_SYMBOL(ndo_dflt_fdb_add);
3608
Florian Westphalb88d12e2017-10-10 17:10:04 +02003609static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
3610 struct netlink_ext_ack *extack)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003611{
3612 u16 vid = 0;
3613
3614 if (vlan_attr) {
3615 if (nla_len(vlan_attr) != sizeof(u16)) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003616 NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003617 return -EINVAL;
3618 }
3619
3620 vid = nla_get_u16(vlan_attr);
3621
3622 if (!vid || vid >= VLAN_VID_MASK) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003623 NL_SET_ERR_MSG(extack, "invalid vlan id");
Jiri Pirkof6f64242014-11-28 14:34:15 +01003624 return -EINVAL;
3625 }
3626 }
3627 *p_vid = vid;
3628 return 0;
3629}
3630
David Ahernc21ef3e2017-04-16 09:48:24 -07003631static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
3632 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003633{
3634 struct net *net = sock_net(skb->sk);
John Fastabend77162022012-04-15 06:43:56 +00003635 struct ndmsg *ndm;
3636 struct nlattr *tb[NDA_MAX+1];
3637 struct net_device *dev;
3638 u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003639 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003640 int err;
3641
David Ahernc21ef3e2017-04-16 09:48:24 -07003642 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
John Fastabend77162022012-04-15 06:43:56 +00003643 if (err < 0)
3644 return err;
3645
3646 ndm = nlmsg_data(nlh);
3647 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003648 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003649 return -EINVAL;
3650 }
3651
3652 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3653 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003654 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003655 return -ENODEV;
3656 }
3657
3658 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003659 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003660 return -EINVAL;
3661 }
3662
Ido Schimmelda715772018-10-29 20:36:43 +00003663 if (dev->type != ARPHRD_ETHER) {
3664 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
3665 return -EINVAL;
3666 }
3667
John Fastabend77162022012-04-15 06:43:56 +00003668 addr = nla_data(tb[NDA_LLADDR]);
John Fastabend77162022012-04-15 06:43:56 +00003669
Florian Westphalb88d12e2017-10-10 17:10:04 +02003670 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003671 if (err)
3672 return err;
3673
John Fastabend77162022012-04-15 06:43:56 +00003674 err = -EOPNOTSUPP;
3675
3676 /* Support fdb on master device the net/bridge default case */
3677 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3678 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003679 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3680 const struct net_device_ops *ops = br_dev->netdev_ops;
3681
Jiri Pirkof6f64242014-11-28 14:34:15 +01003682 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
Petr Machata87b09842019-01-16 23:06:50 +00003683 nlh->nlmsg_flags, extack);
John Fastabend77162022012-04-15 06:43:56 +00003684 if (err)
3685 goto out;
3686 else
3687 ndm->ndm_flags &= ~NTF_MASTER;
3688 }
3689
3690 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003691 if ((ndm->ndm_flags & NTF_SELF)) {
3692 if (dev->netdev_ops->ndo_fdb_add)
3693 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003694 vid,
Petr Machata87b09842019-01-16 23:06:50 +00003695 nlh->nlmsg_flags,
3696 extack);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003697 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003698 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
Vlad Yasevich090096b2013-03-06 15:39:42 +00003699 nlh->nlmsg_flags);
John Fastabend77162022012-04-15 06:43:56 +00003700
John Fastabend3ff661c2012-04-15 06:44:14 +00003701 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003702 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
3703 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003704 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003705 }
John Fastabend77162022012-04-15 06:43:56 +00003706 }
3707out:
3708 return err;
3709}
3710
Vlad Yasevich090096b2013-03-06 15:39:42 +00003711/**
3712 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
3713 */
3714int ndo_dflt_fdb_del(struct ndmsg *ndm,
3715 struct nlattr *tb[],
3716 struct net_device *dev,
Jiri Pirkof6f64242014-11-28 14:34:15 +01003717 const unsigned char *addr, u16 vid)
Vlad Yasevich090096b2013-03-06 15:39:42 +00003718{
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003719 int err = -EINVAL;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003720
3721 /* If aging addresses are supported device will need to
3722 * implement its own handler for this.
3723 */
Sridhar Samudrala64535992013-08-08 15:19:48 -07003724 if (!(ndm->ndm_state & NUD_PERMANENT)) {
Vlad Yasevich090096b2013-03-06 15:39:42 +00003725 pr_info("%s: FDB only supports static addresses\n", dev->name);
Alexander Duyckc8a89c42014-07-15 15:15:20 -07003726 return err;
Vlad Yasevich090096b2013-03-06 15:39:42 +00003727 }
3728
3729 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3730 err = dev_uc_del(dev, addr);
3731 else if (is_multicast_ether_addr(addr))
3732 err = dev_mc_del(dev, addr);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003733
3734 return err;
3735}
3736EXPORT_SYMBOL(ndo_dflt_fdb_del);
3737
David Ahernc21ef3e2017-04-16 09:48:24 -07003738static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
3739 struct netlink_ext_ack *extack)
John Fastabend77162022012-04-15 06:43:56 +00003740{
3741 struct net *net = sock_net(skb->sk);
3742 struct ndmsg *ndm;
Vlad Yasevich1690be62013-02-13 12:00:18 +00003743 struct nlattr *tb[NDA_MAX+1];
John Fastabend77162022012-04-15 06:43:56 +00003744 struct net_device *dev;
3745 int err = -EINVAL;
3746 __u8 *addr;
Jiri Pirkof6f64242014-11-28 14:34:15 +01003747 u16 vid;
John Fastabend77162022012-04-15 06:43:56 +00003748
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07003749 if (!netlink_capable(skb, CAP_NET_ADMIN))
Vlad Yasevich1690be62013-02-13 12:00:18 +00003750 return -EPERM;
3751
David Ahernc21ef3e2017-04-16 09:48:24 -07003752 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003753 if (err < 0)
3754 return err;
John Fastabend77162022012-04-15 06:43:56 +00003755
3756 ndm = nlmsg_data(nlh);
3757 if (ndm->ndm_ifindex == 0) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003758 NL_SET_ERR_MSG(extack, "invalid ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003759 return -EINVAL;
3760 }
3761
3762 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
3763 if (dev == NULL) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003764 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabend77162022012-04-15 06:43:56 +00003765 return -ENODEV;
3766 }
3767
Vlad Yasevich1690be62013-02-13 12:00:18 +00003768 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02003769 NL_SET_ERR_MSG(extack, "invalid address");
John Fastabend77162022012-04-15 06:43:56 +00003770 return -EINVAL;
3771 }
3772
Ido Schimmelda715772018-10-29 20:36:43 +00003773 if (dev->type != ARPHRD_ETHER) {
3774 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
3775 return -EINVAL;
3776 }
3777
Vlad Yasevich1690be62013-02-13 12:00:18 +00003778 addr = nla_data(tb[NDA_LLADDR]);
Vlad Yasevich1690be62013-02-13 12:00:18 +00003779
Florian Westphalb88d12e2017-10-10 17:10:04 +02003780 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
Jiri Pirkof6f64242014-11-28 14:34:15 +01003781 if (err)
3782 return err;
3783
John Fastabend77162022012-04-15 06:43:56 +00003784 err = -EOPNOTSUPP;
3785
3786 /* Support fdb on master device the net/bridge default case */
3787 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
3788 (dev->priv_flags & IFF_BRIDGE_PORT)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00003789 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3790 const struct net_device_ops *ops = br_dev->netdev_ops;
John Fastabend77162022012-04-15 06:43:56 +00003791
Jiri Pirko898e5062013-01-03 22:48:52 +00003792 if (ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003793 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003794
3795 if (err)
3796 goto out;
3797 else
3798 ndm->ndm_flags &= ~NTF_MASTER;
3799 }
3800
3801 /* Embedded bridge, macvlan, and any other device support */
Vlad Yasevich090096b2013-03-06 15:39:42 +00003802 if (ndm->ndm_flags & NTF_SELF) {
3803 if (dev->netdev_ops->ndo_fdb_del)
Jiri Pirkof6f64242014-11-28 14:34:15 +01003804 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
3805 vid);
Vlad Yasevich090096b2013-03-06 15:39:42 +00003806 else
Jiri Pirkof6f64242014-11-28 14:34:15 +01003807 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
John Fastabend77162022012-04-15 06:43:56 +00003808
John Fastabend3ff661c2012-04-15 06:44:14 +00003809 if (!err) {
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003810 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
3811 ndm->ndm_state);
John Fastabend77162022012-04-15 06:43:56 +00003812 ndm->ndm_flags &= ~NTF_SELF;
John Fastabend3ff661c2012-04-15 06:44:14 +00003813 }
John Fastabend77162022012-04-15 06:43:56 +00003814 }
3815out:
3816 return err;
3817}
3818
John Fastabendd83b0602012-04-15 06:44:08 +00003819static int nlmsg_populate_fdb(struct sk_buff *skb,
3820 struct netlink_callback *cb,
3821 struct net_device *dev,
3822 int *idx,
3823 struct netdev_hw_addr_list *list)
3824{
3825 struct netdev_hw_addr *ha;
3826 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003827 u32 portid, seq;
John Fastabendd83b0602012-04-15 06:44:08 +00003828
Eric W. Biederman15e47302012-09-07 20:12:54 +00003829 portid = NETLINK_CB(cb->skb).portid;
John Fastabendd83b0602012-04-15 06:44:08 +00003830 seq = cb->nlh->nlmsg_seq;
3831
3832 list_for_each_entry(ha, &list->list, list) {
Roopa Prabhud2976532016-08-30 21:56:45 -07003833 if (*idx < cb->args[2])
John Fastabendd83b0602012-04-15 06:44:08 +00003834 goto skip;
3835
Hubert Sokolowski1e53d5b2015-04-09 12:16:17 +00003836 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
John Fastabenda7a558f2012-11-01 16:23:10 +00003837 portid, seq,
Nicolas Dichtel1c104a62014-03-19 17:47:49 +01003838 RTM_NEWNEIGH, NTF_SELF,
Hubert Sokolowskib3379042015-12-15 13:20:30 +00003839 NLM_F_MULTI, NUD_PERMANENT);
John Fastabendd83b0602012-04-15 06:44:08 +00003840 if (err < 0)
3841 return err;
3842skip:
3843 *idx += 1;
3844 }
3845 return 0;
3846}
3847
3848/**
Ben Hutchings2c530402012-07-10 10:55:09 +00003849 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
John Fastabendd83b0602012-04-15 06:44:08 +00003850 * @nlh: netlink message header
3851 * @dev: netdevice
3852 *
3853 * Default netdevice operation to dump the existing unicast address list.
John Fastabend91f3e7b2013-03-29 08:18:37 +00003854 * Returns number of addresses from list put in skb.
John Fastabendd83b0602012-04-15 06:44:08 +00003855 */
3856int ndo_dflt_fdb_dump(struct sk_buff *skb,
3857 struct netlink_callback *cb,
3858 struct net_device *dev,
Jamal Hadi Salim5d5eacb2014-07-10 07:01:58 -04003859 struct net_device *filter_dev,
Roopa Prabhud2976532016-08-30 21:56:45 -07003860 int *idx)
John Fastabendd83b0602012-04-15 06:44:08 +00003861{
3862 int err;
3863
Eric Dumazet68883892018-12-04 09:40:35 -08003864 if (dev->type != ARPHRD_ETHER)
3865 return -EINVAL;
3866
John Fastabendd83b0602012-04-15 06:44:08 +00003867 netif_addr_lock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003868 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
John Fastabendd83b0602012-04-15 06:44:08 +00003869 if (err)
3870 goto out;
Zhang Shengju2934c9d2016-11-30 16:37:34 +08003871 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
John Fastabendd83b0602012-04-15 06:44:08 +00003872out:
3873 netif_addr_unlock_bh(dev);
Roopa Prabhud2976532016-08-30 21:56:45 -07003874 return err;
John Fastabendd83b0602012-04-15 06:44:08 +00003875}
3876EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3877
David Ahern8c6e1372018-10-07 20:16:44 -07003878static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
3879 int *br_idx, int *brport_idx,
3880 struct netlink_ext_ack *extack)
3881{
3882 struct nlattr *tb[NDA_MAX + 1];
3883 struct ndmsg *ndm;
3884 int err, i;
3885
3886 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
3887 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
3888 return -EINVAL;
3889 }
3890
3891 ndm = nlmsg_data(nlh);
3892 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
3893 ndm->ndm_flags || ndm->ndm_type) {
3894 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request");
3895 return -EINVAL;
3896 }
3897
3898 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
3899 NULL, extack);
3900 if (err < 0)
3901 return err;
3902
3903 *brport_idx = ndm->ndm_ifindex;
3904 for (i = 0; i <= NDA_MAX; ++i) {
3905 if (!tb[i])
3906 continue;
3907
3908 switch (i) {
3909 case NDA_IFINDEX:
3910 if (nla_len(tb[i]) != sizeof(u32)) {
3911 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
3912 return -EINVAL;
3913 }
3914 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
3915 break;
3916 case NDA_MASTER:
3917 if (nla_len(tb[i]) != sizeof(u32)) {
3918 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
3919 return -EINVAL;
3920 }
3921 *br_idx = nla_get_u32(tb[NDA_MASTER]);
3922 break;
3923 default:
3924 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
3925 return -EINVAL;
3926 }
3927 }
3928
3929 return 0;
3930}
3931
David Ahern8dfbda12018-10-07 20:16:43 -07003932static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
3933 int *br_idx, int *brport_idx,
3934 struct netlink_ext_ack *extack)
3935{
David Ahern8dfbda12018-10-07 20:16:43 -07003936 struct nlattr *tb[IFLA_MAX+1];
3937 int err;
3938
3939 /* A hack to preserve kernel<->userspace interface.
3940 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
3941 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
3942 * So, check for ndmsg with an optional u32 attribute (not used here).
3943 * Fortunately these sizes don't conflict with the size of ifinfomsg
3944 * with an optional attribute.
3945 */
3946 if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
3947 (nlmsg_len(nlh) != sizeof(struct ndmsg) +
3948 nla_attr_size(sizeof(u32)))) {
David Ahern4565d7e2018-10-08 13:57:24 -07003949 struct ifinfomsg *ifm;
3950
David Ahern8dfbda12018-10-07 20:16:43 -07003951 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3952 ifla_policy, extack);
3953 if (err < 0) {
3954 return -EINVAL;
3955 } else if (err == 0) {
3956 if (tb[IFLA_MASTER])
3957 *br_idx = nla_get_u32(tb[IFLA_MASTER]);
3958 }
3959
David Ahern4565d7e2018-10-08 13:57:24 -07003960 ifm = nlmsg_data(nlh);
David Ahern8dfbda12018-10-07 20:16:43 -07003961 *brport_idx = ifm->ifi_index;
3962 }
3963 return 0;
3964}
3965
John Fastabend77162022012-04-15 06:43:56 +00003966static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3967{
John Fastabend77162022012-04-15 06:43:56 +00003968 struct net_device *dev;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003969 struct net_device *br_dev = NULL;
3970 const struct net_device_ops *ops = NULL;
3971 const struct net_device_ops *cops = NULL;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003972 struct net *net = sock_net(skb->sk);
Roopa Prabhud2976532016-08-30 21:56:45 -07003973 struct hlist_head *head;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003974 int brport_idx = 0;
3975 int br_idx = 0;
Roopa Prabhud2976532016-08-30 21:56:45 -07003976 int h, s_h;
3977 int idx = 0, s_idx;
3978 int err = 0;
3979 int fidx = 0;
John Fastabend77162022012-04-15 06:43:56 +00003980
David Ahern8c6e1372018-10-07 20:16:44 -07003981 if (cb->strict_check)
3982 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
3983 cb->extack);
3984 else
3985 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
3986 cb->extack);
David Ahern8dfbda12018-10-07 20:16:43 -07003987 if (err < 0)
3988 return err;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003989
3990 if (br_idx) {
3991 br_dev = __dev_get_by_index(net, br_idx);
3992 if (!br_dev)
3993 return -ENODEV;
3994
3995 ops = br_dev->netdev_ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04003996 }
3997
Roopa Prabhud2976532016-08-30 21:56:45 -07003998 s_h = cb->args[0];
3999 s_idx = cb->args[1];
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004000
Roopa Prabhud2976532016-08-30 21:56:45 -07004001 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4002 idx = 0;
4003 head = &net->dev_index_head[h];
4004 hlist_for_each_entry(dev, head, index_hlist) {
4005
4006 if (brport_idx && (dev->ifindex != brport_idx))
4007 continue;
4008
4009 if (!br_idx) { /* user did not specify a specific bridge */
4010 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4011 br_dev = netdev_master_upper_dev_get(dev);
4012 cops = br_dev->netdev_ops;
4013 }
4014 } else {
4015 if (dev != br_dev &&
4016 !(dev->priv_flags & IFF_BRIDGE_PORT))
4017 continue;
4018
4019 if (br_dev != netdev_master_upper_dev_get(dev) &&
4020 !(dev->priv_flags & IFF_EBRIDGE))
4021 continue;
4022 cops = ops;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004023 }
4024
Roopa Prabhud2976532016-08-30 21:56:45 -07004025 if (idx < s_idx)
4026 goto cont;
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004027
Roopa Prabhud2976532016-08-30 21:56:45 -07004028 if (dev->priv_flags & IFF_BRIDGE_PORT) {
4029 if (cops && cops->ndo_fdb_dump) {
4030 err = cops->ndo_fdb_dump(skb, cb,
4031 br_dev, dev,
4032 &fidx);
4033 if (err == -EMSGSIZE)
4034 goto out;
4035 }
4036 }
Jamal Hadi Salim5e6d2432014-07-10 07:01:59 -04004037
Roopa Prabhud2976532016-08-30 21:56:45 -07004038 if (dev->netdev_ops->ndo_fdb_dump)
4039 err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4040 dev, NULL,
4041 &fidx);
4042 else
4043 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4044 &fidx);
4045 if (err == -EMSGSIZE)
4046 goto out;
4047
4048 cops = NULL;
4049
4050 /* reset fdb offset to 0 for rest of the interfaces */
4051 cb->args[2] = 0;
4052 fidx = 0;
4053cont:
4054 idx++;
John Fastabend77162022012-04-15 06:43:56 +00004055 }
John Fastabend77162022012-04-15 06:43:56 +00004056 }
John Fastabend77162022012-04-15 06:43:56 +00004057
Roopa Prabhud2976532016-08-30 21:56:45 -07004058out:
4059 cb->args[0] = h;
4060 cb->args[1] = idx;
4061 cb->args[2] = fidx;
4062
John Fastabend77162022012-04-15 06:43:56 +00004063 return skb->len;
4064}
4065
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004066static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4067 struct nlattr **tb, u8 *ndm_flags,
4068 int *br_idx, int *brport_idx, u8 **addr,
4069 u16 *vid, struct netlink_ext_ack *extack)
4070{
4071 struct ndmsg *ndm;
4072 int err, i;
4073
4074 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4075 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4076 return -EINVAL;
4077 }
4078
4079 ndm = nlmsg_data(nlh);
4080 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
4081 ndm->ndm_type) {
4082 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4083 return -EINVAL;
4084 }
4085
4086 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4087 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4088 return -EINVAL;
4089 }
4090
4091 err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
4092 nda_policy, extack);
4093 if (err < 0)
4094 return err;
4095
4096 *ndm_flags = ndm->ndm_flags;
4097 *brport_idx = ndm->ndm_ifindex;
4098 for (i = 0; i <= NDA_MAX; ++i) {
4099 if (!tb[i])
4100 continue;
4101
4102 switch (i) {
4103 case NDA_MASTER:
4104 *br_idx = nla_get_u32(tb[i]);
4105 break;
4106 case NDA_LLADDR:
4107 if (nla_len(tb[i]) != ETH_ALEN) {
4108 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4109 return -EINVAL;
4110 }
4111 *addr = nla_data(tb[i]);
4112 break;
4113 case NDA_VLAN:
4114 err = fdb_vid_parse(tb[i], vid, extack);
4115 if (err)
4116 return err;
4117 break;
4118 case NDA_VNI:
4119 break;
4120 default:
4121 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4122 return -EINVAL;
4123 }
4124 }
4125
4126 return 0;
4127}
4128
4129static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4130 struct netlink_ext_ack *extack)
4131{
4132 struct net_device *dev = NULL, *br_dev = NULL;
4133 const struct net_device_ops *ops = NULL;
4134 struct net *net = sock_net(in_skb->sk);
4135 struct nlattr *tb[NDA_MAX + 1];
4136 struct sk_buff *skb;
4137 int brport_idx = 0;
4138 u8 ndm_flags = 0;
4139 int br_idx = 0;
4140 u8 *addr = NULL;
4141 u16 vid = 0;
4142 int err;
4143
4144 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4145 &brport_idx, &addr, &vid, extack);
4146 if (err < 0)
4147 return err;
4148
Nikolay Aleksandrovf989d032018-12-30 14:33:20 +02004149 if (!addr) {
4150 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4151 return -EINVAL;
4152 }
4153
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08004154 if (brport_idx) {
4155 dev = __dev_get_by_index(net, brport_idx);
4156 if (!dev) {
4157 NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4158 return -ENODEV;
4159 }
4160 }
4161
4162 if (br_idx) {
4163 if (dev) {
4164 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4165 return -EINVAL;
4166 }
4167
4168 br_dev = __dev_get_by_index(net, br_idx);
4169 if (!br_dev) {
4170 NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4171 return -EINVAL;
4172 }
4173 ops = br_dev->netdev_ops;
4174 }
4175
4176 if (dev) {
4177 if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4178 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
4179 NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4180 return -EINVAL;
4181 }
4182 br_dev = netdev_master_upper_dev_get(dev);
4183 if (!br_dev) {
4184 NL_SET_ERR_MSG(extack, "Master of device not found");
4185 return -EINVAL;
4186 }
4187 ops = br_dev->netdev_ops;
4188 } else {
4189 if (!(ndm_flags & NTF_SELF)) {
4190 NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4191 return -EINVAL;
4192 }
4193 ops = dev->netdev_ops;
4194 }
4195 }
4196
4197 if (!br_dev && !dev) {
4198 NL_SET_ERR_MSG(extack, "No device specified");
4199 return -ENODEV;
4200 }
4201
4202 if (!ops || !ops->ndo_fdb_get) {
4203 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4204 return -EOPNOTSUPP;
4205 }
4206
4207 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4208 if (!skb)
4209 return -ENOBUFS;
4210
4211 if (br_dev)
4212 dev = br_dev;
4213 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4214 NETLINK_CB(in_skb).portid,
4215 nlh->nlmsg_seq, extack);
4216 if (err)
4217 goto out;
4218
4219 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4220out:
4221 kfree_skb(skb);
4222 return err;
4223}
4224
Scott Feldman2c3c0312014-11-28 14:34:25 +01004225static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4226 unsigned int attrnum, unsigned int flag)
4227{
4228 if (mask & flag)
4229 return nla_put_u8(skb, attrnum, !!(flags & flag));
4230 return 0;
4231}
4232
John Fastabend815cccb2012-10-24 08:13:09 +00004233int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Scott Feldman2c3c0312014-11-28 14:34:25 +01004234 struct net_device *dev, u16 mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004235 u32 flags, u32 mask, int nlflags,
4236 u32 filter_mask,
4237 int (*vlan_fill)(struct sk_buff *skb,
4238 struct net_device *dev,
4239 u32 filter_mask))
John Fastabend815cccb2012-10-24 08:13:09 +00004240{
4241 struct nlmsghdr *nlh;
4242 struct ifinfomsg *ifm;
4243 struct nlattr *br_afspec;
Scott Feldman2c3c0312014-11-28 14:34:25 +01004244 struct nlattr *protinfo;
John Fastabend815cccb2012-10-24 08:13:09 +00004245 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Jiri Pirko898e5062013-01-03 22:48:52 +00004246 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004247 int err = 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004248
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004249 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
John Fastabend815cccb2012-10-24 08:13:09 +00004250 if (nlh == NULL)
4251 return -EMSGSIZE;
4252
4253 ifm = nlmsg_data(nlh);
4254 ifm->ifi_family = AF_BRIDGE;
4255 ifm->__ifi_pad = 0;
4256 ifm->ifi_type = dev->type;
4257 ifm->ifi_index = dev->ifindex;
4258 ifm->ifi_flags = dev_get_flags(dev);
4259 ifm->ifi_change = 0;
4260
4261
4262 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4263 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4264 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
Jiri Pirko898e5062013-01-03 22:48:52 +00004265 (br_dev &&
4266 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
John Fastabend815cccb2012-10-24 08:13:09 +00004267 (dev->addr_len &&
4268 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004269 (dev->ifindex != dev_get_iflink(dev) &&
4270 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
John Fastabend815cccb2012-10-24 08:13:09 +00004271 goto nla_put_failure;
4272
4273 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
4274 if (!br_afspec)
4275 goto nla_put_failure;
4276
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004277 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
John Fastabend815cccb2012-10-24 08:13:09 +00004278 nla_nest_cancel(skb, br_afspec);
4279 goto nla_put_failure;
4280 }
Roopa Prabhu1d460b92014-12-08 14:04:20 -08004281
4282 if (mode != BRIDGE_MODE_UNDEF) {
4283 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4284 nla_nest_cancel(skb, br_afspec);
4285 goto nla_put_failure;
4286 }
4287 }
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004288 if (vlan_fill) {
4289 err = vlan_fill(skb, dev, filter_mask);
4290 if (err) {
4291 nla_nest_cancel(skb, br_afspec);
4292 goto nla_put_failure;
4293 }
4294 }
John Fastabend815cccb2012-10-24 08:13:09 +00004295 nla_nest_end(skb, br_afspec);
4296
Scott Feldman2c3c0312014-11-28 14:34:25 +01004297 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
4298 if (!protinfo)
4299 goto nla_put_failure;
4300
4301 if (brport_nla_put_flag(skb, flags, mask,
4302 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4303 brport_nla_put_flag(skb, flags, mask,
4304 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4305 brport_nla_put_flag(skb, flags, mask,
4306 IFLA_BRPORT_FAST_LEAVE,
4307 BR_MULTICAST_FAST_LEAVE) ||
4308 brport_nla_put_flag(skb, flags, mask,
4309 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4310 brport_nla_put_flag(skb, flags, mask,
4311 IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4312 brport_nla_put_flag(skb, flags, mask,
4313 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4314 brport_nla_put_flag(skb, flags, mask,
4315 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4316 brport_nla_put_flag(skb, flags, mask,
4317 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
4318 nla_nest_cancel(skb, protinfo);
4319 goto nla_put_failure;
4320 }
4321
4322 nla_nest_end(skb, protinfo);
4323
Johannes Berg053c0952015-01-16 22:09:00 +01004324 nlmsg_end(skb, nlh);
4325 return 0;
John Fastabend815cccb2012-10-24 08:13:09 +00004326nla_put_failure:
4327 nlmsg_cancel(skb, nlh);
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004328 return err ? err : -EMSGSIZE;
John Fastabend815cccb2012-10-24 08:13:09 +00004329}
Scott Feldman7d4f8d82015-06-22 00:27:17 -07004330EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
John Fastabend815cccb2012-10-24 08:13:09 +00004331
David Ahern2d011be2018-10-07 20:16:31 -07004332static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4333 bool strict_check, u32 *filter_mask,
4334 struct netlink_ext_ack *extack)
4335{
4336 struct nlattr *tb[IFLA_MAX+1];
4337 int err, i;
4338
4339 if (strict_check) {
4340 struct ifinfomsg *ifm;
4341
4342 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4343 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4344 return -EINVAL;
4345 }
4346
4347 ifm = nlmsg_data(nlh);
4348 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4349 ifm->ifi_change || ifm->ifi_index) {
4350 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4351 return -EINVAL;
4352 }
4353
4354 err = nlmsg_parse_strict(nlh, sizeof(struct ifinfomsg), tb,
4355 IFLA_MAX, ifla_policy, extack);
4356 } else {
4357 err = nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb,
4358 IFLA_MAX, ifla_policy, extack);
4359 }
4360 if (err < 0)
4361 return err;
4362
4363 /* new attributes should only be added with strict checking */
4364 for (i = 0; i <= IFLA_MAX; ++i) {
4365 if (!tb[i])
4366 continue;
4367
4368 switch (i) {
4369 case IFLA_EXT_MASK:
4370 *filter_mask = nla_get_u32(tb[i]);
4371 break;
4372 default:
4373 if (strict_check) {
4374 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4375 return -EINVAL;
4376 }
4377 }
4378 }
4379
4380 return 0;
4381}
4382
John Fastabende5a55a82012-10-24 08:12:57 +00004383static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4384{
David Ahern2d011be2018-10-07 20:16:31 -07004385 const struct nlmsghdr *nlh = cb->nlh;
John Fastabende5a55a82012-10-24 08:12:57 +00004386 struct net *net = sock_net(skb->sk);
4387 struct net_device *dev;
4388 int idx = 0;
4389 u32 portid = NETLINK_CB(cb->skb).portid;
David Ahern2d011be2018-10-07 20:16:31 -07004390 u32 seq = nlh->nlmsg_seq;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004391 u32 filter_mask = 0;
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004392 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00004393
David Ahern2d011be2018-10-07 20:16:31 -07004394 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4395 cb->extack);
4396 if (err < 0 && cb->strict_check)
4397 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004398
4399 rcu_read_lock();
4400 for_each_netdev_rcu(net, dev) {
4401 const struct net_device_ops *ops = dev->netdev_ops;
Jiri Pirko898e5062013-01-03 22:48:52 +00004402 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
John Fastabende5a55a82012-10-24 08:12:57 +00004403
Jiri Pirko898e5062013-01-03 22:48:52 +00004404 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004405 if (idx >= cb->args[0]) {
4406 err = br_dev->netdev_ops->ndo_bridge_getlink(
4407 skb, portid, seq, dev,
4408 filter_mask, NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004409 if (err < 0 && err != -EOPNOTSUPP) {
4410 if (likely(skb->len))
4411 break;
4412
4413 goto out_err;
4414 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004415 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004416 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004417 }
4418
4419 if (ops->ndo_bridge_getlink) {
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004420 if (idx >= cb->args[0]) {
4421 err = ops->ndo_bridge_getlink(skb, portid,
4422 seq, dev,
4423 filter_mask,
4424 NLM_F_MULTI);
David Ahernf6c57752017-05-15 23:19:17 -07004425 if (err < 0 && err != -EOPNOTSUPP) {
4426 if (likely(skb->len))
4427 break;
4428
4429 goto out_err;
4430 }
Roopa Prabhud64f69b2015-09-15 14:44:29 -07004431 }
Ben Hutchings25b1e672012-11-02 12:56:52 +00004432 idx++;
John Fastabende5a55a82012-10-24 08:12:57 +00004433 }
4434 }
David Ahernf6c57752017-05-15 23:19:17 -07004435 err = skb->len;
4436out_err:
John Fastabende5a55a82012-10-24 08:12:57 +00004437 rcu_read_unlock();
4438 cb->args[0] = idx;
4439
David Ahernf6c57752017-05-15 23:19:17 -07004440 return err;
John Fastabende5a55a82012-10-24 08:12:57 +00004441}
4442
John Fastabend2469ffd2012-10-24 08:13:03 +00004443static inline size_t bridge_nlmsg_size(void)
4444{
4445 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4446 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4447 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4448 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */
4449 + nla_total_size(sizeof(u32)) /* IFLA_MTU */
4450 + nla_total_size(sizeof(u32)) /* IFLA_LINK */
4451 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */
4452 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */
4453 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */
4454 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */
4455 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */
4456}
4457
Roopa Prabhu02dba432015-01-14 20:02:25 -08004458static int rtnl_bridge_notify(struct net_device *dev)
John Fastabend2469ffd2012-10-24 08:13:03 +00004459{
4460 struct net *net = dev_net(dev);
John Fastabend2469ffd2012-10-24 08:13:03 +00004461 struct sk_buff *skb;
4462 int err = -EOPNOTSUPP;
4463
Roopa Prabhu02dba432015-01-14 20:02:25 -08004464 if (!dev->netdev_ops->ndo_bridge_getlink)
4465 return 0;
4466
John Fastabend2469ffd2012-10-24 08:13:03 +00004467 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4468 if (!skb) {
4469 err = -ENOMEM;
4470 goto errout;
4471 }
4472
Nicolas Dichtel46c264d2015-04-28 18:33:49 +02004473 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004474 if (err < 0)
4475 goto errout;
John Fastabend2469ffd2012-10-24 08:13:03 +00004476
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004477 if (!skb->len)
4478 goto errout;
4479
John Fastabend2469ffd2012-10-24 08:13:03 +00004480 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
4481 return 0;
4482errout:
4483 WARN_ON(err == -EMSGSIZE);
4484 kfree_skb(skb);
Roopa Prabhu59ccaaa2015-01-28 16:23:11 -08004485 if (err)
4486 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
John Fastabend2469ffd2012-10-24 08:13:03 +00004487 return err;
4488}
4489
David Ahernc21ef3e2017-04-16 09:48:24 -07004490static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
4491 struct netlink_ext_ack *extack)
John Fastabende5a55a82012-10-24 08:12:57 +00004492{
4493 struct net *net = sock_net(skb->sk);
4494 struct ifinfomsg *ifm;
4495 struct net_device *dev;
John Fastabend2469ffd2012-10-24 08:13:03 +00004496 struct nlattr *br_spec, *attr = NULL;
4497 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004498 u16 flags = 0;
John Fastabendc38e01b2012-11-02 16:32:36 +00004499 bool have_flags = false;
John Fastabende5a55a82012-10-24 08:12:57 +00004500
4501 if (nlmsg_len(nlh) < sizeof(*ifm))
4502 return -EINVAL;
4503
4504 ifm = nlmsg_data(nlh);
4505 if (ifm->ifi_family != AF_BRIDGE)
4506 return -EPFNOSUPPORT;
4507
4508 dev = __dev_get_by_index(net, ifm->ifi_index);
4509 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004510 NL_SET_ERR_MSG(extack, "unknown ifindex");
John Fastabende5a55a82012-10-24 08:12:57 +00004511 return -ENODEV;
4512 }
4513
John Fastabend2469ffd2012-10-24 08:13:03 +00004514 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4515 if (br_spec) {
4516 nla_for_each_nested(attr, br_spec, rem) {
4517 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004518 if (nla_len(attr) < sizeof(flags))
4519 return -EINVAL;
4520
John Fastabendc38e01b2012-11-02 16:32:36 +00004521 have_flags = true;
John Fastabend2469ffd2012-10-24 08:13:03 +00004522 flags = nla_get_u16(attr);
4523 break;
4524 }
4525 }
4526 }
4527
4528 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
Jiri Pirko898e5062013-01-03 22:48:52 +00004529 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4530
4531 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004532 err = -EOPNOTSUPP;
4533 goto out;
4534 }
4535
Petr Machata2fd527b2018-12-12 17:02:48 +00004536 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
4537 extack);
John Fastabende5a55a82012-10-24 08:12:57 +00004538 if (err)
4539 goto out;
John Fastabend2469ffd2012-10-24 08:13:03 +00004540
4541 flags &= ~BRIDGE_FLAGS_MASTER;
John Fastabende5a55a82012-10-24 08:12:57 +00004542 }
4543
John Fastabend2469ffd2012-10-24 08:13:03 +00004544 if ((flags & BRIDGE_FLAGS_SELF)) {
4545 if (!dev->netdev_ops->ndo_bridge_setlink)
4546 err = -EOPNOTSUPP;
4547 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004548 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
Petr Machata2fd527b2018-12-12 17:02:48 +00004549 flags,
4550 extack);
Roopa Prabhu02dba432015-01-14 20:02:25 -08004551 if (!err) {
John Fastabend2469ffd2012-10-24 08:13:03 +00004552 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004553
4554 /* Generate event to notify upper layer of bridge
4555 * change
4556 */
4557 err = rtnl_bridge_notify(dev);
4558 }
John Fastabend2469ffd2012-10-24 08:13:03 +00004559 }
4560
John Fastabendc38e01b2012-11-02 16:32:36 +00004561 if (have_flags)
John Fastabend2469ffd2012-10-24 08:13:03 +00004562 memcpy(nla_data(attr), &flags, sizeof(flags));
John Fastabende5a55a82012-10-24 08:12:57 +00004563out:
4564 return err;
4565}
4566
David Ahernc21ef3e2017-04-16 09:48:24 -07004567static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
4568 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +00004569{
4570 struct net *net = sock_net(skb->sk);
4571 struct ifinfomsg *ifm;
4572 struct net_device *dev;
4573 struct nlattr *br_spec, *attr = NULL;
4574 int rem, err = -EOPNOTSUPP;
Rosen, Rami4de8b412015-01-19 11:45:04 +02004575 u16 flags = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00004576 bool have_flags = false;
4577
4578 if (nlmsg_len(nlh) < sizeof(*ifm))
4579 return -EINVAL;
4580
4581 ifm = nlmsg_data(nlh);
4582 if (ifm->ifi_family != AF_BRIDGE)
4583 return -EPFNOSUPPORT;
4584
4585 dev = __dev_get_by_index(net, ifm->ifi_index);
4586 if (!dev) {
Florian Westphalb88d12e2017-10-10 17:10:04 +02004587 NL_SET_ERR_MSG(extack, "unknown ifindex");
Vlad Yasevich407af322013-02-13 12:00:12 +00004588 return -ENODEV;
4589 }
4590
4591 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
4592 if (br_spec) {
4593 nla_for_each_nested(attr, br_spec, rem) {
4594 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
Thomas Graf6e8d1c52014-11-26 13:42:16 +01004595 if (nla_len(attr) < sizeof(flags))
4596 return -EINVAL;
4597
Vlad Yasevich407af322013-02-13 12:00:12 +00004598 have_flags = true;
4599 flags = nla_get_u16(attr);
4600 break;
4601 }
4602 }
4603 }
4604
Vlad Yasevich407af322013-02-13 12:00:12 +00004605 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
4606 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4607
4608 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
4609 err = -EOPNOTSUPP;
4610 goto out;
4611 }
4612
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004613 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004614 if (err)
4615 goto out;
4616
4617 flags &= ~BRIDGE_FLAGS_MASTER;
4618 }
4619
4620 if ((flags & BRIDGE_FLAGS_SELF)) {
4621 if (!dev->netdev_ops->ndo_bridge_dellink)
4622 err = -EOPNOTSUPP;
4623 else
Roopa Prabhuadd511b2015-01-29 22:40:12 -08004624 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
4625 flags);
Vlad Yasevich407af322013-02-13 12:00:12 +00004626
Roopa Prabhu02dba432015-01-14 20:02:25 -08004627 if (!err) {
Vlad Yasevich407af322013-02-13 12:00:12 +00004628 flags &= ~BRIDGE_FLAGS_SELF;
Roopa Prabhu02dba432015-01-14 20:02:25 -08004629
4630 /* Generate event to notify upper layer of bridge
4631 * change
4632 */
4633 err = rtnl_bridge_notify(dev);
4634 }
Vlad Yasevich407af322013-02-13 12:00:12 +00004635 }
4636
4637 if (have_flags)
4638 memcpy(nla_data(attr), &flags, sizeof(flags));
Vlad Yasevich407af322013-02-13 12:00:12 +00004639out:
4640 return err;
4641}
4642
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004643static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
4644{
4645 return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
4646 (!idxattr || idxattr == attrid);
4647}
4648
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004649#define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1)
4650static int rtnl_get_offload_stats_attr_size(int attr_id)
4651{
4652 switch (attr_id) {
4653 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
4654 return sizeof(struct rtnl_link_stats64);
4655 }
4656
4657 return 0;
4658}
4659
4660static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev,
4661 int *prividx)
4662{
4663 struct nlattr *attr = NULL;
4664 int attr_id, size;
4665 void *attr_data;
4666 int err;
4667
4668 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4669 dev->netdev_ops->ndo_get_offload_stats))
4670 return -ENODATA;
4671
4672 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4673 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
4674 if (attr_id < *prividx)
4675 continue;
4676
4677 size = rtnl_get_offload_stats_attr_size(attr_id);
4678 if (!size)
4679 continue;
4680
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004681 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004682 continue;
4683
4684 attr = nla_reserve_64bit(skb, attr_id, size,
4685 IFLA_OFFLOAD_XSTATS_UNSPEC);
4686 if (!attr)
4687 goto nla_put_failure;
4688
4689 attr_data = nla_data(attr);
4690 memset(attr_data, 0, size);
4691 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev,
4692 attr_data);
4693 if (err)
4694 goto get_offload_stats_failure;
4695 }
4696
4697 if (!attr)
4698 return -ENODATA;
4699
4700 *prividx = 0;
4701 return 0;
4702
4703nla_put_failure:
4704 err = -EMSGSIZE;
4705get_offload_stats_failure:
4706 *prividx = attr_id;
4707 return err;
4708}
4709
4710static int rtnl_get_offload_stats_size(const struct net_device *dev)
4711{
4712 int nla_size = 0;
4713 int attr_id;
4714 int size;
4715
4716 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats &&
4717 dev->netdev_ops->ndo_get_offload_stats))
4718 return 0;
4719
4720 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST;
4721 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) {
Or Gerlitz3df5b3c2016-11-22 23:09:54 +02004722 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id))
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004723 continue;
4724 size = rtnl_get_offload_stats_attr_size(attr_id);
4725 nla_size += nla_total_size_64bit(size);
4726 }
4727
4728 if (nla_size != 0)
4729 nla_size += nla_total_size(0);
4730
4731 return nla_size;
4732}
4733
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004734static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
4735 int type, u32 pid, u32 seq, u32 change,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004736 unsigned int flags, unsigned int filter_mask,
4737 int *idxattr, int *prividx)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004738{
4739 struct if_stats_msg *ifsm;
4740 struct nlmsghdr *nlh;
4741 struct nlattr *attr;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004742 int s_prividx = *prividx;
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004743 int err;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004744
4745 ASSERT_RTNL();
4746
4747 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
4748 if (!nlh)
4749 return -EMSGSIZE;
4750
4751 ifsm = nlmsg_data(nlh);
Nikolay Aleksandrovce024f42017-10-03 13:20:48 +03004752 ifsm->family = PF_UNSPEC;
4753 ifsm->pad1 = 0;
4754 ifsm->pad2 = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004755 ifsm->ifindex = dev->ifindex;
4756 ifsm->filter_mask = filter_mask;
4757
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004758 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004759 struct rtnl_link_stats64 *sp;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004760
Nicolas Dichtel58414d32016-04-21 18:58:25 +02004761 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
4762 sizeof(struct rtnl_link_stats64),
4763 IFLA_STATS_UNSPEC);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004764 if (!attr)
4765 goto nla_put_failure;
4766
4767 sp = nla_data(attr);
4768 dev_get_stats(dev, sp);
4769 }
4770
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004771 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
4772 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
4773
4774 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004775 *idxattr = IFLA_STATS_LINK_XSTATS;
4776 attr = nla_nest_start(skb,
4777 IFLA_STATS_LINK_XSTATS);
4778 if (!attr)
4779 goto nla_put_failure;
4780
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004781 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
4782 nla_nest_end(skb, attr);
4783 if (err)
4784 goto nla_put_failure;
4785 *idxattr = 0;
4786 }
4787 }
4788
4789 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
4790 *idxattr)) {
4791 const struct rtnl_link_ops *ops = NULL;
4792 const struct net_device *master;
4793
4794 master = netdev_master_upper_dev_get(dev);
4795 if (master)
4796 ops = master->rtnl_link_ops;
4797 if (ops && ops->fill_linkxstats) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004798 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
4799 attr = nla_nest_start(skb,
4800 IFLA_STATS_LINK_XSTATS_SLAVE);
4801 if (!attr)
4802 goto nla_put_failure;
4803
4804 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004805 nla_nest_end(skb, attr);
4806 if (err)
4807 goto nla_put_failure;
4808 *idxattr = 0;
4809 }
4810 }
4811
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004812 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
4813 *idxattr)) {
4814 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
4815 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS);
4816 if (!attr)
4817 goto nla_put_failure;
4818
4819 err = rtnl_get_offload_stats(skb, dev, prividx);
4820 if (err == -ENODATA)
4821 nla_nest_cancel(skb, attr);
4822 else
4823 nla_nest_end(skb, attr);
4824
4825 if (err && err != -ENODATA)
4826 goto nla_put_failure;
4827 *idxattr = 0;
4828 }
4829
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004830 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
4831 struct rtnl_af_ops *af_ops;
4832
4833 *idxattr = IFLA_STATS_AF_SPEC;
4834 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC);
4835 if (!attr)
4836 goto nla_put_failure;
4837
Florian Westphal5fa85a02017-10-16 15:44:36 +02004838 rcu_read_lock();
4839 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004840 if (af_ops->fill_stats_af) {
4841 struct nlattr *af;
4842 int err;
4843
4844 af = nla_nest_start(skb, af_ops->family);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004845 if (!af) {
4846 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004847 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004848 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004849 err = af_ops->fill_stats_af(skb, dev);
4850
Florian Westphal5fa85a02017-10-16 15:44:36 +02004851 if (err == -ENODATA) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004852 nla_nest_cancel(skb, af);
Florian Westphal5fa85a02017-10-16 15:44:36 +02004853 } else if (err < 0) {
4854 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004855 goto nla_put_failure;
Florian Westphal5fa85a02017-10-16 15:44:36 +02004856 }
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004857
4858 nla_nest_end(skb, af);
4859 }
4860 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004861 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004862
4863 nla_nest_end(skb, attr);
4864
4865 *idxattr = 0;
4866 }
4867
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004868 nlmsg_end(skb, nlh);
4869
4870 return 0;
4871
4872nla_put_failure:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004873 /* not a multi message or no progress mean a real error */
4874 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
4875 nlmsg_cancel(skb, nlh);
4876 else
4877 nlmsg_end(skb, nlh);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004878
4879 return -EMSGSIZE;
4880}
4881
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004882static size_t if_nlmsg_stats_size(const struct net_device *dev,
4883 u32 filter_mask)
4884{
4885 size_t size = 0;
4886
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004887 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004888 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
4889
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004890 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
4891 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004892 int attr = IFLA_STATS_LINK_XSTATS;
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004893
4894 if (ops && ops->get_linkxstats_size) {
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004895 size += nla_total_size(ops->get_linkxstats_size(dev,
4896 attr));
Nikolay Aleksandrov97a47fa2016-04-30 10:25:27 +02004897 /* for IFLA_STATS_LINK_XSTATS */
4898 size += nla_total_size(0);
4899 }
4900 }
4901
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02004902 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
4903 struct net_device *_dev = (struct net_device *)dev;
4904 const struct rtnl_link_ops *ops = NULL;
4905 const struct net_device *master;
4906
4907 /* netdev_master_upper_dev_get can't take const */
4908 master = netdev_master_upper_dev_get(_dev);
4909 if (master)
4910 ops = master->rtnl_link_ops;
4911 if (ops && ops->get_linkxstats_size) {
4912 int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
4913
4914 size += nla_total_size(ops->get_linkxstats_size(dev,
4915 attr));
4916 /* for IFLA_STATS_LINK_XSTATS_SLAVE */
4917 size += nla_total_size(0);
4918 }
4919 }
4920
Nogah Frankel69ae6ad2016-09-16 15:05:37 +02004921 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0))
4922 size += rtnl_get_offload_stats_size(dev);
4923
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004924 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
4925 struct rtnl_af_ops *af_ops;
4926
4927 /* for IFLA_STATS_AF_SPEC */
4928 size += nla_total_size(0);
4929
Florian Westphal5fa85a02017-10-16 15:44:36 +02004930 rcu_read_lock();
4931 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004932 if (af_ops->get_stats_af_size) {
4933 size += nla_total_size(
4934 af_ops->get_stats_af_size(dev));
4935
4936 /* for AF_* */
4937 size += nla_total_size(0);
4938 }
4939 }
Florian Westphal5fa85a02017-10-16 15:44:36 +02004940 rcu_read_unlock();
Robert Shearmanaefb4d42017-01-16 14:16:36 +00004941 }
4942
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004943 return size;
4944}
4945
Jakub Kicinski51bc8602019-01-18 10:46:14 -08004946static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
4947 bool is_dump, struct netlink_ext_ack *extack)
4948{
4949 struct if_stats_msg *ifsm;
4950
4951 if (nlh->nlmsg_len < sizeof(*ifsm)) {
4952 NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
4953 return -EINVAL;
4954 }
4955
4956 if (!strict_check)
4957 return 0;
4958
4959 ifsm = nlmsg_data(nlh);
4960
4961 /* only requests using strict checks can pass data to influence
4962 * the dump. The legacy exception is filter_mask.
4963 */
4964 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
4965 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
4966 return -EINVAL;
4967 }
4968 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) {
4969 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header");
4970 return -EINVAL;
4971 }
Jakub Kicinski6300acb2019-01-18 10:46:15 -08004972 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
4973 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
4974 return -EINVAL;
4975 }
Jakub Kicinski51bc8602019-01-18 10:46:14 -08004976
4977 return 0;
4978}
4979
David Ahernc21ef3e2017-04-16 09:48:24 -07004980static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
4981 struct netlink_ext_ack *extack)
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004982{
4983 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004984 struct net_device *dev = NULL;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02004985 int idxattr = 0, prividx = 0;
4986 struct if_stats_msg *ifsm;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004987 struct sk_buff *nskb;
4988 u32 filter_mask;
4989 int err;
4990
Jakub Kicinski51bc8602019-01-18 10:46:14 -08004991 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
4992 false, extack);
4993 if (err)
4994 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01004995
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07004996 ifsm = nlmsg_data(nlh);
4997 if (ifsm->ifindex > 0)
4998 dev = __dev_get_by_index(net, ifsm->ifindex);
4999 else
5000 return -EINVAL;
5001
5002 if (!dev)
5003 return -ENODEV;
5004
5005 filter_mask = ifsm->filter_mask;
5006 if (!filter_mask)
5007 return -EINVAL;
5008
5009 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
5010 if (!nskb)
5011 return -ENOBUFS;
5012
5013 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5014 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005015 0, filter_mask, &idxattr, &prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005016 if (err < 0) {
5017 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5018 WARN_ON(err == -EMSGSIZE);
5019 kfree_skb(nskb);
5020 } else {
5021 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5022 }
5023
5024 return err;
5025}
5026
5027static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5028{
David Ahern841891e2018-10-07 20:16:32 -07005029 struct netlink_ext_ack *extack = cb->extack;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005030 int h, s_h, err, s_idx, s_idxattr, s_prividx;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005031 struct net *net = sock_net(skb->sk);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005032 unsigned int flags = NLM_F_MULTI;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005033 struct if_stats_msg *ifsm;
5034 struct hlist_head *head;
5035 struct net_device *dev;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005036 u32 filter_mask = 0;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005037 int idx = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005038
5039 s_h = cb->args[0];
5040 s_idx = cb->args[1];
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005041 s_idxattr = cb->args[2];
5042 s_prividx = cb->args[3];
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005043
5044 cb->seq = net->dev_base_seq;
5045
Jakub Kicinski51bc8602019-01-18 10:46:14 -08005046 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5047 if (err)
5048 return err;
Mathias Krause4775cc12016-12-28 17:52:15 +01005049
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005050 ifsm = nlmsg_data(cb->nlh);
5051 filter_mask = ifsm->filter_mask;
David Ahern841891e2018-10-07 20:16:32 -07005052 if (!filter_mask) {
5053 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005054 return -EINVAL;
David Ahern841891e2018-10-07 20:16:32 -07005055 }
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005056
5057 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5058 idx = 0;
5059 head = &net->dev_index_head[h];
5060 hlist_for_each_entry(dev, head, index_hlist) {
5061 if (idx < s_idx)
5062 goto cont;
5063 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5064 NETLINK_CB(cb->skb).portid,
5065 cb->nlh->nlmsg_seq, 0,
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005066 flags, filter_mask,
5067 &s_idxattr, &s_prividx);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005068 /* If we ran out of room on the first message,
5069 * we're in trouble
5070 */
5071 WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5072
5073 if (err < 0)
5074 goto out;
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005075 s_prividx = 0;
5076 s_idxattr = 0;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005077 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5078cont:
5079 idx++;
5080 }
5081 }
5082out:
Nikolay Aleksandrove8872a22016-04-30 10:25:26 +02005083 cb->args[3] = s_prividx;
5084 cb->args[2] = s_idxattr;
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005085 cb->args[1] = idx;
5086 cb->args[0] = h;
5087
5088 return skb->len;
5089}
5090
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091/* Process one rtnetlink message. */
5092
Johannes Berg2d4bc932017-04-12 14:34:04 +02005093static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
5094 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005096 struct net *net = sock_net(skb->sk);
Florian Westphaladdf9b92017-12-02 21:44:05 +01005097 struct rtnl_link *link;
Florian Westphale4202512017-12-02 21:44:06 +01005098 struct module *owner;
Florian Westphal6853dd42017-08-09 20:41:51 +02005099 int err = -EOPNOTSUPP;
Thomas Grafe2849862007-03-22 11:48:11 -07005100 rtnl_doit_func doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005101 unsigned int flags;
Alexander Kuleshov617cfc72016-01-10 21:26:57 +06005102 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int family;
5104 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 if (type > RTM_MAX)
Thomas Graf038890f2007-04-05 14:35:52 -07005108 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
5110 type -= RTM_BASE;
5111
5112 /* All the messages must have at least 1 byte length */
Hong zhi guo573ce262013-03-27 06:47:04 +00005113 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 return 0;
5115
Hong zhi guo573ce262013-03-27 06:47:04 +00005116 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 kind = type&3;
5118
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07005119 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07005120 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
Florian Westphal6853dd42017-08-09 20:41:51 +02005122 rcu_read_lock();
David S. Millerb8f3ab42011-01-18 12:40:38 -08005123 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005124 struct sock *rtnl;
Thomas Grafe2849862007-03-22 11:48:11 -07005125 rtnl_dumpit_func dumpit;
Greg Rosec7ac8672011-06-10 01:27:09 +00005126 u16 min_dump_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Florian Westphaladdf9b92017-12-02 21:44:05 +01005128 link = rtnl_get_link(family, type);
5129 if (!link || !link->dumpit) {
Florian Westphal6853dd42017-08-09 20:41:51 +02005130 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005131 link = rtnl_get_link(family, type);
5132 if (!link || !link->dumpit)
Florian Westphal6853dd42017-08-09 20:41:51 +02005133 goto err_unlock;
5134 }
Florian Westphale4202512017-12-02 21:44:06 +01005135 owner = link->owner;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005136 dumpit = link->dumpit;
Florian Westphale1fa6d22017-08-09 20:41:47 +02005137
Florian Westphal5c2bb9b2017-08-10 16:52:58 +02005138 if (type == RTM_GETLINK - RTM_BASE)
Florian Westphale1fa6d22017-08-09 20:41:47 +02005139 min_dump_alloc = rtnl_calcit(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Florian Westphale4202512017-12-02 21:44:06 +01005141 err = 0;
5142 /* need to do this before rcu_read_unlock() */
5143 if (!try_module_get(owner))
5144 err = -EPROTONOSUPPORT;
5145
Florian Westphal6853dd42017-08-09 20:41:51 +02005146 rcu_read_unlock();
5147
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005148 rtnl = net->rtnl;
Florian Westphale4202512017-12-02 21:44:06 +01005149 if (err == 0) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005150 struct netlink_dump_control c = {
5151 .dump = dumpit,
5152 .min_dump_alloc = min_dump_alloc,
Florian Westphale4202512017-12-02 21:44:06 +01005153 .module = owner,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005154 };
5155 err = netlink_dump_start(rtnl, skb, nlh, &c);
Florian Westphale4202512017-12-02 21:44:06 +01005156 /* netlink_dump_start() will keep a reference on
5157 * module if dump is still in progress.
5158 */
5159 module_put(owner);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00005160 }
Eric Dumazet2907c352011-05-25 07:34:04 +00005161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 }
5163
Florian Westphaladdf9b92017-12-02 21:44:05 +01005164 link = rtnl_get_link(family, type);
5165 if (!link || !link->doit) {
Florian Westphal8caa38b2017-08-10 16:53:01 +02005166 family = PF_UNSPEC;
Florian Westphaladdf9b92017-12-02 21:44:05 +01005167 link = rtnl_get_link(PF_UNSPEC, type);
5168 if (!link || !link->doit)
5169 goto out_unlock;
Florian Westphal8caa38b2017-08-10 16:53:01 +02005170 }
5171
Florian Westphale4202512017-12-02 21:44:06 +01005172 owner = link->owner;
5173 if (!try_module_get(owner)) {
5174 err = -EPROTONOSUPPORT;
5175 goto out_unlock;
5176 }
5177
Florian Westphaladdf9b92017-12-02 21:44:05 +01005178 flags = link->flags;
Florian Westphal62256f92017-08-09 20:41:52 +02005179 if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
Florian Westphaladdf9b92017-12-02 21:44:05 +01005180 doit = link->doit;
Florian Westphal62256f92017-08-09 20:41:52 +02005181 rcu_read_unlock();
5182 if (doit)
5183 err = doit(skb, nlh, extack);
Florian Westphale4202512017-12-02 21:44:06 +01005184 module_put(owner);
Florian Westphal62256f92017-08-09 20:41:52 +02005185 return err;
5186 }
Florian Westphal6853dd42017-08-09 20:41:51 +02005187 rcu_read_unlock();
5188
Florian Westphal0cc09022017-08-09 20:41:50 +02005189 rtnl_lock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005190 link = rtnl_get_link(family, type);
5191 if (link && link->doit)
5192 err = link->doit(skb, nlh, extack);
Florian Westphal0cc09022017-08-09 20:41:50 +02005193 rtnl_unlock();
Florian Westphaladdf9b92017-12-02 21:44:05 +01005194
Florian Westphale4202512017-12-02 21:44:06 +01005195 module_put(owner);
5196
Florian Westphaladdf9b92017-12-02 21:44:05 +01005197 return err;
5198
5199out_unlock:
5200 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005201 return err;
5202
5203err_unlock:
Florian Westphal6853dd42017-08-09 20:41:51 +02005204 rcu_read_unlock();
Florian Westphal0cc09022017-08-09 20:41:50 +02005205 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206}
5207
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005208static void rtnetlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07005210 netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211}
5212
Julien Gomes5f729ea2017-06-20 13:54:16 -07005213static int rtnetlink_bind(struct net *net, int group)
5214{
5215 switch (group) {
5216 case RTNLGRP_IPV4_MROUTE_R:
5217 case RTNLGRP_IPV6_MROUTE_R:
5218 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
5219 return -EPERM;
5220 break;
5221 }
5222 return 0;
5223}
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
5226{
Jiri Pirko351638e2013-05-28 01:30:21 +00005227 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02005228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 switch (event) {
Vlad Yasevich5138e862017-04-04 09:23:41 -04005230 case NETDEV_REBOOT:
Xin Long8a212582017-10-15 18:13:41 +08005231 case NETDEV_CHANGEMTU:
David Ahern37536542017-07-19 10:22:40 -07005232 case NETDEV_CHANGEADDR:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005233 case NETDEV_CHANGENAME:
5234 case NETDEV_FEAT_CHANGE:
5235 case NETDEV_BONDING_FAILOVER:
Xin Longe6e66592017-10-15 18:13:43 +08005236 case NETDEV_POST_TYPE_CHANGE:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005237 case NETDEV_NOTIFY_PEERS:
Xin Longdc709f32017-10-15 18:13:44 +08005238 case NETDEV_CHANGEUPPER:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005239 case NETDEV_RESEND_IGMP:
Vlad Yasevich5138e862017-04-04 09:23:41 -04005240 case NETDEV_CHANGEINFODATA:
Xin Longeeda3fb2017-10-24 13:54:19 +08005241 case NETDEV_CHANGELOWERSTATE:
Xin Longebdcf042017-10-15 18:13:42 +08005242 case NETDEV_CHANGE_TX_QUEUE_LEN:
Vlad Yasevich3d3ea5a2017-05-27 10:14:34 -04005243 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
Nicolas Dichtel38e01b32018-01-25 15:01:39 +01005244 GFP_KERNEL, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 break;
5248 }
5249 return NOTIFY_DONE;
5250}
5251
5252static struct notifier_block rtnetlink_dev_notifier = {
5253 .notifier_call = rtnetlink_event,
5254};
5255
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005256
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005257static int __net_init rtnetlink_net_init(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005258{
5259 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005260 struct netlink_kernel_cfg cfg = {
5261 .groups = RTNLGRP_MAX,
5262 .input = rtnetlink_rcv,
5263 .cb_mutex = &rtnl_mutex,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00005264 .flags = NL_CFG_F_NONROOT_RECV,
Julien Gomes5f729ea2017-06-20 13:54:16 -07005265 .bind = rtnetlink_bind,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00005266 };
5267
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00005268 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005269 if (!sk)
5270 return -ENOMEM;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005271 net->rtnl = sk;
5272 return 0;
5273}
5274
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005275static void __net_exit rtnetlink_net_exit(struct net *net)
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005276{
Denis V. Lunev775516b2008-01-18 23:55:19 -08005277 netlink_kernel_release(net->rtnl);
5278 net->rtnl = NULL;
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005279}
5280
5281static struct pernet_operations rtnetlink_net_ops = {
5282 .init = rtnetlink_net_init,
5283 .exit = rtnetlink_net_exit,
5284};
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286void __init rtnetlink_init(void)
5287{
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005288 if (register_pernet_subsys(&rtnetlink_net_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 panic("rtnetlink_init: cannot initialize rtnetlink\n");
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08005290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 register_netdevice_notifier(&rtnetlink_dev_notifier);
Thomas Graf340d17f2007-03-22 11:49:22 -07005292
Greg Rosec7ac8672011-06-10 01:27:09 +00005293 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
Florian Westphalb97bac62017-08-09 20:41:48 +02005294 rtnl_dump_ifinfo, 0);
5295 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
5296 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
5297 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
Thomas Graf687ad8c2007-03-22 11:59:42 -07005298
Florian Westphalb97bac62017-08-09 20:41:48 +02005299 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
5300 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
5301 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
John Fastabend77162022012-04-15 06:43:56 +00005302
Florian Westphalb97bac62017-08-09 20:41:48 +02005303 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
5304 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
Roopa Prabhu5b2f94b2018-12-15 22:35:08 -08005305 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
John Fastabende5a55a82012-10-24 08:12:57 +00005306
Florian Westphalb97bac62017-08-09 20:41:48 +02005307 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
5308 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
5309 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
Roopa Prabhu10c9ead2016-04-20 08:43:43 -07005310
5311 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
Florian Westphalb97bac62017-08-09 20:41:48 +02005312 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}